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.ebayopensource.vjet.eclipse.ui.actions.nature.java.JavaProjectAddVjoNaturePolicy.java

License:Open Source License

/**
 * get the dltk build path entries based on java project 
 * //from w ww. ja v  a 2  s .c  o m
 * @param javaProject
 * @return
 */
private IBuildpathEntry[] buildBuildpathEntry(IJavaProject javaProject) {
    try {
        List<IBuildpathEntry> buildpathEntryList = new ArrayList<IBuildpathEntry>();

        //create source dltk build path entries
        IClasspathEntry[] classPathEntries = javaProject.getRawClasspath();
        for (int i = 0; i < classPathEntries.length; i++) {
            if (IClasspathEntry.CPE_SOURCE == classPathEntries[i].getEntryKind()) {
                IBuildpathEntry buildpathEntry = DLTKCore.newSourceEntry(classPathEntries[i].getPath());
                buildpathEntryList.add(buildpathEntry);
            }
        }

        //create default interpreter build path entry
        IBuildpathEntry defaultInterpreterEntry = ScriptRuntime.getDefaultInterpreterContainerEntry();
        buildpathEntryList.add(defaultInterpreterEntry);
        //create default SDK build path entry
        IBuildpathEntry defaultSdkEntry = VjetSdkRuntime.getDefaultSdkContainerEntry();
        buildpathEntryList.add(defaultSdkEntry);
        //create build path entry for vjet jar
        buildpathEntryList.addAll(PiggyBackClassPathUtil.fetchBuildEntryFromJavaProject(javaProject));
        return buildpathEntryList.toArray(new IBuildpathEntry[buildpathEntryList.size()]);
    } catch (Exception e) {
        e.printStackTrace();
        return new IBuildpathEntry[0];
    }
}

From source file:org.eclim.plugin.jdt.command.junit.JUnitCommand.java

License:Open Source License

private void createBatchTest(IJavaProject javaProject, JUnitTask junit, String pattern) throws Exception {
    if (!pattern.endsWith(".java")) {
        pattern += ".java";
    }//from w w w. j ava  2s  . c o  m
    BatchTest batch = junit.createBatchTest();
    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            String path = ProjectUtils.getFilePath(javaProject.getProject(), entry.getPath().toOSString());
            FileSet fileset = new FileSet();
            fileset.setDir(new File(path));
            fileset.setIncludes(pattern);
            batch.addFileSet(fileset);
        }
    }
}

From source file:org.eclim.plugin.jdt.command.launching.JavaCommand.java

License:Open Source License

private String findMainClass(IJavaProject javaProject) throws Exception {
    ArrayList<IJavaElement> srcs = new ArrayList<IJavaElement>();
    for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            for (IPackageFragmentRoot root : javaProject.findPackageFragmentRoots(entry)) {
                srcs.add(root);//from  w  w w. ja  v a2  s  . c om
            }
        }
    }

    final ArrayList<IMethod> methods = new ArrayList<IMethod>();
    int context = IJavaSearchConstants.DECLARATIONS;
    int type = IJavaSearchConstants.METHOD;
    int matchType = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(srcs.toArray(new IJavaElement[srcs.size()]));
    SearchPattern pattern = SearchPattern.createPattern("main(String[])", type, context, matchType);
    SearchRequestor requestor = new SearchRequestor() {
        public void acceptSearchMatch(SearchMatch match) {
            if (match.getAccuracy() != SearchMatch.A_ACCURATE) {
                return;
            }

            try {
                IMethod method = (IMethod) match.getElement();
                String[] params = method.getParameterTypes();
                if (params.length != 1) {
                    return;
                }

                if (!Signature.SIG_VOID.equals(method.getReturnType())) {
                    return;
                }

                int flags = method.getFlags();
                if (!Flags.isPublic(flags) || !Flags.isStatic(flags)) {
                    return;
                }

                methods.add(method);
            } catch (JavaModelException e) {
                // ignore
            }
        }
    };

    SearchEngine engine = new SearchEngine();
    SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
    engine.search(pattern, participants, scope, requestor, null);

    // if we found only 1 result, we can use it.
    if (methods.size() == 1) {
        IMethod method = methods.get(0);
        ICompilationUnit cu = method.getCompilationUnit();
        IPackageDeclaration[] packages = cu.getPackageDeclarations();
        if (packages != null && packages.length > 0) {
            return packages[0].getElementName() + "." + cu.getElementName();
        }
        return cu.getElementName();
    }
    return null;
}

From source file:org.eclim.plugin.jdt.project.JavaProjectManager.java

License:Open Source License

/**
 * Creates a new project.//  w w  w.  jav a 2 s  .  c  o  m
 *
 * @param project The project.
 * @param depends Comma separated project names this project depends on.
 */
protected void create(IProject project, String depends) throws Exception {
    // with scala-ide installed, apparently this needs to be explicitly done
    IProjectDescription desc = project.getDescription();
    if (!desc.hasNature(PluginResources.NATURE)) {
        String[] natures = desc.getNatureIds();
        String[] newNatures = new String[natures.length + 1];
        System.arraycopy(natures, 0, newNatures, 0, natures.length);
        newNatures[natures.length] = PluginResources.NATURE;
        desc.setNatureIds(newNatures);
        project.setDescription(desc, new NullProgressMonitor());
    }

    IJavaProject javaProject = JavaCore.create(project);
    ((JavaProject) javaProject).configure();

    if (!project.getFile(CLASSPATH).exists()) {
        ArrayList<IClasspathEntry> classpath = new ArrayList<IClasspathEntry>();
        boolean source = false;
        boolean container = false;

        ClassPathDetector detector = new ClassPathDetector(project, null);
        for (IClasspathEntry entry : detector.getClasspath()) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                source = true;
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                container = true;
            }
            classpath.add(entry);
        }

        // default source folder
        if (!source) {
            IResource src;
            IPreferenceStore store = PreferenceConstants.getPreferenceStore();
            String name = store.getString(PreferenceConstants.SRCBIN_SRCNAME);
            boolean srcBinFolders = store.getBoolean(PreferenceConstants.SRCBIN_FOLDERS_IN_NEWPROJ);
            if (srcBinFolders && name.length() > 0) {
                src = javaProject.getProject().getFolder(name);
            } else {
                src = javaProject.getProject();
            }

            classpath.add(new CPListElement(javaProject, IClasspathEntry.CPE_SOURCE, src.getFullPath(), src)
                    .getClasspathEntry());

            File srcPath = new File(ProjectUtils.getFilePath(project, src.getFullPath().toString()));
            if (!srcPath.exists()) {
                srcPath.mkdirs();
            }
        }

        // default containers
        if (!container) {
            for (IClasspathEntry entry : PreferenceConstants.getDefaultJRELibrary()) {
                classpath.add(entry);
            }
        }

        // dependencies on other projects
        for (IClasspathEntry entry : createOrUpdateDependencies(javaProject, depends)) {
            classpath.add(entry);
        }

        javaProject.setRawClasspath(classpath.toArray(new IClasspathEntry[classpath.size()]), null);

        // output location
        IPath output = detector.getOutputLocation();
        if (output == null) {
            output = BuildPathsBlock.getDefaultOutputLocation(javaProject);
        }
        javaProject.setOutputLocation(output, null);
    }
    javaProject.makeConsistent(null);
    javaProject.save(null, false);
}

From source file:org.eclim.plugin.jdt.util.ClasspathUtils.java

License:Open Source License

/**
 * Gets an array of paths representing the project's source paths.
 *
 * @param project The java project instance.
 * @return Array of paths.//from   w w  w . ja  v  a2s  .  c  om
 */
public static String[] getSrcPaths(IJavaProject project) throws Exception {
    ArrayList<String> paths = new ArrayList<String>();
    IClasspathEntry[] entries = project.getResolvedClasspath(true);
    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            paths.add(ProjectUtils.getFilePath(project.getProject(), entry.getPath().toOSString()));
        }
    }

    return paths.toArray(new String[paths.size()]);
}

From source file:org.eclim.plugin.jdt.util.ClasspathUtils.java

License:Open Source License

/**
 * Recursively collects classpath entries from the current and dependent
 * projects./*from   w  ww. j  a v a 2  s  . c o  m*/
 */
private static void collect(IJavaProject javaProject, List<String> paths, Set<IJavaProject> visited,
        boolean isFirstProject) throws Exception {
    if (visited.contains(javaProject)) {
        return;
    }
    visited.add(javaProject);

    try {
        IPath out = javaProject.getOutputLocation();
        paths.add(ProjectUtils.getFilePath(javaProject.getProject(), out.addTrailingSeparator().toOSString()));
    } catch (JavaModelException ignore) {
        // ignore... just signals that no output dir was configured.
    }

    IProject project = javaProject.getProject();
    String name = project.getName();

    IClasspathEntry[] entries = null;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException jme) {
        // this may or may not be a problem.
        logger.warn("Unable to retrieve resolved classpath for project: " + name, jme);
        return;
    }

    final List<IJavaProject> nextProjects = new ArrayList<IJavaProject>();
    for (IClasspathEntry entry : entries) {
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_LIBRARY:
        case IClasspathEntry.CPE_CONTAINER:
        case IClasspathEntry.CPE_VARIABLE:
            String path = entry.getPath().toOSString().replace('\\', '/');
            if (path.startsWith("/" + name + "/")) {
                path = ProjectUtils.getFilePath(project, path);
            }
            paths.add(path);
            break;
        case IClasspathEntry.CPE_PROJECT:
            if (isFirstProject || entry.isExported()) {
                javaProject = JavaUtils.getJavaProject(entry.getPath().segment(0));
                if (javaProject != null) {
                    // breadth first, not depth first, to preserve dependency ordering
                    nextProjects.add(javaProject);
                }
            }
            break;
        case IClasspathEntry.CPE_SOURCE:
            IPath out = entry.getOutputLocation();
            if (out != null) {
                paths.add(ProjectUtils.getFilePath(javaProject.getProject(),
                        out.addTrailingSeparator().toOSString()));
            }
            break;
        }
    }
    // depth second
    for (final IJavaProject nextProject : nextProjects) {
        collect(nextProject, paths, visited, false);
    }
}

From source file:org.eclipse.acceleo.common.internal.utils.workspace.AcceleoWorkspaceUtil.java

License:Open Source License

/**
 * This will return the set of output folders name for the given (java) project.
 * <p>//from   www.  j  a v  a  2s  .c  o m
 * For example, if a project has a source folder "src" with its output folder set as "bin" and a source
 * folder "src-gen" with its output folder set as "bin-gen", this will return a LinkedHashSet containing
 * both "bin" and "bin-gen".
 * </p>
 * 
 * @param project
 *            The project we seek the output folders of.
 * @return The set of output folders name for the given (java) project.
 */
private static Set<String> getOutputFolders(IProject project) {
    final Set<String> classpathEntries = new CompactLinkedHashSet<String>();
    final IJavaProject javaProject = JavaCore.create(project);
    try {
        for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                final IPath output = entry.getOutputLocation();
                if (output != null) {
                    classpathEntries.add(output.removeFirstSegments(1).toString());
                }
            }
        }
        /*
         * Add the default output location to the classpath anyway since source folders are not required
         * to have their own
         */
        final IPath output = javaProject.getOutputLocation();
        classpathEntries.add(output.removeFirstSegments(1).toString());
    } catch (JavaModelException e) {
        AcceleoCommonPlugin.log(e, false);
    }
    return classpathEntries;
}

From source file:org.eclipse.acceleo.ide.ui.resources.AcceleoProject.java

License:Open Source License

/**
 * Constructor./*from  ww  w.ja v  a 2s  .  c  o m*/
 * 
 * @param project
 *            is the project
 */
public AcceleoProject(IProject project) {
    if (!registryInitialized) {
        registryInitialized = true;
        registerPackages();
    }
    this.project = project;
    this.sourceFolders = new ArrayList<IPath>();
    final IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] entries;
    try {
        entries = javaProject.getResolvedClasspath(true);
    } catch (JavaModelException e1) {
        entries = new IClasspathEntry[] {};
    }
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            this.sourceFolders.add(entry.getPath());
        }
    }
}

From source file:org.eclipse.acceleo.internal.compatibility.parser.mt.ast.core.ProjectParser.java

License:Open Source License

/**
 * Creates the templates for the existing '.mt' files.
 * //ww w . j  av  a2s . co m
 * @param projects
 *            the projects to browse
 * @param root
 *            the root element of the model to create
 * @return a map to attach an '.mt' file and its model representation
 * @throws CoreException
 *             if an issue occurs, it contains a status object describing the cause of the exception
 */
private static Map<IFile, Template> createTemplates(IProject[] projects, ResourceSet root)
        throws CoreException {
    Map<IFile, Template> files = new HashMap<IFile, Template>();
    Set<IFolder> done = new CompactHashSet<IFolder>();
    for (int i = 0; i < projects.length; i++) {
        IProject project = projects[i];
        final IJavaProject javaProject = JavaCore.create(project);
        final IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
        for (int j = 0; j < entries.length; j++) {
            final IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getPath().segmentCount() > 1) {
                final IFolder inputFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(entry.getPath());
                if (!done.contains(inputFolder) && inputFolder.exists()) {
                    done.add(inputFolder);
                    createTemplates(files, inputFolder.getFullPath(), inputFolder, root);
                }
            }
        }
    }
    return files;
}

From source file:org.eclipse.acceleo.internal.ide.ui.builders.AcceleoBuilder.java

License:Open Source License

/**
 * Computes the classpath for the given java project.
 * /*w w  w  . j ava  2 s.  com*/
 * @param javaProject
 *            The Java project
 * @return The classpath entries
 */
private Set<AcceleoProjectClasspathEntry> computeProjectClassPath(IJavaProject javaProject) {
    Set<AcceleoProjectClasspathEntry> classpathEntries = new LinkedHashSet<AcceleoProjectClasspathEntry>();

    // Compute the classpath of the acceleo project
    IClasspathEntry[] rawClasspath;
    try {
        rawClasspath = javaProject.getRawClasspath();
        for (IClasspathEntry iClasspathEntry : rawClasspath) {
            int entryKind = iClasspathEntry.getEntryKind();
            if (IClasspathEntry.CPE_SOURCE == entryKind) {
                // We have the source folders of the project.
                IPath inputFolderPath = iClasspathEntry.getPath();
                IPath outputFolderPath = iClasspathEntry.getOutputLocation();

                if (outputFolderPath == null) {
                    outputFolderPath = javaProject.getOutputLocation();
                }

                IProject project = ResourcesPlugin.getWorkspace().getRoot()
                        .getProject(inputFolderPath.lastSegment());
                if (!(project != null && project.exists() && project.equals(javaProject.getProject()))) {
                    IContainer inputContainer = ResourcesPlugin.getWorkspace().getRoot()
                            .getFolder(inputFolderPath);
                    IContainer outputContainer = ResourcesPlugin.getWorkspace().getRoot()
                            .getFolder(outputFolderPath);

                    if (inputContainer != null && outputContainer != null) {
                        File inputDirectory = inputContainer.getLocation().toFile();
                        File outputDirectory = outputContainer.getLocation().toFile();
                        AcceleoProjectClasspathEntry entry = new AcceleoProjectClasspathEntry(inputDirectory,
                                outputDirectory);
                        classpathEntries.add(entry);

                        this.outputFolders.add(outputDirectory);
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        AcceleoUIActivator.log(e, true);
    }
    return classpathEntries;
}