下面列出了org.eclipse.jdt.core.dom.ASTNode#METHOD_INVOCATION 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean consumes(SemanticToken token) {
// 1: match types
SimpleName name = token.getNode();
ASTNode node = name.getParent();
int nodeType = node.getNodeType();
if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.THIS_EXPRESSION && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.TYPE_DECLARATION
&& nodeType != ASTNode.METHOD_INVOCATION) {
return false;
}
while (nodeType == ASTNode.QUALIFIED_NAME) {
node = node.getParent();
nodeType = node.getNodeType();
if (nodeType == ASTNode.IMPORT_DECLARATION) {
return false;
}
}
// 2: match classes
IBinding binding = token.getBinding();
return binding instanceof ITypeBinding && ((ITypeBinding) binding).isClass();
}
@Override
public boolean consumes(SemanticToken token) {
// 1: match types
SimpleName name= token.getNode();
ASTNode node= name.getParent();
int nodeType= node.getNodeType();
if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.THIS_EXPRESSION && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.TYPE_DECLARATION && nodeType != ASTNode.METHOD_INVOCATION)
return false;
while (nodeType == ASTNode.QUALIFIED_NAME) {
node= node.getParent();
nodeType= node.getNodeType();
if (nodeType == ASTNode.IMPORT_DECLARATION)
return false;
}
// 2: match classes
IBinding binding= token.getBinding();
if (binding instanceof ITypeBinding) {
ITypeBinding typeBinding= (ITypeBinding) binding;
// see also ClassHighlighting
return typeBinding.isClass() && (typeBinding.getModifiers() & Modifier.ABSTRACT) != 0;
}
return false;
}
private static ASTNode getInlineableMethodNode(ASTNode node, IJavaElement unit) {
if (node == null) {
return null;
}
switch (node.getNodeType()) {
case ASTNode.SIMPLE_NAME:
StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
if (locationInParent == MethodDeclaration.NAME_PROPERTY) {
return node.getParent();
} else if (locationInParent == MethodInvocation.NAME_PROPERTY || locationInParent == SuperMethodInvocation.NAME_PROPERTY) {
return unit instanceof ICompilationUnit ? node.getParent() : null; // don't start on invocations in binary
}
return null;
case ASTNode.EXPRESSION_STATEMENT:
node = ((ExpressionStatement) node).getExpression();
}
switch (node.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
return node;
case ASTNode.METHOD_INVOCATION:
case ASTNode.SUPER_METHOD_INVOCATION:
case ASTNode.CONSTRUCTOR_INVOCATION:
return unit instanceof ICompilationUnit ? node : null; // don't start on invocations in binary
}
return null;
}
/**
* Creates a new inline method refactoring
* @param unit the compilation unit or class file
* @param node the compilation unit node
* @param selectionStart start
* @param selectionLength length
* @return returns the refactoring
*/
public static InlineMethodRefactoring create(ITypeRoot unit, CompilationUnit node, int selectionStart, int selectionLength) {
ASTNode target= RefactoringAvailabilityTester.getInlineableMethodNode(unit, node, selectionStart, selectionLength);
if (target == null)
return null;
if (target.getNodeType() == ASTNode.METHOD_DECLARATION) {
return new InlineMethodRefactoring(unit, (MethodDeclaration)target, selectionStart, selectionLength);
} else {
ICompilationUnit cu= (ICompilationUnit) unit;
if (target.getNodeType() == ASTNode.METHOD_INVOCATION) {
return new InlineMethodRefactoring(cu, (MethodInvocation)target, selectionStart, selectionLength);
} else if (target.getNodeType() == ASTNode.SUPER_METHOD_INVOCATION) {
return new InlineMethodRefactoring(cu, (SuperMethodInvocation)target, selectionStart, selectionLength);
} else if (target.getNodeType() == ASTNode.CONSTRUCTOR_INVOCATION) {
return new InlineMethodRefactoring(cu, (ConstructorInvocation)target, selectionStart, selectionLength);
}
}
return null;
}
/**
* Creates the appropriate exporter implementation based on the type of the
* given node.
*
* @param <T>
* The node type which is parsed by the exporter.
*/
@SuppressWarnings("unchecked")
public static <T extends ASTNode> ExporterBase<T> createExporter(T curElement, PlantUmlCompiler compiler) {
switch (curElement.getNodeType()) {
case ASTNode.TYPE_DECLARATION:
return (ExporterBase<T>) new InteractionExporter(compiler);
case ASTNode.METHOD_INVOCATION:
return (ExporterBase<T>) MethodInvocationExporter.createExporter(curElement, compiler);
case ASTNode.WHILE_STATEMENT:
case ASTNode.FOR_STATEMENT:
case ASTNode.ENHANCED_FOR_STATEMENT:
case ASTNode.DO_STATEMENT:
return (ExporterBase<T>) new LoopFragmentExporter(compiler);
case ASTNode.IF_STATEMENT:
return (ExporterBase<T>) new OptAltFragmentExporter(compiler);
}
return null;
}
private Expression getAssignedValue(ParameterObjectFactory pof, String parameterName, IJavaProject javaProject, RefactoringStatus status, ASTRewrite rewrite, ParameterInfo pi, boolean useSuper, ITypeBinding typeBinding, Expression qualifier, ASTNode replaceNode, ITypeRoot typeRoot) {
AST ast= rewrite.getAST();
boolean is50OrHigher= JavaModelUtil.is50OrHigher(javaProject);
Expression assignedValue= handleSimpleNameAssignment(replaceNode, pof, parameterName, ast, javaProject, useSuper);
if (assignedValue == null) {
NullLiteral marker= qualifier == null ? null : ast.newNullLiteral();
Expression fieldReadAccess= pof.createFieldReadAccess(pi, parameterName, ast, javaProject, useSuper, marker);
assignedValue= GetterSetterUtil.getAssignedValue(replaceNode, rewrite, fieldReadAccess, typeBinding, is50OrHigher);
boolean markerReplaced= replaceMarker(rewrite, qualifier, assignedValue, marker);
if (markerReplaced) {
switch (qualifier.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
case ASTNode.CLASS_INSTANCE_CREATION:
case ASTNode.SUPER_METHOD_INVOCATION:
case ASTNode.PARENTHESIZED_EXPRESSION:
status.addWarning(RefactoringCoreMessages.ExtractClassRefactoring_warning_semantic_change, JavaStatusContext.create(typeRoot, replaceNode));
break;
}
}
}
return assignedValue;
}
private static ASTNode getInlineableMethodNode(ASTNode node, IJavaElement unit) {
if (node == null)
return null;
switch (node.getNodeType()) {
case ASTNode.SIMPLE_NAME:
StructuralPropertyDescriptor locationInParent= node.getLocationInParent();
if (locationInParent == MethodDeclaration.NAME_PROPERTY) {
return node.getParent();
} else if (locationInParent == MethodInvocation.NAME_PROPERTY
|| locationInParent == SuperMethodInvocation.NAME_PROPERTY) {
return unit instanceof ICompilationUnit ? node.getParent() : null; // don't start on invocations in binary
}
return null;
case ASTNode.EXPRESSION_STATEMENT:
node= ((ExpressionStatement)node).getExpression();
}
switch (node.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
return node;
case ASTNode.METHOD_INVOCATION:
case ASTNode.SUPER_METHOD_INVOCATION:
case ASTNode.CONSTRUCTOR_INVOCATION:
return unit instanceof ICompilationUnit ? node : null; // don't start on invocations in binary
}
return null;
}
public static boolean isIntroduceIndirectionAvailable(final JavaTextSelection selection) throws JavaModelException {
final IJavaElement[] elements= selection.resolveElementAtOffset();
if (elements.length == 1)
return (elements[0] instanceof IMethod) && isIntroduceIndirectionAvailable(((IMethod) elements[0]));
ASTNode[] selectedNodes= selection.resolveSelectedNodes();
if (selectedNodes == null || selectedNodes.length != 1)
return false;
switch (selectedNodes[0].getNodeType()) {
case ASTNode.METHOD_DECLARATION:
case ASTNode.METHOD_INVOCATION:
case ASTNode.SUPER_METHOD_INVOCATION:
return true;
default:
return false;
}
}
private static ASTNode checkNode(ASTNode node) {
if (node == null)
return null;
if (node.getNodeType() == ASTNode.SIMPLE_NAME) {
node= node.getParent();
} else if (node.getNodeType() == ASTNode.EXPRESSION_STATEMENT) {
node= ((ExpressionStatement) node).getExpression();
}
switch (node.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
case ASTNode.METHOD_DECLARATION:
case ASTNode.SUPER_METHOD_INVOCATION:
return node;
}
return null;
}
public static ITypeBinding[] getInferredTypeArguments(Expression invocation) {
IMethodBinding methodBinding;
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
methodBinding= ((MethodInvocation) invocation).resolveMethodBinding();
return methodBinding == null ? null : methodBinding.getTypeArguments();
case ASTNode.SUPER_METHOD_INVOCATION:
methodBinding= ((SuperMethodInvocation) invocation).resolveMethodBinding();
return methodBinding == null ? null : methodBinding.getTypeArguments();
case ASTNode.CLASS_INSTANCE_CREATION:
Type type= ((ClassInstanceCreation) invocation).getType();
ITypeBinding typeBinding= type.resolveBinding();
return typeBinding == null ? null : typeBinding.getTypeArguments();
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static boolean isResolvedTypeInferredFromExpectedType(Expression invocation) {
if (invocation == null)
return false;
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation) invocation).isResolvedTypeInferredFromExpectedType();
case ASTNode.SUPER_METHOD_INVOCATION:
return ((SuperMethodInvocation) invocation).isResolvedTypeInferredFromExpectedType();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation) invocation).isResolvedTypeInferredFromExpectedType();
default:
return false;
}
}
public static boolean isInvocationWithArguments(ASTNode node) {
switch (node.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
case ASTNode.SUPER_METHOD_INVOCATION:
case ASTNode.CONSTRUCTOR_INVOCATION:
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
case ASTNode.CLASS_INSTANCE_CREATION:
case ASTNode.ENUM_CONSTANT_DECLARATION:
return true;
default:
return false;
}
}
@Override
public boolean consumes(SemanticToken token) {
// 1: match types
SimpleName name = token.getNode();
ASTNode node = name.getParent();
int nodeType = node.getNodeType();
if (nodeType != ASTNode.SIMPLE_TYPE && nodeType != ASTNode.THIS_EXPRESSION && nodeType != ASTNode.QUALIFIED_TYPE && nodeType != ASTNode.QUALIFIED_NAME && nodeType != ASTNode.TYPE_DECLARATION
&& nodeType != ASTNode.METHOD_INVOCATION) {
return false;
}
while (nodeType == ASTNode.QUALIFIED_NAME) {
node = node.getParent();
nodeType = node.getNodeType();
if (nodeType == ASTNode.IMPORT_DECLARATION) {
return false;
}
}
// 2: match classes
IBinding binding = token.getBinding();
if (binding instanceof ITypeBinding) {
ITypeBinding typeBinding = (ITypeBinding) binding;
// see also ClassHighlighting
return typeBinding.isClass() && (typeBinding.getModifiers() & Modifier.ABSTRACT) != 0;
}
return false;
}
private static ITypeBinding extractExpressionType(ExpressionStatement statement) {
Expression expression= statement.getExpression();
if (expression.getNodeType() == ASTNode.METHOD_INVOCATION
|| expression.getNodeType() == ASTNode.FIELD_ACCESS) {
return expression.resolveTypeBinding();
}
return null;
}
/**
* Visits {@link SimpleName} AST nodes. Resolves the binding of the simple
* name and looks for it in the {@link #variableScope} map. If the binding
* is found, this is a reference to a variable.
*
* @param node
* the node to visit
*/
@Override
public boolean visit(final SimpleName node) {
if (node.getParent().getNodeType() == ASTNode.METHOD_INVOCATION) {
final MethodInvocation invocation = (MethodInvocation) node.getParent();
if (invocation.getName() == node) {
return true;
}
}
addBindingData(node.getIdentifier(), node, variableNames.get(node));
return true;
}
@Override
public boolean validElement(ASTNode curElement) {
int nodeType = curElement.getNodeType();
return nodeType == ASTNode.DO_STATEMENT || nodeType == ASTNode.ENHANCED_FOR_STATEMENT
|| nodeType == ASTNode.WHILE_STATEMENT || nodeType == ASTNode.FOR_STATEMENT
|| nodeType == ASTNode.IF_STATEMENT || nodeType == ASTNode.METHOD_INVOCATION;
}
private Expression convert(org.eclipse.jdt.core.dom.Expression expression) {
switch (expression.getNodeType()) {
case ASTNode.ARRAY_ACCESS:
return convert((org.eclipse.jdt.core.dom.ArrayAccess) expression);
case ASTNode.ARRAY_CREATION:
return convert((org.eclipse.jdt.core.dom.ArrayCreation) expression);
case ASTNode.ARRAY_INITIALIZER:
return convert((org.eclipse.jdt.core.dom.ArrayInitializer) expression);
case ASTNode.ASSIGNMENT:
return convert((org.eclipse.jdt.core.dom.Assignment) expression);
case ASTNode.BOOLEAN_LITERAL:
return convert((org.eclipse.jdt.core.dom.BooleanLiteral) expression);
case ASTNode.CAST_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.CastExpression) expression);
case ASTNode.CHARACTER_LITERAL:
return convert((org.eclipse.jdt.core.dom.CharacterLiteral) expression);
case ASTNode.CLASS_INSTANCE_CREATION:
return convert((org.eclipse.jdt.core.dom.ClassInstanceCreation) expression);
case ASTNode.CONDITIONAL_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.ConditionalExpression) expression);
case ASTNode.EXPRESSION_METHOD_REFERENCE:
return convert((org.eclipse.jdt.core.dom.ExpressionMethodReference) expression);
case ASTNode.CREATION_REFERENCE:
return convert((org.eclipse.jdt.core.dom.CreationReference) expression);
case ASTNode.TYPE_METHOD_REFERENCE:
return convert((org.eclipse.jdt.core.dom.TypeMethodReference) expression);
case ASTNode.SUPER_METHOD_REFERENCE:
return convert((org.eclipse.jdt.core.dom.SuperMethodReference) expression);
case ASTNode.FIELD_ACCESS:
return convert((org.eclipse.jdt.core.dom.FieldAccess) expression);
case ASTNode.INFIX_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.InfixExpression) expression);
case ASTNode.INSTANCEOF_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.InstanceofExpression) expression);
case ASTNode.LAMBDA_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.LambdaExpression) expression);
case ASTNode.METHOD_INVOCATION:
return convert((org.eclipse.jdt.core.dom.MethodInvocation) expression);
case ASTNode.NULL_LITERAL:
return NullLiteral.get();
case ASTNode.NUMBER_LITERAL:
return convert((org.eclipse.jdt.core.dom.NumberLiteral) expression);
case ASTNode.PARENTHESIZED_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.ParenthesizedExpression) expression);
case ASTNode.POSTFIX_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.PostfixExpression) expression);
case ASTNode.PREFIX_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.PrefixExpression) expression);
case ASTNode.QUALIFIED_NAME:
return convert((org.eclipse.jdt.core.dom.QualifiedName) expression);
case ASTNode.SIMPLE_NAME:
return convert((org.eclipse.jdt.core.dom.SimpleName) expression);
case ASTNode.STRING_LITERAL:
return convert((org.eclipse.jdt.core.dom.StringLiteral) expression);
case ASTNode.SUPER_FIELD_ACCESS:
return convert((org.eclipse.jdt.core.dom.SuperFieldAccess) expression);
case ASTNode.SUPER_METHOD_INVOCATION:
return convert((org.eclipse.jdt.core.dom.SuperMethodInvocation) expression);
case ASTNode.THIS_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.ThisExpression) expression);
case ASTNode.TYPE_LITERAL:
return convert((org.eclipse.jdt.core.dom.TypeLiteral) expression);
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
return convert((org.eclipse.jdt.core.dom.VariableDeclarationExpression) expression);
default:
throw internalCompilerError(
"Unexpected type for Expression: %s", expression.getClass().getName());
}
}
private void checkInvocationContext(RefactoringStatus result, int severity) {
if (fInvocation.getNodeType() == ASTNode.METHOD_INVOCATION) {
if (((MethodInvocation)fInvocation).resolveTypeBinding() == null) {
addEntry(result, RefactoringCoreMessages.CallInliner_receiver_type,
RefactoringStatusCodes.INLINE_METHOD_NULL_BINDING, severity);
return;
}
}
int nodeType= fTargetNode.getNodeType();
if (nodeType == ASTNode.EXPRESSION_STATEMENT) {
if (fSourceProvider.isExecutionFlowInterrupted()) {
addEntry(result, RefactoringCoreMessages.CallInliner_execution_flow,
RefactoringStatusCodes.INLINE_METHOD_EXECUTION_FLOW, severity);
return;
}
} else if (nodeType == ASTNode.METHOD_INVOCATION) {
ASTNode parent= fTargetNode.getParent();
if (isReturnStatement(parent)) {
//support inlining even if the execution flow is interrupted
return;
}
if (fSourceProvider.isExecutionFlowInterrupted()) {
addEntry(result, RefactoringCoreMessages.CallInliner_execution_flow,
RefactoringStatusCodes.INLINE_METHOD_EXECUTION_FLOW, severity);
return;
}
if (isAssignment(parent) || isSingleDeclaration(parent)) {
// we support inlining expression in assigment and initializers as
// long as the execution flow isn't interrupted.
return;
} else {
boolean isFieldDeclaration= ASTNodes.getParent(fInvocation, FieldDeclaration.class) != null;
if (!fSourceProvider.isSimpleFunction()) {
if (isMultiDeclarationFragment(parent)) {
addEntry(result, RefactoringCoreMessages.CallInliner_multiDeclaration,
RefactoringStatusCodes.INLINE_METHOD_INITIALIZER_IN_FRAGEMENT, severity);
} else if (isFieldDeclaration) {
addEntry(result,
RefactoringCoreMessages.CallInliner_field_initializer_simple,
RefactoringStatusCodes.INLINE_METHOD_FIELD_INITIALIZER, severity);
} else {
addEntry(result, RefactoringCoreMessages.CallInliner_simple_functions,
RefactoringStatusCodes.INLINE_METHOD_ONLY_SIMPLE_FUNCTIONS, severity);
}
return;
}
if (isFieldDeclaration) {
int argumentsCount= fContext.arguments.length;
for (int i= 0; i < argumentsCount; i++) {
ParameterData parameter= fSourceProvider.getParameterData(i);
if(parameter.isWrite()) {
addEntry(result,
RefactoringCoreMessages.CallInliner_field_initialize_write_parameter,
RefactoringStatusCodes.INLINE_METHOD_FIELD_INITIALIZER, severity);
return;
}
}
if(fLocals.size() > 0) {
addEntry(result,
RefactoringCoreMessages.CallInliner_field_initialize_new_local,
RefactoringStatusCodes.INLINE_METHOD_FIELD_INITIALIZER, severity);
return;
}
// verify that the field is not referenced by the initializer method
VariableDeclarationFragment variable= (VariableDeclarationFragment)ASTNodes.getParent(fInvocation, ASTNode.VARIABLE_DECLARATION_FRAGMENT);
if(fSourceProvider.isVariableReferenced(variable.resolveBinding())) {
addEntry(result,
RefactoringCoreMessages.CallInliner_field_initialize_self_reference,
RefactoringStatusCodes.INLINE_METHOD_FIELD_INITIALIZER, severity);
return;
}
}
}
}
}
@Override
public boolean validElement(ASTNode curElement) {
return curElement.getNodeType() == ASTNode.METHOD_INVOCATION;
}
public static boolean isInvocation(ASTNode node) {
int type= node.getNodeType();
return type == ASTNode.METHOD_INVOCATION || type == ASTNode.SUPER_METHOD_INVOCATION ||
type == ASTNode.CONSTRUCTOR_INVOCATION;
}