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

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

Introduction

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

Prototype

int CPE_SOURCE

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a folder containing package fragments with source code to be compiled.

Usage

From source file:org.eclipse.edt.ide.ui.wizards.EGLProjectUtility.java

License:Open Source License

public static boolean sourceClasspathEntryExists(IJavaProject javaProject) throws JavaModelException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE
                && entries[i].getPath().segmentCount() > 1) {
            return true;
        }//from w  w w .  j  a v  a2  s  .  c o  m
    }
    return false;
}

From source file:org.eclipse.emf.ecore.xcore.ui.EmptyXcoreProjectWizard.java

License:Open Source License

@Override
public void modifyWorkspace(IProgressMonitor progressMonitor)
        throws CoreException, UnsupportedEncodingException, IOException {
    super.modifyWorkspace(progressMonitor);
    IProjectDescription projectDescription = project.getDescription();
    String[] natureIds = projectDescription.getNatureIds();
    String[] newNatureIds = new String[natureIds.length + 1];
    System.arraycopy(natureIds, 0, newNatureIds, 0, natureIds.length);
    newNatureIds[natureIds.length] = XtextProjectHelper.NATURE_ID;
    projectDescription.setNatureIds(newNatureIds);
    project.setDescription(projectDescription, progressMonitor);

    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] classpath = javaProject.getRawClasspath();
    IClasspathEntry[] newClasspath = new IClasspathEntry[classpath.length + 1];
    for (int i = 0, index = 0, length = newClasspath.length; index < length; ++i, ++index) {
        newClasspath[index] = classpath[i];
        if (classpath[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath path = classpath[i].getPath();
            IPath srcGenPath = path.removeLastSegments(1).append(path.lastSegment() + "-gen");
            IClasspathEntry srcGen = JavaCore.newSourceEntry(srcGenPath);
            CodeGenUtil.EclipseUtil.findOrCreateContainer(srcGenPath, true, genModelProjectLocation,
                    progressMonitor);//  w  ww  .ja  va  2  s.c  o  m
            newClasspath[++index] = srcGen;
        }
    }
    javaProject.setRawClasspath(newClasspath, progressMonitor);
}

From source file:org.eclipse.emf.ecore.xcore.ui.XcoreJavaProjectProvider.java

License:Open Source License

public ClassLoader getClassLoader(ResourceSet resourceSet) {
    IJavaProject project = getJavaProject(resourceSet);
    if (project != null) {
        IProject iProject = project.getProject();
        IWorkspaceRoot workspaceRoot = iProject.getWorkspace().getRoot();
        List<URL> libraryURLs = new UniqueEList<URL>();
        try {//  ww  w. j  a  v a 2s. c  o  m
            getAllReferencedProjects(libraryURLs, new IProject[] { iProject });
            IClasspathEntry[] classpath = project.getResolvedClasspath(true);
            if (classpath != null) {
                String projectName = iProject.getName();
                for (int i = 0; i < classpath.length; ++i) {
                    IClasspathEntry classpathEntry = classpath[i];
                    switch (classpathEntry.getEntryKind()) {
                    case IClasspathEntry.CPE_LIBRARY:
                    case IClasspathEntry.CPE_CONTAINER: {
                        IPath path = classpathEntry.getPath();
                        if (path.segment(0).equals(projectName)) {
                            path = iProject.getLocation().append(path.removeFirstSegments(1));
                        }
                        libraryURLs.add(new URL(URI.createFileURI(path.toString()).toString()));
                        break;
                    }
                    case IClasspathEntry.CPE_PROJECT: {
                        IPath path = classpathEntry.getPath();
                        IProject referencedProject = workspaceRoot.getProject(path.segment(0));
                        IJavaProject referencedJavaProject = JavaCore.create(referencedProject);
                        IContainer container = workspaceRoot
                                .getFolder(referencedJavaProject.getOutputLocation());
                        libraryURLs.add(new URL(
                                URI.createFileURI(container.getLocation().toString() + "/").toString()));

                        IProjectDescription description = referencedProject.getDescription();
                        getAllReferencedProjects(libraryURLs, description.getReferencedProjects());
                        getAllReferencedProjects(libraryURLs, description.getDynamicReferences());
                        break;
                    }
                    case IClasspathEntry.CPE_SOURCE:
                    case IClasspathEntry.CPE_VARIABLE:
                    default: {
                        break;
                    }
                    }
                }
            }

            return new URLClassLoader(libraryURLs.toArray(new URL[libraryURLs.size()]),
                    getClass().getClassLoader());
        } catch (MalformedURLException exception) {
            exception.printStackTrace();
        } catch (JavaModelException exception) {
            exception.printStackTrace();
        } catch (CoreException exception) {
            exception.printStackTrace();
        }
    }
    for (Resource resource : resourceSet.getResources()) {
        URI uri = resource.getURI();
        if (uri.isPlatformPlugin()) {
            final Bundle bundle = Platform.getBundle(uri.segments()[1]);
            return new ClassLoader() {
                @Override
                public Enumeration<URL> findResources(String name) throws IOException {
                    return bundle.getResources(name);
                }

                @Override
                public URL findResource(String name) {
                    return bundle.getResource(name);
                }

                @Override
                public URL getResource(String name) {
                    return findResource(name);
                }

                @Override
                public Class<?> findClass(String name) throws ClassNotFoundException {
                    return bundle.loadClass(name);
                }

                @Override
                protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
                    Class<?> clazz = findClass(name);
                    if (resolve) {
                        resolveClass(clazz);
                    }
                    return clazz;
                }
            };
        }
    }
    return null;
}

From source file:org.eclipse.emf.java.presentation.JavaEditor.java

License:Open Source License

public void setupClassLoader(IProject project) {
    JavaPackageResourceImpl javaPackageResource = (JavaPackageResourceImpl) editingDomain
            .loadResource(JavaUtil.JAVA_PACKAGE_RESOURCE);

    IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();

    List<URL> libraryURLs = new UniqueEList<URL>();
    List<String> sourceURIs = new ArrayList<String>();
    try {//from   w  w  w  . j  ava2s .c  o m
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] classpath = javaProject.getResolvedClasspath(true);
        if (classpath != null) {
            for (int i = 0; i < classpath.length; ++i) {
                IClasspathEntry classpathEntry = classpath[i];
                switch (classpathEntry.getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                case IClasspathEntry.CPE_CONTAINER: {
                    libraryURLs.add(new URL(URI.createFileURI(classpathEntry.getPath().toString()).toString()));
                    break;
                }
                case IClasspathEntry.CPE_SOURCE: {
                    sourceURIs.add(
                            URI.createPlatformResourceURI(classpathEntry.getPath().toString(), true) + "/");
                    break;
                }
                case IClasspathEntry.CPE_PROJECT: {
                    IProject referencedProject = workspaceRoot.getProject(classpathEntry.getPath().segment(0));
                    IJavaProject referencedJavaProject = JavaCore.create(referencedProject);
                    IContainer container = workspaceRoot.getFolder(referencedJavaProject.getOutputLocation());
                    libraryURLs.add(
                            new URL(URI.createFileURI(container.getLocation().toString() + "/").toString()));

                    getAllReferencedProjects(libraryURLs,
                            referencedProject.getDescription().getReferencedProjects());
                    getAllReferencedProjects(libraryURLs,
                            referencedProject.getDescription().getDynamicReferences());
                    break;
                }
                case IClasspathEntry.CPE_VARIABLE:
                default: {
                    break;
                }
                }
            }
        }

        javaPackageResource.setClassLoader(new URLClassLoader(libraryURLs.toArray(new URL[libraryURLs.size()]),
                new URLClassLoader(new URL[0], null)));
        javaPackageResource.getSourceURIs().addAll(sourceURIs);
    } catch (MalformedURLException exception) {
        exception.printStackTrace();
    } catch (JavaModelException exception) {
        exception.printStackTrace();
    } catch (CoreException exception) {
        exception.printStackTrace();
    }
}

From source file:org.eclipse.emf.mwe.internal.ui.debug.sourcelookup.SourceFolderSourceContainer.java

License:Open Source License

/**
 * create a FolderSourceContainer for each declared source folder of the java project
 *//*  ww  w .j  ava2s  .com*/
@Override
protected ISourceContainer[] createSourceContainers() throws CoreException {
    if (fSourceFolders == null) {
        List<ISourceContainer> containers = new ArrayList<ISourceContainer>();
        IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
        if (fProject.getProject().isOpen()) {
            IClasspathEntry[] entries = fProject.getRawClasspath();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                switch (entry.getEntryKind()) {
                case IClasspathEntry.CPE_SOURCE:
                    IPath path = entry.getPath();
                    IResource resource = root.findMember(path);
                    if (resource instanceof IContainer) {
                        containers.add(new FolderSourceContainer((IContainer) resource, false));
                    }
                    break;
                }
            }
        }
        fSourceFolders = containers.toArray(new ISourceContainer[containers.size()]);
    }
    return fSourceFolders;
}

From source file:org.eclipse.imp.asl.compiler.ASLCompiler.java

License:Open Source License

private IPath findSrcFolderRelativePath(IFile file, IJavaProject javaProject) throws JavaModelException {
    final IPath folderPath = file.getFullPath().removeLastSegments(1);
    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);

    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            final IPath entryPath = entry.getPath();
            if (entryPath.isPrefixOf(folderPath)) {
                int segCount = entryPath.matchingFirstSegments(folderPath);
                return folderPath.removeFirstSegments(segCount);
            }/*from  w  w w .  java2 s  .  co  m*/
        }
    }
    return null;
}

From source file:org.eclipse.imp.java.hosted.BuildPathUtils.java

License:Open Source License

/**
 * /*w  ww .  j  a v  a 2  s . c om*/
 * @param filePath
 * @param project
 * @return a String representing the filePath relative to the source directory that contains it, and without a file extension.
 * @throws JavaModelException 
 */
public static String getBareName(IPath filePath, IJavaProject project) throws JavaModelException {
    for (final IClasspathEntry cpEntry : project.getRawClasspath()) {
        if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE && cpEntry.getPath().isPrefixOf(filePath)
                && !BuildPathUtils.isExcluded(filePath, cpEntry)) {
            return filePath.makeRelativeTo(cpEntry.getPath()).removeFileExtension().toOSString();
        }
    }
    return null;
}

From source file:org.eclipse.imp.java.hosted.BuildPathUtils.java

License:Open Source License

/**
 * @param filePath/*from w  ww .j  a  v  a  2s.  c o m*/
 * @param project
 * @return true if the given file is excluded from the source path of the given project
 */
public static boolean isExcluded(IPath filePath, final IJavaProject project) {
    try {
        // --- Determine the classpath source entry that corresponds to file.
        if (project == null) {
            return true; // --- If there is no java project associated with this file, then the file must be
                         // excluded.
        }
        // --- If filePath is not relative to the workspace, make it so.
        IPath workspace = project.getProject().getLocation().removeLastSegments(1);
        if (workspace.isPrefixOf(filePath)) {
            filePath = filePath.makeRelativeTo(workspace);
        }
        for (final IClasspathEntry cpEntry : project.getRawClasspath()) {
            if (cpEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE && cpEntry.getPath().isPrefixOf(filePath)
                    && !isExcluded(filePath, cpEntry)) {
                return false;
            }
        }
    } catch (JavaModelException e) {
        return true;
    }
    return true;
}

From source file:org.eclipse.imp.java.hosted.ProjectUtils.java

License:Open Source License

public void addExtenderForJavaHostedProjects(Language lang) {
    ModelFactory.getInstance().installExtender(new IFactoryExtender() {
        public void extend(ISourceProject project) {
            initializeBuildPathFromJavaProject(project);
        }/*from  w  w w .  j  a v a 2s  .  c om*/

        public void extend(ICompilationUnit unit) {
        }

        /**
         * Read the IJavaProject classpath configuration and populate the ISourceProject's
         * build path accordingly.
         */
        public void initializeBuildPathFromJavaProject(ISourceProject project) {
            IJavaProject javaProject = JavaCore.create(project.getRawProject());
            if (javaProject.exists()) {
                try {
                    IClasspathEntry[] cpEntries = javaProject.getResolvedClasspath(true);
                    List<IPathEntry> buildPath = new ArrayList<IPathEntry>(cpEntries.length);
                    for (int i = 0; i < cpEntries.length; i++) {
                        IClasspathEntry entry = cpEntries[i];
                        IPathEntry.PathEntryType type;
                        IPath path = entry.getPath();

                        switch (entry.getEntryKind()) {
                        case IClasspathEntry.CPE_CONTAINER:
                            type = PathEntryType.CONTAINER;
                            break;
                        case IClasspathEntry.CPE_LIBRARY:
                            type = PathEntryType.ARCHIVE;
                            break;
                        case IClasspathEntry.CPE_PROJECT:
                            type = PathEntryType.PROJECT;
                            break;
                        case IClasspathEntry.CPE_SOURCE:
                            type = PathEntryType.SOURCE_FOLDER;
                            break;
                        default:
                            // case IClasspathEntry.CPE_VARIABLE:
                            throw new IllegalArgumentException("Encountered variable class-path entry: "
                                    + entry.getPath().toPortableString());
                        }
                        IPathEntry pathEntry = ModelFactory.createPathEntry(type, path);
                        buildPath.add(pathEntry);
                    }
                    project.setBuildPath(buildPath);
                } catch (JavaModelException e) {
                    ErrorHandler.reportError(e.getMessage(), e);
                }
            }
        }
    }, lang);
}

From source file:org.eclipse.imp.prefspecs.compiler.PrefspecsCompiler.java

License:Open Source License

private List<IClasspathEntry> getSourceCPEntries(IJavaProject javaProj) {
    List<IClasspathEntry> result = new ArrayList<IClasspathEntry>();
    try {//from  www.  ja va2s  .  c  o m
        IClasspathEntry[] cpEntries = javaProj.getResolvedClasspath(true);
        for (int i = 0; i < cpEntries.length; i++) {
            if (cpEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                result.add(cpEntries[i]);
            }
        }
    } catch (JavaModelException e) {

    }
    return result;
}