private OccurrenceUpdate<? extends ASTNode> createOccurrenceUpdate( ASTNode node, CompilationUnitRewrite cuRewrite, RefactoringStatus result) { if (BUG_89686 && node instanceof SimpleName && node.getParent() instanceof EnumConstantDeclaration) node = node.getParent(); if (Invocations.isInvocationWithArguments(node)) return new ReferenceUpdate(node, cuRewrite, result); else if (node instanceof SimpleName && node.getParent() instanceof MethodDeclaration) return new DeclarationUpdate((MethodDeclaration) node.getParent(), cuRewrite, result); else if (node instanceof MemberRef || node instanceof MethodRef) return new DocReferenceUpdate(node, cuRewrite, result); else if (ASTNodes.getParent(node, ImportDeclaration.class) != null) return new StaticImportUpdate( (ImportDeclaration) ASTNodes.getParent(node, ImportDeclaration.class), cuRewrite, result); else if (node instanceof LambdaExpression) return new LambdaExpressionUpdate((LambdaExpression) node, cuRewrite, result); else if (node.getLocationInParent() == ExpressionMethodReference.NAME_PROPERTY) return new ExpressionMethodRefUpdate( (ExpressionMethodReference) node.getParent(), cuRewrite, result); else return new NullOccurrenceUpdate(node, cuRewrite, result); }
@Override protected void changeParamgumentType(ParameterInfo info) { if (!(fNode instanceof MethodRef)) return; MethodRefParameter oldParam = (MethodRefParameter) ((MethodRef) fNode).parameters().get(info.getOldIndex()); Type oldTypeNode = oldParam.getType(); Type newTypeNode = createNewDocRefType(info); if (info.isNewVarargs()) { if (info.isOldVarargs() && !oldParam.isVarargs()) { // leave as array reference if old reference was not vararg newTypeNode = ASTNodeFactory.newArrayType(newTypeNode); } else { getASTRewrite() .set(oldParam, MethodRefParameter.VARARGS_PROPERTY, Boolean.TRUE, fDescription); } } else { if (oldParam.isVarargs()) { getASTRewrite() .set(oldParam, MethodRefParameter.VARARGS_PROPERTY, Boolean.FALSE, fDescription); } } getASTRewrite().replace(oldTypeNode, newTypeNode, fDescription); registerImportRemoveNode(oldTypeNode); }
@Override public boolean visit(MethodRef node) { IBinding binding= node.resolveBinding(); if (isSourceAccess(binding)) { if (isMovedMember(binding)) { if (node.getQualifier() != null) rewrite(node, fTarget); } else rewrite(node, fSource); } else if (isTargetAccess(binding)) { // remove qualifier: SimpleName replace= (SimpleName)fCuRewrite.getASTRewrite().createCopyTarget(node.getName()); fCuRewrite.getASTRewrite().replace(node, replace, null); fCuRewrite.getImportRemover().registerRemovedNode(node); } return super.visit(node); }
private OccurrenceUpdate<? extends ASTNode> createOccurrenceUpdate(ASTNode node, CompilationUnitRewrite cuRewrite, RefactoringStatus result) { if (BUG_89686 && node instanceof SimpleName && node.getParent() instanceof EnumConstantDeclaration) node= node.getParent(); if (Invocations.isInvocationWithArguments(node)) return new ReferenceUpdate(node, cuRewrite, result); else if (node instanceof SimpleName && node.getParent() instanceof MethodDeclaration) return new DeclarationUpdate((MethodDeclaration) node.getParent(), cuRewrite, result); else if (node instanceof MemberRef || node instanceof MethodRef) return new DocReferenceUpdate(node, cuRewrite, result); else if (ASTNodes.getParent(node, ImportDeclaration.class) != null) return new StaticImportUpdate((ImportDeclaration) ASTNodes.getParent(node, ImportDeclaration.class), cuRewrite, result); else if (node instanceof LambdaExpression) return new LambdaExpressionUpdate((LambdaExpression) node, cuRewrite, result); else if (node.getLocationInParent() == ExpressionMethodReference.NAME_PROPERTY) return new ExpressionMethodRefUpdate((ExpressionMethodReference) node.getParent(), cuRewrite, result); else return new NullOccurrenceUpdate(node, cuRewrite, result); }
private void addDelegate() throws JavaModelException { DelegateMethodCreator creator= new DelegateMethodCreator(); creator.setDeclaration(fMethDecl); creator.setDeclareDeprecated(fDelegateDeprecation); creator.setSourceRewrite(fCuRewrite); creator.prepareDelegate(); /* * The delegate now contains a call and a javadoc reference to the * old method (i.e., to itself). * * Use ReferenceUpdate() / DocReferenceUpdate() to update these * references like any other reference. */ final ASTNode delegateInvocation= creator.getDelegateInvocation(); if (delegateInvocation != null) // may be null if the delegate is an interface method or // abstract -> no body new ReferenceUpdate(delegateInvocation, creator.getDelegateRewrite(), fResult).updateNode(); MethodRef javadocReference= creator.getJavadocReference(); if (javadocReference != null) new DocReferenceUpdate(javadocReference, creator.getDelegateRewrite(), fResult).updateNode(); creator.createEdit(); }
@Override protected void changeParamgumentType(ParameterInfo info) { if (! (fNode instanceof MethodRef)) return; MethodRefParameter oldParam= (MethodRefParameter) ((MethodRef) fNode).parameters().get(info.getOldIndex()); Type oldTypeNode= oldParam.getType(); Type newTypeNode= createNewDocRefType(info); if (info.isNewVarargs()) { if (info.isOldVarargs() && ! oldParam.isVarargs()) { // leave as array reference if old reference was not vararg newTypeNode= ASTNodeFactory.newArrayType(newTypeNode); } else { getASTRewrite().set(oldParam, MethodRefParameter.VARARGS_PROPERTY, Boolean.TRUE, fDescription); } } else { if (oldParam.isVarargs()) { getASTRewrite().set(oldParam, MethodRefParameter.VARARGS_PROPERTY, Boolean.FALSE, fDescription); } } getASTRewrite().replace(oldTypeNode, newTypeNode, fDescription); registerImportRemoveNode(oldTypeNode); }
private OccurrenceUpdate<? extends ASTNode> createOccurrenceUpdate(ASTNode node, CompilationUnitRewrite cuRewrite, RefactoringStatus result) { if (BUG_89686 && node instanceof SimpleName && node.getParent() instanceof EnumConstantDeclaration) node= node.getParent(); if (Invocations.isInvocationWithArguments(node)) return new ReferenceUpdate(node, cuRewrite, result); else if (node instanceof SimpleName && node.getParent() instanceof MethodDeclaration) return new DeclarationUpdate((MethodDeclaration) node.getParent(), cuRewrite, result); else if (node instanceof MemberRef || node instanceof MethodRef) return new DocReferenceUpdate(node, cuRewrite, result); else if (ASTNodes.getParent(node, ImportDeclaration.class) != null) return new StaticImportUpdate((ImportDeclaration) ASTNodes.getParent(node, ImportDeclaration.class), cuRewrite, result); else return new NullOccurrenceUpdate(node, cuRewrite, result); }
@Override protected void changeParamgumentType(ParameterInfo info) { if (! (fNode instanceof MethodRef)) return; MethodRefParameter oldParam= (MethodRefParameter) ((MethodRef) fNode).parameters().get(info.getOldIndex()); Type oldTypeNode= oldParam.getType(); Type newTypeNode= createNewDocRefType(info); if (info.isNewVarargs()) { if (info.isOldVarargs() && ! oldParam.isVarargs()) { // leave as array reference of old reference was not vararg newTypeNode= getASTRewrite().getAST().newArrayType(newTypeNode); } else { getASTRewrite().set(oldParam, MethodRefParameter.VARARGS_PROPERTY, Boolean.TRUE, fDescription); } } else { if (oldParam.isVarargs()) { getASTRewrite().set(oldParam, MethodRefParameter.VARARGS_PROPERTY, Boolean.FALSE, fDescription); } } getASTRewrite().replace(oldTypeNode, newTypeNode, fDescription); registerImportRemoveNode(oldTypeNode); }
@Override public boolean visit(MethodRef node) { if (node.getQualifier() != null) { node.getQualifier().accept(this); } this.fBuffer.append("#");//$NON-NLS-1$ node.getName().accept(this); this.fBuffer.append("(");//$NON-NLS-1$ for (Iterator<MethodRefParameter> it= node.parameters().iterator(); it.hasNext();) { MethodRefParameter e= it.next(); e.accept(this); if (it.hasNext()) { this.fBuffer.append(",");//$NON-NLS-1$ } } this.fBuffer.append(")");//$NON-NLS-1$ return false; }
public boolean visit(MethodRef node) { if (node.getQualifier() != null) { node.getQualifier().accept(this); } this.buffer.append("#");//$NON-NLS-1$ node.getName().accept(this); this.buffer.append("(");//$NON-NLS-1$ for (Iterator it = node.parameters().iterator(); it.hasNext(); ) { MethodRefParameter e = (MethodRefParameter) it.next(); e.accept(this); if (it.hasNext()) { this.buffer.append(",");//$NON-NLS-1$ } } this.buffer.append(")");//$NON-NLS-1$ return false; }
@Override public boolean visit(MethodRef node) { Name qualifier= node.getQualifier(); if (qualifier != null) { typeRefFound(qualifier); } List<MethodRefParameter> list= node.parameters(); if (list != null) { doVisitChildren(list); // visit MethodRefParameter with Type } return false; }
@Override public boolean visit(MethodRef node) { Name qualifier = node.getQualifier(); if (qualifier != null) { typeRefFound(qualifier); } List<MethodRefParameter> list = node.parameters(); if (list != null) { doVisitChildren(list); // visit MethodRefParameter with Type } return false; }
private void addDelegate() throws JavaModelException { DelegateMethodCreator creator = new DelegateMethodCreator(); creator.setDeclaration(fMethDecl); creator.setDeclareDeprecated(fDelegateDeprecation); creator.setSourceRewrite(fCuRewrite); creator.prepareDelegate(); /* * The delegate now contains a call and a javadoc reference to the * old method (i.e., to itself). * * Use ReferenceUpdate() / DocReferenceUpdate() to update these * references like any other reference. */ final ASTNode delegateInvocation = creator.getDelegateInvocation(); if (delegateInvocation != null) // may be null if the delegate is an interface method or // abstract -> no body new ReferenceUpdate(delegateInvocation, creator.getDelegateRewrite(), fResult).updateNode(); MethodRef javadocReference = creator.getJavadocReference(); if (javadocReference != null) new DocReferenceUpdate(javadocReference, creator.getDelegateRewrite(), fResult) .updateNode(); creator.createEdit(); }
@Override public void updateNode() { if (fNode instanceof MethodRef) { changeParamguments(); reshuffleElements(); } if (canChangeNameAndReturnType()) changeMethodName(); }
@Override protected SimpleName getMethodNameNode() { if (fNode instanceof MemberRef) return ((MemberRef) fNode).getName(); if (fNode instanceof MethodRef) return ((MethodRef) fNode).getName(); return null; }
@Override protected void changeParamgumentName(ParameterInfo info) { if (!(fNode instanceof MethodRef)) return; MethodRefParameter oldParam = (MethodRefParameter) ((MethodRef) fNode).parameters().get(info.getOldIndex()); SimpleName oldParamName = oldParam.getName(); if (oldParamName != null) getASTRewrite() .set(oldParamName, SimpleName.IDENTIFIER_PROPERTY, info.getNewName(), fDescription); }
protected void rewrite(MethodRef node, ITypeBinding type) { Name qualifier= node.getQualifier(); if (qualifier == null) { ImportRewriteContext context= new ContextSensitiveImportRewriteContext(node, fCuRewrite.getImportRewrite()); Type result= fCuRewrite.getImportRewrite().addImport(type, fCuRewrite.getAST(), context); fCuRewrite.getImportRemover().registerAddedImport(type.getQualifiedName()); qualifier= ASTNodeFactory.newName(fCuRewrite.getAST(), ASTFlattener.asString(result)); fCuRewrite.getASTRewrite().set(node, MethodRef.QUALIFIER_PROPERTY, qualifier, fCuRewrite.createGroupDescription(REFERENCE_UPDATE)); fNeedsImport= true; } else { rewriteName(qualifier, type); } fProcessed.add(node.getName()); }
/** * Creates the necessary change to updated a comment reference represented * by a search match. * * @param rewrite * the current compilation unit rewrite * @param declaration * the source method declaration * @param match * the search match representing the method reference * @param status * the refactoring status */ protected void createMethodJavadocReference(CompilationUnitRewrite rewrite, MethodDeclaration declaration, SearchMatch match, RefactoringStatus status) { Assert.isNotNull(rewrite); Assert.isNotNull(declaration); Assert.isNotNull(match); Assert.isNotNull(status); final ASTNode node= ASTNodeSearchUtil.findNode(match, rewrite.getRoot()); if (node instanceof MethodRef) { final AST ast= node.getAST(); final MethodRef successor= ast.newMethodRef(); rewrite.getASTRewrite().replace(node, successor, null); } }
@Override protected void changeParamgumentName(ParameterInfo info) { if (! (fNode instanceof MethodRef)) return; MethodRefParameter oldParam= (MethodRefParameter) ((MethodRef) fNode).parameters().get(info.getOldIndex()); SimpleName oldParamName= oldParam.getName(); if (oldParamName != null) getASTRewrite().set(oldParamName, SimpleName.IDENTIFIER_PROPERTY, info.getNewName(), fDescription); }
/** @return {@inheritDoc} (element type: MethodRefParameter) */ @Override protected ListRewrite getParamgumentsRewrite() { return getASTRewrite().getListRewrite(fNode, MethodRef.PARAMETERS_PROPERTY); }
private void rewriteJavadocReference( MethodRef javadocRef, ASTRewrite unitRewriter, TextEditGroup gd) { AST ast = unitRewriter.getAST(); unitRewriter.replace(javadocRef.getName(), ast.newSimpleName(fNewMethodName), gd); }
/** {@inheritDoc} */ @Override public void endVisit(MethodRef node) { logger.warn("Method endVisitMethodRef for " + node + " for " + node + " not implemented!"); super.endVisit(node); }
/** {@inheritDoc} */ @Override public boolean visit(MethodRef node) { logger.warn("Method visitMethodRef for " + node + " not implemented!"); return super.visit(node); }
@Override public boolean visit(final MethodRef node) { return false; }
@Override public boolean visit(final MethodRef node) { // will never be visited assert false; return false; }
@Override public boolean visit(MethodRef node) { if (isMovedMember(node.resolveBinding())) rewrite(node, fTarget); return false; }
@Override public boolean visit(MethodRef node) { if (node.subtreeMatch(fMatcher, fNodeToMatch)) return matches(node); return super.visit(node); }
private void rewriteJavadocReference(MethodRef javadocRef, ASTRewrite unitRewriter, TextEditGroup gd) { AST ast= unitRewriter.getAST(); unitRewriter.replace(javadocRef.getName(), ast.newSimpleName(fNewMethodName), gd); }
/** * Look "in the vicinity" of the given range to find the <code>ClassInstanceCreation</code> * node that this search hit identified. Necessary because the <code>SearchEngine</code> * doesn't always cough up text extents that <code>NodeFinder.perform()</code> agrees with. * @param start * @param length * @param unitAST * @return return a {@link ClassInstanceCreation} or a {@link MethodRef} or <code>null</code> if this is really a constructor->constructor call (e.g. "this(...)") * @throws CoreException */ private ASTNode getCtorCallAt(int start, int length, CompilationUnit unitAST) throws CoreException { ICompilationUnit unitHandle= ASTCreator.getCu(unitAST); ASTNode node= NodeFinder.perform(unitAST, start, length); if (node == null) throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noASTNodeForConstructorSearchHit, new Object[] { Integer.toString(start), Integer.toString(start + length), BasicElementLabels.getJavaCodeString(unitHandle.getSource().substring(start, start + length)), BasicElementLabels.getFileName(unitHandle) }), null)); if (node instanceof ClassInstanceCreation) { if (((ClassInstanceCreation)node).getAnonymousClassDeclaration() != null) { // Cannot replace anonymous inner class, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=250660 fConstructorVisibility= Modifier.PROTECTED; return null; } return node; } else if (node instanceof VariableDeclaration) { Expression init= ((VariableDeclaration) node).getInitializer(); if (init instanceof ClassInstanceCreation) { return init; } else if (init != null) throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedInitializerNodeType, new Object[] { BasicElementLabels.getJavaCodeString(init.toString()), BasicElementLabels.getFileName(unitHandle) }), null)); else throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noConstructorCallNodeInsideFoundVarbleDecl, BasicElementLabels.getJavaCodeString(node.toString())), null)); } else if (node instanceof ConstructorInvocation) { // This is a call we can bypass; it's from one constructor flavor // to another flavor on the same class. return null; } else if (node instanceof SuperConstructorInvocation) { // This is a call we can bypass; it's from one constructor flavor // to another flavor on the same class. fConstructorVisibility= Modifier.PROTECTED; return null; } else if (node instanceof ExpressionStatement) { Expression expr= ((ExpressionStatement) node).getExpression(); if (expr instanceof ClassInstanceCreation) return expr; else throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaCodeString(expr.toString()), BasicElementLabels.getFileName(unitHandle) }), null)); } else if (node instanceof SimpleName && (node.getParent() instanceof MethodDeclaration || node.getParent() instanceof AbstractTypeDeclaration)) { // We seem to have been given a hit for an implicit call to the base-class constructor. // Do nothing with this (implicit) call, but have to make sure we make the derived class // doesn't lose access to the base-class constructor (so make it 'protected', not 'private'). fConstructorVisibility= Modifier.PROTECTED; return null; } else if (node instanceof MethodRef) { return node; } else throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaElementName(node.getClass().getName() + "('" + node.toString() + "')"), BasicElementLabels.getFileName(unitHandle) }), //$NON-NLS-1$ //$NON-NLS-2$ null)); }