下面列出了org.eclipse.jdt.core.dom.ASTNode#CONSTRUCTOR_INVOCATION 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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 List<Expression> getArguments(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation)invocation).arguments();
case ASTNode.SUPER_METHOD_INVOCATION:
return ((SuperMethodInvocation)invocation).arguments();
case ASTNode.CONSTRUCTOR_INVOCATION:
return ((ConstructorInvocation)invocation).arguments();
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return ((SuperConstructorInvocation)invocation).arguments();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation)invocation).arguments();
case ASTNode.ENUM_CONSTANT_DECLARATION:
return ((EnumConstantDeclaration)invocation).arguments();
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static ChildListPropertyDescriptor getArgumentsProperty(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return MethodInvocation.ARGUMENTS_PROPERTY;
case ASTNode.SUPER_METHOD_INVOCATION:
return SuperMethodInvocation.ARGUMENTS_PROPERTY;
case ASTNode.CONSTRUCTOR_INVOCATION:
return ConstructorInvocation.ARGUMENTS_PROPERTY;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return SuperConstructorInvocation.ARGUMENTS_PROPERTY;
case ASTNode.CLASS_INSTANCE_CREATION:
return ClassInstanceCreation.ARGUMENTS_PROPERTY;
case ASTNode.ENUM_CONSTANT_DECLARATION:
return EnumConstantDeclaration.ARGUMENTS_PROPERTY;
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static Expression getExpression(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation)invocation).getExpression();
case ASTNode.SUPER_METHOD_INVOCATION:
return null;
case ASTNode.CONSTRUCTOR_INVOCATION:
return null;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return ((SuperConstructorInvocation)invocation).getExpression();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation)invocation).getExpression();
case ASTNode.ENUM_CONSTANT_DECLARATION:
return null;
default:
throw new IllegalArgumentException(invocation.toString());
}
}
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;
}
}
public static IMethodBinding resolveBinding(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation)invocation).resolveMethodBinding();
case ASTNode.SUPER_METHOD_INVOCATION:
return ((SuperMethodInvocation)invocation).resolveMethodBinding();
case ASTNode.CONSTRUCTOR_INVOCATION:
return ((ConstructorInvocation)invocation).resolveConstructorBinding();
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return ((SuperConstructorInvocation)invocation).resolveConstructorBinding();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation)invocation).resolveConstructorBinding();
case ASTNode.ENUM_CONSTANT_DECLARATION:
return ((EnumConstantDeclaration)invocation).resolveConstructorBinding();
default:
throw new IllegalArgumentException(invocation.toString());
}
}
/**
* 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;
}
private void checkMethodDeclaration(RefactoringStatus result, int severity) {
MethodDeclaration methodDeclaration= fSourceProvider.getDeclaration();
// it is not allowed to inline constructor invocation only if it is used for class instance creation
// if constructor is invoked from another constructor then we can inline such invocation
if (fInvocation.getNodeType() != ASTNode.CONSTRUCTOR_INVOCATION && methodDeclaration.isConstructor()) {
result.addEntry(new RefactoringStatusEntry(
severity,
RefactoringCoreMessages.CallInliner_constructors,
JavaStatusContext.create(fCUnit, fInvocation)));
}
if (fSourceProvider.hasSuperMethodInvocation() && fInvocation.getNodeType() == ASTNode.METHOD_INVOCATION) {
Expression receiver= ((MethodInvocation)fInvocation).getExpression();
if (receiver instanceof ThisExpression) {
result.addEntry(new RefactoringStatusEntry(
severity,
RefactoringCoreMessages.CallInliner_super_into_this_expression,
JavaStatusContext.create(fCUnit, fInvocation)));
}
}
}
private Statement convertStatement(org.eclipse.jdt.core.dom.Statement statement) {
switch (statement.getNodeType()) {
case ASTNode.ASSERT_STATEMENT:
return convert((org.eclipse.jdt.core.dom.AssertStatement) statement);
case ASTNode.BLOCK:
return convert((org.eclipse.jdt.core.dom.Block) statement);
case ASTNode.BREAK_STATEMENT:
return convert((org.eclipse.jdt.core.dom.BreakStatement) statement);
case ASTNode.CONSTRUCTOR_INVOCATION:
return convert((org.eclipse.jdt.core.dom.ConstructorInvocation) statement);
case ASTNode.CONTINUE_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ContinueStatement) statement);
case ASTNode.DO_STATEMENT:
return convert((org.eclipse.jdt.core.dom.DoStatement) statement);
case ASTNode.EMPTY_STATEMENT:
return new EmptyStatement(getSourcePosition(statement));
case ASTNode.EXPRESSION_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ExpressionStatement) statement);
case ASTNode.FOR_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ForStatement) statement);
case ASTNode.ENHANCED_FOR_STATEMENT:
return convert((org.eclipse.jdt.core.dom.EnhancedForStatement) statement);
case ASTNode.IF_STATEMENT:
return convert((org.eclipse.jdt.core.dom.IfStatement) statement);
case ASTNode.LABELED_STATEMENT:
return convert((org.eclipse.jdt.core.dom.LabeledStatement) statement);
case ASTNode.RETURN_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ReturnStatement) statement);
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return convert((org.eclipse.jdt.core.dom.SuperConstructorInvocation) statement);
case ASTNode.SWITCH_STATEMENT:
return convert((org.eclipse.jdt.core.dom.SwitchStatement) statement);
case ASTNode.SYNCHRONIZED_STATEMENT:
return convert((org.eclipse.jdt.core.dom.SynchronizedStatement) statement);
case ASTNode.THROW_STATEMENT:
return convert((org.eclipse.jdt.core.dom.ThrowStatement) statement);
case ASTNode.TRY_STATEMENT:
return convert((org.eclipse.jdt.core.dom.TryStatement) statement);
case ASTNode.TYPE_DECLARATION_STATEMENT:
return convert((org.eclipse.jdt.core.dom.TypeDeclarationStatement) statement);
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
return convert((org.eclipse.jdt.core.dom.VariableDeclarationStatement) statement);
case ASTNode.WHILE_STATEMENT:
return convert((org.eclipse.jdt.core.dom.WhileStatement) statement);
default:
throw internalCompilerError(
"Unexpected type for Statement: %s", statement.getClass().getName());
}
}
public boolean mustInnerClassBeStatic() {
ITypeBinding typeBinding = ((AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class)).resolveBinding();
ASTNode current = fAnonymousInnerClassNode.getParent();
boolean ans = false;
while(current != null) {
switch(current.getNodeType()) {
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
case ASTNode.CONSTRUCTOR_INVOCATION:
return true;
case ASTNode.ANONYMOUS_CLASS_DECLARATION:
{
AnonymousClassDeclaration enclosingAnonymousClassDeclaration= (AnonymousClassDeclaration)current;
ITypeBinding binding= enclosingAnonymousClassDeclaration.resolveBinding();
if (binding != null && Bindings.isSuperType(typeBinding, binding.getSuperclass())) {
return false;
}
break;
}
case ASTNode.FIELD_DECLARATION:
{
FieldDeclaration enclosingFieldDeclaration= (FieldDeclaration)current;
if (Modifier.isStatic(enclosingFieldDeclaration.getModifiers())) {
ans = true;
}
break;
}
case ASTNode.METHOD_DECLARATION:
{
MethodDeclaration enclosingMethodDeclaration = (MethodDeclaration)current;
if (Modifier.isStatic(enclosingMethodDeclaration.getModifiers())) {
ans = true;
}
break;
}
case ASTNode.TYPE_DECLARATION:
{
return ans;
}
}
current = current.getParent();
}
return ans;
}
public static boolean isInvocation(ASTNode node) {
int type= node.getNodeType();
return type == ASTNode.METHOD_INVOCATION || type == ASTNode.SUPER_METHOD_INVOCATION ||
type == ASTNode.CONSTRUCTOR_INVOCATION;
}