Java Code Examples for org.eclipse.jdt.core.IMethod#equals()

The following examples show how to use org.eclipse.jdt.core.IMethod#equals() . 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: FindLinksHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static IMethod findOverriddenMethod(IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
	if (!(element instanceof IMethod)) {
		return null;
	}

	IMethod method = (IMethod) element;
	IType type = method.getDeclaringType();
	if (type == null || type.isInterface() || method.isConstructor()) {
		return null;
	}

	ITypeHierarchy hierarchy = type.newSupertypeHierarchy(monitor);
	MethodOverrideTester tester = new MethodOverrideTester(type, hierarchy);
	IMethod found = tester.findOverriddenMethod(method, true);
	if (found != null && !found.equals(method)) {
		return found;
	}

	return null;
}
 
Example 2
Source File: MethodChecks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static IMethod isDeclaredInInterface(IMethod method, ITypeHierarchy hierarchy, IProgressMonitor monitor) throws JavaModelException {
	Assert.isTrue(isVirtual(method));
	IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 1);
	try {
		IType[] classes= hierarchy.getAllClasses();
		subMonitor.beginTask("", classes.length); //$NON-NLS-1$
		for (int i= 0; i < classes.length; i++) {
			final IType clazz= classes[i];
			IType[] superinterfaces= null;
			if (clazz.equals(hierarchy.getType()))
				superinterfaces= hierarchy.getAllSuperInterfaces(clazz);
			else
				superinterfaces= clazz.newSupertypeHierarchy(new SubProgressMonitor(subMonitor, 1)).getAllSuperInterfaces(clazz);
			for (int j= 0; j < superinterfaces.length; j++) {
				IMethod found= Checks.findSimilarMethod(method, superinterfaces[j]);
				if (found != null && !found.equals(method))
					return found;
			}
			subMonitor.worked(1);
		}
		return null;
	} finally {
		subMonitor.done();
	}
}
 
Example 3
Source File: MethodChecks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Locates the topmost method of an override ripple and returns it. If none
 * is found, null is returned.
 *
 * @param method the IMethod which may be part of a ripple
 * @param typeHierarchy a ITypeHierarchy of the declaring type of the method. May be null
 * @param monitor an IProgressMonitor
 * @return the topmost method of the ripple, or null if none
 * @throws JavaModelException
 */
public static IMethod getTopmostMethod(IMethod method, ITypeHierarchy typeHierarchy, IProgressMonitor monitor) throws JavaModelException {

	Assert.isNotNull(method);

	ITypeHierarchy hierarchy= typeHierarchy;
	IMethod topmostMethod= null;
	final IType declaringType= method.getDeclaringType();
	if (!declaringType.isInterface()) {
		if ((hierarchy == null) || !declaringType.equals(hierarchy.getType()))
			hierarchy= declaringType.newTypeHierarchy(monitor);

		IMethod inInterface= isDeclaredInInterface(method, hierarchy, monitor);
		if (inInterface != null && !inInterface.equals(method))
			topmostMethod= inInterface;
	}
	if (topmostMethod == null) {
		if (hierarchy == null)
			hierarchy= declaringType.newSupertypeHierarchy(monitor);
		IMethod overrides= overridesAnotherMethod(method, hierarchy);
		if (overrides != null && !overrides.equals(method))
			topmostMethod= overrides;
	}
	return topmostMethod;
}
 
Example 4
Source File: MethodsLabelProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IType getDefiningType(Object element) throws JavaModelException {
	int kind= ((IJavaElement) element).getElementType();

	if (kind != IJavaElement.METHOD && kind != IJavaElement.FIELD && kind != IJavaElement.INITIALIZER) {
		return null;
	}
	IType declaringType= ((IMember) element).getDeclaringType();
	if (kind != IJavaElement.METHOD) {
		return declaringType;
	}
	ITypeHierarchy hierarchy= fHierarchy.getHierarchy();
	if (hierarchy == null) {
		return declaringType;
	}
	IMethod method= (IMethod) element;
	MethodOverrideTester tester= new MethodOverrideTester(declaringType, hierarchy);
	IMethod res= tester.findDeclaringMethod(method, true);
	if (res == null || method.equals(res)) {
		return declaringType;
	}
	return res.getDeclaringType();
}
 
Example 5
Source File: JavaOutlineInformationControl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IType getDefiningType(Object element) throws JavaModelException {
	int kind= ((IJavaElement) element).getElementType();

	if (kind != IJavaElement.METHOD && kind != IJavaElement.FIELD && kind != IJavaElement.INITIALIZER) {
		return null;
	}
	IType declaringType= ((IMember) element).getDeclaringType();
	if (kind != IJavaElement.METHOD) {
		return declaringType;
	}
	ITypeHierarchy hierarchy= getSuperTypeHierarchy(declaringType);
	if (hierarchy == null) {
		return declaringType;
	}
	IMethod method= (IMethod) element;
	MethodOverrideTester tester= new MethodOverrideTester(declaringType, hierarchy);
	IMethod res= tester.findDeclaringMethod(method, true);
	if (res == null || method.equals(res)) {
		return declaringType;
	}
	return res.getDeclaringType();
}
 
Example 6
Source File: MethodChecks.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static IMethod isDeclaredInInterface(IMethod method, ITypeHierarchy hierarchy, IProgressMonitor monitor) throws JavaModelException {
	Assert.isTrue(isVirtual(method));
	IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 1);
	try {
		IType[] classes= hierarchy.getAllClasses();
		subMonitor.beginTask("", classes.length); //$NON-NLS-1$
		for (int i= 0; i < classes.length; i++) {
			final IType clazz= classes[i];
			IType[] superinterfaces= null;
			if (clazz.equals(hierarchy.getType())) {
				superinterfaces= hierarchy.getAllSuperInterfaces(clazz);
			} else {
				superinterfaces= clazz.newSupertypeHierarchy(new SubProgressMonitor(subMonitor, 1)).getAllSuperInterfaces(clazz);
			}
			for (int j= 0; j < superinterfaces.length; j++) {
				IMethod found= Checks.findSimilarMethod(method, superinterfaces[j]);
				if (found != null && !found.equals(method)) {
					return found;
				}
			}
			subMonitor.worked(1);
		}
		return null;
	} finally {
		subMonitor.done();
	}
}
 
Example 7
Source File: MethodChecks.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static IMethod overridesAnotherMethod(IMethod method, ITypeHierarchy hierarchy) throws JavaModelException {
	MethodOverrideTester tester= new MethodOverrideTester(method.getDeclaringType(), hierarchy);
	IMethod found= tester.findDeclaringMethod(method, true);
	boolean overrides= (found != null && !found.equals(method) && (!JdtFlags.isStatic(found)) && (!JdtFlags.isPrivate(found)));
	if (overrides) {
		return found;
	} else {
		return null;
	}
}
 
Example 8
Source File: MethodChecks.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Locates the topmost method of an override ripple and returns it. If none
 * is found, null is returned.
 *
 * @param method the IMethod which may be part of a ripple
 * @param typeHierarchy a ITypeHierarchy of the declaring type of the method. May be null
 * @param monitor an IProgressMonitor
 * @return the topmost method of the ripple, or null if none
 * @throws JavaModelException
 */
public static IMethod getTopmostMethod(IMethod method, ITypeHierarchy typeHierarchy, IProgressMonitor monitor) throws JavaModelException {

	Assert.isNotNull(method);

	ITypeHierarchy hierarchy= typeHierarchy;
	IMethod topmostMethod= null;
	final IType declaringType= method.getDeclaringType();
	if (!declaringType.isInterface()) {
		if ((hierarchy == null) || !declaringType.equals(hierarchy.getType())) {
			hierarchy= declaringType.newTypeHierarchy(monitor);
		}

		IMethod inInterface= isDeclaredInInterface(method, hierarchy, monitor);
		if (inInterface != null && !inInterface.equals(method)) {
			topmostMethod= inInterface;
		}
	}
	if (topmostMethod == null) {
		if (hierarchy == null) {
			hierarchy= declaringType.newSupertypeHierarchy(monitor);
		}
		IMethod overrides= overridesAnotherMethod(method, hierarchy);
		if (overrides != null && !overrides.equals(method)) {
			topmostMethod= overrides;
		}
	}
	return topmostMethod;
}
 
Example 9
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void checkMethodReturnTypes(final IProgressMonitor monitor, final RefactoringStatus status, final Set<IMember> notDeletedMembersInSubtypes) throws JavaModelException {
	final Map<IMember, Set<IMember>> mapping= getMatchingMembers(getDestinationTypeHierarchy(monitor), getDestinationType(), true);
	final IMember[] members= getCreatedDestinationMembers();
	for (int i= 0; i < members.length; i++) {
		if (members[i].getElementType() != IJavaElement.METHOD)
			continue;
		final IMethod method= (IMethod) members[i];
		if (mapping.containsKey(method)) {
			final Set<IMember> set= mapping.get(method);
			if (set != null) {
				final String returnType= Signature.toString(Signature.getReturnType(method.getSignature()).toString());
				for (final Iterator<IMember> iter= set.iterator(); iter.hasNext();) {
					final IMethod matchingMethod= (IMethod) iter.next();
					if (method.equals(matchingMethod))
						continue;
					if (!notDeletedMembersInSubtypes.contains(matchingMethod))
						continue;
					if (returnType.equals(Signature.toString(Signature.getReturnType(matchingMethod.getSignature()).toString())))
						continue;
					final String[] keys= { JavaElementLabels.getTextLabel(matchingMethod, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getTextLabel(matchingMethod.getDeclaringType(), JavaElementLabels.ALL_FULLY_QUALIFIED)};
					final String message= Messages.format(RefactoringCoreMessages.PullUpRefactoring_different_method_return_type, keys);
					final RefactoringStatusContext context= JavaStatusContext.create(matchingMethod.getCompilationUnit(), matchingMethod.getNameRange());
					status.addError(message, context);
				}
			}
		}
	}
}
 
Example 10
Source File: MethodChecks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IMethod overridesAnotherMethod(IMethod method, ITypeHierarchy hierarchy) throws JavaModelException {
	MethodOverrideTester tester= new MethodOverrideTester(method.getDeclaringType(), hierarchy);
	IMethod found= tester.findDeclaringMethod(method, true);
	boolean overrides= (found != null && !found.equals(method) && (!JdtFlags.isStatic(found)) && (!JdtFlags.isPrivate(found)));
	if (overrides)
		return found;
	else
		return null;
}
 
Example 11
Source File: RenameMethodUserInterfaceStarter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean activate(Refactoring refactoring, Shell parent, int saveMode) throws CoreException {
	RenameVirtualMethodProcessor processor= (RenameVirtualMethodProcessor)refactoring.getAdapter(RenameVirtualMethodProcessor.class);
	if (processor != null) {
		RefactoringStatus status= processor.checkInitialConditions(new NullProgressMonitor());
		if (!status.hasFatalError()) {
			IMethod method= processor.getMethod();
			if (!method.equals(processor.getOriginalMethod())) {
				String message= null;
				if (method.getDeclaringType().isInterface()) {
					message= Messages.format(
						RefactoringCoreMessages.MethodChecks_implements,
						new String[]{
							JavaElementUtil.createMethodSignature(method),
							JavaElementLabels.getElementLabel(method.getDeclaringType(), JavaElementLabels.ALL_FULLY_QUALIFIED)});
				} else {
					message= Messages.format(
						RefactoringCoreMessages.MethodChecks_overrides,
						new String[]{
							JavaElementUtil.createMethodSignature(method),
							JavaElementLabels.getElementLabel(method.getDeclaringType(), JavaElementLabels.ALL_FULLY_QUALIFIED)});
				}
				message= Messages.format(
					ReorgMessages.RenameMethodUserInterfaceStarter_message,
					message);
				if (!MessageDialog.openQuestion(parent,
						ReorgMessages.RenameMethodUserInterfaceStarter_name,
						message)) {
					return false;
				}
			}
		}
	}
	return super.activate(refactoring, parent, saveMode);
}