Example usage for org.eclipse.jdt.core JavaCore newContainerEntry

List of usage examples for org.eclipse.jdt.core JavaCore newContainerEntry

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaCore newContainerEntry.

Prototype

public static IClasspathEntry newContainerEntry(IPath containerPath, IAccessRule[] accessRules,
        IClasspathAttribute[] extraAttributes, boolean isExported) 

Source Link

Document

Creates and returns a new classpath entry of kind CPE_CONTAINER for the given path.

Usage

From source file:at.bestsolution.efxclipse.tooling.pde.ui.classpath.JavaFXClassPathExtender.java

License:Open Source License

@Override
public List<IClasspathEntry> getInitialEntries(BundleDescription project) {
    for (String e : project.getExecutionEnvironments()) {
        IExecutionEnvironment env = EnvironmentsManager.getDefault().getEnvironment(e);

        if (env == null) {
            continue;
        }// ww w .  j  a  v  a 2 s  .  co  m

        IVMInstall vm = env.getDefaultVM();

        if (vm == null) {
            for (IVMInstall i : env.getCompatibleVMs()) {
                vm = i;
                break;
            }
        }

        if (vm != null) {
            IPath[] paths = BuildPathSupport.getFxJarPath(vm);
            if (paths == null) {
                return Collections.emptyList();
            } else {
                List<IAccessRule> l = new ArrayList<IAccessRule>();
                for (ImportPackageSpecification i : project.getImportPackages()) {
                    if (i.getName().startsWith("javafx")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_ACCESSIBLE));
                    } else if (i.getName().startsWith("com.sun.browser")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("com.sun.deploy")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("com.sun.glass")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("com.sun.javafx")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("com.sun.media")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("com.sun.openpisces")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("com.sun.prism")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("com.sun.scenario")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("com.sun.t2k")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("com.sun.webpane")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    } else if (i.getName().startsWith("netscape.javascript")) {
                        l.add(JavaCore.newAccessRule(new Path(i.getName().replace('.', '/') + "/*"),
                                IAccessRule.K_DISCOURAGED));
                    }
                }

                IClasspathAttribute[] extraAttributes = {
                        JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
                                paths[1] == null || !paths[1].toFile().exists()
                                        ? BuildPathSupport.WEB_JAVADOC_LOCATION
                                        : paths[1].toFile().toURI().toString()) };

                return Collections.singletonList(JavaCore.newContainerEntry(paths[0],
                        l.toArray(new IAccessRule[0]), extraAttributes, false));
            }
        }
    }
    return Collections.emptyList();
}

From source file:at.bestsolution.fxide.jdt.maven.MavenModuleTypeService.java

License:Open Source License

@Override
public Status createModule(IProject project, IResource resource) {
    IProjectDescription description = project.getWorkspace().newProjectDescription(project.getName());
    description.setNatureIds(new String[] { JavaCore.NATURE_ID, "org.eclipse.m2e.core.maven2Nature" });

    ICommand[] commands = new ICommand[2];

    {// w  ww.  j  a  v  a2 s  .c  o m
        ICommand cmd = description.newCommand();
        cmd.setBuilderName(JavaCore.BUILDER_ID);
        commands[0] = cmd;
    }

    {
        ICommand cmd = description.newCommand();
        cmd.setBuilderName("org.eclipse.m2e.core.maven2Builder");
        commands[1] = cmd;
    }

    if (resource != null) {
        // If we get a parent path we create a nested project
        try {
            if (resource.getProject().getNature("org.eclipse.m2e.core.maven2Nature") != null) {
                IFolder folder = resource.getProject().getFolder(project.getName());
                if (folder.exists()) {
                    return Status.status(State.ERROR, -1, "Folder already exists", null);
                }
                folder.create(true, true, null);

                description.setLocation(folder.getLocation());
            }
        } catch (CoreException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return Status.status(State.ERROR, -1, "Could not create parent relation", e1);
        }
    }

    description.setBuildSpec(commands);

    try {
        project.create(description, null);
        project.open(null);

        project.getFolder(new Path("target")).create(true, true, null);
        project.getFolder(new Path("target").append("classes")).create(true, true, null);

        project.getFolder(new Path("target")).setDerived(true, null);
        project.getFolder(new Path("target").append("classes")).setDerived(true, null);

        project.getFolder(new Path("src")).create(true, true, null);

        project.getFolder(new Path("src").append("main")).create(true, true, null);
        project.getFolder(new Path("src").append("main").append("java")).create(true, true, null);
        project.getFolder(new Path("src").append("main").append("resources")).create(true, true, null);

        project.getFolder(new Path("src").append("test")).create(true, true, null);
        project.getFolder(new Path("src").append("test").append("java")).create(true, true, null);
        project.getFolder(new Path("src").append("test").append("resources")).create(true, true, null);

        {
            IFile file = project.getFile(new Path("pom.xml"));
            try (InputStream templateStream = getClass().getResourceAsStream("template-pom.xml")) {
                String pomContent = IOUtils.readToString(templateStream, Charset.forName("UTF-8"));
                Map<String, String> map = new HashMap<>();
                map.put("groupId", project.getName());
                map.put("artifactId", project.getName());
                map.put("version", "1.0.0");

                file.create(new ByteArrayInputStream(StrSubstitutor.replace(pomContent, map).getBytes()), true,
                        null);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        IJavaProject jProject = JavaCore.create(project);
        jProject.setOutputLocation(project.getFolder(new Path("target").append("classes")).getFullPath(), null);

        List<IClasspathEntry> entries = new ArrayList<>();

        {
            IClasspathAttribute[] attributes = new IClasspathAttribute[2];
            attributes[0] = JavaCore.newClasspathAttribute("optional", "true");
            attributes[1] = JavaCore.newClasspathAttribute("maven.pomderived", "true");
            IPath path = new Path("src").append("main").append("java");
            IPath output = new Path("target").append("classes");
            IClasspathEntry sourceEntry = JavaCore.newSourceEntry(
                    project.getProject().getFullPath().append(path), null, null,
                    project.getProject().getFullPath().append(output), attributes);
            entries.add(sourceEntry);
        }

        {
            IClasspathAttribute[] attributes = new IClasspathAttribute[1];
            attributes[0] = JavaCore.newClasspathAttribute("maven.pomderived", "true");
            IPath path = new Path("src").append("main").append("resources");
            IPath output = new Path("target").append("classes");
            IPath[] exclusions = new IPath[] { new Path("**") };
            entries.add(JavaCore.newSourceEntry(project.getProject().getFullPath().append(path), null,
                    exclusions, project.getProject().getFullPath().append(output), attributes));
        }

        {
            IClasspathAttribute[] attributes = new IClasspathAttribute[2];
            attributes[0] = JavaCore.newClasspathAttribute("optional", "true");
            attributes[1] = JavaCore.newClasspathAttribute("maven.pomderived", "true");
            IPath path = new Path("src").append("test").append("java");
            IPath output = new Path("target").append("test-classes");
            IClasspathEntry sourceEntry = JavaCore.newSourceEntry(
                    project.getProject().getFullPath().append(path), null, null,
                    project.getProject().getFullPath().append(output), attributes);
            entries.add(sourceEntry);
        }

        {
            IClasspathAttribute[] attributes = new IClasspathAttribute[1];
            attributes[0] = JavaCore.newClasspathAttribute("maven.pomderived", "true");
            IPath path = new Path("src").append("test").append("resources");
            IPath output = new Path("target").append("test-classes");
            IPath[] exclusions = new IPath[] { new Path("**") };
            entries.add(JavaCore.newSourceEntry(project.getProject().getFullPath().append(path), null,
                    exclusions, project.getProject().getFullPath().append(output), attributes));
        }

        {
            IClasspathAttribute[] attributes = new IClasspathAttribute[1];
            attributes[0] = JavaCore.newClasspathAttribute("maven.pomderived", "true");

            IPath path = JavaRuntime.newDefaultJREContainerPath();
            entries.add(JavaCore.newContainerEntry(path, null, attributes, false));
        }

        {
            IClasspathAttribute[] attributes = new IClasspathAttribute[1];
            attributes[0] = JavaCore.newClasspathAttribute("maven.pomderived", "true");

            IPath path = new Path("org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER");
            entries.add(JavaCore.newContainerEntry(path, null, attributes, false));
        }

        jProject.setRawClasspath(entries.toArray(new IClasspathEntry[0]), null);
        project.getWorkspace().save(true, null);
        return Status.ok();
    } catch (CoreException ex) {
        //TODO
        ex.printStackTrace();
        return Status.status(State.ERROR, -1, "Failed to create project", ex);
    }
}

From source file:at.bestsolution.javafx.ide.jdt.internal.jdt.CPListElement.java

License:Open Source License

private IClasspathEntry newClasspathEntry() {

    IClasspathAttribute[] extraAttributes = getClasspathAttributes();
    switch (fEntryKind) {
    case IClasspathEntry.CPE_SOURCE:
        IPath[] inclusionPattern = (IPath[]) getAttribute(INCLUSION);
        IPath[] exclusionPattern = (IPath[]) getAttribute(EXCLUSION);
        IPath outputLocation = (IPath) getAttribute(OUTPUT);
        return JavaCore.newSourceEntry(fPath, inclusionPattern, exclusionPattern, outputLocation,
                extraAttributes);//  www.  j a v a  2s.  co m
    case IClasspathEntry.CPE_LIBRARY: {
        IPath attach = (IPath) getAttribute(SOURCEATTACHMENT);
        IAccessRule[] accesRules = (IAccessRule[]) getAttribute(ACCESSRULES);
        return JavaCore.newLibraryEntry(fPath, attach, null, accesRules, extraAttributes, isExported());
    }
    case IClasspathEntry.CPE_PROJECT: {
        IAccessRule[] accesRules = (IAccessRule[]) getAttribute(ACCESSRULES);
        boolean combineAccessRules = ((Boolean) getAttribute(COMBINE_ACCESSRULES)).booleanValue();
        return JavaCore.newProjectEntry(fPath, accesRules, combineAccessRules, extraAttributes, isExported());
    }
    case IClasspathEntry.CPE_CONTAINER: {
        IAccessRule[] accesRules = (IAccessRule[]) getAttribute(ACCESSRULES);
        return JavaCore.newContainerEntry(fPath, accesRules, extraAttributes, isExported());
    }
    case IClasspathEntry.CPE_VARIABLE: {
        IPath varAttach = (IPath) getAttribute(SOURCEATTACHMENT);
        IAccessRule[] accesRules = (IAccessRule[]) getAttribute(ACCESSRULES);
        return JavaCore.newVariableEntry(fPath, varAttach, null, accesRules, extraAttributes, isExported());
    }
    default:
        return null;
    }
}

From source file:com.google.cloud.tools.eclipse.appengine.facets.FacetInstallDelegate.java

License:Open Source License

/**
 * Adds jars associated with the App Engine facet if they don't already exist in
 * <code>project</code>//  ww w.  j  a v  a 2 s .  c om
 */
private void addAppEngineJarsToClasspath(IProject project, IProgressMonitor monitor) throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    IClasspathEntry appEngineContainer = JavaCore.newContainerEntry(
            new Path(AppEngineSdkClasspathContainer.CONTAINER_ID), new IAccessRule[0],
            new IClasspathAttribute[] {
                    UpdateClasspathAttributeUtil.createDependencyAttribute(true /*isWebApp */) },
            true /* isExported */);

    // Check if App Engine container entry already exists
    for (int i = 0; i < rawClasspath.length; i++) {
        if (rawClasspath[i].equals(appEngineContainer)) {
            return;
        }
    }

    IClasspathEntry[] newClasspath = new IClasspathEntry[rawClasspath.length + 1];
    System.arraycopy(rawClasspath, 0, newClasspath, 0, rawClasspath.length);
    newClasspath[newClasspath.length - 1] = appEngineContainer;
    javaProject.setRawClasspath(newClasspath, monitor);
}

From source file:com.google.cloud.tools.eclipse.appengine.libraries.BuildPath.java

License:Apache License

@VisibleForTesting
static IClasspathEntry makeClasspathEntry(Library library) throws CoreException {
    IClasspathAttribute[] classpathAttributes = new IClasspathAttribute[1];
    if (library.isExport()) {
        boolean isWebApp = true;
        classpathAttributes[0] = UpdateClasspathAttributeUtil.createDependencyAttribute(isWebApp);
    } else {/*from  w  w w.j  av a 2  s. c  o  m*/
        classpathAttributes[0] = UpdateClasspathAttributeUtil.createNonDependencyAttribute();
    }

    IClasspathEntry libraryContainer = JavaCore.newContainerEntry(library.getContainerPath(),
            new IAccessRule[0], classpathAttributes, false);
    return libraryContainer;
}

From source file:com.google.cloud.tools.eclipse.appengine.newproject.CreateAppEngineStandardWtpProject.java

License:Apache License

private void addAppEngineLibrariesToBuildPath(IProject newProject, List<Library> libraries,
        IProgressMonitor monitor) throws CoreException {
    if (libraries.isEmpty()) {
        return;//from ww  w . jav a 2 s.c  o m
    }
    SubMonitor subMonitor = SubMonitor.convert(monitor, "Adding App Engine libraries", libraries.size());
    IJavaProject javaProject = JavaCore.create(newProject);
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    IClasspathEntry[] newRawClasspath = Arrays.copyOf(rawClasspath, rawClasspath.length + libraries.size());
    for (int i = 0; i < libraries.size(); i++) {
        Library library = libraries.get(i);
        IClasspathAttribute[] classpathAttributes;
        if (library.isExport()) {
            classpathAttributes = new IClasspathAttribute[] {
                    UpdateClasspathAttributeUtil.createDependencyAttribute(true /* isWebApp */) };
        } else {
            classpathAttributes = new IClasspathAttribute[] {
                    UpdateClasspathAttributeUtil.createNonDependencyAttribute() };
        }

        IClasspathEntry libraryContainer = JavaCore.newContainerEntry(library.getContainerPath(),
                new IAccessRule[0], classpathAttributes, false);
        newRawClasspath[rawClasspath.length + i] = libraryContainer;
        subMonitor.worked(1);
    }
    javaProject.setRawClasspath(newRawClasspath, monitor);

    runContainerResolverJob(javaProject);
}

From source file:com.google.cloud.tools.eclipse.appengine.newproject.CreateAppEngineStandardWtpProject.java

License:Apache License

private void addJunit4ToClasspath(IProgressMonitor monitor, final IProject newProject)
        throws CoreException, JavaModelException {
    IJavaProject javaProject = JavaCore.create(newProject);
    IClasspathAttribute nonDependencyAttribute = UpdateClasspathAttributeUtil.createNonDependencyAttribute();
    IClasspathEntry junit4Container = JavaCore.newContainerEntry(JUnitCore.JUNIT4_CONTAINER_PATH,
            new IAccessRule[0], new IClasspathAttribute[] { nonDependencyAttribute }, false);
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    IClasspathEntry[] newRawClasspath = Arrays.copyOf(rawClasspath, rawClasspath.length + 1);
    newRawClasspath[newRawClasspath.length - 1] = junit4Container;
    javaProject.setRawClasspath(newRawClasspath, monitor);
}

From source file:com.google.cloud.tools.eclipse.appengine.newproject.CreateAppEngineWtpProject.java

License:Apache License

private static void addJunit4ToClasspath(IProgressMonitor monitor, IProject newProject) throws CoreException {
    IJavaProject javaProject = JavaCore.create(newProject);
    IClasspathAttribute nonDependencyAttribute = UpdateClasspathAttributeUtil.createNonDependencyAttribute();
    IClasspathEntry junit4Container = JavaCore.newContainerEntry(JUnitCore.JUNIT4_CONTAINER_PATH,
            new IAccessRule[0], new IClasspathAttribute[] { nonDependencyAttribute }, false);
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    IClasspathEntry[] newRawClasspath = Arrays.copyOf(rawClasspath, rawClasspath.length + 1);
    newRawClasspath[newRawClasspath.length - 1] = junit4Container;
    javaProject.setRawClasspath(newRawClasspath, monitor);
}

From source file:com.googlecode.osde.internal.ui.wizards.newrestprj.RestfulAccessProjectFactory.java

License:Apache License

private void applyClasspath(IProgressMonitor monitor, final IJavaProject javaProject,
        Set<IClasspathEntry> entries, IPath libPath, IFolder libDir) throws JavaModelException {
    IClasspathContainer libContainer = new ClasspathContainerImpl(libDir, libPath);
    JavaCore.setClasspathContainer(libPath, new IJavaProject[] { javaProject },
            new IClasspathContainer[] { libContainer }, null);
    ////w w w .j a va2s.co m
    IClasspathEntry libEntry = JavaCore.newContainerEntry(libPath, null, new IClasspathAttribute[] {}, false);
    entries.add(libEntry);
    //
    entries.add(JavaRuntime.getDefaultJREContainerEntry());
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);
}

From source file:com.liferay.ide.project.core.library.PluginLibraryInstallOperation.java

License:Open Source License

@Override
public void execute(LibraryProviderOperationConfig config, IProgressMonitor monitor) throws CoreException {
    IFacetedProjectBase facetedProject = config.getFacetedProject();

    IProject project = facetedProject.getProject();

    IJavaProject javaProject = JavaCore.create(project);

    IPath containerPath = getClasspathContainerPath();

    // IDE-413 check to make sure that the containerPath doesn't already existing.

    IClasspathEntry[] entries = javaProject.getRawClasspath();

    for (IClasspathEntry entry : entries) {
        if (entry.getPath().equals(containerPath)) {
            return;
        }/*w w w  . j  ava2s  . c  o m*/
    }

    IAccessRule[] accessRules = new IAccessRule[] {};

    IClasspathAttribute[] attributes = new IClasspathAttribute[] { JavaCore.newClasspathAttribute(
            IClasspathDependencyConstants.CLASSPATH_COMPONENT_NON_DEPENDENCY, StringPool.EMPTY) };

    IClasspathEntry newEntry = JavaCore.newContainerEntry(containerPath, accessRules, attributes, false);

    IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];

    System.arraycopy(entries, 0, newEntries, 0, entries.length);

    newEntries[entries.length] = newEntry;

    javaProject.setRawClasspath(newEntries, monitor);
}