Java Code Examples for org.eclipse.jdt.core.IType#newSupertypeHierarchy()

The following examples show how to use org.eclipse.jdt.core.IType#newSupertypeHierarchy() . 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: 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 2
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 3
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 4
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 5
Source File: JavaImplementorFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public Collection<IType> findInterfaces(IType type, IProgressMonitor progressMonitor) {
    ITypeHierarchy typeHierarchy;

    try {
        typeHierarchy = type.newSupertypeHierarchy(progressMonitor);

        IType[] interfaces = typeHierarchy.getAllSuperInterfaces(type);
        HashSet<IType> result = new HashSet<IType>(Arrays.asList(interfaces));

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

    return null;
}
 
Example 6
Source File: JavaInterfaceCodeAppenderImpl.java    From jenerate with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean isImplementedOrExtendedInSupertype(final IType objectClass, final String interfaceName)
        throws JavaModelException {

    if (isImplementedInSupertype(objectClass, interfaceName)) {
        return true;
    }

    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.getExtendingInterfaces(in);
            for (int j = 0; j < types.length; j++) {
                if (!types[j].getFullyQualifiedName().equals(objectClass.getFullyQualifiedName())) {
                    return true;
                }
            }
            break;
        }
    }
    return false;
}
 
Example 7
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 8
Source File: JDTMethodHelper.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
private static List<IField> getAllDeclaredFields(final IType type) throws JavaModelException {
    List<IField> fields = new ArrayList<>();
    fields.addAll(Arrays.asList(type.getFields()));
    ITypeHierarchy typeHierarchy = type.newSupertypeHierarchy(Repository.NULL_PROGRESS_MONITOR);
    Stream.of(typeHierarchy.getAllSuperclasses(type))
            .filter(t -> !Object.class.getName().equals(t.getElementName()))
            .filter(t -> isGroovySourceType(t))
            .flatMap(t -> {
                try {
                    return Stream.of(t.getFields());
                } catch (JavaModelException e) {
                    return Stream.empty();
                }
            })
            .forEach(fields::add);
    return fields;
}
 
Example 9
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 10
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 11
Source File: LazyGenericTypeProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Computes one inheritance path from <code>superType</code> to <code>subType</code> or
 * <code>null</code> if <code>subType</code> does not inherit from <code>superType</code>. Note
 * that there may be more than one inheritance path - this method simply returns one.
 * <p>
 * The returned array contains <code>superType</code> at its first index, and
 * <code>subType</code> at its last index. If <code>subType</code> equals <code>superType</code>
 * , an array of length 1 is returned containing that type.
 * </p>
 * 
 * @param subType the sub type
 * @param superType the super type
 * @return an inheritance path from <code>superType</code> to <code>subType</code>, or
 *         <code>null</code> if <code>subType</code> does not inherit from
 *         <code>superType</code>
 * @throws JavaModelException if this element does not exist or if an exception occurs while
 *             accessing its corresponding resource
 */
private 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(getProgressMonitor());
	if (!hierarchy.contains(superType))
		return null; // no path

	List<IType> path= new LinkedList<IType>();
	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 12
Source File: DialogFactoryHelperImpl.java    From jenerate with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean isOverriddenInSuperclass(IType objectClass, String methodName, String[] methodParameterTypes,
        String originalClassFullyQualifiedName) throws JavaModelException {
    ITypeHierarchy typeHierarchy = objectClass.newSupertypeHierarchy(null);
    IType[] superclasses = typeHierarchy.getAllSuperclasses(objectClass);

    if (superclasses.length == 0) {
        return false;
    }

    for (IType superclass : superclasses) {
        if (superclass.getFullyQualifiedName().equals(originalClassFullyQualifiedName)) {
            return false;
        }

        IMethod method = methodFinder.findMethodWithNameAndParameters(superclass, methodName, methodParameterTypes);
        if (method != null) {
            return !Flags.isAbstract(method.getFlags());
        }
    }

    return false;
}
 
Example 13
Source File: CompilationUnitCompletion.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if <code>subTypeSignature</code>
 * describes a type which is a true sub type of the type described by
 * <code>superTypeSignature</code>.
 *
 * @param subTypeSignature the potential subtype's signature
 * @param superTypeSignature the potential supertype's signature
 * @return <code>true</code> if the inheritance relationship holds
 */
private boolean isTrueSubtypeOf(String subTypeSignature, String superTypeSignature) {
	// try cheap test first
	if (subTypeSignature.equals(superTypeSignature))
		return true;

	if (SignatureUtil.isJavaLangObject(subTypeSignature))
		return false; // Object has no super types

	if (Signature.getTypeSignatureKind(subTypeSignature) != Signature.BASE_TYPE_SIGNATURE && SignatureUtil.isJavaLangObject(superTypeSignature))
		return true;

	IJavaProject project= fUnit.getJavaProject();

	try {

		if ((Signature.getTypeSignatureKind(subTypeSignature) & (Signature.TYPE_VARIABLE_SIGNATURE | Signature.CLASS_TYPE_SIGNATURE)) == 0)
			return false;
		IType subType= project.findType(SignatureUtil.stripSignatureToFQN(subTypeSignature));
		if (subType == null)
			return false;

		if ((Signature.getTypeSignatureKind(superTypeSignature) & (Signature.TYPE_VARIABLE_SIGNATURE | Signature.CLASS_TYPE_SIGNATURE)) == 0)
			return false;
		IType superType= project.findType(SignatureUtil.stripSignatureToFQN(superTypeSignature));
		if (superType == null)
			return false;

		ITypeHierarchy hierarchy= subType.newSupertypeHierarchy(null);
		IType[] types= hierarchy.getAllSupertypes(subType);

		for (int i= 0; i < types.length; i++)
			if (types[i].equals(superType))
				return true;
	} catch (JavaModelException e) {
		// ignore and return false
	}

	return false;
}
 
Example 14
Source File: ClientBundleResource.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isMethodAlreadyDefined(IJavaProject javaProject, String[] extendedInterfaces)
    throws JavaModelException {
  for (String extendedInterface : extendedInterfaces) {
    IType type = JavaModelSearch.findType(javaProject, extendedInterface);
    if (type == null) {
      // Ignore unresolved types
      continue;
    }

    boolean isInterface = false;
    try {
      isInterface = type.isInterface();
    } catch (JavaModelException e) {
      GWTPluginLog.logError(e);
    }

    if (!isInterface) {
      // Ignore non-interface types
      continue;
    }

    // Find a matching method in this interface or an extended interface
    ITypeHierarchy superHierarchy = type.newSupertypeHierarchy(null);
    IMethod method = JavaModelSearch.findMethodInHierarchy(superHierarchy, type, methodName, new String[0]);
    if (method != null) {
      // We found a matching accessor method
      return true;
    }
  }
  return false;
}
 
Example 15
Source File: RemoteServiceUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> if the type is or extends the RemoteService 
 * interface.
 */
public static boolean isSyncInterface(IType type) throws JavaModelException {
  IJavaProject javaProject = type.getJavaProject();
  if (!GWTNature.isGWTProject(javaProject.getProject())) {
    return false;
  }

  ITypeHierarchy hierarchy = type.newSupertypeHierarchy(null);
  IType remoteServiceInterface = javaProject.findType(REMOTE_SERVICE_QUALIFIED_NAME);
  return remoteServiceInterface != null
      && hierarchy.contains(remoteServiceInterface);
}
 
Example 16
Source File: JavaUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns whether a type is the super of another type.
 * 
 * @param superType the possible super type. If <code>null</code>, this method
 *          always returns <code>false<code>.
 * @param type the type
 */
public static boolean isSubtype(IType superType, IType type)
    throws JavaModelException {
  if (superType == null) {
    return false;
  }

  ITypeHierarchy superTypes = type.newSupertypeHierarchy(null);
  return superTypes.contains(superType);
}
 
Example 17
Source File: JavadocContentAccess.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Reader findDocInHierarchy(IMethod method, boolean isHTML, 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 (int i= 0; i < superTypes.length; i++) {
		IType curr= superTypes[i];
		IMethod overridden= tester.findOverriddenMethodInType(curr, method);
		if (overridden != null) {
			Reader reader;
			if (isHTML)
				reader= getHTMLContentReader(overridden, false, useAttachedJavadoc);
			else
				reader= getContentReader(overridden, false);
			if (reader != null)
				return reader;
		}
	}
	return null;
}
 
Example 18
Source File: PropertiesQuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isEclipseNLSUsed(IType accessor) {
	IJavaProject javaProject= accessor.getJavaProject();
	if (javaProject == null || !javaProject.exists())
		return false;

	try {
		IType nls= javaProject.findType("org.eclipse.osgi.util.NLS"); //$NON-NLS-1$
		if(nls==null)
			return false;
		ITypeHierarchy supertypeHierarchy= accessor.newSupertypeHierarchy(null);
		return supertypeHierarchy.contains(nls);
	} catch (JavaModelException e) {
		return false;
	}
}
 
Example 19
Source File: CombinedJvmJdtRenameContextFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void addDeclaringMethod(JvmIdentifiableElement jvmElement, IJavaElement javaElement,
		Map<URI, IJavaElement> jvm2javaElement) {
	try {
		IType declaringType = ((IMethod) javaElement).getDeclaringType();
		ITypeHierarchy typeHierarchy = declaringType.newSupertypeHierarchy(new NullProgressMonitor());
		MethodOverrideTester methodOverrideTester = new MethodOverrideTester(declaringType, typeHierarchy);
		IMethod declaringMethod = methodOverrideTester.findDeclaringMethod((IMethod) javaElement, true);
		if (declaringMethod != null)
			jvm2javaElement.put(EcoreUtil.getURI(jvmElement), declaringMethod);
		else
			jvm2javaElement.put(EcoreUtil.getURI(jvmElement), javaElement);
	} catch (JavaModelException e) {
		throw new RuntimeException(e);
	}
}
 
Example 20
Source File: CompilationUnitCompletion.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private IType[] getSupertypes(String supertype) {
	IType[] empty= new IType[0];
	String implementorName= SignatureUtil.stripSignatureToFQN(signature);
	if (implementorName.length() == 0)
		return empty;

	boolean qualified= supertype.indexOf('.') != -1;

	if (fUnit == null)
		return empty;

	IJavaProject project= fUnit.getJavaProject();

	try {
		IType sub= project.findType(implementorName);
		if (sub == null)
			return empty;

		if (qualified) {
			IType sup= project.findType(supertype);
			if (sup == null)
				return empty;
			return new IType[] {sup};
		} else {
			ITypeHierarchy hierarchy= sub.newSupertypeHierarchy(null);
			IType[] allTypes= hierarchy.getAllTypes();
			List<IType> matches= new ArrayList<IType>();
			for (int i= 0; i < allTypes.length; i++) {
				IType type= allTypes[i];
				if (type.getElementName().equals(supertype))
					matches.add(type);
			}
			return matches.toArray(new IType[matches.size()]);
		}

	} catch (JavaModelException e) {
		// ignore and return false
	}

	return empty;
}