Example usage for org.eclipse.jdt.core IJavaProject getPath

List of usage examples for org.eclipse.jdt.core IJavaProject getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getPath.

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.eclipse.che.plugin.testing.testng.server.TestNGRunner.java

License:Open Source License

@Override
protected boolean isTestSuite(String filePath, IJavaProject project) {
    int projectPathLength = project.getPath().toString().length();
    String path = filePath.substring(projectPathLength, filePath.length());
    IFile file = project.getProject().getFile(path);

    if (!file.exists()) {
        return false;
    }/*from ww w  .  ja v  a2 s.c  o  m*/

    SAXParserFactory factory = SAXParserFactory.newInstance();
    TestNGSuiteParser suiteParser = new TestNGSuiteParser();
    try {
        SAXParser parser = factory.newSAXParser();
        parser.parse(file.getContents(), suiteParser);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        LOG.error("It is not possible to parse file " + path, e);
    } catch (CoreException e) {
        LOG.error("It is not possible to read file " + path, e);
    }

    return suiteParser.isSuite();
}

From source file:org.eclipse.gemoc.execution.concurrent.ccsljavaxdsml.ui.builder.ToggleNatureAction.java

License:Open Source License

public void addSourceFolder(IProject project, String folder) {
    try {/*from w w w  .  j av a  2 s .  c o  m*/
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];
        System.arraycopy(entries, 0, newEntries, 0, entries.length);

        IPath srcPath = javaProject.getPath().append(folder);
        IClasspathEntry srcEntry = JavaCore.newSourceEntry(srcPath, null);

        boolean entryfound = false;
        for (IClasspathEntry cpe : entries) {
            if (cpe.equals(srcEntry)) {
                entryfound = true;
            }
        }

        if (!entryfound) {
            newEntries[entries.length] = JavaCore.newSourceEntry(srcEntry.getPath());
            javaProject.setRawClasspath(newEntries, null);
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.eclipse.gmt.mod.infra.common.core.internal.utils.ProjectUtils.java

License:Open Source License

/**
 * @author Gregoire DUPE (Mia-Software) - classpath entries modification
 */// w  w  w.j  a  va  2 s. com
public static void configureAsJavaProject(final IProject project, final IProgressMonitor monitor)
        throws CoreException {
    addNature(project, monitor, JavaCore.NATURE_ID);
    IJavaProject javaProject = JavaCore.create(project);
    // Set output folder
    IPath path = project.getFullPath().append("bin"); //$NON-NLS-1$
    javaProject.setOutputLocation(path, null);
    List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    // Set source folder
    IFolder sourceFolder = project.getFolder("src"); //$NON-NLS-1$
    if (!sourceFolder.exists()) {
        sourceFolder.create(false, true, monitor);
        classpathEntries.add(JavaCore.newSourceEntry(javaProject.getPath().append(new Path("src")))); //$NON-NLS-1$
    }
    Path jrePath = new Path(
            JavaRuntime.JRE_CONTAINER + "/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/" //$NON-NLS-1$
                    + JAVA_VERSION);
    boolean hasJrePath = false;
    IClasspathEntry[] existingClassPath = javaProject.getRawClasspath();
    for (IClasspathEntry classpathEntry : existingClassPath) {
        if (jrePath.equals(classpathEntry.getPath())) {
            hasJrePath = true;
        }
    }
    if (!hasJrePath) {
        // add the jre api to the classpath
        classpathEntries.add(JavaCore.newContainerEntry(jrePath));
        javaProject.setRawClasspath(classpathEntries.toArray(new IClasspathEntry[0]), monitor);
    }
}

From source file:org.eclipse.jdt.internal.core.JavaModelManager.java

License:Open Source License

IClasspathContainer initializeContainer(IJavaProject project, IPath containerPath) throws JavaModelException {

    IProgressMonitor monitor = this.batchContainerInitializationsProgress;
    if (monitor != null && monitor.isCanceled())
        throw new OperationCanceledException();

    IClasspathContainer container = null;
    final ClasspathContainerInitializer initializer = JavaCore
            .getClasspathContainerInitializer(containerPath.segment(0));
    if (initializer != null) {
        if (CP_RESOLVE_VERBOSE)
            verbose_triggering_container_initialization(project, containerPath, initializer);
        if (CP_RESOLVE_VERBOSE_ADVANCED)
            verbose_triggering_container_initialization_invocation_trace();
        PerformanceStats stats = null;//from  w w  w. j ava2  s  . c  om
        if (JavaModelManager.PERF_CONTAINER_INITIALIZER) {
            stats = PerformanceStats.getStats(JavaModelManager.CONTAINER_INITIALIZER_PERF, this);
            stats.startRun(containerPath + " of " + project.getPath()); //$NON-NLS-1$
        }
        containerPut(project, containerPath, CONTAINER_INITIALIZATION_IN_PROGRESS); // avoid initialization cycles
        boolean ok = false;
        try {
            if (monitor != null)
                monitor.subTask(Messages.bind(Messages.javamodel_configuring,
                        initializer.getDescription(containerPath, project)));

            // let OperationCanceledException go through
            // (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=59363)
            initializer.initialize(containerPath, project);

            if (monitor != null)
                monitor.subTask(""); //$NON-NLS-1$

            // retrieve value (if initialization was successful)
            container = containerBeingInitializedGet(project, containerPath);
            if (container == null
                    && containerGet(project, containerPath) == CONTAINER_INITIALIZATION_IN_PROGRESS) {
                // initializer failed to do its job: redirect to the failure container
                container = initializer.getFailureContainer(containerPath, project);
                if (container == null) {
                    if (CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                        verbose_container_null_failure_container(project, containerPath, initializer);
                    return null; // break cycle
                }
                if (CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                    verbose_container_using_failure_container(project, containerPath, initializer);
                containerPut(project, containerPath, container);
            }
            ok = true;
        } catch (CoreException e) {
            if (e instanceof JavaModelException) {
                throw (JavaModelException) e;
            } else {
                throw new JavaModelException(e);
            }
        } catch (RuntimeException e) {
            if (JavaModelManager.CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                e.printStackTrace();
            throw e;
        } catch (Error e) {
            if (JavaModelManager.CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                e.printStackTrace();
            throw e;
        } finally {
            if (JavaModelManager.PERF_CONTAINER_INITIALIZER) {
                stats.endRun();
            }
            if (!ok) {
                // just remove initialization in progress and keep previous session container so as to avoid a full build
                // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=92588
                containerRemoveInitializationInProgress(project, containerPath);
                if (CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                    verbose_container_initialization_failed(project, containerPath, container, initializer);
            }
        }
        if (CP_RESOLVE_VERBOSE_ADVANCED)
            verbose_container_value_after_initialization(project, containerPath, container);
    } else {
        // create a dummy initializer and get the default failure container
        container = (new ClasspathContainerInitializer() {
            public void initialize(IPath path, IJavaProject javaProject) throws CoreException {
                // not used
            }
        }).getFailureContainer(containerPath, project);
        if (CP_RESOLVE_VERBOSE_ADVANCED || CP_RESOLVE_VERBOSE_FAILURE)
            verbose_no_container_initializer_found(project, containerPath);
    }
    return container;
}

From source file:org.eclipse.jst.jsf.common.ui.internal.dialogfield.JavaSearchScope.java

License:Open Source License

public IPath[] enclosingProjectsAndJars() {
    if (_enclosingProjectsAndJars == null) {
        ArrayList list = new ArrayList();
        for (int i = 0; i < _relativeProjects.length; i++) {
            try {
                if (_relativeProjects[i].hasNature(JavaCore.NATURE_ID)) {
                    IJavaProject javaProject = JavaCore.create(_relativeProjects[i]);
                    IClasspathEntry[] classpath = javaProject.getResolvedClasspath(true);
                    for (int j = 0; j < classpath.length; j++) {
                        list.add(classpath[j].getPath());
                    }/*from w  ww  . j a v a  2 s.c om*/
                    list.add(javaProject.getPath());
                }
            } catch (CoreException e)// NOPMD
            {
                // skip the project.
            }
        }
        _enclosingProjectsAndJars = (IPath[]) list.toArray(new IPath[(list.size())]);
    }
    return _enclosingProjectsAndJars;
}

From source file:org.eclipse.m2e.jdt.internal.JavaProjectConversionParticipant.java

License:Open Source License

private void configureBuildSourceDirectories(Model model, IJavaProject javaProject) throws CoreException {
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    Set<String> sources = new LinkedHashSet<String>();
    Set<String> potentialTestSources = new LinkedHashSet<String>();
    Set<String> potentialResourceDirectories = new LinkedHashSet<String>();
    Set<String> potentialTestResourceDirectories = new LinkedHashSet<String>();
    IPath projectPath = javaProject.getPath();

    for (int i = 0; i < entries.length; i++) {
        if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPath path = entries[i].getPath().makeRelativeTo(projectPath);
            if (path.isAbsolute()) {
                //We only support paths relative to the project root, so we skip this one
                continue;
            }//from  w  w w  . j a  v  a  2  s.  com
            String portablePath = path.toPortableString();
            boolean isPotentialTestSource = isPotentialTestSource(path);
            boolean isResource = false;
            if (isPotentialTestSource) {
                if (DEFAULT_TEST_RESOURCES.equals(portablePath)) {
                    isResource = potentialTestResourceDirectories.add(portablePath);
                } else {
                    potentialTestSources.add(portablePath);
                }
            } else {
                if (DEFAULT_RESOURCES.equals(portablePath)) {
                    isResource = potentialResourceDirectories.add(portablePath);
                } else {
                    sources.add(portablePath);
                }
            }

            if (!isResource) {
                //For source folders not already flagged as resource folder, check if 
                // they contain non-java sources, so we can add them as resources too
                boolean hasNonJavaResources = false;
                IFolder folder = javaProject.getProject().getFolder(path);
                if (folder.isAccessible()) {
                    NonJavaResourceVisitor nonJavaResourceVisitor = new NonJavaResourceVisitor();
                    try {
                        folder.accept(nonJavaResourceVisitor);
                    } catch (NonJavaResourceFoundException ex) {
                        //Expected
                        hasNonJavaResources = true;
                    } catch (CoreException ex) {
                        //385666 ResourceException is thrown in Helios
                        if (ex.getCause() instanceof NonJavaResourceFoundException) {
                            hasNonJavaResources = true;
                        } else {
                            log.error("An error occured while analysing {} : {}", folder, ex.getMessage());
                        }
                    }
                }

                if (hasNonJavaResources) {
                    if (isPotentialTestSource) {
                        potentialTestResourceDirectories.add(portablePath);
                    } else {
                        potentialResourceDirectories.add(portablePath);
                    }
                }
            }
        }
    }

    Build build = getOrCreateBuild(model);

    if (!sources.isEmpty()) {
        if (sources.size() > 1) {
            //We don't know how to handle multiple sources, i.e. how to map to a resource or test source directory
            //That should be dealt by setting the build-helper-plugin config (http://mojo.codehaus.org/build-helper-maven-plugin/usage.html)
            log.warn("{} has multiple source entries, this is not supported yet", model.getArtifactId()); //$NON-NLS-1$
        }
        String sourceDirectory = sources.iterator().next();
        if (!DEFAULT_JAVA_SOURCE.equals(sourceDirectory)) {
            build.setSourceDirectory(sourceDirectory);
        }

        for (String resourceDirectory : potentialResourceDirectories) {
            if (!DEFAULT_RESOURCES.equals(resourceDirectory) || potentialResourceDirectories.size() > 1) {
                build.addResource(createResource(resourceDirectory));
            }
        }
    }

    if (!potentialTestSources.isEmpty()) {
        if (potentialTestSources.size() > 1) {
            log.warn("{} has multiple test source entries, this is not supported yet", model.getArtifactId()); //$NON-NLS-1$
        }
        String testSourceDirectory = potentialTestSources.iterator().next();
        if (!DEFAULT_JAVA_TEST_SOURCE.equals(testSourceDirectory)) {
            build.setTestSourceDirectory(testSourceDirectory);
        }
        for (String resourceDirectory : potentialTestResourceDirectories) {
            if (!DEFAULT_TEST_RESOURCES.equals(resourceDirectory)
                    || potentialTestResourceDirectories.size() > 1) {
                build.addTestResource(createResource(resourceDirectory));
            }
        }
    }

    //Ensure we don't attach a new empty build definition to the model
    if (build.getSourceDirectory() != null || build.getTestSourceDirectory() != null
            || !build.getResources().isEmpty() || !build.getTestResources().isEmpty()) {
        model.setBuild(build);
    }
}

From source file:org.eclipse.m2e.jdt.internal.launch.MavenRuntimeClasspathProvider.java

License:Open Source License

public IRuntimeClasspathEntry[] resolveClasspath(IRuntimeClasspathEntry[] entries,
        ILaunchConfiguration configuration) throws CoreException {
    IProgressMonitor monitor = new NullProgressMonitor(); // XXX
    int scope = getArtifactScope(configuration);
    Set<IRuntimeClasspathEntry> all = new LinkedHashSet<IRuntimeClasspathEntry>(entries.length);
    for (IRuntimeClasspathEntry entry : entries) {
        if (entry.getType() == IRuntimeClasspathEntry.CONTAINER
                && MavenClasspathHelpers.isMaven2ClasspathContainer(entry.getPath())) {
            addMavenClasspathEntries(all, entry, configuration, scope, monitor);
        } else if (entry.getType() == IRuntimeClasspathEntry.PROJECT) {
            IJavaProject javaProject = JavaRuntime.getJavaProject(configuration);
            if (javaProject.getPath().equals(entry.getPath())) {
                addProjectEntries(all, entry.getPath(), scope, THIS_PROJECT_CLASSIFIER, configuration, monitor);
            } else {
                addStandardClasspathEntries(all, entry, configuration);
            }/*from   w  w  w .j  av a2s . co m*/
        } else {
            addStandardClasspathEntries(all, entry, configuration);
        }
    }
    return all.toArray(new IRuntimeClasspathEntry[all.size()]);
}

From source file:org.eclipse.objectteams.otdt.test.builder.OTEquinoxBuilderTests.java

License:Open Source License

public void testForcedExports() throws CoreException, IOException {
    IJavaProject trac18b = fileManager.setUpJavaProject("Trac18b");
    env.addProject(trac18b.getProject());
    IJavaProject trac18a = fileManager.setUpJavaProject("Trac18a");
    env.addProject(trac18a.getProject());
    fullBuild();//w  w  w  .  j  av a  2 s .  co  m
    expectingNoProblemsFor(trac18b.getPath());
    expectingOnlySpecificProblemsFor(trac18a.getPath(), new Problem[] {
            getDecapsulationProblem(trac18a, "trac18b.actions.SampleAction", "trac18a/Team18.java", 42, 70), // import
            getDecapsulationProblem(trac18a, "trac18b.actions.SampleAction", "trac18a/Team18.java", 199, 211), // playedBy
    });
    // after success now break it:
    fileManager.replaceWorkspaceFile("Trac18a2/plugin.xml", trac18a, "plugin.xml");
    incrementalBuild();
    expectingNoProblemsFor(trac18b.getPath());
    expectAccessRestriction(trac18a, "src/trac18a/Team18.java", 42, 70);
}

From source file:org.eclipse.objectteams.otdt.test.builder.OTEquinoxBuilderTests.java

License:Open Source License

public void testForcedExportsMissing() throws CoreException, IOException {
    IJavaProject trac18b = fileManager.setUpJavaProject("Trac18b");
    env.addProject(trac18b.getProject());
    IJavaProject trac18a = fileManager.setUpJavaProject("Trac18a2");
    env.addProject(trac18a.getProject());
    fullBuild();//from   www.  java  2s. c om
    expectingNoProblemsFor(trac18b.getPath());
    expectAccessRestriction(trac18a, "src/trac18a/Team18.java", 42, 70);
    // fix the error
    fileManager.replaceWorkspaceFile("Trac18a/plugin.xml", trac18a, "plugin.xml");
    incrementalBuild();
    expectingNoProblemsFor(trac18b.getPath());
    expectingOnlySpecificProblemsFor(trac18a.getPath(),
            new Problem[] {
                    getDecapsulationProblem(trac18a, "trac18b.actions.SampleAction", "trac18a/Team18.java", 42,
                            70),
                    getDecapsulationProblem(trac18a, "trac18b.actions.SampleAction", "trac18a/Team18.java", 163,
                            175), });
}

From source file:org.eclipse.objectteams.otdt.test.builder.OTEquinoxBuilderTests.java

License:Open Source License

public void testForcedExportsGeneratedMethodRefs() throws CoreException, IOException {
    IJavaProject trac18b = fileManager.setUpJavaProject("Trac18b");
    env.addProject(trac18b.getProject());
    IJavaProject trac18a = fileManager.setUpJavaProject("Trac18a3");
    env.addProject(trac18a.getProject());
    fullBuild();//  w  ww  . j  a  v  a2 s.  c  o  m
    expectingNoProblemsFor(trac18b.getPath());
    expectingOnlySpecificProblemsFor(trac18a.getPath(), new Problem[] {
            getDecapsulationProblem(trac18a, "trac18b.actions.SampleAction", "trac18a/Team18.java", 42, 70),
            getDecapsulationProblem(trac18a, "trac18b.actions.SampleAction", "trac18a/Team18.java", 163, 175)
            // base-ctor call no longer flagged
    });
}