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 boolean visit(SuperConstructorInvocation node) { IMethodBinding mmtb = node.resolveConstructorBinding(); if (mtbStack.isEmpty()) // not part of a method return true; // make field access fact try { facts.add(Fact.makeCallsFact(getQualifiedName(mtbStack.peek()), getQualifiedName(mmtb))); } catch (Exception e) { System.err .println("Cannot resolve super constructor invocation in \"" + "\""); } return true; }
public boolean visit(SuperConstructorInvocation node) { IMethodBinding mmtb = node.resolveConstructorBinding(); if (this.mtbStack.isEmpty()) { return true; } try { this.facts.add(Fact.makeCallsFact(getQualifiedName((IMethodBinding)this.mtbStack.peek()), getQualifiedName(mmtb))); } catch (Exception e) { System.err.println("Cannot resolve super constructor invocation in \"\""); } return true; }
public boolean visit(SuperConstructorInvocation node) { IMethodBinding mmtb = node.resolveConstructorBinding(); if (this.mtbStack.isEmpty()) { return true; } try { this.facts.add(Fact.makeCallsFact(getQualifiedName((IMethodBinding)this.mtbStack.peek()), getQualifiedName(mmtb))); } catch (Exception localException) { System.err.println("Cannot resolve super constructor invocation in \"\""); } return true; }
private void addNewConstructorToSubclass( AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast = subclass.getAST(); MethodDeclaration newConstructor = ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setJavadoc(null); newConstructor .modifiers() .addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body = ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall = ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg = RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description = cuRewrite.createGroupDescription(msg); cuRewrite .getASTRewrite() .getListRewrite(subclass, subclass.getBodyDeclarationsProperty()) .insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
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 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 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()); } }
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; } }
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast= subclass.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setJavadoc(null); newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body= ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description= cuRewrite.createGroupDescription(msg); cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
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 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 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()); } }
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; } }
private SuperConstructorInvocation addEnclosingInstanceAccess(ASTRewrite rewrite, ImportRewriteContext importRewriteContext, List<SingleVariableDeclaration> parameters, String[] paramNames, ITypeBinding enclosingInstance) { AST ast= rewrite.getAST(); SuperConstructorInvocation invocation= ast.newSuperConstructorInvocation(); SingleVariableDeclaration var= ast.newSingleVariableDeclaration(); var.setType(getImportRewrite().addImport(enclosingInstance, ast, importRewriteContext)); String[] enclosingArgNames= StubUtility.getArgumentNameSuggestions(getCompilationUnit().getJavaProject(), enclosingInstance.getTypeDeclaration().getName(), 0, paramNames); String firstName= enclosingArgNames[0]; var.setName(ast.newSimpleName(firstName)); parameters.add(var); Name enclosing= ast.newSimpleName(firstName); invocation.setExpression(enclosing); String key= "arg_name_" + firstName; //$NON-NLS-1$ addLinkedPosition(rewrite.track(enclosing), false, key); for (int i= 0; i < enclosingArgNames.length; i++) { addLinkedPositionProposal(key, enclosingArgNames[i], null); // alternative names } return invocation; }
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) { AST ast= subclass.getAST(); MethodDeclaration newConstructor= ast.newMethodDeclaration(); newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier())); newConstructor.setConstructor(true); newConstructor.setExtraDimensions(0); newConstructor.setJavadoc(null); newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass))); newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID)); Block body= ast.newBlock(); newConstructor.setBody(body); SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); body.statements().add(superCall); String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor; TextEditGroup description= cuRewrite.createGroupDescription(msg); cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description); // TODO use AbstractTypeDeclaration }
@Override public boolean visit(final SuperConstructorInvocation node) { // method binding for this creation final IMethodBinding method = node.resolveConstructorBinding().getMethodDeclaration(); // create the resolved call final IResolvedCall rcall = createResolvedCall(node, node.getStartPosition(), method); // process the arguments if (rcall != null) { // actual-ins processActualParameters(node, node.getStartPosition(), rcall, node.arguments()); // append the call after all its arguments uses.add(rcall); } // do not visit children return false; }
/** * Creates constructors which just call super constructors. */ private void createConstructors() { for (int i = 0; i < constructors.size(); i++) { MethodDeclaration md = (MethodDeclaration)ASTNode.copySubtree(ast, constructors.get(i)); // set name SimpleName name = (SimpleName)ASTNode.copySubtree(ast, td.getName()); md.setName(name); // set public modifier setPublic(md); // add to the TD body declarations td.bodyDeclarations().add(md); // call a parent constructor SuperConstructorInvocation superCall = ast.newSuperConstructorInvocation(); for (int j = 0; j < md.parameters().size(); j++) { SingleVariableDeclaration argument = (SingleVariableDeclaration)md.parameters().get(j); Name argName = (Name)ASTNode.copySubtree(ast, argument.getName()); superCall.arguments().add(argName); } md.getBody().statements().add(superCall); } }
public Binding getBindingForSuperConstructorInvocation( final SuperConstructorInvocation constructorCall) { Binding result = null; IMethodBinding binding = constructorCall.resolveConstructorBinding(); if (binding == null || binding.getDeclaringClass() == null) { // managing misc binding.getName() NPE if (this.logJDTBindingsIssues) { MoDiscoLogger.logWarning("*** WARNING : binding '" //$NON-NLS-1$ + constructorCall.toString() + "' unresolved.", //$NON-NLS-1$ JavaActivator.getDefault()); } result = new UnresolvedBinding(constructorCall.toString()); } else { result = getMethodBinding(binding); } return result; }
@Override public boolean visit(SuperConstructorInvocation node) { //System.out.println("Found: " + node.getClass()); // TODO Type arguments print("super("); int printed = 0; for (Object o : node.arguments()) { Expression t = (Expression)o; if (printed > 0) { print(", "); } t.accept(this); printed++; } println(");"); return false; }
private void populateBodyWithSuperConstructorCall(AST ast, TypeDeclaration builderType, Block body, List<ConstructorParameterSetterBuilderField> builderFields) { SuperConstructorInvocation superInvocation = ast.newSuperConstructorInvocation(); builderFields.stream() .sorted((first, second) -> first.getIndex().compareTo(second.getIndex())) .forEach(constructorParameter -> addConstructorParameter(ast, builderType, superInvocation, constructorParameter)); if (!builderFields.isEmpty()) { body.statements().add(superInvocation); } }
@Override public boolean visit(SuperConstructorInvocation node) { if (!isAffected(node)) { return false; } evalQualifyingExpression(node.getExpression(), null); doVisitChildren(node.typeArguments()); doVisitChildren(node.arguments()); return false; }
@Override public boolean visit(SuperConstructorInvocation node) { if (!isSelected(node)) { return false; } return handleExceptions(node.resolveConstructorBinding(), node); }
@Override public void endVisit(SuperConstructorInvocation node) { if (getSelection().getEndVisitSelectionMode(node) == Selection.SELECTED) { invalidSelection(RefactoringCoreMessages.SurroundWithTryCatchAnalyzer_cannotHandleSuper, JavaStatusContext.create(fCUnit, node)); } super.endVisit(node); }
@Override public boolean visit(SuperConstructorInvocation loc) { if (currentMethod == null) return false; if (loc.resolveConstructorBinding() == null) return false; if (loc.resolveConstructorBinding().getMethodDeclaration() == null || loc.resolveConstructorBinding().getMethodDeclaration().getJavaElement() == null) return true; if (loc.resolveConstructorBinding().getMethodDeclaration().getJavaElement().isReadOnly()) return true; if (loc.resolveConstructorBinding().getJavaElement() != null) addCalledMethod(loc.resolveConstructorBinding()); return true; }
public boolean visit(SuperConstructorInvocation node) { if (node.getStartPosition() == this.loc) { final Expression param = (Expression) node.arguments().get( this.paramNumber); this.expressions.add(param); } return true; }
private void findFormalsForVariable(SuperConstructorInvocation ctorCall) throws JavaModelException, CoreException { final IMethod meth = (IMethod) ctorCall.resolveConstructorBinding() .getJavaElement(); final IMethod top = Util.getTopMostSourceMethod(meth, this.monitor); if (top == null) throw new DefinitelyNotEnumerizableException(Messages.ASTNodeProcessor_SourceNotPresent, ctorCall); else this.findFormalsForVariable(top, getParamNumber(ctorCall .arguments(), this.name)); }
private void addExplicitSuperConstructorCall( MethodDeclaration constructor, CompilationUnitRewrite cuRewrite) { SuperConstructorInvocation superCall = constructor.getAST().newSuperConstructorInvocation(); addArgumentsToNewSuperConstructorCall(superCall, cuRewrite); String msg = RefactoringCoreMessages.ChangeSignatureRefactoring_add_super_call; TextEditGroup description = cuRewrite.createGroupDescription(msg); cuRewrite .getASTRewrite() .getListRewrite(constructor.getBody(), Block.STATEMENTS_PROPERTY) .insertFirst(superCall, description); }
private void addArgumentsToNewSuperConstructorCall( SuperConstructorInvocation superCall, CompilationUnitRewrite cuRewrite) { Iterator<ParameterInfo> iter = getNotDeletedInfos().iterator(); while (iter.hasNext()) { ParameterInfo info = iter.next(); Expression newExpression = createNewExpression( info, getParameterInfos(), superCall.arguments(), cuRewrite, (MethodDeclaration) ASTNodes.getParent(superCall, MethodDeclaration.class)); if (newExpression != null) superCall.arguments().add(newExpression); } }
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; }