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.key_project.util.test.testcase.JDTUtilTest.java

License:Open Source License

/**
 * Tests {@link JDTUtil#getSourceResources(IProject)}
 *///from ww  w  .  j a  va 2  s.co  m
@Test
public void testGetSourceResources() throws CoreException, InterruptedException {
    // Test null
    List<IResource> locations = JDTUtil.getSourceResources(null);
    assertNotNull(locations);
    assertEquals(0, locations.size());
    // Test general project
    IProject project = TestUtilsUtil.createProject("JDTUtilTest_testGetSourceResources_general");
    locations = JDTUtil.getSourceResources(project);
    assertNotNull(locations);
    assertEquals(0, locations.size());
    // Test Java project with one source location in project
    IJavaProject javaProject = TestUtilsUtil.createJavaProject("JDTUtilTest_testGetSourceResources_Java");
    project = javaProject.getProject();
    IFolder src = project.getFolder("src");
    assertNotNull(src);
    assertTrue(src.exists());
    locations = JDTUtil.getSourceResources(project);
    assertNotNull(locations);
    assertEquals(1, locations.size());
    assertEquals(src, locations.get(0));
    // Add second source location in project
    IFolder secondSrc = project.getFolder("secondSrc");
    if (!secondSrc.exists()) {
        secondSrc.create(true, true, null);
    }
    JDTUtil.addClasspathEntry(javaProject, JavaCore.newSourceEntry(secondSrc.getFullPath()));
    // Test Java project with two source location in project
    locations = JDTUtil.getSourceResources(project);
    assertNotNull(locations);
    assertEquals(2, locations.size());
    assertEquals(src, locations.get(0));
    assertEquals(secondSrc, locations.get(1));
    // Add class path entry for a different java project
    IJavaProject refJavaProject = TestUtilsUtil
            .createJavaProject("JDTUtilTest_testGetSourceResources_Java_Refereneed");
    IProject refProject = refJavaProject.getProject();
    IFolder refSrc = refProject.getFolder("src");
    assertNotNull(refSrc);
    assertTrue(refSrc.exists());
    JDTUtil.addClasspathEntry(javaProject, JavaCore.newSourceEntry(refJavaProject.getPath()));
    // Test Java project with two source location in project and project reference
    locations = JDTUtil.getSourceResources(project);
    assertNotNull(locations);
    assertEquals(3, locations.size());
    assertEquals(src, locations.get(0));
    assertEquals(secondSrc, locations.get(1));
    assertEquals(refSrc, locations.get(2));
    locations = JDTUtil.getSourceResources(refProject);
    assertNotNull(locations);
    assertEquals(1, locations.size());
    assertEquals(refSrc, locations.get(0));
    // Create cycle by also referencing javaProject in refJavaProject
    JDTUtil.addClasspathEntry(refJavaProject, JavaCore.newSourceEntry(javaProject.getPath()));
    // Test Java project with two source location in project and cyclic project reference
    locations = JDTUtil.getSourceResources(project);
    assertNotNull(locations);
    assertEquals(3, locations.size());
    assertEquals(src, locations.get(0));
    assertEquals(secondSrc, locations.get(1));
    assertEquals(refSrc, locations.get(2));
    locations = JDTUtil.getSourceResources(refProject);
    assertNotNull(locations);
    assertEquals(3, locations.size());
    assertEquals(refSrc, locations.get(0));
    assertEquals(src, locations.get(1));
    assertEquals(secondSrc, locations.get(2));
}

From source file:org.limy.eclipse.qalab.ant.DefaultCreator.java

License:Open Source License

/**
 * pathvf?itest.all.classpath?j???B/*from  w  w w . j a va2  s. c o m*/
 * @param root ?[gvf
 * @param env 
 * @throws FileNotFoundException Kvt@C???
 * @throws CoreException RAO
 */
private void createTestAllClasspath(XmlElement root, LimyQalabEnvironment env)
        throws FileNotFoundException, CoreException {

    IJavaProject project = env.getJavaProject();
    IPreferenceStore store = env.getStore();

    XmlElement pathEl = XmlUtils.createElement(root, "path");
    pathEl.setAttribute("id", "test.all.classpath");

    createPathelement(pathEl, "${dest.dir}/instrumented");

    Collection<IPath> binPaths = env.getBinPaths(true);
    for (IPath path : binPaths) {
        createPathelement(pathEl, LimyQalabUtils.createFullPath(project, path));
    }

    String libDir = store.getString(LimyQalabConstants.TEST_LIBDIR);
    if (libDir.length() > 0) {
        IPath path = project.getPath().append(libDir);
        IFolder folder = (IFolder) LimyResourceUtils.newFolder(path);
        for (IResource resource : folder.members()) {
            if (resource.getType() == IFile.FILE) {
                createPathelement(pathEl, resource.getLocation().toString());
            }
        }
    }
}

From source file:org.objectstyle.wolips.builder.internal.BuildVisitor.java

License:Open Source License

public void reinitForNextBuild(IProject project) {
    super.reinitForNextBuild(project);
    try {//from   w w  w.  j a v a2s.c  o  m
        IJavaProject jp = this.getJavaProject();
        outputPath = jp.getOutputLocation();
        _checkJavaOutputPath = !outputPath.equals(jp.getPath());
    } catch (CoreException up) {
        outputPath = new Path("/dummy");
    }
    count = 0;
    _destinations.clear();
}

From source file:org.openquark.cal.eclipse.embedded.EmbeddedClasspathVariableInitializer.java

License:Open Source License

public static void addQuarkBinariesToClasspath(IProject project) {
    IJavaProject javaProject = JavaCore.create(project);
    try {/*w w  w.ja  va 2s  . c  om*/
        IClasspathEntry[] originalCP = javaProject.getRawClasspath();

        // find the quark binaries project
        IJavaProject quarkBinaries = getQuarkBinaries();

        if (quarkBinaries == null) {
            // could not find quark binaries project
            return;
        }

        // check to see if classpath entry already exists
        for (final IClasspathEntry classpathEntry : originalCP) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                if (classpathEntry.getPath().toString().equals(quarkBinaries.getPath())) {
                    // found
                    return;
                }
            }
        }

        // add it
        IClasspathEntry quarkBinariesEntry = JavaCore.newProjectEntry(quarkBinaries.getPath());
        int originalCPLength = originalCP.length;
        IClasspathEntry[] newCP = new IClasspathEntry[originalCPLength + 1];
        System.arraycopy(originalCP, 0, newCP, 0, originalCPLength);
        newCP[originalCPLength] = quarkBinariesEntry;
        javaProject.setRawClasspath(newCP, new NullProgressMonitor());

    } catch (JavaModelException e) {
    }
}

From source file:org.parallelj.designer.extension.command.OpenJavaFileFromShortCutKey.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {

    // Fetch the currently selected Workbench Part
    IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
    if (part == null || !(part instanceof ParallelJDiagramEditor)) {
        return null;
    }//from w  ww. ja va  2s .co  m
    shellz = part.getSite().getShell();

    // Fetch the currently selected Workbench Window
    this.workbenchWindow = HandlerUtil.getActiveWorkbenchWindowChecked(event);

    // Fetch the currently selected Workbench Selection Part
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    Object selected = ((IStructuredSelection) selection).getFirstElement();
    destinationFile = null;

    // Fetch Editor Input from Editor
    ParallelJDiagramEditor editor = (ParallelJDiagramEditor) part;

    IEditorInput editorInput = ((DiagramDocumentEditor) editor).getEditorInput();
    if (!(editorInput instanceof FileEditorInput)) {
        return null;
    }

    FileEditorInput fileEditorInput = (FileEditorInput) editorInput;
    final IFile file = fileEditorInput.getFile();
    final IJavaProject javaProject = JavaCore.create(file.getProject());

    if (selected instanceof ProgramExtendedEditPart) {
        final String selectedProgram = ((Program) (((ProgramExtendedEditPart) selected).getPrimaryView()
                .getElement())).getName();
        IJavaSearchScope javaSearchScope = SearchEngine
                .createJavaSearchScope(new IJavaElement[] { javaProject });
        SearchEngine searchEngine = new SearchEngine();

        TypeNameRequestor typeNameRequestor = new TypeNameRequestor() {
            @Override
            public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName,
                    char[][] enclosingTypeNames, String path) {
                if (path.contains(javaProject.getPath().toString())) {
                    String typePatternAsString = selectedProgram;
                    String packagePatternAsString = "";
                    if (typePatternAsString.contains(FULLY_QUALIFIED_PATTERN)) {
                        packagePatternAsString = typePatternAsString.substring(0,
                                typePatternAsString.lastIndexOf("."));
                        typePatternAsString = typePatternAsString
                                .substring(typePatternAsString.lastIndexOf(FULLY_QUALIFIED_PATTERN) + 1);
                    }
                    String testPath = path;
                    if (testPath.contains(PATH_SEPARATOR)) {
                        testPath = testPath.substring(testPath.lastIndexOf(PATH_SEPARATOR) + 1);
                    }
                    if (packagePatternAsString.contains(FULLY_QUALIFIED_PATTERN)) {
                        if (path.contains(
                                packagePatternAsString.replace(FULLY_QUALIFIED_PATTERN, PATH_SEPARATOR))) {
                            if (testPath.equalsIgnoreCase(typePatternAsString + CLASS_FILE_EXTENSION)) {
                                String pathChunks[] = path.split(PATH_SEPARATOR, 3);
                                IFile currentfile = file.getProject().getFile(pathChunks[2]);
                                destinationFile = currentfile;
                            }
                        }
                    } else if (testPath.equalsIgnoreCase(typePatternAsString + CLASS_FILE_EXTENSION)) {
                        String pathChunks[] = path.split(PATH_SEPARATOR, 3);
                        IFile currentfile = file.getProject().getFile(pathChunks[2]);
                        destinationFile = currentfile;
                    }
                }
            }
        };

        ResourceOpener.searchAllJavaTypes(javaSearchScope, searchEngine, typeNameRequestor);

        if (destinationFile != null) {
            ResourceOpener.openResource(destinationFile, this.workbenchWindow, this.shellz);
        } else {
            MessageDialog.openInformation(new Shell(), "File Not Created yet !!!",
                    "The corresponding Java class has not been generated yet...");
        }
    }
    return null;

}

From source file:org.robovm.eclipse.internal.NewProjectWizard.java

License:Open Source License

@Override
public boolean performFinish() {
    try {// w w w.j a  va2s  .c  o  m
        page2.performFinish(new NullProgressMonitor());
        IJavaProject javaProject = page2.getJavaProject();
        IProject project = javaProject.getProject();

        // TODO create selection screen for the template type
        String templateName = getTemplateName();
        Templater templater = new Templater(RoboVMPlugin.getConsoleLogger(), templateName);
        File projectRoot = project.getLocation().toFile();
        customizeTemplate(templater);
        templater.buildProject(projectRoot);

        page1.storePreferences(project);
        IClasspathEntry[] oldClasspath = javaProject.getRawClasspath();
        List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>();

        String rootSrc = javaProject.getPath().append("src").toString();

        for (IClasspathEntry entry : oldClasspath) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER
                    && entry.getPath().toString().equals("org.eclipse.jdt.launching.JRE_CONTAINER")) {

                newClasspath.add(JavaCore.newContainerEntry(new Path(RoboVMClasspathContainer.ID)));
            } else if (entry.getPath().toString().startsWith(rootSrc)) {
                // Cannot have nested classpath entries.
                newClasspath.add(JavaCore.newSourceEntry(javaProject.getPath().append("src/main/java")));
            } else {
                newClasspath.add(entry);
            }
        }
        newClasspath = customizeClasspath(newClasspath);
        javaProject.setRawClasspath(newClasspath.toArray(new IClasspathEntry[newClasspath.size()]),
                new NullProgressMonitor());
        RoboVMNature.configureNatures(project, new NullProgressMonitor());

        project.refreshLocal(IResource.DEPTH_INFINITE, null);
    } catch (Exception e) {
        RoboVMPlugin.log(e);
        return false;
    }
    OpenJavaPerspectiveAction action = new OpenJavaPerspectiveAction();
    action.run();

    return true;
}

From source file:org.sonar.ide.eclipse.internal.jdt.JavaProjectConfigurator.java

License:Open Source License

private String getAbsolutePath(IJavaProject javaProject, IPath path) {
    File baseDir = javaProject.getProject().getLocation().toFile();
    String relativePath = path.makeRelativeTo(javaProject.getPath()).toOSString();
    return new File(baseDir, relativePath).getAbsolutePath();
}

From source file:org.sonar.ide.eclipse.jdt.internal.JavaProjectConfigurator.java

License:Open Source License

private String getRelativePath(IJavaProject javaProject, IPath path) {
    return path.makeRelativeTo(javaProject.getPath()).toOSString();
}

From source file:org.sonar.ide.eclipse.jdt.internal.JavaProjectConfiguratorTest.java

License:Open Source License

@Test
public void shouldConfigureSimpleProject() throws JavaModelException, IOException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    File workspaceRoot = root.getLocation().toFile();
    File projectRoot = new File(workspaceRoot, "myProject");
    projectRoot.mkdir();/*from w  ww.  jav  a  2s  .  com*/
    File sourceFolder = new File(projectRoot, "src");
    sourceFolder.mkdir();
    File testFolder = new File(projectRoot, "test");
    testFolder.mkdir();
    File outputFolder = new File(projectRoot, "bin");
    outputFolder.mkdir();

    IJavaProject project = mock(IJavaProject.class);
    Properties sonarProperties = new Properties();

    when(project.getOption(JavaCore.COMPILER_SOURCE, true)).thenReturn("1.6");
    when(project.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true)).thenReturn("1.6");
    when(project.getPath()).thenReturn(new Path(projectRoot.getAbsolutePath()));

    IClasspathEntry[] cpes = new IClasspathEntry[] { createCPE(IClasspathEntry.CPE_SOURCE, sourceFolder),
            createCPE(IClasspathEntry.CPE_SOURCE, testFolder) };

    when(project.getResolvedClasspath(true)).thenReturn(cpes);
    when(project.getOutputLocation()).thenReturn(new Path(outputFolder.getAbsolutePath()));

    configurator.configureJavaProject(project, sonarProperties, true);

    // TODO Find a way to mock a project inside Eclipse

    // assertTrue(sonarProperties.containsKey("sonar.sources"));
    // assertThat(sonarProperties.getProperty("sonar.sources"), is(sourceFolder.getPath()));
    // assertTrue(sonarProperties.containsKey("sonar.tests"));
    // assertThat(sonarProperties.getProperty("sonar.tests"), is(testFolder.getPath()));
    // assertTrue(sonarProperties.containsKey("sonar.binaries"));
    // assertThat(sonarProperties.getProperty("sonar.binaries"), is(outputFolder.getPath()));
}

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;//from  w  w w.  j a  v  a 2 s . c  om
    }
    if (entry.getOutputLocation() != null) {
        processOutputDir(entry.getOutputLocation(), context, topProject);
    }
}