Java Code Examples for org.eclipse.jdt.core.ITypeHierarchy#contains()

The following examples show how to use org.eclipse.jdt.core.ITypeHierarchy#contains() . 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: 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 2
Source File: RippleMethodFinder.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 3
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 4
Source File: RippleMethodFinder2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.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 5
Source File: SuperTypeHierarchyCache.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static ITypeHierarchy findTypeHierarchyInCache(IType type) {
	synchronized (fgHierarchyCache) {
		for (int i= fgHierarchyCache.size() - 1; i>= 0; i--) {
			HierarchyCacheEntry curr= fgHierarchyCache.get(i);
			ITypeHierarchy hierarchy= curr.getTypeHierarchy();
			if (!hierarchy.exists()) {
				removeHierarchyEntryFromCache(curr);
			} else {
				if (hierarchy.contains(type)) {
					curr.markAsAccessed();
					return hierarchy;
				}
			}
		}
	}
	return null;
}
 
Example 6
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 7
Source File: ContentAssistHistory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Remembers the selection of a right hand side type (proposal type) for a certain left hand side (expected
 * type) in content assist.
 *
 * @param lhs the left hand side / expected type
 * @param rhs the selected right hand side
 */
public void remember(IType lhs, IType rhs) {
	Assert.isLegal(lhs != null);
	Assert.isLegal(rhs != null);

	try {
		if (!isCacheableRHS(rhs))
			return;
		ITypeHierarchy hierarchy= rhs.newSupertypeHierarchy(getProgressMonitor());
		if (hierarchy.contains(lhs)) {
			// TODO remember for every member of the LHS hierarchy or not? Yes for now.
			IType[] allLHSides= hierarchy.getAllSupertypes(lhs);
			String rhsQualifiedName= rhs.getFullyQualifiedName();
			for (int i= 0; i < allLHSides.length; i++)
				rememberInternal(allLHSides[i], rhsQualifiedName);
			rememberInternal(lhs, rhsQualifiedName);
		}
	} catch (JavaModelException x) {
		JavaPlugin.log(x);
	}
}
 
Example 8
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 9
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 10
Source File: PullUpRefactoringProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean canBeAccessedFrom(final IMember member, final IType target, final ITypeHierarchy hierarchy) throws JavaModelException {
	if (super.canBeAccessedFrom(member, target, hierarchy)) {
		if (target.isInterface())
			return true;
		if (target.equals(member.getDeclaringType()))
			return true;
		if (target.equals(member))
			return true;
		if (member instanceof IMethod) {
			final IMethod method= (IMethod) member;
			final IMethod stub= target.getMethod(method.getElementName(), method.getParameterTypes());
			if (stub.exists())
				return true;
		}
		if (member.getDeclaringType() == null) {
			if (!(member instanceof IType))
				return false;
			if (JdtFlags.isPublic(member))
				return true;
			if (!JdtFlags.isPackageVisible(member))
				return false;
			if (JavaModelUtil.isSamePackage(((IType) member).getPackageFragment(), target.getPackageFragment()))
				return true;
			final IType type= member.getDeclaringType();
			if (type != null)
				return hierarchy.contains(type);
			return false;
		}
		final IType declaringType= member.getDeclaringType();
		if (!canBeAccessedFrom(declaringType, target, hierarchy))
			return false;
		if (declaringType.equals(getDeclaringType()))
			return false;
		return true;
	}
	return false;
}
 
Example 11
Source File: SuperTypeHierarchyCache.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void addTypeHierarchyToCache(ITypeHierarchy hierarchy) {
	synchronized (fgHierarchyCache) {
		int nEntries= fgHierarchyCache.size();
		if (nEntries >= CACHE_SIZE) {
			// find obsolete entries or remove entry that was least recently accessed
			HierarchyCacheEntry oldest= null;
			ArrayList<HierarchyCacheEntry> obsoleteHierarchies= new ArrayList<HierarchyCacheEntry>(CACHE_SIZE);
			for (int i= 0; i < nEntries; i++) {
				HierarchyCacheEntry entry= fgHierarchyCache.get(i);
				ITypeHierarchy curr= entry.getTypeHierarchy();
				if (!curr.exists() || hierarchy.contains(curr.getType())) {
					obsoleteHierarchies.add(entry);
				} else {
					if (oldest == null || entry.getLastAccess() < oldest.getLastAccess()) {
						oldest= entry;
					}
				}
			}
			if (!obsoleteHierarchies.isEmpty()) {
				for (int i= 0; i < obsoleteHierarchies.size(); i++) {
					removeHierarchyEntryFromCache(obsoleteHierarchies.get(i));
				}
			} else if (oldest != null) {
				removeHierarchyEntryFromCache(oldest);
			}
		}
		HierarchyCacheEntry newEntry= new HierarchyCacheEntry(hierarchy);
		fgHierarchyCache.add(newEntry);
	}
}
 
Example 12
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 13
Source File: ClassGenerator.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
private static IType generateImplementationClass(String packageName, String className, SourceRepositoryStore sourceStore,
        IProgressMonitor progressMonitor)
        throws Exception {
    final IJavaProject javaProject = RepositoryManager.getInstance().getCurrentRepository().getJavaProject();

    final IType abstractConnectorType = javaProject.findType(AbstractConnector.class.getName());
    final IType abstractFilterType = javaProject.findType(AbstractUserFilter.class.getName());
    String abstractClassName = "Abstract" + className;
    if (packageName != null && !packageName.isEmpty()) {
        abstractClassName = packageName + "." + abstractClassName;
    }
    final IType classType = javaProject.findType(abstractClassName);
    if (classType == null) {
        throw new ClassNotFoundException(abstractClassName);
    }
    final ITypeHierarchy hierarchy = classType.newTypeHierarchy(javaProject, progressMonitor);
    String tempatePattern = null;
    if (hierarchy.contains(abstractConnectorType)) {
        tempatePattern = "/**\n*The connector execution will follow the steps" +
                "\n* 1 - setInputParameters() --> the connector receives input parameters values" +
                "\n* 2 - validateInputParameters() --> the connector can validate input parameters values" +
                "\n* 3 - connect() --> the connector can establish a connection to a remote server (if necessary)" +
                "\n* 4 - executeBusinessLogic() --> execute the connector" +
                "\n* 5 - getOutputParameters() --> output are retrieved from connector" +
                "\n* 6 - disconnect() --> the connector can close connection to remote server (if any)\n*/";
    } else if (hierarchy.contains(abstractFilterType)) {
        tempatePattern = "/**\n*The actor filter execution will follow the steps" +
                "\n* 1 - setInputParameters() --> the actor filter receives input parameters values" +
                "\n* 2 - validateInputParameters() --> the actor filter can validate input parameters values" +
                "\n* 3 - filter(final String actorName) --> execute the user filter" +
                "\n* 4 - shouldAutoAssignTaskIfSingleResult() --> auto-assign the task if filter returns a single result\n*/";
    }

    final NewClassWizardPage classWizard = new NewClassWizardPage();
    classWizard.enableCommentControl(true);

    final ProjectTemplateStore fTemplateStore = new ProjectTemplateStore(
            RepositoryManager.getInstance().getCurrentRepository().getProject());
    try {
        fTemplateStore.load();
    } catch (final IOException e) {
        BonitaStudioLog.error(e);
    }
    final Template t = fTemplateStore.findTemplateById("org.eclipse.jdt.ui.text.codetemplates.typecomment");
    t.setPattern(tempatePattern);

    classWizard.setSuperClass(abstractClassName, false);
    classWizard.setAddComments(true, false);
    classWizard.setModifiers(classWizard.F_PUBLIC, false);
    classWizard.setTypeName(className, false);
    classWizard.setMethodStubSelection(false, false, false, false);
    IPackageFragmentRoot packageFragmentRoot = null;
    final IResource srcFolder = sourceStore.getResource();
    packageFragmentRoot = javaProject.getPackageFragmentRoot(srcFolder);
    classWizard.setPackageFragmentRoot(packageFragmentRoot, false);
    IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(packageName == null ? "" : packageName);
    if (!packageFragment.exists()) {
        packageFragment = packageFragmentRoot.createPackageFragment(packageName, true, progressMonitor);
    }
    classWizard.setPackageFragment(packageFragment, false);
    classWizard.createType(progressMonitor);
    return classWizard.getCreatedType();
}