Example usage for org.eclipse.jdt.core IClasspathEntry getOutputLocation

List of usage examples for org.eclipse.jdt.core IClasspathEntry getOutputLocation

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry getOutputLocation.

Prototype

IPath getOutputLocation();

Source Link

Document

Returns the full path to the specific location where the builder writes .class files generated for this source entry (entry kind #CPE_SOURCE ).

Usage

From source file:org.sonarlint.eclipse.jdt.internal.JavaProjectConfigurator.java

License:Open Source License

private void processSourceEntry(IClasspathEntry entry, IJavaProject javaProject,
        JavaProjectConfiguration context, boolean topProject) throws JavaModelException {
    String srcDir = getRelativePath(javaProject.getPath(), entry.getPath());
    if (srcDir == null) {
        SonarLintCorePlugin.getDefault()
                .info("Skipping non existing source entry: " + entry.getPath().toOSString());
        return;/*w w w.  jav a 2s  . co m*/
    }
    if (entry.getOutputLocation() != null) {
        processOutputDir(entry.getOutputLocation(), context, topProject);
    }
}

From source file:org.sonarlint.eclipse.jdt.internal.JdtUtils.java

License:Open Source License

private static void processSourceEntry(IClasspathEntry entry, JavaProjectConfiguration context,
        boolean topProject) throws JavaModelException {
    if (isSourceExcluded(entry)) {
        return;//from ww w  .  ja va  2s  .c  o m
    }
    if (entry.getOutputLocation() != null) {
        processOutputDir(entry.getOutputLocation(), context, topProject);
    }
}

From source file:org.sonarlint.eclipse.jdt.internal.JdtUtilsTest.java

License:Open Source License

private IClasspathEntry createCPE(int kind, File path, @Nullable File outputLocation) {
    IClasspathEntry cpe = mock(IClasspathEntry.class);
    when(cpe.getEntryKind()).thenReturn(kind);
    when(cpe.getPath()).thenReturn(new Path(path.getAbsolutePath()));
    when(cpe.getOutputLocation()).thenReturn(new Path(outputLocation.getAbsolutePath()));
    return cpe;//from  w ww .  j a  va2 s.co  m
}

From source file:org.springframework.ide.eclipse.beans.core.model.locate.ProjectScanningBeansConfigLocator.java

License:Open Source License

/**
 * Filters out every {@link IFile} which is has unknown root elements in its
 * XML content.//from  w ww.j a va2  s  .  com
 */
@Override
protected Set<IFile> filterMatchingFiles(Set<IFile> files) {
    // if project is a java project remove bin dirs from the list
    Set<String> outputDirectories = new HashSet<String>();
    IJavaProject javaProject = JdtUtils.getJavaProject(project);
    if (javaProject != null) {
        try {
            // add default output directory
            outputDirectories.add(javaProject.getOutputLocation().toString());

            // add source folder specific output directories
            for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getOutputLocation() != null) {
                    outputDirectories.add(entry.getOutputLocation().toString());
                }
            }
        } catch (JavaModelException e) {
            BeansCorePlugin.log(e);
        }
    }

    Set<IFile> detectedFiles = new LinkedHashSet<IFile>();
    for (IFile file : files) {
        boolean skip = false;
        // first check if the file sits in an output directory
        String path = file.getFullPath().toString();
        for (String outputDirectory : outputDirectories) {
            if (path.startsWith(outputDirectory)) {
                skip = true;
            }
        }
        if (skip) {
            continue;
        }

        // check if the file is known Spring xml file
        IStructuredModel model = null;
        try {
            try {
                model = StructuredModelManager.getModelManager().getExistingModelForRead(file);
            } catch (RuntimeException e) {
                // sometimes WTP throws a NPE in concurrency situations
            }
            if (model == null) {
                model = StructuredModelManager.getModelManager().getModelForRead(file);
            }
            if (model != null) {
                IDOMDocument document = ((DOMModelImpl) model).getDocument();
                if (document != null && document.getDocumentElement() != null) {
                    String namespaceUri = document.getDocumentElement().getNamespaceURI();
                    if (applyNamespaceFilter(file, namespaceUri)) {
                        detectedFiles.add(file);
                    }
                }
            }
        } catch (IOException e) {
            BeansCorePlugin.log(e);
        } catch (CoreException e) {
            BeansCorePlugin.log(e);
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
    return detectedFiles;
}

From source file:org.springframework.ide.eclipse.boot.dash.cloudfoundry.JavaPackageFragmentRootHandler.java

License:Open Source License

/**
 *
 * Determines if the given package fragment root corresponds to the class
 * path entry path./*www.ja  v  a 2  s. c  om*/
 * <p/>
 * Note that different package fragment roots may point to the same class
 * path entry.
 * <p/>
 * Example:
 * <p/>
 * A Java project may have the following package fragment roots:
 * <p/>
 * - src/main/java
 * <p/>
 * - src/main/resources
 * <p/>
 * Both may be using the same output folder:
 * <p/>
 * target/classes.
 * <p/>
 * In this case, the output folder will have a class path entry -
 * target/classes - and it will be the same for both roots, and this method
 * will return true for both roots if passed the entry for target/classes
 *
 * @param root
 *            to check if it corresponds to the given class path entry path
 * @param entry
 * @return true if root is at the given entry
 */
private static boolean isRootAtEntry(IPackageFragmentRoot root, IPath entry) {
    try {
        IClasspathEntry cpe = root.getRawClasspathEntry();
        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath outputLocation = cpe.getOutputLocation();
            if (outputLocation == null) {
                outputLocation = root.getJavaProject().getOutputLocation();
            }

            IPath location = ResourcesPlugin.getWorkspace().getRoot().findMember(outputLocation).getLocation();
            if (entry.equals(location)) {
                return true;
            }
        }
    } catch (JavaModelException e) {
        BootDashActivator.log(e);
    }

    IResource resource = root.getResource();
    if (resource != null && entry.equals(resource.getLocation())) {
        return true;
    }

    IPath path = root.getPath();
    if (path != null && entry.equals(path)) {
        return true;
    }

    return false;
}

From source file:org.springframework.ide.eclipse.core.java.JdtUtils.java

License:Open Source License

public static IResource getSourceResource(IResource classFile) {
    try {/*ww  w.ja v a  2s . c o m*/
        if (isJavaProject(classFile) && classFile.getName().endsWith(CLASS_FILE_EXTENSION)) {
            IPath classFilePath = classFile.getFullPath();
            String classFileName = null;

            IJavaProject project = getJavaProject(classFile);
            IPath defaultOutput = project.getOutputLocation();

            if (defaultOutput.isPrefixOf(classFilePath)) {
                classFileName = classFilePath.removeFirstSegments(defaultOutput.segmentCount()).toString();
            } else {
                for (IClasspathEntry entry : project.getRawClasspath()) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        IPath output = entry.getOutputLocation();
                        if (output != null) {
                            if (classFilePath.isPrefixOf(output)) {
                                classFileName = classFilePath.removeFirstSegments(output.segmentCount())
                                        .toString();
                            }
                        }
                    }
                }
            }

            if (classFileName != null) {
                // Replace file extension
                String sourceFileName = classFileName.replace(".class", ".java");
                for (IClasspathEntry entry : project.getRawClasspath()) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        IPath path = entry.getPath().append(sourceFileName).removeFirstSegments(1);
                        IResource resource = project.getProject().findMember(path);
                        if (resource != null) {
                            return resource;
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
    }
    return null;
}

From source file:org.springframework.ide.eclipse.core.java.ProjectClassLoaderCache.java

License:Open Source License

/**
 * Add {@link URL}s to the given set of <code>paths</code>.
 *//*from  w w  w . j  av  a  2 s. c o m*/
private static void addClassPathUrls(IProject project, List<URL> paths, Set<IProject> resolvedProjects) {

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    // add project to local cache to prevent adding its classpaths multiple times
    if (resolvedProjects.contains(project)) {
        return;
    } else {
        resolvedProjects.add(project);
    }

    try {
        if (JdtUtils.isJavaProject(project)) {
            IJavaProject jp = JavaCore.create(project);
            // configured classpath
            IClasspathEntry[] classpath = jp.getResolvedClasspath(true);

            // add class path entries
            for (int i = 0; i < classpath.length; i++) {
                IClasspathEntry path = classpath[i];
                if (path.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    IPath entryPath = path.getPath();
                    File file = entryPath.toFile();
                    if (file.exists()) {
                        paths.add(file.toURI().toURL());
                    } else {
                        // case for project relative links
                        String projectName = entryPath.segment(0);
                        IProject pathProject = root.getProject(projectName);
                        covertPathToUrl(pathProject, paths, entryPath);
                    }
                } else if (path.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    // add source path as well for non java resources
                    IPath sourcePath = path.getPath();
                    covertPathToUrl(project, paths, sourcePath);
                    // add source output locations for different source
                    // folders
                    IPath sourceOutputPath = path.getOutputLocation();
                    covertPathToUrl(project, paths, sourceOutputPath);
                }
            }
            // add all depending java projects
            for (IJavaProject p : JdtUtils.getAllDependingJavaProjects(jp)) {
                addClassPathUrls(p.getProject(), paths, resolvedProjects);
            }

            // get default output directory
            IPath outputPath = jp.getOutputLocation();
            covertPathToUrl(project, paths, outputPath);
        } else {
            for (IProject p : project.getReferencedProjects()) {
                addClassPathUrls(p, paths, resolvedProjects);
            }
        }
    } catch (Exception e) {
        // ignore
    }
}

From source file:org.springframework.ide.eclipse.core.java.TypeStructureCache.java

License:Open Source License

private static ClassFileReader getClassFileReaderForClassName(String className, IProject project)
        throws JavaModelException, MalformedURLException {
    IJavaProject jp = JavaCore.create(project);

    File outputDirectory = convertPathToFile(project, jp.getOutputLocation());
    File classFile = new File(outputDirectory, ClassUtils.getClassFileName(className));
    if (classFile.exists() && classFile.canRead()) {
        try {/* w  w  w.  ja  va2  s  .c o m*/
            return ClassFileReader.read(classFile);
        } catch (ClassFormatException e) {
        } catch (IOException e) {
        }
    }

    IClasspathEntry[] classpath = jp.getRawClasspath();
    for (int i = 0; i < classpath.length; i++) {
        IClasspathEntry path = classpath[i];
        if (path.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            outputDirectory = convertPathToFile(project, path.getOutputLocation());
            classFile = new File(outputDirectory, ClassUtils.getClassFileName(className));
            if (classFile.exists() && classFile.canRead()) {
                try {
                    return ClassFileReader.read(classFile);
                } catch (ClassFormatException e) {
                } catch (IOException e) {
                }
            }
        }
    }
    return null;
}

From source file:org.springframework.ide.eclipse.core.SpringCoreUtils.java

License:Open Source License

/**
 * Checks if the given {@link IResource} is a OSGi bundle manifest.
 * <p>//  w w  w  .  j  a va2 s . co  m
 * Note: only the name and last segment of the folder name are checked.
 * @since 2.0.5
 */
public static boolean isManifest(IResource resource) {
    // check if it is a MANIFEST.MF file in META-INF
    if (resource != null
            // && resource.isAccessible()
            && resource.getType() == IResource.FILE && resource.getName().equals(BUNDLE_MANIFEST_FILE)
            && resource.getParent() != null && resource.getParent().getProjectRelativePath() != null
            && resource.getParent().getProjectRelativePath().lastSegment() != null
            && resource.getParent().getProjectRelativePath().lastSegment().equals(BUNDLE_MANIFEST_FOLDER)) {

        // check if the manifest is not in an output folder
        IPath filePath = resource.getFullPath();
        IJavaProject javaProject = JdtUtils.getJavaProject(resource);
        if (javaProject != null) {
            try {
                IPath defaultOutputLocation = javaProject.getOutputLocation();
                if (defaultOutputLocation != null && defaultOutputLocation.isPrefixOf(filePath)) {
                    return false;
                }
                for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        IPath outputLocation = entry.getOutputLocation();
                        if (outputLocation != null && outputLocation.isPrefixOf(filePath)) {
                            return false;
                        }
                    }
                }
            } catch (JavaModelException e) {
                // don't care here
            }
            return true;
        } else {
            // if the project is not a java project -> it is the manifest
            return true;
        }
    }
    return false;
}

From source file:org.springframework.tooling.jdt.ls.commons.classpath.ClasspathUtil.java

License:Open Source License

private static CPE createSourceCPE(IJavaProject javaProject, IClasspathEntry entry) throws JavaModelException {
    IPath sourcePath = entry.getPath();/*from  www . j  av a  2  s.c o m*/
    // log("source entry =" + sourcePath);
    IPath absoluteSourcePath = resolveWorkspacePath(sourcePath);
    // log("absoluteSourcePath =" + absoluteSourcePath);
    if (absoluteSourcePath != null) {
        IPath of = entry.getOutputLocation();
        // log("outputFolder =" + of);
        IPath absoluteOutFolder;
        if (of != null) {
            absoluteOutFolder = resolveWorkspacePath(of);
        } else {
            absoluteOutFolder = resolveWorkspacePath(javaProject.getOutputLocation());
        }
        return CPE.source(absoluteSourcePath.toFile(), absoluteOutFolder.toFile());
    }
    return null;
}