org.eclipse.jdt.core.dom.MethodRef Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.MethodRef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SignatureHelpHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: MovedMemberAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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();
		}
 
Example #6
Source File: ImportReferencesCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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;
}
 
Example #7
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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);
}
 
Example #8
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected SimpleName getMethodNameNode() {
	if (fNode instanceof MemberRef)
		return ((MemberRef) fNode).getName();

	if (fNode instanceof MethodRef)
		return ((MethodRef) fNode).getName();

	return null;
}
 
Example #9
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void updateNode() {
	if (fNode instanceof MethodRef) {
		changeParamguments();
		reshuffleElements();
	}
	if (canChangeNameAndReturnType())
		changeMethodName();
}
 
Example #10
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example #11
Source File: MoveStaticMemberAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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());
}
 
Example #12
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@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;
}
 
Example #13
Source File: ChangeSignatureProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/** @return {@inheritDoc} (element type: MethodRefParameter) */
@Override
protected ListRewrite getParamgumentsRewrite() {
	return getASTRewrite().getListRewrite(fNode, MethodRef.PARAMETERS_PROPERTY);
}
 
Example #14
Source File: ReferenceAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(MethodRef node) {
	if (isMovedMember(node.resolveBinding()))
		rewrite(node, fTarget);
	return false;
}
 
Example #15
Source File: AstMatchingNodeFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(MethodRef node) {
	if (node.subtreeMatch(fMatcher, fNodeToMatch))
		return matches(node);
	return super.visit(node);
}
 
Example #16
Source File: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void rewriteJavadocReference(MethodRef javadocRef, ASTRewrite unitRewriter, TextEditGroup gd) {
	AST ast= unitRewriter.getAST();
	unitRewriter.replace(javadocRef.getName(), ast.newSimpleName(fNewMethodName), gd);
}
 
Example #17
Source File: IntroduceFactoryRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 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));
}
 
Example #18
Source File: GenericVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void endVisit(MethodRef node) {
	endVisitNode(node);
}
 
Example #19
Source File: GenericVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(MethodRef node) {
	return visitNode(node);
}
 
Example #20
Source File: DelegateMethodCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * @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;
}
 
Example #21
Source File: DelegateMethodCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * @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;
}