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.xtext.builder.impl.SimpleProjectsIntegrationTest.java

License:Open Source License

@Test
public void testReexportedSource() throws Exception {
    IJavaProject foo = createJavaProject("foo");
    IJavaProject bar = createJavaProject("bar");
    IJavaProject baz = createJavaProject("baz");
    addNature(foo.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(bar.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(baz.getProject(), XtextProjectHelper.NATURE_ID);
    IFile file = foo.getProject().getFile("foo.jar");
    file.create(jarInputStream(new TextFile("foo/Foo" + F_EXT, "object Foo")), true, monitor());
    IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(file.getFullPath(), null, null, true);
    addToClasspath(foo, newLibraryEntry);
    addToClasspath(bar, JavaCore.newProjectEntry(foo.getPath(), true));
    addToClasspath(baz, JavaCore.newProjectEntry(bar.getPath(), false));
    addSourceFolder(baz, "src");
    IFile bazFile = createFile("baz/src/Baz" + F_EXT, "object Baz references Foo");
    waitForBuild();/*  w ww.j  a  v a2 s.com*/
    assertEquals(0, countMarkers(bazFile));
    assertEquals(2, countResourcesInIndex());
    Iterator<IReferenceDescription> references = getContainedReferences(
            URI.createPlatformResourceURI(bazFile.getFullPath().toString(), true)).iterator();
    IReferenceDescription next = references.next();
    assertFalse(references.hasNext());
    assertEquals("platform:/resource/baz/src/Baz.buildertestlanguage#/", next.getSourceEObjectUri().toString());
    assertEquals("archive:platform:/resource/foo/foo.jar!/foo/Foo.buildertestlanguage#/",
            next.getTargetEObjectUri().toString());
    assertEquals(-1, next.getIndexInList());
    assertEquals(BuilderTestLanguagePackage.Literals.ELEMENT__REFERENCES, next.getEReference());
}

From source file:org.eclipse.xtext.builder.impl.SimpleProjectsIntegrationTest.java

License:Open Source License

@Test
public void testNewlyAddedReexportedSource() throws Exception {
    IJavaProject foo = createJavaProject("foo");
    IJavaProject bar = createJavaProject("bar");
    IJavaProject baz = createJavaProject("baz");
    addNature(foo.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(bar.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(baz.getProject(), XtextProjectHelper.NATURE_ID);
    addToClasspath(bar, JavaCore.newProjectEntry(foo.getPath(), true));
    addToClasspath(baz, JavaCore.newProjectEntry(bar.getPath(), false));
    addSourceFolder(baz, "src");
    IFile bazFile = createFile("baz/src/Baz" + F_EXT, "object Baz references Foo");
    waitForBuild();//from  w ww.  jav  a2 s .c  o  m
    assertEquals(1, countMarkers(bazFile));
    IFile file = foo.getProject().getFile("foo.jar");
    file.create(jarInputStream(new TextFile("foo/Foo" + F_EXT, "object Foo")), true, monitor());
    IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(file.getFullPath(), null, null, true);
    addToClasspath(foo, newLibraryEntry);
    waitForBuild();
    assertEquals(0, countMarkers(bazFile));
}

From source file:org.eclipse.xtext.builder.impl.SimpleProjectsIntegrationTest.java

License:Open Source License

@Test
public void testReexportedJarRemoved() throws Exception {
    IJavaProject foo = createJavaProject("foo");
    IJavaProject bar = createJavaProject("bar");
    IJavaProject baz = createJavaProject("baz");
    addNature(foo.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(bar.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(baz.getProject(), XtextProjectHelper.NATURE_ID);
    IFile file = foo.getProject().getFile("foo.jar");
    file.create(jarInputStream(new TextFile("foo/Foo" + F_EXT, "object Foo")), true, monitor());
    IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(file.getFullPath(), null, null, true);
    addToClasspath(foo, newLibraryEntry);
    addToClasspath(bar, JavaCore.newProjectEntry(foo.getPath(), true));
    addToClasspath(baz, JavaCore.newProjectEntry(bar.getPath(), false));
    addSourceFolder(baz, "src");
    IFile bazFile = createFile("baz/src/Baz" + F_EXT, "object Baz references Foo");
    waitForBuild();//from w  w  w. j a v  a 2  s.  c  o  m
    assertEquals(0, countMarkers(bazFile));
    deleteClasspathEntry(foo, newLibraryEntry.getPath());
    waitForBuild();
    assertEquals(1, countMarkers(bazFile));
    assertEquals(1, countResourcesInIndex());
    Iterator<IReferenceDescription> references = getContainedReferences(
            URI.createPlatformResourceURI(bazFile.getFullPath().toString(), true)).iterator();
    assertFalse(references.hasNext());
}

From source file:org.eclipse.xtext.builder.impl.SimpleProjectsIntegrationTest.java

License:Open Source License

@Test
public void testJarOnTwoProjectsRemovedFromOne() throws Exception {
    IJavaProject foo = createJavaProject("foo");
    IJavaProject bar = createJavaProject("bar");
    IJavaProject baz = createJavaProject("baz");
    addNature(foo.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(bar.getProject(), XtextProjectHelper.NATURE_ID);
    addNature(baz.getProject(), XtextProjectHelper.NATURE_ID);
    IFile file = foo.getProject().getFile("foo.jar");
    file.create(jarInputStream(new TextFile("foo/Foo" + F_EXT, "object Foo")), true, monitor());
    IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(file.getFullPath(), null, null, true);
    addToClasspath(foo, newLibraryEntry);
    addToClasspath(bar, JavaCore.newProjectEntry(foo.getPath(), true));
    addToClasspath(bar, JavaCore.newLibraryEntry(file.getFullPath(), null, null, true));
    addToClasspath(baz, JavaCore.newProjectEntry(bar.getPath(), false));
    addSourceFolder(baz, "src");
    IFile bazFile = createFile("baz/src/Baz" + F_EXT, "object Baz references Foo");
    waitForBuild();//from w w  w  . j  a va 2 s.c om
    assertEquals(0, countMarkers(bazFile));
    assertEquals(2, countResourcesInIndex());
    deleteClasspathEntry(foo, newLibraryEntry.getPath());
    waitForBuild();
    assertEquals(0, countMarkers(bazFile));
    assertEquals(2, countResourcesInIndex());
}

From source file:org.eclipse.xtext.builder.JDTAwareEclipseResourceFileSystemAccess2.java

License:Open Source License

/**
 * @since 2.6//from  w w w . j  a  va2s.c o m
 */
protected void addToSourceFolders(IContainer container) throws JavaModelException {
    IJavaProject jp = JavaCore.create(container.getProject());
    if (jp.exists() && !jp.isOnClasspath(container)) {
        IPackageFragmentRoot currentSource = jp
                .findPackageFragmentRoot(jp.getPath().append(getCurrentSource()));
        if (currentSource != null) {
            IClasspathEntry currentClasspathEntry = currentSource.getRawClasspathEntry();
            if (currentClasspathEntry != null) {
                insertClasspathEntry(container, currentClasspathEntry, jp);
                return;
            }
        }
        addClasspathEntry(container, jp);
    }
}

From source file:org.eclipse.xtext.junit4.ui.util.JavaProjectSetupUtil.java

License:Open Source License

public static void addProjectReference(IJavaProject from, IJavaProject to) throws CoreException {
    addToClasspath(from, JavaCore.newProjectEntry(to.getPath()));
}

From source file:org.eclipse.xtext.junit4.ui.util.JavaProjectSetupUtil.java

License:Open Source License

public static void removeProjectReference(IJavaProject from, IJavaProject to) throws CoreException {
    removeFromClasspath(from, IClasspathEntry.CPE_PROJECT, to.getPath());
}

From source file:org.eclipse.xtext.xbase.ui.validation.ProjectAwareUniqueClassNameValidator.java

License:Open Source License

public boolean doCheckUniqueInProject(final QualifiedName name, final JvmDeclaredType type)
        throws JavaModelException {
    final IJavaProject javaProject = this.javaProjectProvider.getJavaProject(type.eResource().getResourceSet());
    this.getContext().put(ProjectAwareUniqueClassNameValidator.OUTPUT_CONFIGS,
            this.outputConfigurationProvider.getOutputConfigurations(type.eResource()));
    final String packageName = type.getPackageName();
    final String typeName = type.getSimpleName();
    final Function1<IPackageFragmentRoot, Boolean> _function = (IPackageFragmentRoot it) -> {
        try {//from w ww.  j av  a  2  s  .com
            int _kind = it.getKind();
            return Boolean.valueOf((_kind == IPackageFragmentRoot.K_SOURCE));
        } catch (Throwable _e) {
            throw Exceptions.sneakyThrow(_e);
        }
    };
    final Iterable<IPackageFragmentRoot> sourceFolders = IterableExtensions.<IPackageFragmentRoot>filter(
            ((Iterable<IPackageFragmentRoot>) Conversions.doWrapArray(javaProject.getPackageFragmentRoots())),
            _function);
    IndexManager indexManager = JavaModelManager.getIndexManager();
    if (((((Object[]) Conversions.unwrapArray(sourceFolders, Object.class)).length == 0)
            || (indexManager.awaitingJobsCount() > 0))) {
        String _elvis = null;
        if (packageName != null) {
            _elvis = packageName;
        } else {
            _elvis = "";
        }
        ProjectAwareUniqueClassNameValidator.SourceTraversal _doCheckUniqueInProjectSource = this
                .doCheckUniqueInProjectSource(_elvis, typeName, type, ((IPackageFragmentRoot[]) Conversions
                        .unwrapArray(sourceFolders, IPackageFragmentRoot.class)));
        if (_doCheckUniqueInProjectSource != null) {
            switch (_doCheckUniqueInProjectSource) {
            case DUPLICATE:
                return false;
            case UNIQUE:
                return true;
            default:
                break;
            }
        } else {
        }
    }
    final HashSet<String> workingCopyPaths = CollectionLiterals.<String>newHashSet();
    ICompilationUnit[] copies = this.getWorkingCopies(type);
    if ((copies != null)) {
        for (final ICompilationUnit workingCopy : copies) {
            {
                final IPath path = workingCopy.getPath();
                if ((javaProject.getPath().isPrefixOf(path) && (!this.isDerived(workingCopy.getResource())))) {
                    boolean _exists = workingCopy.getPackageDeclaration(packageName).exists();
                    if (_exists) {
                        IType result = workingCopy.getType(typeName);
                        boolean _exists_1 = result.exists();
                        if (_exists_1) {
                            this.addIssue(type, workingCopy.getElementName());
                            return false;
                        }
                    }
                    workingCopyPaths.add(workingCopy.getPath().toString());
                }
            }
        }
    }
    char[] _xifexpression = null;
    if ((packageName == null)) {
        _xifexpression = CharOperation.NO_CHAR;
    } else {
        _xifexpression = packageName.toCharArray();
    }
    char[] _charArray = typeName.toCharArray();
    final TypeDeclarationPattern pattern = new TypeDeclarationPattern(_xifexpression,
            CharOperation.NO_CHAR_CHAR, _charArray, IIndexConstants.TYPE_SUFFIX,
            (SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE));
    final IndexQueryRequestor _function_1 = new IndexQueryRequestor() {
        @Override
        public boolean acceptIndexMatch(final String documentPath, final SearchPattern indexRecord,
                final SearchParticipant participant, final AccessRuleSet access) {
            boolean _contains = workingCopyPaths.contains(documentPath);
            if (_contains) {
                return true;
            }
            IWorkspaceRoot _root = ResourcesPlugin.getWorkspace().getRoot();
            Path _path = new Path(documentPath);
            IFile file = _root.getFile(_path);
            boolean _isDerived = ProjectAwareUniqueClassNameValidator.this.isDerived(file);
            boolean _not = (!_isDerived);
            if (_not) {
                ProjectAwareUniqueClassNameValidator.this.addIssue(type, file.getName());
                return false;
            }
            return true;
        }
    };
    IndexQueryRequestor searchRequestor = _function_1;
    try {
        SearchParticipant _defaultSearchParticipant = BasicSearchEngine.getDefaultSearchParticipant();
        IJavaSearchScope _createJavaSearchScope = BasicSearchEngine.createJavaSearchScope(
                ((IJavaElement[]) Conversions.unwrapArray(sourceFolders, IJavaElement.class)));
        PatternSearchJob _patternSearchJob = new PatternSearchJob(pattern, _defaultSearchParticipant,
                _createJavaSearchScope, searchRequestor);
        indexManager.performConcurrentJob(_patternSearchJob, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
                null);
        return true;
    } catch (final Throwable _t) {
        if (_t instanceof OperationCanceledException) {
            final OperationCanceledException oce = (OperationCanceledException) _t;
            return false;
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}

From source file:org.eclipse.zest.dot.internal.ZestProjectWizard.java

License:Open Source License

private void updatePage() {
    IJavaProject jproject = JavaCore.create(mainPage.getProjectHandle());
    if (!jproject.equals(javaPage.getJavaProject())) {
        IClasspathEntry[] buildPath = { JavaCore.newSourceEntry(jproject.getPath().append("src")),
                JavaRuntime.getDefaultJREContainerEntry() };
        IPath outputLocation = jproject.getPath().append("out");
        javaPage.init(jproject, outputLocation, buildPath, false);
    }/* w  ww.ja v a  2  s.  c  o  m*/
}

From source file:org.fusesource.ide.branding.wizards.NewCamelTestWizard.java

License:Open Source License

private IRunnableWithProgress addCamelTestToPomDeps(final IJavaProject project,
        final IRunnableWithProgress runnable) throws Exception {
    return new IRunnableWithProgress() {
        @Override// www.  j av  a 2 s  .com
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            if (monitor == null) {
                monitor = new NullProgressMonitor();
            }
            monitor.beginTask(WizardMessages.NewTestCaseCreationWizard_create_progress, 4);

            // first load the pom file into some model
            IPath pomPathValue = project.getProject().getRawLocation() != null
                    ? project.getProject().getRawLocation().append("pom.xml")
                    : ResourcesPlugin.getWorkspace().getRoot().getLocation()
                            .append(project.getPath().append("pom.xml"));
            String pomPath = pomPathValue.toOSString();
            final File pomFile = new File(pomPath);
            try {
                final Model model = MavenPlugin.getMaven().readModel(pomFile);

                // then check if camel-test is already a dep
                boolean isBlueprint = isBlueprintFile(fPage1.getXmlFileUnderTest().getLocationURI().toString());
                boolean hasCamelSpringTestDep = false;
                boolean hasCamelBPTestDep = false;
                List<Dependency> deps = model.getDependencies();
                for (Dependency dep : deps) {
                    if (dep.getArtifactId().startsWith(CAMEL_ARTIFACT_ID_WILDCARD)
                            && dep.getGroupId().equalsIgnoreCase(CAMEL_GROUP_ID) && camelVersion == null) {
                        camelVersion = dep.getVersion();
                    }
                    if (!isBlueprint && dep.getArtifactId().equalsIgnoreCase(CAMEL_SPRING_TEST_ARTIFACT_ID)) {
                        hasCamelSpringTestDep = true;
                        break;
                    }
                    if (isBlueprint && dep.getArtifactId().equalsIgnoreCase(CAMEL_BP_TEST_ARTIFACT_ID)) {
                        hasCamelBPTestDep = true;
                        break;
                    }
                }

                boolean changes = false;

                if (!isBlueprint && !hasCamelSpringTestDep) {
                    Dependency dep = new Dependency();
                    dep.setGroupId(CAMEL_GROUP_ID);
                    dep.setArtifactId(CAMEL_SPRING_TEST_ARTIFACT_ID);
                    dep.setVersion(camelVersion);
                    dep.setScope(CAMEL_TEST_SCOPE);
                    model.addDependency(dep);
                    changes = true;
                }

                if (isBlueprint && !hasCamelBPTestDep) {
                    Dependency dep = new Dependency();
                    dep.setGroupId(CAMEL_GROUP_ID);
                    dep.setArtifactId(CAMEL_BP_TEST_ARTIFACT_ID);
                    dep.setVersion(camelVersion);
                    dep.setScope(CAMEL_TEST_SCOPE);
                    model.addDependency(dep);
                    changes = true;
                }

                if (changes) {
                    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(pomFile))) {
                        MavenPlugin.getMaven().writeModel(model, os);
                        IFile pomIFile = project.getProject().getFile("pom.xml");
                        if (pomIFile != null) {
                            pomIFile.refreshLocal(IResource.DEPTH_INFINITE, monitor);
                        }
                        runnable.run(new SubProgressMonitor(monitor, 2));
                    } catch (Exception ex) {
                        Activator.getLogger().error(ex);
                    }
                }
            } catch (Exception ex) {
                Activator.getLogger().error(ex);
            } finally {
                monitor.done();
            }
        }
    };
}