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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:com.mountainminds.eclemma.internal.ui.viewers.ClassesViewer.java

License:Open Source License

/**
 * Calculates a label for the class path of the given package fragment root.
 * For external entries this is the full path, otherwise it is the project
 * relative path.//from w  ww. ja v  a  2s  .  c  o m
 * 
 * @param root  package fragement root
 * @return  label for the class path entry
 */
private static String getPathLabel(IPackageFragmentRoot root) {
    IPath path = root.getPath();
    if (!root.isExternal()) {
        path = path.removeFirstSegments(1);
    }
    return path.toString();
}

From source file:com.mtcflow.project.util.MTCProjectSupport.java

License:Open Source License

/**
 * For this marvelous project we need to: - create the default Eclipse
 * project - add the custom project nature - create the folder structure
 * //from  w w  w  .  j  a  v  a  2  s  .  c  om
 * @param projectName
 * @param location
 * @param natureId
 * @return
 */
public static IProject createProject(String projectName, URI location) {
    Assert.isNotNull(projectName);
    Assert.isTrue(projectName.trim().length() > 0);

    try {
        // create eclipse project
        IProject project = createBaseProject(projectName, location);
        project.setDefaultCharset("UTF-8", null);
        addNature(project);
        // create java project
        IJavaProject javaProject = JavaCore.create(project);
        javaProject.setOption(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
        javaProject.setOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
        javaProject.setOption(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);

        // add bin/ouput folder
        IFolder binFolder = project.getFolder("bin");
        // binFolder.create(false, true, null);
        javaProject.setOutputLocation(binFolder.getFullPath(), null);

        // add libs to project class path
        List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
        IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();

        // create source folder
        IFolder sourceFolder = project.getFolder("src");
        sourceFolder.create(false, true, null);
        IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(sourceFolder);
        IClasspathEntry[] cEntries = new IClasspathEntry[3];
        cEntries[0] = JavaRuntime.getDefaultJREContainerEntry();
        // cEntries[2] = JavaCore.new
        cEntries[1] = JavaCore.newSourceEntry(srcRoot.getPath());
        cEntries[2] = JavaCore.newContainerEntry(new Path("org.eclipse.pde.core.requiredPlugins"));
        javaProject.setRawClasspath(cEntries, null);
        /*
         * Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name:
         * LePlugine Bundle-SymbolicName: LePlugine Bundle-Version:
         * 1.0.0.qualifier Bundle-RequiredExecutionEnvironment: JavaSE-1.7
         */
        String[] paths = { "transformations/T2M", //$NON-NLS-1$
                "transformations/M2M", //$NON-NLS-1$
                "transformations/M2T", //$NON-NLS-1$
                "transformations/HOT", //$NON-NLS-1$
                "mtcs", //$NON-NLS-1$ 
                "metamodels", //$NON-NLS-1$
                "libraries", //$NON-NLS-1$
                "scripts", //$NON-NLS-1$
                "validations", //$NON-NLS-1$
                "tests", //$NON-NLS-1$
                "models", "META-INF" }; //$NON-NLS-1$
        addToProjectStructure(javaProject.getProject(), paths);
        createTemplateFileInProjectAt(javaProject.getProject(), "build.properties", "build.properties");
        createTemplateFileInProjectAt(javaProject.getProject(), "default.mtc", "/mtcs/default.mtc");
        createTemplateFileInProjectAt(javaProject.getProject(), "default_diagram.mtcd", "/mtcs/default.mtcd");
        Manifest manifest = new Manifest();
        manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
        manifest.getMainAttributes().putValue("Bundle-ManifestVersion", "2");
        manifest.getMainAttributes().putValue("Bundle-Name", projectName);
        manifest.getMainAttributes().putValue("Bundle-SymbolicName", projectName);
        manifest.getMainAttributes().putValue("Bundle-Version", "1.0.0");
        manifest.getMainAttributes().putValue("Require-Bundle",
                "com.mtcflow.model,com.mtcflow.engine,com.mtcflow.engine.core");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        manifest.write(out);
        javaProject.getProject().getFile("/META-INF/MANIFEST.MF")
                .create(new ByteArrayInputStream(out.toByteArray()), true, null);
        return javaProject.getProject();
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException("Error creating the project", ex);
    }
}

From source file:com.mtcflow.project.util.MTCProjectSupport.java

License:Open Source License

private IJavaProject createProject(String projName) throws Exception {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    if (projName == null || projName.trim().length() == 0)
        return null;

    // create eclipse project
    IProject project = root.getProject(projName);
    if (project.exists())
        project.delete(true, null);/*w  w  w  . j ava  2 s  .  com*/

    project.create(null);
    project.open(null);

    addNature(project);

    // create java project
    IJavaProject javaProject = JavaCore.create(project);

    // add bin/ouput folder
    IFolder binFolder = project.getFolder("bin");
    binFolder.create(false, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    // add libs to project class path
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }

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

    // create source folder
    IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    IPackageFragmentRoot srcRoot = javaProject.getPackageFragmentRoot(sourceFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(srcRoot.getPath());
    javaProject.setRawClasspath(newEntries, null);

    return javaProject;
}

From source file:com.nginious.http.plugin.NewProjectWizard.java

License:Apache License

public boolean performFinish() {
    String name = pageOne.getProjectName();
    IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(name);

    if (newProject.exists()) {
        throw new RuntimeException("Project exists!");
    }/*from   w  w w .  jav  a2 s .  c  o m*/

    try {
        int listenPort = pageOne.getListenPort();

        if (!pageOne.validate(listenPort)) {
            return false;
        }

        IProgressMonitor progressMonitor = new NullProgressMonitor();

        // Create project
        newProject.create(progressMonitor);
        newProject.open(progressMonitor);

        // Create folder structure
        String[] paths = { "src", "WebContent", "WebContent/WEB-INF", "WebContent/WEB-INF/classes",
                "WebContent/WEB-INF/lib", "WebContent/WEB-INF/xsp" };
        addToProjectStructure(newProject, paths);

        ClassPathBuilder builder = new ClassPathBuilder(newProject, progressMonitor);
        builder.build(progressMonitor);

        // Set project nature
        IProjectDescription description = newProject.getDescription();
        description.setNatureIds(new String[] { JavaCore.NATURE_ID, NginiousPlugin.NATURE_ID });
        newProject.setDescription(description, null);

        // Create java project
        IJavaProject javaProject = JavaCore.create(newProject);

        // Set classes output folder
        IFolder classesFolder = newProject.getFolder("WebContent/WEB-INF/classes");
        javaProject.setOutputLocation(classesFolder.getFullPath(), null);

        // Set classpath
        IClasspathEntry[] entries = builder.getClassPath();
        javaProject.setRawClasspath(entries, progressMonitor);

        // Set source folder
        IFolder sourceFolder = newProject.getFolder("src");
        IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder);
        IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
        IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
        System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
        newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath());
        javaProject.setRawClasspath(newEntries, null);
        BasicNewProjectResourceWizard.updatePerspective(this.configurationElement);

        newProject.setPersistentProperty(NginiousPlugin.LISTEN_PORT_PROP_KEY, Integer.toString(listenPort));
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_URL_PROP_KEY, pageOne.getPublishUrl());
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_USERNAME_PROP_KEY,
                pageOne.getPublishUsername());
        newProject.setPersistentProperty(NginiousPlugin.PUBLISH_PASSWORD_PROP_KEY,
                pageOne.getPublishPassword());
        newProject.setPersistentProperty(NginiousPlugin.MIN_MEMORY_PROP_KEY,
                Integer.toString(pageOne.getMinMemory()));
        newProject.setPersistentProperty(NginiousPlugin.MAX_MEMORY_PROP_KEY,
                Integer.toString(pageOne.getMaxMemory()));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    return true;
}

From source file:com.redhat.ceylon.eclipse.code.correct.CeylonCorrectionProcessor.java

License:Open Source License

public CeylonCorrectionProcessor(IMarker marker) {
    IFileEditorInput input = MarkerUtils.getInput(marker);
    if (input != null) {
        file = input.getFile();//  ww  w .  j a v  a  2s .  co m
        IProject project = file.getProject();
        IJavaProject javaProject = JavaCore.create(project);
        TypeChecker tc = getProjectTypeChecker(project);
        if (tc != null) {
            try {
                for (IPackageFragmentRoot pfr : javaProject.getPackageFragmentRoots()) {
                    if (pfr.getPath().isPrefixOf(file.getFullPath())) {
                        IPath relPath = file.getFullPath().makeRelativeTo(pfr.getPath());
                        model = tc.getPhasedUnitFromRelativePath(relPath.toString()).getCompilationUnit();
                    }
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
    }
    setQuickAssistProcessor(this);
}

From source file:com.redhat.ceylon.eclipse.code.quickfix.CeylonQuickFixController.java

License:Open Source License

public CeylonQuickFixController(IMarker marker) {
    assistant = new CeylonQuickFixAssistant();
    IFileEditorInput input = MarkerUtils.getInput(marker);
    if (input != null) {
        file = input.getFile();//  w w  w . ja va 2 s .co m
        IProject project = file.getProject();
        IJavaProject javaProject = JavaCore.create(project);
        TypeChecker tc = getProjectTypeChecker(project);
        if (tc != null) {
            try {
                for (IPackageFragmentRoot pfr : javaProject.getPackageFragmentRoots()) {
                    if (pfr.getPath().isPrefixOf(file.getFullPath())) {
                        IPath relPath = file.getFullPath().makeRelativeTo(pfr.getPath());
                        model = tc.getPhasedUnitFromRelativePath(relPath.toString()).getCompilationUnit();
                    }
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
    }
    setQuickAssistProcessor(this);
}

From source file:com.redhat.ceylon.eclipse.core.model.JDTModuleManager.java

License:Open Source License

@Override
protected JDTModule createModule(List<String> moduleName, String version) {
    JDTModule module = null;//from www  .  j  a  v  a  2  s.c om
    String moduleNameString = Util.getName(moduleName);
    List<IPackageFragmentRoot> roots = new ArrayList<IPackageFragmentRoot>();
    if (javaProject != null) {
        try {
            if (moduleNameString.equals(Module.DEFAULT_MODULE_NAME)) {
                // Add the list of source package fragment roots
                for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                    if (root.exists() && javaProject.isOnClasspath(root)) {
                        IClasspathEntry entry = root.getResolvedClasspathEntry();
                        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && !root.isExternal()) {
                            roots.add(root);
                        }
                    }
                }
            } else {
                for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                    if (root.exists() && javaProject.isOnClasspath(root)) {
                        if (JDKUtils.isJDKModule(moduleNameString)) {
                            // find the first package that exists in this root
                            for (String pkg : JDKUtils.getJDKPackagesByModule(moduleNameString)) {
                                if (root.getPackageFragment(pkg).exists()) {
                                    roots.add(root);
                                    break;
                                }
                            }
                        } else if (JDKUtils.isOracleJDKModule(moduleNameString)) {
                            // find the first package that exists in this root
                            for (String pkg : JDKUtils.getOracleJDKPackagesByModule(moduleNameString)) {
                                if (root.getPackageFragment(pkg).exists()) {
                                    roots.add(root);
                                    break;
                                }
                            }
                        } else if (!(root instanceof JarPackageFragmentRoot)
                                && !CeylonBuilder.isInCeylonClassesOutputFolder(root.getPath())) {
                            String packageToSearch = moduleNameString;
                            if (root.getPackageFragment(packageToSearch).exists()) {
                                roots.add(root);
                            }
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            e.printStackTrace();
        }
    }

    module = new JDTModule(this, roots);
    module.setName(moduleName);
    module.setVersion(version);
    setupIfJDKModule(module);
    return module;
}

From source file:com.redhat.ceylon.eclipse.core.model.loader.JDTModelLoader.java

License:Open Source License

private ClassMirror buildClassMirror(String name) {
    try {//from  w w  w  .  ja v a 2 s . c o  m
        IType type = javaProject.findType(name);
        if (type == null) {
            return null;
        }

        LookupEnvironment theLookupEnvironment = getLookupEnvironment();
        if (type.isBinary()) {
            ClassFile classFile = (ClassFile) type.getClassFile();

            if (classFile != null) {
                IPackageFragmentRoot fragmentRoot = classFile.getPackageFragmentRoot();
                if (fragmentRoot != null) {
                    if (isInCeylonClassesOutputFolder(fragmentRoot.getPath())) {
                        return null;
                    }
                }

                IFile classFileRsrc = (IFile) classFile.getCorrespondingResource();
                IBinaryType binaryType = classFile.getBinaryTypeInfo(classFileRsrc, true);
                if (classFileRsrc != null && !classFileRsrc.exists()) {
                    //the .class file has been deleted
                    return null;
                }
                BinaryTypeBinding binaryTypeBinding = theLookupEnvironment.cacheBinaryType(binaryType, null);
                if (binaryTypeBinding == null) {
                    char[][] compoundName = CharOperation.splitOn('/', binaryType.getName());
                    ReferenceBinding existingType = theLookupEnvironment.getCachedType(compoundName);
                    if (existingType == null || !(existingType instanceof BinaryTypeBinding)) {
                        return null;
                    }
                    binaryTypeBinding = (BinaryTypeBinding) existingType;
                }
                return new JDTClass(binaryTypeBinding, theLookupEnvironment);
            }
        } else {
            char[][] compoundName = CharOperation.splitOn('.', type.getFullyQualifiedName().toCharArray());
            ReferenceBinding referenceBinding = theLookupEnvironment.getType(compoundName);
            if (referenceBinding != null) {
                if (referenceBinding instanceof ProblemReferenceBinding) {
                    ProblemReferenceBinding problemReferenceBinding = (ProblemReferenceBinding) referenceBinding;
                    if (problemReferenceBinding.problemId() == ProblemReasons.InternalNameProvided) {
                        referenceBinding = problemReferenceBinding.closestReferenceMatch();
                    } else {
                        System.out.println(ProblemReferenceBinding
                                .problemReasonString(problemReferenceBinding.problemId()));
                        return null;
                    }
                }
                return new JDTClass(referenceBinding, theLookupEnvironment);
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.redhat.ceylon.eclipse.ui.test.CeylonEditorTest.java

License:Open Source License

private IProject createJavaProject(String name) throws CoreException, JavaModelException {
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IProject project = root.getProject(name);
    project.create(null);/*  w  w w. j  a v  a2  s . c om*/
    project.open(null);

    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);

    IJavaProject javaProject = JavaCore.create(project);

    IFolder binFolder = project.getFolder("bin");
    binFolder.create(false, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);

    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations) {
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));
    }
    //add libs to project class path
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);

    IPackageFragmentRoot packageroot = javaProject.getPackageFragmentRoot(sourceFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(packageroot.getPath());
    javaProject.setRawClasspath(newEntries, null);
    return project;
}

From source file:com.redhat.ceylon.eclipse.ui.test.headless.ModelAndPhasedUnitsTests.java

License:Open Source License

@Test
public void checkMainProjectJavaCeylonUnits() throws CoreException {
    if (compilationError != null) {
        throw compilationError;
    }/*from   w  ww  .  j  ava  2s.  c  om*/

    IJavaProject javaProject = JavaCore.create(mainProject);

    String rootPath = null;
    ICompilationUnit javaClassElement = null;
    ICompilationUnit javaObjectElement = null;
    ICompilationUnit javaMethodElement = null;
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
        IPackageFragment pkg = root.getPackageFragment("mainModule");
        if (pkg.exists()
                && pkg.getCompilationUnit("JavaCeylonTopLevelClass_Main_Ceylon_Project.java").exists()) {
            javaClassElement = pkg.getCompilationUnit("JavaCeylonTopLevelClass_Main_Ceylon_Project.java");
            javaObjectElement = pkg.getCompilationUnit("javaCeylonTopLevelObject_Main_Ceylon_Project_.java");
            javaMethodElement = pkg.getCompilationUnit("javaCeylonTopLevelMethod_Main_Ceylon_Project_.java");
            rootPath = root.getPath().toOSString();
            break;
        }
    }

    Module module = modelLoader.findModule("mainModule", "1.0.0");
    JavaCompilationUnit javaClassCompilationUnit = checkDeclarationUnit(module,
            "mainModule.JavaCeylonTopLevelClass_Main_Ceylon_Project", JavaCompilationUnit.class,
            rootPath + "/" + "mainModule/JavaCeylonTopLevelClass_Main_Ceylon_Project.java",
            "mainModule/JavaCeylonTopLevelClass_Main_Ceylon_Project.java",
            "JavaCeylonTopLevelClass_Main_Ceylon_Project.java");
    Assert.assertEquals("Wrong Java Element for Class : ", javaClassElement,
            javaClassCompilationUnit.getTypeRoot());
    Assert.assertNotNull("Project Resource  for Class should not be null :",
            javaClassCompilationUnit.getProjectResource());
    Assert.assertNotNull("Root Folder Resource  for Class should not be null :",
            javaClassCompilationUnit.getRootFolderResource());
    Assert.assertNotNull("File Resource should  for Class not be null :",
            javaClassCompilationUnit.getFileResource());

    JavaCompilationUnit javaObjectCompilationUnit = checkDeclarationUnit(module,
            "mainModule.javaCeylonTopLevelObject_Main_Ceylon_Project", JavaCompilationUnit.class,
            rootPath + "/" + "mainModule/javaCeylonTopLevelObject_Main_Ceylon_Project_.java",
            "mainModule/javaCeylonTopLevelObject_Main_Ceylon_Project_.java",
            "javaCeylonTopLevelObject_Main_Ceylon_Project_.java");
    Assert.assertEquals("Wrong Java Element for Object : ", javaObjectElement,
            javaObjectCompilationUnit.getTypeRoot());
    Assert.assertNotNull("Project Resource  for Object should not be null :",
            javaObjectCompilationUnit.getProjectResource());
    Assert.assertNotNull("Root Folder Resource  for Object should not be null :",
            javaObjectCompilationUnit.getRootFolderResource());
    Assert.assertNotNull("File Resource should  for Object not be null :",
            javaObjectCompilationUnit.getFileResource());

    JavaCompilationUnit javaMethodCompilationUnit = checkDeclarationUnit(module,
            "mainModule.javaCeylonTopLevelMethod_Main_Ceylon_Project_", JavaCompilationUnit.class,
            rootPath + "/" + "mainModule/javaCeylonTopLevelMethod_Main_Ceylon_Project_.java",
            "mainModule/javaCeylonTopLevelMethod_Main_Ceylon_Project_.java",
            "javaCeylonTopLevelMethod_Main_Ceylon_Project_.java");
    Assert.assertEquals("Wrong Java Element for Method : ", javaMethodElement,
            javaMethodCompilationUnit.getTypeRoot());
    Assert.assertNotNull("Project Resource  for Method should not be null :",
            javaMethodCompilationUnit.getProjectResource());
    Assert.assertNotNull("Root Folder Resource  for Method should not be null :",
            javaMethodCompilationUnit.getRootFolderResource());
    Assert.assertNotNull("File Resource should  for Method not be null :",
            javaMethodCompilationUnit.getFileResource());
}