下面列出了怎么用org.eclipse.jdt.core.dom.ConstructorInvocation的API类实例代码及写法,或者点击链接到github查看源代码。
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 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;
}
public RefactoringStatus setCurrentMode(Mode mode) throws JavaModelException {
if (fCurrentMode == mode)
return new RefactoringStatus();
Assert.isTrue(getInitialMode() == Mode.INLINE_SINGLE);
fCurrentMode= mode;
if (mode == Mode.INLINE_SINGLE) {
if (fInitialNode instanceof MethodInvocation)
fTargetProvider= TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (MethodInvocation)fInitialNode);
else if (fInitialNode instanceof SuperMethodInvocation)
fTargetProvider= TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (SuperMethodInvocation)fInitialNode);
else if (fInitialNode instanceof ConstructorInvocation)
fTargetProvider= TargetProvider.create((ICompilationUnit) fInitialTypeRoot, (ConstructorInvocation)fInitialNode);
else
throw new IllegalStateException(String.valueOf(fInitialNode));
} else {
fTargetProvider= TargetProvider.create(fSourceProvider.getDeclaration());
}
return fTargetProvider.checkActivation();
}
private static IBinding resolveBinding(ASTNode node) {
if (node instanceof SimpleName) {
SimpleName simpleName= (SimpleName) node;
// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
ASTNode normalized= ASTNodes.getNormalizedNode(simpleName);
if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
ClassInstanceCreation cic= (ClassInstanceCreation) normalized.getParent();
IMethodBinding constructorBinding= cic.resolveConstructorBinding();
if (constructorBinding == null)
return null;
ITypeBinding declaringClass= constructorBinding.getDeclaringClass();
if (!declaringClass.isAnonymous())
return constructorBinding;
ITypeBinding superTypeDeclaration= declaringClass.getSuperclass().getTypeDeclaration();
return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
}
return simpleName.resolveBinding();
} else if (node instanceof SuperConstructorInvocation) {
return ((SuperConstructorInvocation) node).resolveConstructorBinding();
} else if (node instanceof ConstructorInvocation) {
return ((ConstructorInvocation) node).resolveConstructorBinding();
} else {
return null;
}
}
public StatementObject(Statement statement, StatementType type, AbstractMethodFragment parent) {
super(statement, type, parent);
ExpressionExtractor expressionExtractor = new ExpressionExtractor();
List<Expression> assignments = expressionExtractor.getAssignments(statement);
List<Expression> postfixExpressions = expressionExtractor.getPostfixExpressions(statement);
List<Expression> prefixExpressions = expressionExtractor.getPrefixExpressions(statement);
processVariables(expressionExtractor.getVariableInstructions(statement), assignments, postfixExpressions, prefixExpressions);
processMethodInvocations(expressionExtractor.getMethodInvocations(statement));
processClassInstanceCreations(expressionExtractor.getClassInstanceCreations(statement));
processArrayCreations(expressionExtractor.getArrayCreations(statement));
//processArrayAccesses(expressionExtractor.getArrayAccesses(statement));
processLiterals(expressionExtractor.getLiterals(statement));
if(statement instanceof ThrowStatement) {
processThrowStatement((ThrowStatement)statement);
}
if(statement instanceof ConstructorInvocation) {
processConstructorInvocation((ConstructorInvocation)statement);
}
}
public boolean visit(ConstructorInvocation node) {
int start = _unit.getLineNumber(node.getStartPosition());
if(start == _extendedLine){
_extendedStatement = node;
return false;
}
return true;
}
private ConstructorInv visit(ConstructorInvocation node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
ConstructorInv constructorInv = new ConstructorInv(startLine, endLine, node);
List<Expr> arguments = new ArrayList<>();
for(Object object : node.arguments()){
Expr expr = (Expr) process((ASTNode) object);
expr.setParent(constructorInv);
arguments.add(expr);
}
constructorInv.setArguments(arguments);
return constructorInv;
}
@Override
public boolean visit(ConstructorInvocation node) {
// XXX Hack for performance reasons (should loop over fJobSemanticHighlightings can call consumes(*))
if (fJobDeprecatedMemberHighlighting != null) {
IMethodBinding constructorBinding= node.resolveConstructorBinding();
if (constructorBinding != null && constructorBinding.isDeprecated()) {
int offset= node.getStartPosition();
int length= 4;
if (offset > -1 && length > 0) {
addPosition(offset, length, fJobDeprecatedMemberHighlighting);
}
}
}
return true;
}
private static IBinding resolveBinding(ASTNode node) {
if (node instanceof SimpleName) {
SimpleName simpleName = (SimpleName) node;
// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
IMethodBinding constructorBinding = cic.resolveConstructorBinding();
if (constructorBinding == null) {
return null;
}
ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
if (!declaringClass.isAnonymous()) {
return constructorBinding;
}
ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
}
return simpleName.resolveBinding();
} else if (node instanceof SuperConstructorInvocation) {
return ((SuperConstructorInvocation) node).resolveConstructorBinding();
} else if (node instanceof ConstructorInvocation) {
return ((ConstructorInvocation) node).resolveConstructorBinding();
} else if (node instanceof LambdaExpression) {
return ((LambdaExpression) node).resolveMethodBinding();
} else {
return null;
}
}
@Override
public void endVisit(ConstructorInvocation node) {
if (skipNode(node)) {
return;
}
processSequential(node, node.arguments());
}
private boolean isUsedInExplicitConstructorCall() throws JavaModelException {
Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
if (ASTNodes.getParent(selectedExpression, ConstructorInvocation.class) != null) {
return true;
}
if (ASTNodes.getParent(selectedExpression, SuperConstructorInvocation.class) != null) {
return true;
}
return false;
}
private boolean isUsedInExplicitConstructorCall() throws JavaModelException {
Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
if (ASTNodes.getParent(selectedExpression, ConstructorInvocation.class) != null) {
return true;
}
if (ASTNodes.getParent(selectedExpression, SuperConstructorInvocation.class) != null) {
return true;
}
return false;
}
private static boolean shouldInsertTempInitialization(MethodDeclaration constructor) {
Assert.isTrue(constructor.isConstructor());
if (constructor.getBody() == null) {
return false;
}
List<Statement> statements = constructor.getBody().statements();
if (statements == null) {
return false;
}
if (statements.size() > 0 && statements.get(0) instanceof ConstructorInvocation) {
return false;
}
return true;
}
@Override
public boolean visit(ConstructorInvocation node) {
if (!isSelected(node)) {
return false;
}
return handleExceptions(node.resolveConstructorBinding(), node);
}
private static Expression getBooleanExpression(ASTNode node) {
if (!(node instanceof Expression)) {
return null;
}
// check if the node is a location where it can be negated
StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
if (locationInParent == QualifiedName.NAME_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
Expression expression = (Expression) node;
if (!isBoolean(expression)) {
return null;
}
if (expression.getParent() instanceof InfixExpression) {
return expression;
}
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY
|| locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY
|| locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
return expression;
}
return null;
}
@Override
public boolean visit(final ConstructorInvocation node) {
boolean _isEmpty = node.typeArguments().isEmpty();
boolean _not = (!_isEmpty);
if (_not) {
this.appendTypeParameters(node.typeArguments());
}
this.appendToBuffer("this(");
this.visitAllSeparatedByComma(node.arguments());
this.appendToBuffer(")");
return false;
}
public OperationInvocation(CompilationUnit cu, String filePath, ConstructorInvocation invocation) {
this.locationInfo = new LocationInfo(cu, filePath, invocation, CodeElementType.CONSTRUCTOR_INVOCATION);
this.methodName = "this";
this.typeArguments = invocation.arguments().size();
this.arguments = new ArrayList<String>();
List<Expression> args = invocation.arguments();
for(Expression argument : args) {
this.arguments.add(argument.toString());
}
}
/**
* handles this(parameter)
*/
@SuppressWarnings("unchecked")
@Override
public boolean visit(ConstructorInvocation node) {
Invocation invocation = importer.createInvocationFromMethodBinding(node.resolveConstructorBinding(),
node.toString().trim());
importer.createLightweightSourceAnchor(invocation, node);
node.arguments().stream().forEach(arg -> importer.createAccessFromExpression((Expression) arg));
return true;
}
private static boolean containsImplicitCallToSuperConstructor(MethodDeclaration constructor) {
Assert.isTrue(constructor.isConstructor());
Block body= constructor.getBody();
if (body == null)
return false;
if (body.statements().size() == 0)
return true;
if (body.statements().get(0) instanceof ConstructorInvocation)
return false;
if (body.statements().get(0) instanceof SuperConstructorInvocation)
return false;
return true;
}
private boolean isRecursiveReference() {
MethodDeclaration enclosingMethodDeclaration= (MethodDeclaration) ASTNodes.getParent(fNode, MethodDeclaration.class);
if (enclosingMethodDeclaration == null)
return false;
IMethodBinding enclosingMethodBinding= enclosingMethodDeclaration.resolveBinding();
if (enclosingMethodBinding == null)
return false;
if (fNode instanceof MethodInvocation)
return enclosingMethodBinding == ((MethodInvocation)fNode).resolveMethodBinding();
if (fNode instanceof SuperMethodInvocation) {
IMethodBinding methodBinding= ((SuperMethodInvocation)fNode).resolveMethodBinding();
return isSameMethod(methodBinding, enclosingMethodBinding);
}
if (fNode instanceof ClassInstanceCreation)
return enclosingMethodBinding == ((ClassInstanceCreation)fNode).resolveConstructorBinding();
if (fNode instanceof ConstructorInvocation)
return enclosingMethodBinding == ((ConstructorInvocation)fNode).resolveConstructorBinding();
if (fNode instanceof SuperConstructorInvocation) {
return false; //Constructors don't override -> enclosing has not been changed -> no recursion
}
if (fNode instanceof EnumConstantDeclaration) {
return false; //cannot define enum constant inside enum constructor
}
Assert.isTrue(false);
return false;
}
public static ASTNode getAstNode(CompilationUnit cuNode, int start, int length){
SelectionAnalyzer analyzer= new SelectionAnalyzer(Selection.createFromStartLength(start, length), true);
cuNode.accept(analyzer);
//XXX workaround for jdt core feature 23527
ASTNode node= analyzer.getFirstSelectedNode();
if (node == null && analyzer.getLastCoveringNode() instanceof SuperConstructorInvocation)
node= analyzer.getLastCoveringNode().getParent();
else if (node == null && analyzer.getLastCoveringNode() instanceof ConstructorInvocation)
node= analyzer.getLastCoveringNode().getParent();
if (node == null)
return null;
ASTNode parentNode= node.getParent();
if (parentNode instanceof MethodDeclaration){
MethodDeclaration md= (MethodDeclaration)parentNode;
if (!(node instanceof SimpleName)
&& md.isConstructor()
&& md.getBody() != null
&& md.getBody().statements().size() > 0
&&(md.getBody().statements().get(0) instanceof ConstructorInvocation || md.getBody().statements().get(0) instanceof SuperConstructorInvocation)
&&((ASTNode)md.getBody().statements().get(0)).getLength() == length + 1)
return (ASTNode)md.getBody().statements().get(0);
}
if (parentNode instanceof SuperConstructorInvocation){
if (parentNode.getLength() == length + 1)
return parentNode;
}
if (parentNode instanceof ConstructorInvocation){
if (parentNode.getLength() == length + 1)
return parentNode;
}
return node;
}
private static boolean shouldInsertTempInitialization(MethodDeclaration constructor){
Assert.isTrue(constructor.isConstructor());
if (constructor.getBody() == null)
return false;
List<Statement> statements= constructor.getBody().statements();
if (statements == null)
return false;
if (statements.size() > 0 && statements.get(0) instanceof ConstructorInvocation)
return false;
return true;
}
@Override
public boolean visit(ConstructorInvocation node) {
if (matches(node.resolveConstructorBinding()) && fCurrent != null) {
fCurrent.addInvocation(node);
}
return true;
}
private boolean isUsedInExplicitConstructorCall() throws JavaModelException {
Expression selectedExpression= getSelectedExpression().getAssociatedExpression();
if (ASTNodes.getParent(selectedExpression, ConstructorInvocation.class) != null)
return true;
if (ASTNodes.getParent(selectedExpression, SuperConstructorInvocation.class) != null)
return true;
return false;
}
@Override
public ITypeConstraint[] create(ConstructorInvocation invocation){
List<Expression> arguments= invocation.arguments();
List<ITypeConstraint> result= new ArrayList<ITypeConstraint>(arguments.size());
IMethodBinding methodBinding= invocation.resolveConstructorBinding();
result.addAll(Arrays.asList(getArgumentConstraints(arguments, methodBinding)));
return result.toArray(new ITypeConstraint[result.size()]);
}
@Override
public void endVisit(ConstructorInvocation node) {
if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED) {
invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_cannotHandleThis, JavaStatusContext.create(fCUnit, node));
}
super.endVisit(node);
}
/**
* Find all constructor invocations (<code>this(...)</code>) from the called method.
* Since we only traverse into the AST on the wanted method declaration, this method
* should not hit on more constructor invocations than those in the wanted method.
*
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.ConstructorInvocation)
*/
@Override
public boolean visit(ConstructorInvocation node) {
progressMonitorWorked(1);
if (!isFurtherTraversalNecessary(node)) {
return false;
}
if (isNodeWithinMethod(node)) {
addMethodCall(node.resolveConstructorBinding(), node);
}
return true;
}
private static boolean isMethodArgument(Expression expression) {
ASTNode parent= expression;
while (parent instanceof Expression) {
if (parent.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY)
return true;
if (parent.getLocationInParent() == ConstructorInvocation.ARGUMENTS_PROPERTY)
return true;
parent= ((Expression) parent).getParent();
}
return false;
}