Example usage for org.eclipse.jdt.core IClasspathEntry getEntryKind

List of usage examples for org.eclipse.jdt.core IClasspathEntry getEntryKind

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry getEntryKind.

Prototype

int getEntryKind();

Source Link

Document

Returns the kind of this classpath entry.

Usage

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

License:Open Source License

public synchronized List<IPackageFragmentRoot> getPackageFragmentRoots() {
    if (packageFragmentRoots.isEmpty() && !moduleManager.isExternalModuleLoadedFromSource(getNameAsString())) {
        IJavaProject javaProject = moduleManager.getJavaProject();
        if (javaProject != null) {
            if (this.equals(getLanguageModule())) {
                IClasspathEntry runtimeClasspathEntry = null;

                try {
                    for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath().segment(0)
                                .equals(CeylonLanguageModuleContainer.CONTAINER_ID)) {
                            runtimeClasspathEntry = entry;
                            break;
                        }//www  . j  a v  a2  s . c  om
                    }

                    if (runtimeClasspathEntry != null) {
                        for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                            if (root.exists() && javaProject.isOnClasspath(root)
                                    && root.getRawClasspathEntry().equals(runtimeClasspathEntry)) {
                                packageFragmentRoots.add(root);
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                File jarToSearch = null;
                try {
                    jarToSearch = returnCarFile();
                    if (jarToSearch == null) {
                        RepositoryManager repoMgr = CeylonBuilder
                                .getProjectRepositoryManager(javaProject.getProject());
                        if (repoMgr != null) {
                            jarToSearch = CeylonProjectModulesContainer.getModuleArtifact(repoMgr, this);
                        }
                    }

                    if (jarToSearch != null) {
                        IPackageFragmentRoot root = moduleManager.getJavaProject()
                                .getPackageFragmentRoot(jarToSearch.toString());
                        if (root instanceof JarPackageFragmentRoot) {
                            JarPackageFragmentRoot jarRoot = (JarPackageFragmentRoot) root;
                            if (jarRoot.getJar().getName().equals(jarToSearch.getPath())) {
                                packageFragmentRoots.add(root);
                            }
                        }
                    }
                } catch (CoreException e) {
                    if (jarToSearch != null) {
                        System.err.println("Exception trying to get Jar file '" + jarToSearch + "' :");
                    }
                    e.printStackTrace();
                }
            }
        }
    }
    return packageFragmentRoots;
}

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   ww  w  .j av a2s.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

@Override
public synchronized boolean loadPackage(String packageName, boolean loadDeclarations) {
    packageName = Util.quoteJavaKeywords(packageName);
    if (loadDeclarations && !loadedPackages.add(packageName)) {
        return true;
    }//  www .j  a v a  2 s. c  om
    Module module = lookupModuleInternal(packageName);

    if (module instanceof JDTModule) {
        JDTModule jdtModule = (JDTModule) module;
        List<IPackageFragmentRoot> roots = jdtModule.getPackageFragmentRoots();
        IPackageFragment packageFragment = null;
        for (IPackageFragmentRoot root : roots) {
            // skip packages that are not present
            if (!root.exists())
                continue;
            try {
                IClasspathEntry entry = root.getRawClasspathEntry();

                //TODO: is the following really necessary?
                //Note that getContentKind() returns an undefined
                //value for a classpath container or variable
                if (entry.getEntryKind() != IClasspathEntry.CPE_CONTAINER
                        && entry.getEntryKind() != IClasspathEntry.CPE_VARIABLE
                        && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE
                        && !CeylonBuilder.isCeylonSourceEntry(entry)) {
                    continue;
                }

                packageFragment = root.getPackageFragment(packageName);
                if (packageFragment.exists()) {
                    if (!loadDeclarations) {
                        // we found the package
                        return true;
                    } else {
                        try {
                            for (IClassFile classFile : packageFragment.getClassFiles()) {
                                // skip removed class files
                                if (!classFile.exists())
                                    continue;
                                IType type = classFile.getType();
                                if (type.exists() && !type.isMember()
                                        && !sourceDeclarations.containsKey(
                                                getQualifiedName(type.getPackageFragment().getElementName(),
                                                        type.getTypeQualifiedName()))) {
                                    convertToDeclaration(type.getFullyQualifiedName(), DeclarationType.VALUE);
                                }
                            }
                            for (org.eclipse.jdt.core.ICompilationUnit compilationUnit : packageFragment
                                    .getCompilationUnits()) {
                                // skip removed CUs
                                if (!compilationUnit.exists())
                                    continue;
                                for (IType type : compilationUnit.getTypes()) {
                                    if (type.exists() && !type.isMember()
                                            && !sourceDeclarations.containsKey(
                                                    getQualifiedName(type.getPackageFragment().getElementName(),
                                                            type.getTypeQualifiedName()))) {
                                        convertToDeclaration(type.getFullyQualifiedName(),
                                                DeclarationType.VALUE);
                                    }
                                }
                            }
                        } catch (JavaModelException e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (JavaModelException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

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

License:Open Source License

@Override
protected Module createModule(List<String> moduleName, String version) {
    JDTModule module = null;//from  w w w. jav a2 s  .c  o  m
    String moduleNameString = Util.getName(moduleName);
    List<IPackageFragmentRoot> roots = new ArrayList<IPackageFragmentRoot>();
    try {
        if (moduleNameString.equals(Module.DEFAULT_MODULE_NAME)) {
            // Add the list of source package fragment roots
            for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                IClasspathEntry entry = root.getResolvedClasspathEntry();
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && !root.isExternal()) {
                    roots.add(root);
                }
            }
        } else {
            for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                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)) {
                    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.siteview.mde.internal.core.builders.BuildErrorReporter.java

License:Open Source License

/**
 * Matches the javacSource, javacTarget, javacWarnings, javacErrors and jre.compilation.prile entries in build.properties with the 
 * project specific Java Compiler properties and reports the errors found.
 * //from w ww . j a va  2 s .co m
 * @param javacSourceEntry
 * @param javacTargetEntry
 * @param jreCompilationProfileEntry
 * @param javacWarningsEntries
 * @param javacErrorsEntries 
 * @param libraryNames list of library names (javacWarnings/javacErrors require an entry for each source library)
 */
private void validateExecutionEnvironment(IBuildEntry javacSourceEntry, IBuildEntry javacTargetEntry,
        IBuildEntry jreCompilationProfileEntry, ArrayList javacWarningsEntries, ArrayList javacErrorsEntries,
        List libraryNames) {
    // if there is no source to compile, don't worry about compiler settings
    IJavaProject project = JavaCore.create(fProject);
    if (project.exists()) {
        IClasspathEntry[] classpath = null;
        try {
            classpath = project.getRawClasspath();
        } catch (JavaModelException e) {
            MDECore.log(e);
            return;
        }
        boolean source = false;
        for (int i = 0; i < classpath.length; i++) {
            IClasspathEntry cpe = classpath[i];
            if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                source = true;
            }
        }
        if (!source) {
            return;
        }

        String projectComplianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, false);

        if (projectComplianceLevel != null) {

            IMonitorModelBase model = MonitorRegistry.findModel(fProject);
            String[] execEnvs = null;
            if (model != null) {
                BundleDescription bundleDesc = model.getBundleDescription();
                if (bundleDesc != null) {
                    execEnvs = bundleDesc.getExecutionEnvironments();
                }
            }

            if (execEnvs == null || execEnvs.length == 0) {
                return;
            }

            //PDE Build uses top most entry to build the plug-in
            String execEnv = execEnvs[0];

            String projectSourceCompatibility = project.getOption(JavaCore.COMPILER_SOURCE, true);
            String projectClassCompatibility = project.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM,
                    true);
            if (projectComplianceLevel
                    .equals(findMatchingEE(projectSourceCompatibility, projectClassCompatibility, false))
                    && execEnv.equals(
                            findMatchingEE(projectSourceCompatibility, projectClassCompatibility, true))) {
                return; //The project compliance settings matches the BREE
            }

            Map defaultComplianceOptions = new HashMap();
            JavaCore.setComplianceOptions(projectComplianceLevel, defaultComplianceOptions);

            //project compliance does not match the BREE
            String projectJavaCompatibility = findMatchingEE(projectSourceCompatibility,
                    projectClassCompatibility, true);
            String message = null;
            if (projectJavaCompatibility != null) {
                if (jreCompilationProfileEntry == null) {
                    message = NLS.bind(
                            MDECoreMessages.BuildErrorReporter_ProjectSpecificJavaComplianceMissingEntry,
                            PROPERTY_JRE_COMPILATION_PROFILE,
                            MDECoreMessages.BuildErrorReporter_CompilercomplianceLevel);
                    prepareError(PROPERTY_JRE_COMPILATION_PROFILE, projectJavaCompatibility, message,
                            MDEMarkerFactory.B_JAVA_ADDDITION, fJavaComplianceSeverity,
                            MDEMarkerFactory.CAT_EE);
                } else {
                    if (!projectJavaCompatibility.equalsIgnoreCase(jreCompilationProfileEntry.getTokens()[0])) {
                        message = NLS.bind(
                                MDECoreMessages.BuildErrorReporter_ProjectSpecificJavaComplianceDifferentToken,
                                PROPERTY_JRE_COMPILATION_PROFILE,
                                MDECoreMessages.BuildErrorReporter_CompilercomplianceLevel);
                        prepareError(PROPERTY_JRE_COMPILATION_PROFILE, projectJavaCompatibility, message,
                                MDEMarkerFactory.B_REPLACE, fJavaComplianceSeverity, MDEMarkerFactory.CAT_EE);
                    }
                }
            } else {
                // Check source level setting
                if (projectSourceCompatibility.equals(defaultComplianceOptions.get(JavaCore.COMPILER_SOURCE))) {
                    if (javacSourceEntry != null) {
                        message = NLS.bind(
                                MDECoreMessages.BuildErrorReporter_BuildEntryNotRequiredMatchesDefault,
                                PROPERTY_JAVAC_SOURCE, MDECoreMessages.BuildErrorReporter_SourceCompatibility);
                        prepareError(PROPERTY_JAVAC_SOURCE, null, message, MDEMarkerFactory.B_REMOVAL,
                                fJavaComplianceSeverity, MDEMarkerFactory.CAT_EE);
                    }
                } else {
                    if (javacSourceEntry == null) {
                        message = NLS.bind(
                                MDECoreMessages.BuildErrorReporter_ProjectSpecificJavaComplianceMissingEntry,
                                PROPERTY_JAVAC_SOURCE, MDECoreMessages.BuildErrorReporter_SourceCompatibility);
                        prepareError(PROPERTY_JAVAC_SOURCE, projectSourceCompatibility, message,
                                MDEMarkerFactory.B_JAVA_ADDDITION, fJavaComplianceSeverity,
                                MDEMarkerFactory.CAT_EE);
                    } else {
                        if (!projectSourceCompatibility.equalsIgnoreCase(javacSourceEntry.getTokens()[0])) {
                            message = NLS.bind(
                                    MDECoreMessages.BuildErrorReporter_ProjectSpecificJavaComplianceDifferentToken,
                                    PROPERTY_JAVAC_SOURCE,
                                    MDECoreMessages.BuildErrorReporter_SourceCompatibility);
                            prepareError(PROPERTY_JAVAC_SOURCE, projectSourceCompatibility, message,
                                    MDEMarkerFactory.B_REPLACE, fJavaComplianceSeverity,
                                    MDEMarkerFactory.CAT_EE);
                        }
                    }
                }

                // Check target level setting
                if (projectClassCompatibility
                        .equals(defaultComplianceOptions.get(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM))) {
                    if (javacTargetEntry != null) {
                        message = NLS.bind(
                                MDECoreMessages.BuildErrorReporter_BuildEntryNotRequiredMatchesDefault,
                                PROPERTY_JAVAC_TARGET,
                                MDECoreMessages.BuildErrorReporter_GeneratedClassFilesCompatibility);
                        prepareError(PROPERTY_JAVAC_TARGET, null, message, MDEMarkerFactory.B_REMOVAL,
                                fJavaComplianceSeverity, MDEMarkerFactory.CAT_EE);
                    }
                } else {
                    if (javacTargetEntry == null) {
                        message = NLS.bind(
                                MDECoreMessages.BuildErrorReporter_ProjectSpecificJavaComplianceMissingEntry,
                                PROPERTY_JAVAC_TARGET,
                                MDECoreMessages.BuildErrorReporter_GeneratedClassFilesCompatibility);
                        prepareError(PROPERTY_JAVAC_TARGET, projectClassCompatibility, message,
                                MDEMarkerFactory.B_JAVA_ADDDITION, fJavaComplianceSeverity,
                                MDEMarkerFactory.CAT_EE);
                    } else {
                        if (!projectClassCompatibility.equalsIgnoreCase(javacTargetEntry.getTokens()[0])) {
                            message = NLS.bind(
                                    MDECoreMessages.BuildErrorReporter_ProjectSpecificJavaComplianceDifferentToken,
                                    PROPERTY_JAVAC_TARGET,
                                    MDECoreMessages.BuildErrorReporter_GeneratedClassFilesCompatibility);
                            prepareError(PROPERTY_JAVAC_TARGET, projectClassCompatibility, message,
                                    MDEMarkerFactory.B_REPLACE, fJavaComplianceSeverity,
                                    MDEMarkerFactory.CAT_EE);
                        }
                    }
                }
            }

            boolean warnForJavacWarnings = message != null || javacSourceEntry != null
                    || javacTargetEntry != null || jreCompilationProfileEntry != null;
            if (warnForJavacWarnings == false) {
                return;
            }

            checkJavaComplianceSettings(projectComplianceLevel, javacWarningsEntries, javacErrorsEntries,
                    libraryNames);
        }
    }
}

From source file:com.siteview.mde.internal.core.ClasspathComputer.java

License:Open Source License

private static void addSourceAndLibraries(IProject project, IMonitorModelBase model, IBuild build,
        boolean clear, Map sourceLibraryMap, ArrayList result) throws CoreException {

    HashSet paths = new HashSet();

    // keep existing source folders
    if (!clear) {
        IClasspathEntry[] entries = JavaCore.create(project).getRawClasspath();
        for (int i = 0; i < entries.length; i++) {
            IClasspathEntry entry = entries[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                if (paths.add(entry.getPath()))
                    result.add(entry);//  ww  w  .ja v  a 2 s.c om
            }
        }
    }

    IClasspathAttribute[] attrs = getClasspathAttributes(project, model);
    IMonitorLibrary[] libraries = model.getMonitorBase().getLibraries();
    for (int i = 0; i < libraries.length; i++) {
        IBuildEntry buildEntry = build == null ? null : build.getEntry("source." + libraries[i].getName()); //$NON-NLS-1$
        if (buildEntry != null) {
            addSourceFolder(buildEntry, project, paths, result);
        } else {
            IPath sourceAttachment = sourceLibraryMap != null
                    ? (IPath) sourceLibraryMap.get(libraries[i].getName())
                    : null;
            if (libraries[i].getName().equals(".")) //$NON-NLS-1$
                addJARdPlugin(project, ClasspathUtilCore.getFilename(model), sourceAttachment, attrs, result);
            else
                addLibraryEntry(project, libraries[i], sourceAttachment, attrs, result);
        }
    }
    if (libraries.length == 0) {
        if (build != null) {
            IBuildEntry buildEntry = build == null ? null : build.getEntry("source.."); //$NON-NLS-1$
            if (buildEntry != null) {
                addSourceFolder(buildEntry, project, paths, result);
            }
        } else if (ClasspathUtilCore.hasBundleStructure(model)) {
            IPath sourceAttachment = sourceLibraryMap != null ? (IPath) sourceLibraryMap.get(".") : null; //$NON-NLS-1$
            addJARdPlugin(project, ClasspathUtilCore.getFilename(model), sourceAttachment, attrs, result);
        }
    }
}

From source file:com.siteview.mde.internal.core.JavaElementChangeListener.java

License:Open Source License

private boolean ignoreDelta(IJavaElementDelta delta) {
    try {// w ww.j a  va  2 s.co m
        IJavaElement element = delta.getElement();
        if (element instanceof IPackageFragmentRoot) {
            IPackageFragmentRoot root = (IPackageFragmentRoot) element;
            IClasspathEntry entry = root.getRawClasspathEntry();
            if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER)
                return true;
        }
    } catch (JavaModelException e) {
    }
    return false;
}

From source file:com.siteview.mde.internal.core.project.BundleProjectDescription.java

License:Open Source License

/**
 * Creates and returns a bundle claspath specifications for the given source.<library> build
 * entry/*from w  ww. j a v  a  2 s  . c o  m*/
 * 
 * @param project
 * @param entry
 * @param binary whether a binary folder (<code>true</code>) or source folder (<code>false</code>)
 * @return associated bundle classpath specifications or <code>null</code> if a malformed entry
 * @throws CoreException if unable to access Java build path
 */
private IBundleClasspathEntry[] getClasspathEntries(IProject project, IBuildEntry entry, boolean binary)
        throws CoreException {
    String[] tokens = entry.getTokens();
    IPath lib = null;
    if (binary) {
        lib = new Path(entry.getName().substring(IBuildEntry.OUTPUT_PREFIX.length()));
    } else {
        lib = new Path(entry.getName().substring(IBuildEntry.JAR_PREFIX.length()));
    }
    if (tokens != null && tokens.length > 0) {
        IBundleClasspathEntry[] bces = new IBundleClasspathEntry[tokens.length];
        for (int i = 0; i < tokens.length; i++) {
            IPath path = new Path(tokens[i]);
            IBundleClasspathEntry spec = null;
            if (binary) {
                spec = getBundleProjectService().newBundleClasspathEntry(null, path, lib);
            } else {
                IJavaProject jp = JavaCore.create(project);
                IPath output = null;
                if (jp.exists()) {
                    IClasspathEntry[] rawClasspath = jp.getRawClasspath();
                    for (int j = 0; j < rawClasspath.length; j++) {
                        IClasspathEntry cpe = rawClasspath[j];
                        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                            if (cpe.getPath().removeFirstSegments(1).equals(path)) {
                                output = cpe.getOutputLocation();
                                if (output != null) {
                                    output = output.removeFirstSegments(1);
                                }
                                break;
                            }
                        }
                    }
                }
                spec = getBundleProjectService().newBundleClasspathEntry(path, output, lib);
            }
            bces[i] = spec;
        }
        return bces;
    }
    return null;
}

From source file:com.siteview.mde.internal.core.project.ProjectModifyOperation.java

License:Open Source License

/**
 * Creates or modifies a project based on the given description.
 * /*from  w  ww  .j av  a2 s .c  o  m*/
 * @param monitor progress monitor or <code>null</code>
 * @param description project description
 * @throws CoreException if project creation fails
 */
public void execute(IProgressMonitor monitor, IBundleProjectDescription description) throws CoreException {
    // retrieve current description of the project to detect differences
    IProject project = description.getProject();
    IBundleProjectService service = (IBundleProjectService) MDECore.getDefault()
            .acquireService(IBundleProjectService.class.getName());
    IBundleProjectDescription before = service.getDescription(project);
    boolean considerRoot = !project.exists();
    String taskName = null;
    boolean jpExisted = false;
    if (project.exists()) {
        taskName = Messages.ProjectModifyOperation_0;
        jpExisted = before.hasNature(JavaCore.NATURE_ID);
    } else {
        taskName = Messages.ProjectModifyOperation_1;
        // new bundle projects get Java and Plug-in natures
        if (description.getNatureIds().length == 0) {
            description
                    .setNatureIds(new String[] { IBundleProjectDescription.PLUGIN_NATURE, JavaCore.NATURE_ID });
        }
    }
    boolean becomeBundle = !before.hasNature(IBundleProjectDescription.PLUGIN_NATURE)
            && description.hasNature(IBundleProjectDescription.PLUGIN_NATURE);

    // set default values when migrating from Java project to bundle project
    if (jpExisted && becomeBundle) {
        if (description.getExecutionEnvironments() == null) {
            // use EE from Java project when unspecified in the description, and a bundle nature is being added
            IJavaProject jp = JavaCore.create(project);
            if (jp.exists()) {
                IClasspathEntry[] classpath = jp.getRawClasspath();
                for (int i = 0; i < classpath.length; i++) {
                    IClasspathEntry entry = classpath[i];
                    if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        String id = JavaRuntime.getExecutionEnvironmentId(entry.getPath());
                        if (id != null) {
                            description.setExecutionEnvironments(new String[] { id });
                            break;
                        }
                    }
                }
            }
        }
    }
    // other default values when becoming a bundle
    if (becomeBundle) {
        // set default values for where unspecified
        if (description.getBundleVersion() == null) {
            description.setBundleVersion(new Version(1, 0, 0, "qualifier")); //$NON-NLS-1$
        }
    }

    SubMonitor sub = SubMonitor.convert(monitor, taskName, 6);
    // create and open project
    createProject(description);
    // set bundle root for new projects
    if (considerRoot) {
        IFolder folder = null;
        IPath root = description.getBundleRoot();
        if (root != null && !root.isEmpty()) {
            folder = project.getFolder(root);
            CoreUtility.createFolder(folder);
        }
        PDEProject.setBundleRoot(project, folder);
    }
    sub.worked(1);
    configureNatures(description);
    sub.worked(1);
    if (project.hasNature(JavaCore.NATURE_ID)) {
        configureJavaProject(description, before, jpExisted);
    }
    sub.worked(1);
    configureManifest(description, before);
    sub.worked(1);
    configureBuildPropertiesFile(description, before);
    sub.worked(1);

    // project settings for Equinox, Extension Registry, Automated dependency policy,
    // manifest editor launch shortcuts and export wizard
    IEclipsePreferences pref = new ProjectScope(project).getNode(MDECore.PLUGIN_ID);
    if (pref != null) {
        // best guess for automated dependency management: Equinox + Extensions = use required bundle
        if (description.isEquinox() && description.isExtensionRegistry()) {
            pref.remove(ICoreConstants.RESOLVE_WITH_REQUIRE_BUNDLE); // i.e. use required bundle
        } else {
            pref.putBoolean(ICoreConstants.RESOLVE_WITH_REQUIRE_BUNDLE, false);
        }
        if (description.isExtensionRegistry()) {
            pref.remove(ICoreConstants.EXTENSIONS_PROPERTY); // i.e. support extensions
        } else {
            pref.putBoolean(ICoreConstants.EXTENSIONS_PROPERTY, false);
        }
        if (description.isEquinox()) {
            pref.remove(ICoreConstants.EQUINOX_PROPERTY); // i.e. using Equinox
        } else {
            pref.putBoolean(ICoreConstants.EQUINOX_PROPERTY, false);
        }
        String[] shorts = description.getLaunchShortcuts();
        if (shorts == null || shorts.length == 0) {
            pref.remove(ICoreConstants.MANIFEST_LAUNCH_SHORTCUTS); // use defaults
        } else {
            StringBuffer value = new StringBuffer();
            for (int i = 0; i < shorts.length; i++) {
                if (i > 0) {
                    value.append(',');
                }
                value.append(shorts[i]);
            }
            pref.put(ICoreConstants.MANIFEST_LAUNCH_SHORTCUTS, value.toString());
        }
        if (description.getExportWizardId() == null) {
            pref.remove(ICoreConstants.MANIFEST_EXPORT_WIZARD);
        } else {
            pref.put(ICoreConstants.MANIFEST_EXPORT_WIZARD, description.getExportWizardId());
        }
        try {
            pref.flush();
        } catch (BackingStoreException e) {
            throw new CoreException(
                    new Status(IStatus.ERROR, MDECore.PLUGIN_ID, Messages.ProjectModifyOperation_2, e));
        }
    }

    if (fModel.isDirty()) {
        fModel.save();
    }
    sub.worked(1);
    sub.done();
    if (monitor != null) {
        monitor.done();
    }
}

From source file:com.siteview.mde.internal.core.project.ProjectModifyOperation.java

License:Open Source License

/**
 * Configures the build path and output location of the described Java project.
 * If the Java project existed before this operation, new build path entries are
 * added for the bundle class path, if required, but we don't change the exiting
 * build path./*from  w  w w .j  a  va2 s .  c  o  m*/
 * 
 * @param description desired project description
 * @param before state before the operation
 * @param existed whether the Java project existed before the operation
 */
private void configureJavaProject(IBundleProjectDescription description, IBundleProjectDescription before,
        boolean existed) throws CoreException {
    IProject project = description.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    // create source folders as required
    IBundleClasspathEntry[] bces = description.getBundleClasspath();
    if (bces != null && bces.length > 0) {
        for (int i = 0; i < bces.length; i++) {
            IPath folder = bces[i].getSourcePath();
            if (folder != null) {
                CoreUtility.createFolder(project.getFolder(folder));
            }
        }
    }
    // Set default output folder
    if (description.getDefaultOutputFolder() != null) {
        IPath path = project.getFullPath().append(description.getDefaultOutputFolder());
        javaProject.setOutputLocation(path, null);
    }

    // merge the class path if the project existed before
    IBundleClasspathEntry[] prev = before.getBundleClasspath();
    if (!isEqual(bces, prev)) {
        if (existed) {
            // add entries not already present
            IClasspathEntry[] entries = getSourceFolderEntries(javaProject, description);
            IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
            List add = new ArrayList();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                boolean present = false;
                for (int j = 0; j < rawClasspath.length; j++) {
                    IClasspathEntry existingEntry = rawClasspath[j];
                    if (existingEntry.getEntryKind() == entry.getEntryKind()) {
                        if (existingEntry.getPath().equals(entry.getPath())) {
                            present = true;
                            break;
                        }
                    }
                }
                if (!present) {
                    add.add(entry);
                }
            }
            // check if the 'required plug-ins' container is present
            boolean addRequired = false;
            if (description.hasNature(IBundleProjectDescription.PLUGIN_NATURE)) {
                addRequired = true;
                for (int i = 0; i < rawClasspath.length; i++) {
                    IClasspathEntry cpe = rawClasspath[i];
                    if (cpe.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                        if (MDECore.REQUIRED_PLUGINS_CONTAINER_PATH.equals(cpe.getPath())) {
                            addRequired = false;
                            break;
                        }
                    }
                }
            }
            if (addRequired) {
                add.add(ClasspathComputer.createContainerEntry());
            }
            if (!add.isEmpty()) {
                List all = new ArrayList();
                for (int i = 0; i < rawClasspath.length; i++) {
                    all.add(rawClasspath[i]);
                }
                all.addAll(add);
                javaProject.setRawClasspath((IClasspathEntry[]) all.toArray(new IClasspathEntry[all.size()]),
                        null);
            }
        } else {
            IClasspathEntry[] entries = getClassPathEntries(javaProject, description);
            javaProject.setRawClasspath(entries, null);
        }
    }
}