org.eclipse.jdt.core.ITypeHierarchy Java Examples

The following examples show how to use org.eclipse.jdt.core.ITypeHierarchy. 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: 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 #2
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Set<IType> getSkippedSuperTypes(final IProgressMonitor monitor) throws JavaModelException {
	monitor.beginTask(RefactoringCoreMessages.PullUpRefactoring_checking, 1);
	try {
		if (fCachedSkippedSuperTypes != null && getDestinationTypeHierarchy(new SubProgressMonitor(monitor, 1)).getType().equals(getDestinationType()))
			return fCachedSkippedSuperTypes;
		final ITypeHierarchy hierarchy= getDestinationTypeHierarchy(new SubProgressMonitor(monitor, 1));
		fCachedSkippedSuperTypes= new HashSet<IType>(2);
		IType current= hierarchy.getSuperclass(getDeclaringType());
		while (current != null && !current.equals(getDestinationType())) {
			fCachedSkippedSuperTypes.add(current);
			current= hierarchy.getSuperclass(current);
		}
		return fCachedSkippedSuperTypes;
	} finally {
		monitor.done();
	}
}
 
Example #3
Source File: TypeProposalUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
static IType[] computeInheritancePath(IType subType, IType superType) throws JavaModelException {
	if (superType == null) {
		return null;
	}

	// optimization: avoid building the type hierarchy for the identity case
	if (superType.equals(subType)) {
		return new IType[] { subType };
	}

	ITypeHierarchy hierarchy= subType.newSupertypeHierarchy(new NullProgressMonitor());
	if (!hierarchy.contains(superType))
	{
		return null; // no path
	}

	List<IType> path= new LinkedList<>();
	path.add(superType);
	do {
		// any sub type must be on a hierarchy chain from superType to subType
		superType= hierarchy.getSubtypes(superType)[0];
		path.add(superType);
	} while (!superType.equals(subType)); // since the equality case is handled above, we can spare one check

	return path.toArray(new IType[path.size()]);
}
 
Example #4
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private Map<IMember, Set<IMember>> getMatchingMembers(final ITypeHierarchy hierarchy, final IType type, final boolean includeAbstract) throws JavaModelException {
	final Map<IMember, Set<IMember>> result= new HashMap<IMember, Set<IMember>>();
	result.putAll(getMatchingMembersMapping(type));
	final IType[] subTypes= hierarchy.getAllSubtypes(type);
	for (int i= 0; i < subTypes.length; i++) {
		final Map<IMember, Set<IMember>> map= getMatchingMembersMapping(subTypes[i]);
		mergeMaps(result, map);
		upgradeMap(result, map);
	}
	if (includeAbstract)
		return result;

	for (int i= 0; i < fAbstractMethods.length; i++) {
		if (result.containsKey(fAbstractMethods[i]))
			result.remove(fAbstractMethods[i]);
	}
	return result;
}
 
Example #5
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Finds the first available attached Javadoc in the hierarchy of the given method.
 *
 * @param method the method
 * @return the inherited Javadoc from the Javadoc attachment, or <code>null</code> if none
 * @throws JavaModelException unexpected problem
 */
private static String findAttachedDocInHierarchy(final IMethod method) throws JavaModelException {
	IType type= method.getDeclaringType();
	ITypeHierarchy hierarchy= SuperTypeHierarchyCache.getTypeHierarchy(type);
	final MethodOverrideTester tester= SuperTypeHierarchyCache.getMethodOverrideTester(type);

	return (String) new InheritDocVisitor() {
		@Override
		public Object visit(IType currType) throws JavaModelException {
			IMethod overridden= tester.findOverriddenMethodInType(currType, method);
			if (overridden == null)
				return InheritDocVisitor.CONTINUE;

			if (overridden.getOpenable().getBuffer() == null) { // only if no source available
				//TODO: BaseURL for method can be wrong for attached Javadoc from overridden
				// (e.g. when overridden is from rt.jar). Fix would be to add baseURL here.
				String attachedJavadoc= overridden.getAttachedJavadoc(null);
				if (attachedJavadoc != null)
					return attachedJavadoc;
			}
			return CONTINUE;
		}
	}.visitInheritDoc(type, hierarchy);
}
 
Example #6
Source File: TypeHierarchyLifeCycle.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ITypeHierarchy createTypeHierarchy(IJavaElement[] elements, IProgressMonitor pm) throws JavaModelException {
	if (elements.length == 1 && elements[0].getElementType() == IJavaElement.TYPE) {
		IType type= (IType)elements[0];
		if (fIsSuperTypesOnly) {
			return type.newSupertypeHierarchy(pm);
		} else {
			return type.newTypeHierarchy(pm);
		}
	} else {
		IRegion region= JavaCore.newRegion();
		for (int i= 0; i < elements.length; i++) {
			if (elements[i].getElementType() == IJavaElement.JAVA_PROJECT) {
				// for projects only add the contained source folders
				IPackageFragmentRoot[] roots= ((IJavaProject)elements[i]).getPackageFragmentRoots();
				for (int j= 0; j < roots.length; j++) {
					if (!roots[j].isExternal()) {
						region.add(roots[j]);
					}
				}
			} else {
				region.add(elements[i]);
			}
		}
		return JavaCore.newTypeHierarchy(region, null, pm);
	}
}
 
Example #7
Source File: Binding2JavaModel.java    From lapse-plus with GNU General Public License v3.0 6 votes vote down vote up
public static IType[] getAllSuperTypes(IType type, IProgressMonitor pm) throws JavaModelException {
    //workaround for bugs 23644 and 23656
    try{
        pm.beginTask("", 3); //$NON-NLS-1$
        ITypeHierarchy hierarchy= type.newSupertypeHierarchy(new SubProgressMonitor(pm, 1));
        
        IProgressMonitor subPm= new SubProgressMonitor(pm, 2);
        List<IType> typeList= Arrays.asList(hierarchy.getAllSupertypes(type));
        subPm.beginTask("", typeList.size()); //$NON-NLS-1$
        Set<IType> types= new HashSet<IType>(typeList);
        for (Iterator iter= typeList.iterator(); iter.hasNext();) {
            IType superType= (IType)iter.next();
            IType[] superTypes= getAllSuperTypes(superType, new SubProgressMonitor(subPm, 1));
            types.addAll(Arrays.asList(superTypes));
        }
        types.add(type.getJavaProject().findType("java.lang.Object"));//$NON-NLS-1$
        subPm.done();
        return (IType[]) types.toArray(new IType[types.size()]);
    } finally {
        pm.done();
    }   
}
 
Example #8
Source File: SuperTypeHierarchyCache.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static MethodOverrideTester getMethodOverrideTester(IType type) throws JavaModelException {
	MethodOverrideTester test= null;
	synchronized (fgMethodOverrideTesterCache) {
		test= fgMethodOverrideTesterCache.get(type);
	}
	if (test == null) {
		ITypeHierarchy hierarchy= getTypeHierarchy(type); // don't nest the locks
		synchronized (fgMethodOverrideTesterCache) {
			test= fgMethodOverrideTesterCache.get(type); // test again after waiting a long time for 'getTypeHierarchy'
			if (test == null) {
				test= new MethodOverrideTester(type, hierarchy);
				fgMethodOverrideTesterCache.put(type, test);
			}
		}
	}
	return test;
}
 
Example #9
Source File: JavaImplementorFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Collection<IType> findImplementingTypes(IType type, IProgressMonitor progressMonitor) {
    ITypeHierarchy typeHierarchy;

    try {
        typeHierarchy = type.newTypeHierarchy(progressMonitor);

        IType[] implementingTypes = typeHierarchy.getAllClasses();
        HashSet<IType> result = new HashSet<IType>(Arrays.asList(implementingTypes));

        return result;
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
    }

    return null;
}
 
Example #10
Source File: RippleMethodFinder2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ITypeHierarchy getCachedHierarchy(IType type, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException {
	IType rep= fUnionFind.find(type);
	if (rep != null) {
		Collection<IType> collection= fRootReps.get(rep);
		for (Iterator<IType> iter= collection.iterator(); iter.hasNext();) {
			IType root= iter.next();
			ITypeHierarchy hierarchy= fRootHierarchies.get(root);
			if (hierarchy == null) {
				hierarchy= root.newTypeHierarchy(owner, new SubProgressMonitor(monitor, 1));
				fRootHierarchies.put(root, hierarchy);
			}
			if (hierarchy.contains(type)) {
				return hierarchy;
			}
		}
	}
	return null;
}
 
Example #11
Source File: JavadocContentAccess.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static Reader findDocInHierarchy(IMethod method, boolean useAttachedJavadoc) throws JavaModelException {
	/*
	 * Catch ExternalJavaProject in which case
	 * no hierarchy can be built.
	 */
	if (!method.getJavaProject().exists()) {
		return null;
	}

	IType type= method.getDeclaringType();
	ITypeHierarchy hierarchy= type.newSupertypeHierarchy(null);

	MethodOverrideTester tester= new MethodOverrideTester(type, hierarchy);

	IType[] superTypes= hierarchy.getAllSupertypes(type);
	for (IType curr : superTypes) {
		IMethod overridden= tester.findOverriddenMethodInType(curr, method);
		if (overridden != null) {
			Reader reader = getHTMLContentReader(overridden, false, useAttachedJavadoc);
			if (reader != null) {
				return reader;
			}
		}
	}
	return null;
}
 
Example #12
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isSuperType(ITypeHierarchy hierarchy, IType possibleSuperType, IType type) {
	// filed bug 112635 to add this method to ITypeHierarchy
	IType superClass= hierarchy.getSuperclass(type);
	if (superClass != null && (possibleSuperType.equals(superClass) || isSuperType(hierarchy, possibleSuperType, superClass))) {
		return true;
	}
	if (Flags.isInterface(hierarchy.getCachedFlags(possibleSuperType))) {
		IType[] superInterfaces= hierarchy.getSuperInterfaces(type);
		for (int i= 0; i < superInterfaces.length; i++) {
			IType curr= superInterfaces[i];
			if (possibleSuperType.equals(curr) || isSuperType(hierarchy, possibleSuperType, curr)) {
				return true;
			}
		}
	}
	return false;
}
 
Example #13
Source File: MemberVisibilityAdjustor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the visibility threshold from a type to a field.
 *
 * @param referencing the referencing type
 * @param referenced the referenced field
 * @param monitor the progress monitor to use
 * @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
 * @throws JavaModelException if the java elements could not be accessed
 */
private ModifierKeyword thresholdTypeToField(final IType referencing, final IField referenced, final IProgressMonitor monitor) throws JavaModelException {
	ModifierKeyword keyword= ModifierKeyword.PUBLIC_KEYWORD;
	final ICompilationUnit referencedUnit= referenced.getCompilationUnit();
	if (referenced.getDeclaringType().equals(referencing))
		keyword= ModifierKeyword.PRIVATE_KEYWORD;
	else {
		final ITypeHierarchy hierarchy= getTypeHierarchy(referencing, new SubProgressMonitor(monitor, 1));
		final IType[] types= hierarchy.getSupertypes(referencing);
		IType superType= null;
		for (int index= 0; index < types.length; index++) {
			superType= types[index];
			if (superType.equals(referenced.getDeclaringType())) {
				keyword= ModifierKeyword.PROTECTED_KEYWORD;
				return keyword;
			}
		}
	}
	final ICompilationUnit typeUnit= referencing.getCompilationUnit();
	if (referencedUnit != null && referencedUnit.equals(typeUnit))
		keyword= ModifierKeyword.PRIVATE_KEYWORD;
	else if (referencedUnit != null && typeUnit != null && referencedUnit.getParent().equals(typeUnit.getParent()))
		keyword= null;
	return keyword;
}
 
Example #14
Source File: DispatchRenameSupport.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected Iterable<JvmGenericType> getSubTypes(JvmGenericType type, ResourceSet tempResourceSet) {
	IType javaType = (IType) javaElementFinder.findExactElementFor(type);
	List<JvmGenericType> allSubTypes = newArrayList();
	try {
		ITypeHierarchy typeHierarchy = javaType.newTypeHierarchy(javaType.getJavaProject(),
				new NullProgressMonitor());
		for (IType subType : typeHierarchy.getSubtypes(javaType)) {
			EObject jvmSubType = jvmElementFinder.getCorrespondingJvmElement(subType, tempResourceSet);
			if (jvmSubType instanceof JvmGenericType) {
				EObject indexJvmSubType = jvmElementFinder.findJvmElementDeclarationInIndex(jvmSubType, subType
						.getJavaProject().getProject(), type.eResource().getResourceSet());
				if (indexJvmSubType instanceof JvmGenericType) {
					allSubTypes.add((JvmGenericType) indexJvmSubType);
				} else {
					EObject jvmSubTypeInOtherResourceSet = type.eResource().getResourceSet().getEObject(EcoreUtil2.getPlatformResourceOrNormalizedURI(jvmSubType), true);
					if(jvmSubTypeInOtherResourceSet instanceof JvmGenericType)
						allSubTypes.add((JvmGenericType) jvmSubTypeInOtherResourceSet);
				}
			}
		}
	} catch (JavaModelException e) {
		LOG.error("Error calculating subtypes", e);
	}
	return allSubTypes;

}
 
Example #15
Source File: TypeHierarchyViewPart.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private KeyListener createKeyListener() {
	return new KeyAdapter() {
		@Override
		public void keyReleased(KeyEvent event) {
			if (event.stateMask == 0) {
				if (event.keyCode == SWT.F5) {
					ITypeHierarchy hierarchy= fHierarchyLifeCycle.getHierarchy();
					if (hierarchy != null) {
						fHierarchyLifeCycle.typeHierarchyChanged(hierarchy);
						doTypeHierarchyChangedOnViewers(null);
					}
					updateHierarchyViewer(false);
					return;
				}
				}
		}
	};
}
 
Example #16
Source File: MemberVisibilityAdjustor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns a cached type hierarchy for the specified type.
 *
 * @param type the type to get the hierarchy for
 * @param monitor the progress monitor to use
 * @return the type hierarchy
 * @throws JavaModelException if the type hierarchy could not be created
 */
private ITypeHierarchy getTypeHierarchy(final IType type, final IProgressMonitor monitor) throws JavaModelException {
	ITypeHierarchy hierarchy= null;
	try {
		monitor.beginTask("", 1); //$NON-NLS-1$
		monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
		try {
			hierarchy= fTypeHierarchies.get(type);
			if (hierarchy == null) {
				if (fOwner == null) {
					hierarchy= type.newSupertypeHierarchy(new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
				} else {
					hierarchy= type.newSupertypeHierarchy(fOwner, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
				}
				fTypeHierarchies.put(type, hierarchy);
			}
		} finally {
			monitor.done();
		}
	} finally {
		monitor.done();
	}
	return hierarchy;
}
 
Example #17
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 #18
Source File: TypeHierarchyCache.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
public void typeHierarchyChanged(ITypeHierarchy typeHierarchy) {
  try {
    IType type = typeHierarchy.getType();
    if (type.exists()) {
      typeHierarchy.refresh(null);
    } else {
      synchronized (hierarchies) {
        // Prune non-existent types from the cache
        hierarchies.remove(type);
      }
    }
  } catch (JavaModelException e) {
    CorePluginLog.logError(e, "Could not refresh the type hierarchy of "
        + typeHierarchy.getType().getElementName());
  }
}
 
Example #19
Source File: SubTypeHierarchyViewer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected final void getTypesInHierarchy(IType type, List<IType> res) {
	ITypeHierarchy hierarchy= getHierarchy();
	if (hierarchy != null) {
		IType[] types= hierarchy.getSubtypes(type);
		if (isObject(type)) {
			for (int i= 0; i < types.length; i++) {
				IType curr= types[i];
				if (!isAnonymousFromInterface(curr)) {
					res.add(curr);
				}
			}
		} else {
			for (int i= 0; i < types.length; i++) {
				res.add(types[i]);
			}
		}
	}

}
 
Example #20
Source File: RenameFieldProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @return Error message or <code>null</code> if setter can be renamed.
 * @throws CoreException should not happen
 */
public String canEnableSetterRenaming() throws CoreException{
	if (fField.getDeclaringType().isInterface())
		return getSetter() == null ? "": null; //$NON-NLS-1$

	IMethod setter= getSetter();
	if (setter == null)
		return "";	 //$NON-NLS-1$
	final NullProgressMonitor monitor= new NullProgressMonitor();
	if (MethodChecks.isVirtual(setter)) {
		final ITypeHierarchy hierarchy= setter.getDeclaringType().newTypeHierarchy(monitor);
		if (MethodChecks.isDeclaredInInterface(setter, hierarchy, monitor) != null || MethodChecks.overridesAnotherMethod(setter, hierarchy) != null)
			return RefactoringCoreMessages.RenameFieldRefactoring_declared_in_supertype;
	}
	return null;
}
 
Example #21
Source File: GWTJUnitPropertyTester.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isGWTTestCaseOrSuite(IType type) {
  try {
    ITypeHierarchy hierarchy = type.newSupertypeHierarchy(null);
    IType[] superclasses = hierarchy.getAllSuperclasses(type);
    for (IType superclass : superclasses) {
      if (GWT_TEST_CASE.equals(superclass.getFullyQualifiedName())
          || GWT_TEST_SUITE.equals(superclass.getFullyQualifiedName())) {
        return true;
      }
    }
    return false;
  } catch (CoreException e) {
    GWTPluginLog.logError(e);
    return false;
  }
}
 
Example #22
Source File: PushDownRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private RefactoringStatus checkAccessedTypes(IType[] subclasses, IProgressMonitor pm) throws JavaModelException {
	RefactoringStatus result= new RefactoringStatus();
	IType[] accessedTypes= getTypesReferencedInMovedMembers(pm);
	for (int index= 0; index < subclasses.length; index++) {
		IType targetClass= subclasses[index];
		ITypeHierarchy targetSupertypes= targetClass.newSupertypeHierarchy(null);
		for (int offset= 0; offset < accessedTypes.length; offset++) {
			IType type= accessedTypes[offset];
			if (!canBeAccessedFrom(type, targetClass, targetSupertypes)) {
				String message= Messages.format(RefactoringCoreMessages.PushDownRefactoring_type_not_accessible, new String[] { JavaElementLabels.getTextLabel(type, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getTextLabel(targetClass, JavaElementLabels.ALL_FULLY_QUALIFIED) });
				result.addError(message, JavaStatusContext.create(type));
			}
		}
	}
	pm.done();
	return result;
}
 
Example #23
Source File: AbstractHierarchyViewerSorter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IType getDefiningType(IMethod method) throws JavaModelException {
	int flags= method.getFlags();
	if (Flags.isPrivate(flags) || Flags.isStatic(flags) || method.isConstructor()) {
		return null;
	}

	IType declaringType= method.getDeclaringType();
	ITypeHierarchy hierarchy= getHierarchy(declaringType);
	if (hierarchy != null) {
		MethodOverrideTester tester= new MethodOverrideTester(declaringType, hierarchy);
		IMethod res= tester.findDeclaringMethod(method, true);
		if (res != null) {
			return res.getDeclaringType();
		}
	}
	return null;
}
 
Example #24
Source File: PushDownRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private RefactoringStatus checkAccessedMethods(IType[] subclasses, IProgressMonitor pm) throws JavaModelException {
	RefactoringStatus result= new RefactoringStatus();
	IMember[] membersToPushDown= MemberActionInfo.getMembers(getInfosForMembersToBeCreatedInSubclassesOfDeclaringClass());
	List<IMember> pushedDownList= Arrays.asList(membersToPushDown);
	IMethod[] accessedMethods= ReferenceFinderUtil.getMethodsReferencedIn(membersToPushDown, pm);
	for (int index= 0; index < subclasses.length; index++) {
		IType targetClass= subclasses[index];
		ITypeHierarchy targetSupertypes= targetClass.newSupertypeHierarchy(null);
		for (int offset= 0; offset < accessedMethods.length; offset++) {
			IMethod method= accessedMethods[offset];
			boolean isAccessible= pushedDownList.contains(method) || canBeAccessedFrom(method, targetClass, targetSupertypes);
			if (!isAccessible) {
				String message= Messages.format(RefactoringCoreMessages.PushDownRefactoring_method_not_accessible, new String[] { JavaElementLabels.getTextLabel(method, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getTextLabel(targetClass, JavaElementLabels.ALL_FULLY_QUALIFIED) });
				result.addError(message, JavaStatusContext.create(method));
			}
		}
	}
	pm.done();
	return result;
}
 
Example #25
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 #26
Source File: JavaInterfaceCodeAppenderImpl.java    From jenerate with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean isImplementedInSupertype(final IType objectClass, final String interfaceName)
        throws JavaModelException {

    String simpleName = getSimpleInterfaceName(interfaceName);

    ITypeHierarchy typeHierarchy = objectClass.newSupertypeHierarchy(null);
    IType[] interfaces = typeHierarchy.getAllInterfaces();
    for (int i = 0; i < interfaces.length; i++) {
        if (interfaces[i].getElementName().equals(simpleName)) {
            IType in = interfaces[i];
            IType[] types = typeHierarchy.getImplementingClasses(in);
            for (int j = 0; j < types.length; j++) {
                if (!types[j].getFullyQualifiedName().equals(objectClass.getFullyQualifiedName())) {
                    return true;
                }
            }
            break;
        }
    }
    return false;
}
 
Example #27
Source File: RenameMethodProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
final static IMethod[] hierarchyDeclaresMethodName(IProgressMonitor pm, ITypeHierarchy hierarchy, IMethod method, String newName) throws CoreException {
	try {
		Set<IMethod> result= new HashSet<>();
		IType type= method.getDeclaringType();
		IMethod foundMethod= Checks.findMethod(newName, method.getParameterTypes().length, false, type);
		if (foundMethod != null) {
			result.add(foundMethod);
		}
		IMethod[] foundInHierarchyClasses= classesDeclareMethodName(hierarchy, Arrays.asList(hierarchy.getAllClasses()), method, newName);
		if (foundInHierarchyClasses != null) {
			result.addAll(Arrays.asList(foundInHierarchyClasses));
		}
		IType[] implementingClasses= hierarchy.getImplementingClasses(type);
		IMethod[] foundInImplementingClasses= classesDeclareMethodName(hierarchy, Arrays.asList(implementingClasses), method, newName);
		if (foundInImplementingClasses != null) {
			result.addAll(Arrays.asList(foundInImplementingClasses));
		}
		return result.toArray(new IMethod[result.size()]);
	} finally {
		if (pm != null) {
			pm.done();
		}
	}
}
 
Example #28
Source File: PullUpMethodPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean anySubtypeCanBeShown(final IType type, final Map<IType, IMember[]> typeToMemberArray, final ITypeHierarchy hierarchy) {
	final IType[] subTypes= hierarchy.getSubtypes(type);
	for (int i= 0; i < subTypes.length; i++) {
		if (canBeShown(subTypes[i], typeToMemberArray, hierarchy))
			return true;
	}
	return false;
}
 
Example #29
Source File: JavaElementDelegateJunitLaunch.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected IType[] getAllSuperTypes(IType type) throws JavaModelException {
	/*
	 * https://stackoverflow.com/questions/49611587/developing-a-eclipse-how-to-get-all-inherited-methods-from-a-icompilationunit
	 */
	ITypeHierarchy th = type.newTypeHierarchy(new NullProgressMonitor());
	IType[] superTypes = th.getAllSuperclasses(type);
	return superTypes;
}
 
Example #30
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;
}