Search in sources :

Example 61 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.

the class JavaElementUtil method getReferencingProjects.

/**
	 * @param root the package fragment root
	 * @return array of projects that have the specified root on their classpath
	 * @throws JavaModelException if getting the raw classpath or all Java projects fails
	 */
public static IJavaProject[] getReferencingProjects(IPackageFragmentRoot root) throws JavaModelException {
    IClasspathEntry cpe = root.getRawClasspathEntry();
    if (cpe.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
        cpe = root.getResolvedClasspathEntry();
    IJavaProject[] allJavaProjects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
    List<IJavaProject> result = new ArrayList<IJavaProject>(allJavaProjects.length);
    for (int i = 0; i < allJavaProjects.length; i++) {
        IJavaProject project = allJavaProjects[i];
        IPackageFragmentRoot[] roots = project.findPackageFragmentRoots(cpe);
        if (roots.length > 0)
            result.add(project);
    }
    return result.toArray(new IJavaProject[result.size()]);
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 62 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.

the class JavaElementUtil method getPackageAndSubpackages.

/**
	 * @param pack a package fragment
	 * @return an array containing the given package and all subpackages
	 * @throws JavaModelException if getting the all sibling packages fails
	 */
public static IPackageFragment[] getPackageAndSubpackages(IPackageFragment pack) throws JavaModelException {
    if (pack.isDefaultPackage())
        return new IPackageFragment[] { pack };
    IPackageFragmentRoot root = (IPackageFragmentRoot) pack.getParent();
    IJavaElement[] allPackages = root.getChildren();
    ArrayList<IPackageFragment> subpackages = new ArrayList<IPackageFragment>();
    subpackages.add(pack);
    String prefix = pack.getElementName() + '.';
    for (int i = 0; i < allPackages.length; i++) {
        IPackageFragment currentPackage = (IPackageFragment) allPackages[i];
        if (currentPackage.getElementName().startsWith(prefix))
            subpackages.add(currentPackage);
    }
    return subpackages.toArray(new IPackageFragment[subpackages.size()]);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 63 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project generator by mybatis.

the class EclipseShellCallback method getSpecificSourceFolder.

private IPackageFragmentRoot getSpecificSourceFolder(IJavaProject javaProject, String targetProject) throws ShellException {
    try {
        Path path = new Path("/" + targetProject);
        IPackageFragmentRoot pfr = javaProject.findPackageFragmentRoot(path);
        if (pfr == null) {
            StringBuffer sb = new StringBuffer();
            sb.append("Cannot find source folder ");
            sb.append(targetProject);
            throw new ShellException(sb.toString());
        }
        return pfr;
    } catch (CoreException e) {
        throw new ShellException(e.getStatus().getMessage(), e);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) CoreException(org.eclipse.core.runtime.CoreException) ShellException(org.mybatis.generator.exception.ShellException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 64 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project generator by mybatis.

the class EclipseShellCallback method getSourceFolder.

private IPackageFragmentRoot getSourceFolder(String targetProject) throws ShellException {
    IPackageFragmentRoot answer = sourceFolders.get(targetProject);
    if (answer == null) {
        // first parse the targetProject into project and source folder
        // values
        int index = targetProject.indexOf('/');
        if (index == -1) {
            index = targetProject.indexOf('\\');
        }
        if (index == -1) {
            // no source folder specified
            IJavaProject javaProject = getJavaProject(targetProject);
            answer = getFirstSourceFolder(javaProject);
        } else {
            IJavaProject javaProject = getJavaProject(targetProject.substring(0, index));
            answer = getSpecificSourceFolder(javaProject, targetProject);
        }
        sourceFolders.put(targetProject, answer);
    }
    return answer;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 65 with IPackageFragmentRoot

use of org.eclipse.jdt.core.IPackageFragmentRoot in project che by eclipse.

the class IndexSelector method getFocusedElementsAndTypes.

/*
 * Create the list of focused jars or projects.
 */
private static IJavaElement[] getFocusedElementsAndTypes(SearchPattern pattern, IJavaElement focusElement, ObjectVector superTypes) throws JavaModelException {
    if (pattern instanceof MethodPattern) {
        // For method pattern, it needs to walk along the focus type super hierarchy
        // and add jars/projects of all the encountered types.
        IType type = (IType) pattern.focus.getAncestor(IJavaElement.TYPE);
        MethodPattern methodPattern = (MethodPattern) pattern;
        String selector = new String(methodPattern.selector);
        int parameterCount = methodPattern.parameterCount;
        ITypeHierarchy superHierarchy = type.newSupertypeHierarchy(null);
        IType[] allTypes = superHierarchy.getAllSupertypes(type);
        int length = allTypes.length;
        SimpleSet focusSet = new SimpleSet(length + 1);
        if (focusElement != null)
            focusSet.add(focusElement);
        for (int i = 0; i < length; i++) {
            IMethod[] methods = allTypes[i].getMethods();
            int mLength = methods.length;
            for (int m = 0; m < mLength; m++) {
                if (parameterCount == methods[m].getNumberOfParameters() && methods[m].getElementName().equals(selector)) {
                    IPackageFragmentRoot root = (IPackageFragmentRoot) allTypes[i].getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
                    IJavaElement element = root.isArchive() ? root : root.getParent();
                    focusSet.add(element);
                    if (superTypes != null)
                        superTypes.add(allTypes[i]);
                    break;
                }
            }
        }
        // Rebuilt a contiguous array
        IJavaElement[] focuses = new IJavaElement[focusSet.elementSize];
        Object[] values = focusSet.values;
        int count = 0;
        for (int i = values.length; --i >= 0; ) {
            if (values[i] != null) {
                focuses[count++] = (IJavaElement) values[i];
            }
        }
        return focuses;
    }
    if (focusElement == null)
        return new IJavaElement[0];
    return new IJavaElement[] { focusElement };
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SimpleSet(org.eclipse.jdt.internal.compiler.util.SimpleSet) MethodPattern(org.eclipse.jdt.internal.core.search.matching.MethodPattern) IType(org.eclipse.jdt.core.IType) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)179 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)96 Test (org.junit.Test)95 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)78 OrganizeImportsOperation (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation)67 IChooseImportQuery (org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImportsOperation.IChooseImportQuery)67 IJavaProject (org.eclipse.jdt.core.IJavaProject)37 File (java.io.File)26 IJavaElement (org.eclipse.jdt.core.IJavaElement)22 IResource (org.eclipse.core.resources.IResource)17 JarEntry (org.eclipse.che.ide.ext.java.shared.JarEntry)16 IPath (org.eclipse.core.runtime.IPath)15 ArrayList (java.util.ArrayList)13 IType (org.eclipse.jdt.core.IType)12 CoreException (org.eclipse.core.runtime.CoreException)11 Path (org.eclipse.core.runtime.Path)11 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)11 JavaModelException (org.eclipse.jdt.core.JavaModelException)10 IFile (org.eclipse.core.resources.IFile)9 IProject (org.eclipse.core.resources.IProject)9