Java Code Examples for org.eclipse.jdt.internal.corext.util.JavaModelUtil#findMethod()

The following examples show how to use org.eclipse.jdt.internal.corext.util.JavaModelUtil#findMethod() . 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: AddGetterSetterOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Generates a new getter method for the specified field
 *
 * @param field the field
 * @param rewrite the list rewrite to use
 * @throws CoreException if an error occurs
 * @throws OperationCanceledException if the operation has been cancelled
 */
private void generateGetterMethod(final IField field, final ListRewrite rewrite) throws CoreException, OperationCanceledException {
	final IType type= field.getDeclaringType();
	final String name= GetterSetterUtil.getGetterName(field, null);
	final IMethod existing= JavaModelUtil.findMethod(name, EMPTY_STRINGS, false, type);
	if (existing == null || !querySkipExistingMethods(existing)) {
		IJavaElement sibling= null;
		if (existing != null) {
			sibling= StubUtility.findNextSibling(existing);
			removeExistingAccessor(existing, rewrite);
		} else
			sibling= fInsert;
		ASTNode insertion= StubUtility2.getNodeToInsertBefore(rewrite, sibling);
		addNewAccessor(type, field, GetterSetterUtil.getGetterStub(field, name, fSettings.createComments, fVisibility | (field.getFlags() & Flags.AccStatic)), rewrite, insertion);
	}
}
 
Example 2
Source File: AddGetterSetterOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Generates a new setter method for the specified field
 * 
 * @param field the field
 * @param astRewrite the AST rewrite to use
 * @param rewrite the list rewrite to use
 * @throws CoreException if an error occurs
 * @throws OperationCanceledException if the operation has been cancelled
 */
private void generateSetterMethod(final IField field, ASTRewrite astRewrite, final ListRewrite rewrite) throws CoreException, OperationCanceledException {
	final IType type= field.getDeclaringType();
	final String name= GetterSetterUtil.getSetterName(field, null);
	final IMethod existing= JavaModelUtil.findMethod(name, new String[] { field.getTypeSignature()}, false, type);
	if (existing == null || !querySkipExistingMethods(existing)) {
		IJavaElement sibling= null;
		if (existing != null) {
			sibling= StubUtility.findNextSibling(existing);
			removeExistingAccessor(existing, rewrite);
		} else
			sibling= fInsert;
		ASTNode insertion= StubUtility2.getNodeToInsertBefore(rewrite, sibling);
		addNewAccessor(type, field, GetterSetterUtil.getSetterStub(field, name, fSettings.createComments, fVisibility | (field.getFlags() & Flags.AccStatic)), rewrite, insertion);
		if (Flags.isFinal(field.getFlags())) {
			ASTNode fieldDecl= ASTNodes.getParent(NodeFinder.perform(fASTRoot, field.getNameRange()), FieldDeclaration.class);
			if (fieldDecl != null) {
				ModifierRewrite.create(astRewrite, fieldDecl).setModifiers(0, Modifier.FINAL, null);
			}
		}

	}
}
 
Example 3
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Resolves the method described by the receiver and returns it if found.
 * Returns <code>null</code> if no corresponding member can be found.
 *
 * @param proposal
 *            - completion proposal
 * @param javaProject
 *            - Java project
 *
 * @return the resolved method or <code>null</code> if none is found
 * @throws JavaModelException
 *             if accessing the java model fails
 */

public static IMethod resolveMethod(CompletionProposal proposal, IJavaProject javaProject) throws JavaModelException {
	char[] declarationSignature = proposal.getDeclarationSignature();
	String typeName = SignatureUtil.stripSignatureToFQN(String.valueOf(declarationSignature));
	IType type = javaProject.findType(typeName);
	if (type != null) {
		String name = String.valueOf(proposal.getName());
		if (proposal.getKind() == CompletionProposal.ANNOTATION_ATTRIBUTE_REF) {
			IMethod method = type.getMethod(name, CharOperation.NO_STRINGS);
			if (method.exists()) {
				return method;
			} else {
				return null;
			}
		}
		char[] signature = proposal.getSignature();
		if (proposal instanceof InternalCompletionProposal) {
			Binding binding = ((InternalCompletionProposal) proposal).getBinding();
			if (binding instanceof MethodBinding) {
				MethodBinding methodBinding = (MethodBinding) binding;
				MethodBinding original = methodBinding.original();
				if (original != binding) {
					signature = Engine.getSignature(original);
				}
			}
		}
		String[] parameters = Signature.getParameterTypes(String.valueOf(SignatureUtil.fix83600(signature)));
		for (int i = 0; i < parameters.length; i++) {
			parameters[i] = SignatureUtil.getLowerBound(parameters[i]);
		}
		boolean isConstructor = proposal.isConstructor();

		return JavaModelUtil.findMethod(name, parameters, isConstructor, type);
	}

	return null;
}
 
Example 4
Source File: RenameFieldProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public String getNewGetterName() throws CoreException {
	IMethod primaryGetterCandidate= JavaModelUtil.findMethod(GetterSetterUtil.getGetterName(fField, new String[0]), new String[0], false, fField.getDeclaringType());
	if (! JavaModelUtil.isBoolean(fField) || (primaryGetterCandidate != null && primaryGetterCandidate.exists())) {
		return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), JavaModelUtil.isBoolean(fField), null);
	}
	//bug 30906 describes why we need to look for other alternatives here
	return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), false, null);
}
 
Example 5
Source File: RenameFieldProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private RefactoringStatus checkNewAccessor(IMethod existingAccessor, String newAccessorName) throws CoreException{
	RefactoringStatus result= new RefactoringStatus();
	IMethod accessor= JavaModelUtil.findMethod(newAccessorName, existingAccessor.getParameterTypes(), false, fField.getDeclaringType());
	if (accessor == null || !accessor.exists()) {
		return null;
	}

	String message= Messages.format(RefactoringCoreMessages.RenameFieldRefactoring_already_exists,
			new String[]{JavaElementUtil.createMethodSignature(accessor), BasicElementLabels.getJavaElementName(fField.getDeclaringType().getFullyQualifiedName('.'))});
	result.addError(message, JavaStatusContext.create(accessor));
	return result;
}
 
Example 6
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addNecessaryMethodStubs(final List<IType> affected, final CompilationUnit root, final CompilationUnitRewrite unitRewriter, final Map<IMember, IncomingMemberVisibilityAdjustment> adjustments, final IProgressMonitor monitor, final RefactoringStatus status) throws CoreException {
	final IType declaringType= getDeclaringType();
	final IMethod[] methods= getAbstractMethods();
	try {
		monitor.beginTask(RefactoringCoreMessages.PullUpRefactoring_checking, affected.size());
		for (final Iterator<IType> iter= affected.iterator(); iter.hasNext();) {
			final IType type= iter.next();
			if (type.equals(declaringType))
				continue;
			final AbstractTypeDeclaration declaration= ASTNodeSearchUtil.getAbstractTypeDeclarationNode(type, unitRewriter.getRoot());
			final ICompilationUnit unit= type.getCompilationUnit();
			final IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 1);
			try {
				subMonitor.beginTask(RefactoringCoreMessages.PullUpRefactoring_checking, methods.length);
				for (int j= 0; j < methods.length; j++) {
					final IMethod method= methods[j];
					if (null == JavaModelUtil.findMethod(method.getElementName(), method.getParameterTypes(), method.isConstructor(), type)) {
						addMethodStubForAbstractMethod(method, root, declaration, unit, unitRewriter, adjustments, new SubProgressMonitor(subMonitor, 1), status);
					}
				}
				subMonitor.done();
			} finally {
				subMonitor.done();
			}
		}
	} finally {
		monitor.done();
	}
}
 
Example 7
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkIfMethodDeclaredIn(final IMethod iMethod, final IType type) throws JavaModelException {
	final IMethod methodInType= JavaModelUtil.findMethod(iMethod.getElementName(), iMethod.getParameterTypes(), iMethod.isConstructor(), type);
	if (methodInType == null || !methodInType.exists())
		return null;
	final String[] keys= { JavaElementLabels.getTextLabel(methodInType, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getTextLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED)};
	final String msg= Messages.format(RefactoringCoreMessages.PullUpRefactoring_Method_declared_in_class, keys);
	final RefactoringStatusContext context= JavaStatusContext.create(methodInType);
	return RefactoringStatus.createWarningStatus(msg, context);
}
 
Example 8
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isAvailableInDestination(final IMethod method, final IProgressMonitor monitor) throws JavaModelException {
	try {
		final IType destination= getDestinationType();
		final IMethod first= JavaModelUtil.findMethod(method.getElementName(), method.getParameterTypes(), false, destination);
		if (first != null && MethodChecks.isVirtual(first))
			return true;
		final ITypeHierarchy hierarchy= getDestinationTypeHierarchy(monitor);
		final IMethod found= JavaModelUtil.findMethodInHierarchy(hierarchy, destination, method.getElementName(), method.getParameterTypes(), false);
		return found != null && MethodChecks.isVirtual(found);
	} finally {
		monitor.done();
	}
}
 
Example 9
Source File: RenameFieldProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public String getNewGetterName() throws CoreException {
	IMethod primaryGetterCandidate= JavaModelUtil.findMethod(GetterSetterUtil.getGetterName(fField, new String[0]), new String[0], false, fField.getDeclaringType());
	if (! JavaModelUtil.isBoolean(fField) || (primaryGetterCandidate != null && primaryGetterCandidate.exists()))
		return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), JavaModelUtil.isBoolean(fField), null);
	//bug 30906 describes why we need to look for other alternatives here
	return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), false, null);
}
 
Example 10
Source File: RenameFieldProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus checkNewAccessor(IMethod existingAccessor, String newAccessorName) throws CoreException{
	RefactoringStatus result= new RefactoringStatus();
	IMethod accessor= JavaModelUtil.findMethod(newAccessorName, existingAccessor.getParameterTypes(), false, fField.getDeclaringType());
	if (accessor == null || !accessor.exists())
		return null;

	String message= Messages.format(RefactoringCoreMessages.RenameFieldRefactoring_already_exists,
			new String[]{JavaElementUtil.createMethodSignature(accessor), BasicElementLabels.getJavaElementName(fField.getDeclaringType().getFullyQualifiedName('.'))});
	result.addError(message, JavaStatusContext.create(accessor));
	return result;
}
 
Example 11
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IMethod getGetter(IField field) throws JavaModelException{
	String getterName= getGetterName(field, EMPTY, true);
	IMethod primaryCandidate= JavaModelUtil.findMethod(getterName, new String[0], false, field.getDeclaringType());
	if (! JavaModelUtil.isBoolean(field) || (primaryCandidate != null && primaryCandidate.exists()))
		return primaryCandidate;
	//bug 30906 describes why we need to look for other alternatives here (try with get... for booleans)
	String secondCandidateName= getGetterName(field, EMPTY, false);
	return JavaModelUtil.findMethod(secondCandidateName, new String[0], false, field.getDeclaringType());
}
 
Example 12
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static IMethod getSetter(IField field) throws JavaModelException{
	String[] args= new String[] { field.getTypeSignature() };
	return JavaModelUtil.findMethod(getSetterName(field, EMPTY), args, false, field.getDeclaringType());
}