Search in sources :

Example 11 with IClasspathEntry

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

the class JavaProjectHelper method addRequiredProject.

//	/**
//	 * Adds a variable entry with source attachment to a IJavaProject.
//	 * Can return null if variable can not be resolved.
//	 * @param jproject The parent project
//	 * @param path The variable path
//	 * @param sourceAttachPath The source attachment path (variable path)
//	 * @param sourceAttachRoot The source attachment root path (variable path)
//	 * @return The added package fragment root
//	 * @throws JavaModelException
//	 */
//	public static IPackageFragmentRoot addVariableEntry(IJavaProject jproject, IPath path, IPath sourceAttachPath, IPath sourceAttachRoot)
// throws JavaModelException {
//		IClasspathEntry cpe= JavaCore.newVariableEntry(path, sourceAttachPath, sourceAttachRoot);
//		addToClasspath(jproject, cpe);
//		IPath resolvedPath= JavaCore.getResolvedVariablePath(path);
//		if (resolvedPath != null) {
//			return jproject.getPackageFragmentRoot(resolvedPath.toString());
//		}
//		return null;
//	}
//
//	public static IPackageFragmentRoot addVariableRTJar13(IJavaProject jproject, String libVarName, String srcVarName, String
// srcrootVarName) throws CoreException {
//		return addVariableRTJar(jproject, RT_STUBS_13, libVarName, srcVarName, srcrootVarName);
//	}
//	/**
//	 * Adds a variable entry pointing to a current JRE (stubs only)
//	 * and sets the compiler compliance level on the project accordingly.
//	 * The arguments specify the names of the variables to be used.
//	 * Currently, the compiler compliance level is set to 1.5.
//	 *
//	 * @param jproject the project to add the variable RT JAR
//	 * @param libVarName Name of the variable for the library
//	 * @param srcVarName Name of the variable for the source attachment. Can be <code>null</code>.
//	 * @param srcrootVarName name of the variable for the source attachment root. Can be <code>null</code>.
//	 * @return the new package fragment root
//	 * @throws CoreException Creation failed
//	 */
//	public static IPackageFragmentRoot addVariableRTJar(IJavaProject jproject, String libVarName, String srcVarName, String
// srcrootVarName) throws CoreException {
//		return addVariableRTJar(jproject, RT_STUBS_15, libVarName, srcVarName, srcrootVarName);
//	}
//
//	/**
//	 * Adds a variable entry pointing to a current JRE (stubs only).
//	 * The arguments specify the names of the variables to be used.
//	 * Clients must not forget to set the right compiler compliance level on the project.
//	 *
//	 * @param jproject the project to add the variable RT JAR
//	 * @param rtStubsPath path to an rt.jar
//	 * @param libVarName name of the variable for the library
//	 * @param srcVarName Name of the variable for the source attachment. Can be <code>null</code>.
//	 * @param srcrootVarName Name of the variable for the source attachment root. Can be <code>null</code>.
//	 * @return the new package fragment root
//	 * @throws CoreException Creation failed
//	 */
//	private static IPackageFragmentRoot addVariableRTJar(IJavaProject jproject, IPath rtStubsPath, String libVarName, String srcVarName,
// String srcrootVarName) throws CoreException {
//		IPath[] rtJarPaths= findRtJar(rtStubsPath);
//		IPath libVarPath= new Path(libVarName);
//		IPath srcVarPath= null;
//		IPath srcrootVarPath= null;
//		JavaCore.setClasspathVariable(libVarName, rtJarPaths[0], null);
//		if (srcVarName != null) {
//			IPath varValue= rtJarPaths[1] != null ? rtJarPaths[1] : Path.EMPTY;
//			JavaCore.setClasspathVariable(srcVarName, varValue, null);
//			srcVarPath= new Path(srcVarName);
//		}
//		if (srcrootVarName != null) {
//			IPath varValue= rtJarPaths[2] != null ? rtJarPaths[2] : Path.EMPTY;
//			JavaCore.setClasspathVariable(srcrootVarName, varValue, null);
//			srcrootVarPath= new Path(srcrootVarName);
//		}
//		return addVariableEntry(jproject, libVarPath, srcVarPath, srcrootVarPath);
//	}
/**
     * Adds a required project entry.
     * @param jproject Parent project
     * @param required Project to add to the build path
     * @throws JavaModelException Creation failed
     */
public static void addRequiredProject(IJavaProject jproject, IJavaProject required) throws JavaModelException {
    IClasspathEntry cpe = JavaCore.newProjectEntry(required.getProject().getFullPath());
    addToClasspath(jproject, cpe);
}
Also used : IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 12 with IClasspathEntry

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

the class IndexSelector method canSeeFocus.

private static int canSeeFocus(IJavaElement focus, JavaProject javaProject, char[][][] focusQualifiedNames) {
    try {
        if (focus == null)
            return PROJECT_CAN_NOT_SEE_FOCUS;
        if (focus.equals(javaProject))
            return PROJECT_CAN_SEE_FOCUS;
        if (focus instanceof JarPackageFragmentRoot) {
            // focus is part of a jar
            IPath focusPath = focus.getPath();
            IClasspathEntry[] entries = javaProject.getExpandedClasspath();
            for (int i = 0, length = entries.length; i < length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY && entry.getPath().equals(focusPath))
                    return PROJECT_CAN_SEE_FOCUS;
            }
            return PROJECT_CAN_NOT_SEE_FOCUS;
        }
        // look for dependent projects
        IPath focusPath = ((JavaProject) focus).getProject().getFullPath();
        IClasspathEntry[] entries = javaProject.getExpandedClasspath();
        for (int i = 0, length = entries.length; i < length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT && entry.getPath().equals(focusPath)) {
                if (focusQualifiedNames != null) {
                    // builder state is usable, hence use it to try to reduce project which can see the focus...
                    State projectState = (State) JavaModelManager.getJavaModelManager().getLastBuiltState(javaProject.getProject(), null);
                    if (projectState != null) {
                        Object[] values = projectState.getReferences().valueTable;
                        int vLength = values.length;
                        for (int j = 0; j < vLength; j++) {
                            if (values[j] == null)
                                continue;
                            ReferenceCollection references = (ReferenceCollection) values[j];
                            if (references.includes(focusQualifiedNames, null, null)) {
                                return PROJECT_CAN_SEE_FOCUS;
                            }
                        }
                        return PROJECT_SOURCE_CAN_NOT_SEE_FOCUS;
                    }
                }
                return PROJECT_CAN_SEE_FOCUS;
            }
        }
        return PROJECT_CAN_NOT_SEE_FOCUS;
    } catch (JavaModelException e) {
        return PROJECT_CAN_NOT_SEE_FOCUS;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) JarPackageFragmentRoot(org.eclipse.jdt.internal.core.JarPackageFragmentRoot) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) State(org.eclipse.jdt.internal.core.builder.State) ReferenceCollection(org.eclipse.jdt.internal.core.builder.ReferenceCollection)

Example 13 with IClasspathEntry

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

the class IndexSelector method canSeeFocus.

/**
	 * Returns whether elements of the given project or jar can see the given focus (an IJavaProject or
	 * a JarPackageFragmentRot) either because the focus is part of the project or the jar, or because it is
	 * accessible throught the project's classpath
	 */
public static int canSeeFocus(SearchPattern pattern, IPath projectOrJarPath) {
    try {
        IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
        IJavaProject project = getJavaProject(projectOrJarPath, model);
        IJavaElement[] focuses = getFocusedElementsAndTypes(pattern, project, null);
        if (focuses.length == 0)
            return PROJECT_CAN_NOT_SEE_FOCUS;
        if (project != null) {
            return canSeeFocus(focuses, (JavaProject) project, null);
        }
        // projectOrJarPath is a jar
        // it can see the focus only if it is on the classpath of a project that can see the focus
        int result = PROJECT_CAN_NOT_SEE_FOCUS;
        IJavaProject[] allProjects = model.getJavaProjects();
        for (int i = 0, length = allProjects.length; i < length; i++) {
            JavaProject otherProject = (JavaProject) allProjects[i];
            IClasspathEntry entry = otherProject.getClasspathEntryFor(projectOrJarPath);
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                int canSeeFocus = canSeeFocus(focuses, otherProject, null);
                if (canSeeFocus == PROJECT_CAN_SEE_FOCUS)
                    return PROJECT_CAN_SEE_FOCUS;
                if (canSeeFocus == PROJECT_SOURCE_CAN_NOT_SEE_FOCUS)
                    result = PROJECT_SOURCE_CAN_NOT_SEE_FOCUS;
            }
        }
        return result;
    } catch (JavaModelException e) {
        return PROJECT_CAN_NOT_SEE_FOCUS;
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 14 with IClasspathEntry

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

the class IndexSelector method initializeIndexLocations.

/*
 *  Compute the list of paths which are keying index files.
 */
private void initializeIndexLocations() {
    IPath[] projectsAndJars = this.searchScope.enclosingProjectsAndJars();
    IndexManager manager = JavaModelManager.getIndexManager();
    // use a linked set to preserve the order during search: see bug 348507
    LinkedHashSet locations = new LinkedHashSet();
    IJavaElement focus = MatchLocator.projectOrJarFocus(this.pattern);
    if (focus == null) {
        for (int i = 0; i < projectsAndJars.length; i++) {
            IPath path = projectsAndJars[i];
            Object target = JavaModel.getTarget(path, false);
            if (// case of an external folder
            target instanceof IFolder)
                path = ((IFolder) target).getFullPath();
            locations.add(manager.computeIndexLocation(path));
        }
    } else {
        try {
            // See whether the state builder might be used to reduce the number of index locations
            // find the projects from projectsAndJars that see the focus then walk those projects looking for the jars from projectsAndJars
            int length = projectsAndJars.length;
            JavaProject[] projectsCanSeeFocus = new JavaProject[length];
            SimpleSet visitedProjects = new SimpleSet(length);
            int projectIndex = 0;
            SimpleSet externalLibsToCheck = new SimpleSet(length);
            ObjectVector superTypes = new ObjectVector();
            IJavaElement[] focuses = getFocusedElementsAndTypes(this.pattern, focus, superTypes);
            char[][][] focusQualifiedNames = null;
            boolean isAutoBuilding = ResourcesPlugin.getWorkspace().getDescription().isAutoBuilding();
            if (isAutoBuilding && focus instanceof IJavaProject) {
                focusQualifiedNames = getQualifiedNames(superTypes);
            }
            IJavaModel model = JavaModelManager.getJavaModelManager().getJavaModel();
            for (int i = 0; i < length; i++) {
                IPath path = projectsAndJars[i];
                JavaProject project = (JavaProject) getJavaProject(path, model);
                if (project != null) {
                    visitedProjects.add(project);
                    /*We are adding all modules to the locations for searching in each one.
					Now the location contains not only current module and Jars on which depends, but also all modules from the workspace.*/
                    locations.add(manager.computeIndexLocation(path));
                /*int canSeeFocus = canSeeFocus(focuses, project, focusQualifiedNames);
					if (canSeeFocus == PROJECT_CAN_SEE_FOCUS) {
						locations.add(manager.computeIndexLocation(path));
					}
					if (canSeeFocus != PROJECT_CAN_NOT_SEE_FOCUS) {
						projectsCanSeeFocus[projectIndex++] = project;
					}*/
                } else {
                    externalLibsToCheck.add(path);
                }
            }
            for (int i = 0; i < projectIndex && externalLibsToCheck.elementSize > 0; i++) {
                IClasspathEntry[] entries = projectsCanSeeFocus[i].getResolvedClasspath();
                for (int j = entries.length; --j >= 0; ) {
                    IClasspathEntry entry = entries[j];
                    if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPath path = entry.getPath();
                        if (externalLibsToCheck.remove(path) != null) {
                            Object target = JavaModel.getTarget(path, false);
                            if (// case of an external folder
                            target instanceof IFolder)
                                path = ((IFolder) target).getFullPath();
                            locations.add(manager.computeIndexLocation(path));
                        }
                    }
                }
            }
            // jar files can be included in the search scope without including one of the projects that references them, so scan all projects that have not been visited
            if (externalLibsToCheck.elementSize > 0) {
                IJavaProject[] allProjects = model.getJavaProjects();
                for (int i = 0, l = allProjects.length; i < l && externalLibsToCheck.elementSize > 0; i++) {
                    JavaProject project = (JavaProject) allProjects[i];
                    if (!visitedProjects.includes(project)) {
                        IClasspathEntry[] entries = project.getResolvedClasspath();
                        for (int j = entries.length; --j >= 0; ) {
                            IClasspathEntry entry = entries[j];
                            if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                                IPath path = entry.getPath();
                                if (externalLibsToCheck.remove(path) != null) {
                                    Object target = JavaModel.getTarget(path, false);
                                    if (// case of an external folder
                                    target instanceof IFolder)
                                        path = ((IFolder) target).getFullPath();
                                    locations.add(manager.computeIndexLocation(path));
                                }
                            }
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
        // ignored
        }
    }
    // Ensure no nulls
    locations.remove(null);
    this.indexLocations = (IndexLocation[]) locations.toArray(new IndexLocation[locations.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaProject(org.eclipse.jdt.internal.core.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) SimpleSet(org.eclipse.jdt.internal.compiler.util.SimpleSet) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ObjectVector(org.eclipse.jdt.internal.compiler.util.ObjectVector) IndexManager(org.eclipse.jdt.internal.core.search.indexing.IndexManager) IJavaProject(org.eclipse.jdt.core.IJavaProject) IndexLocation(org.eclipse.jdt.internal.core.index.IndexLocation) IFolder(org.eclipse.core.resources.IFolder) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 15 with IClasspathEntry

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

the class JavaModelManager method determineIfOnClasspath.

/**
     * Returns the package fragment root represented by the resource, or
     * the package fragment the given resource is located in, or <code>null</code>
     * if the given resource is not on the classpath of the given project.
     */
public static IJavaElement determineIfOnClasspath(IResource resource, IJavaProject project) {
    IPath resourcePath = resource.getFullPath();
    boolean isExternal = ExternalFoldersManager.isExternalFolderPath(resourcePath);
    if (isExternal)
        resourcePath = resource.getLocation();
    try {
        JavaProjectElementInfo projectInfo = (JavaProjectElementInfo) org.eclipse.jdt.internal.core.JavaModelManager.getJavaModelManager().getInfo(project);
        JavaProjectElementInfo.ProjectCache projectCache = projectInfo == null ? null : projectInfo.projectCache;
        HashtableOfArrayToObject allPkgFragmentsCache = projectCache == null ? null : projectCache.allPkgFragmentsCache;
        boolean isJavaLike = Util.isJavaLikeFileName(resourcePath.lastSegment());
        IClasspathEntry[] entries = // JAVA file can only live inside SRC folder (on the raw path)
        isJavaLike ? // JAVA file can only live inside SRC folder (on the raw path)
        project.getRawClasspath() : ((JavaProject) project).getResolvedClasspath();
        int length = entries.length;
        if (length > 0) {
            String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
            for (int i = 0; i < length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT)
                    continue;
                IPath rootPath = entry.getPath();
                if (rootPath.equals(resourcePath)) {
                    if (isJavaLike)
                        return null;
                    return project.getPackageFragmentRoot(resource);
                } else if (rootPath.isPrefixOf(resourcePath)) {
                    // allow creation of package fragment if it contains a .java file that is included
                    if (!Util.isExcluded(resourcePath, ((ClasspathEntry) entry).fullInclusionPatternChars(), ((ClasspathEntry) entry).fullExclusionPatternChars(), true)) {
                        // given we have a resource child of the root, it cannot be a JAR pkg root
                        PackageFragmentRoot root = isExternal ? new ExternalPackageFragmentRoot(rootPath, (JavaProject) project) : (PackageFragmentRoot) ((JavaProject) project).getFolderPackageFragmentRoot(rootPath);
                        if (root == null)
                            return null;
                        IPath pkgPath = resourcePath.removeFirstSegments(rootPath.segmentCount());
                        if (resource.getType() == IResource.FILE) {
                            // if the resource is a file, then remove the last segment which
                            // is the file name in the package
                            pkgPath = pkgPath.removeLastSegments(1);
                        }
                        String[] pkgName = pkgPath.segments();
                        // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=133141)
                        if (allPkgFragmentsCache != null && allPkgFragmentsCache.containsKey(pkgName))
                            return root.getPackageFragment(pkgName);
                        if (pkgName.length != 0 && JavaConventions.validatePackageName(Util.packageName(pkgPath, sourceLevel, complianceLevel), sourceLevel, complianceLevel).getSeverity() == IStatus.ERROR) {
                            return null;
                        }
                        return root.getPackageFragment(pkgName);
                    }
                }
            }
        }
    } catch (JavaModelException npe) {
        return null;
    }
    return null;
}
Also used : IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) HashtableOfArrayToObject(org.eclipse.jdt.internal.core.util.HashtableOfArrayToObject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Aggregations

IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)115 IPath (org.eclipse.core.runtime.IPath)50 IJavaProject (org.eclipse.jdt.core.IJavaProject)40 ArrayList (java.util.ArrayList)34 Path (org.eclipse.core.runtime.Path)25 JavaModelException (org.eclipse.jdt.core.JavaModelException)20 IProject (org.eclipse.core.resources.IProject)17 IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)16 File (java.io.File)15 IFolder (org.eclipse.core.resources.IFolder)13 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 IJavaElement (org.eclipse.jdt.core.IJavaElement)12 CoreException (org.eclipse.core.runtime.CoreException)11 JavaProject (org.eclipse.jdt.internal.core.JavaProject)11 HashMap (java.util.HashMap)9 IResource (org.eclipse.core.resources.IResource)9 IFile (org.eclipse.core.resources.IFile)8 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 URL (java.net.URL)6