下面列出了怎么用org.eclipse.jdt.core.dom.MethodRef的API类实例代码及写法,或者点击链接到github查看源代码。
private IMethod getMethod(ASTNode node) throws JavaModelException {
IBinding binding;
if (node instanceof MethodInvocation) {
binding = ((MethodInvocation) node).resolveMethodBinding();
} else if (node instanceof MethodRef) {
binding = ((MethodRef) node).resolveBinding();
} else if (node instanceof ClassInstanceCreation) {
binding = ((ClassInstanceCreation) node).resolveConstructorBinding();
} else {
binding = null;
}
if (binding != null) {
IJavaElement javaElement = binding.getJavaElement();
if (javaElement instanceof IMethod) {
IMethod method = (IMethod) javaElement;
return method;
}
}
return null;
}
@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);
}
@Override
public boolean visit(final MethodRef node) {
Name _qualifier = node.getQualifier();
boolean _tripleNotEquals = (_qualifier != null);
if (_tripleNotEquals) {
node.getQualifier().accept(this);
}
this.appendToBuffer("#");
node.getName().accept(this);
this.appendToBuffer("(");
this.visitAllSeparatedByComma(node.parameters());
this.appendToBuffer(")");
return false;
}
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
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);
}
@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) {
if (isMovedMember(node.resolveBinding()))
rewrite(node, fTarget);
return false;
}
/** @return {@inheritDoc} (element type: MethodRefParameter) */
@Override
protected ListRewrite getParamgumentsRewrite() {
return getASTRewrite().getListRewrite(fNode, MethodRef.PARAMETERS_PROPERTY);
}
@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));
}
@Override
public void endVisit(MethodRef node) {
endVisitNode(node);
}
@Override
public boolean visit(MethodRef node) {
return visitNode(node);
}
/**
* @return the javadoc reference to the old method in the javadoc comment.
* May be null if no comment was created.
*/
public MethodRef getJavadocReference() {
return fDocMethodReference;
}
/**
* @return the javadoc reference to the old method in the javadoc comment.
* May be null if no comment was created.
*/
public MethodRef getJavadocReference() {
return fDocMethodReference;
}