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

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

Introduction

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

Prototype

int CPE_SOURCE

To view the source code for org.eclipse.jdt.core IClasspathEntry CPE_SOURCE.

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a folder containing package fragments with source code to be compiled.

Usage

From source file:org.eclipse.pde.internal.ui.properties.SelfHostingPropertyPage.java

License:Open Source License

private String[] getOutputFolders() {
    IProject project = (IProject) getElement().getAdapter(IProject.class);
    ArrayList<String> list = new ArrayList<String>();
    try {//from   w ww . j  a  va2 s . co m
        if (project.hasNature(JavaCore.NATURE_ID)) {
            IJavaProject jProject = JavaCore.create(project);
            list.add(jProject.getOutputLocation().toString());
            IClasspathEntry[] entries = jProject.getRawClasspath();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                        && entry.getContentKind() == IPackageFragmentRoot.K_SOURCE) {
                    IPath path = entry.getOutputLocation();
                    if (path != null)
                        list.add(path.toString());
                }
            }
        }
    } catch (JavaModelException e) {
    } catch (CoreException e) {
    }
    return list.toArray(new String[list.size()]);
}

From source file:org.eclipse.pde.internal.ui.wizards.plugin.NewProjectCreationOperation.java

License:Open Source License

private void addAllSourcePackages(IProject project, Set<String> list) {
    try {/*from w ww . j  a  v  a  2s  .  c o  m*/
        IJavaProject javaProject = JavaCore.create(project);
        IClasspathEntry[] classpath = javaProject.getRawClasspath();
        for (int i = 0; i < classpath.length; i++) {
            IClasspathEntry entry = classpath[i];
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath path = entry.getPath().removeFirstSegments(1);
                if (path.segmentCount() > 0) {
                    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(project.getFolder(path));
                    IJavaElement[] children = root.getChildren();
                    for (int j = 0; j < children.length; j++) {
                        IPackageFragment frag = (IPackageFragment) children[j];
                        if (frag.getChildren().length > 0 || frag.getNonJavaResources().length > 0)
                            list.add(children[j].getElementName());
                    }
                }
            }
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.pde.internal.ui.wizards.tools.ConvertProjectToPluginOperation.java

License:Open Source License

private void loadClasspathEntries(IProject project, IProgressMonitor monitor) {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] currentClassPath = new IClasspathEntry[0];
    ArrayList<String> sources = new ArrayList<String>();
    ArrayList<String> libraries = new ArrayList<String>();
    try {//w  w w.  ja v  a2  s . co m
        currentClassPath = javaProject.getRawClasspath();
    } catch (JavaModelException e) {
    }
    for (int i = 0; i < currentClassPath.length; i++) {
        int contentType = currentClassPath[i].getEntryKind();
        if (contentType == IClasspathEntry.CPE_SOURCE) {
            String relativePath = getRelativePath(currentClassPath[i], project);
            if (relativePath.equals("")) { //$NON-NLS-1$
                sources.add("."); //$NON-NLS-1$
            } else {
                sources.add(relativePath + "/"); //$NON-NLS-1$
            }
        } else if (contentType == IClasspathEntry.CPE_LIBRARY) {
            String path = getRelativePath(currentClassPath[i], project);
            if (path.length() > 0)
                libraries.add(path);
            else
                libraries.add("."); //$NON-NLS-1$
        }
    }
    fSrcEntries = sources.toArray(new String[sources.size()]);
    fLibEntries = libraries.toArray(new String[libraries.size()]);

    IClasspathEntry[] classPath = new IClasspathEntry[currentClassPath.length + 1];
    System.arraycopy(currentClassPath, 0, classPath, 0, currentClassPath.length);
    classPath[classPath.length - 1] = ClasspathComputer.createContainerEntry();
    try {
        javaProject.setRawClasspath(classPath, monitor);
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.pde.nls.internal.ui.model.ResourceBundleModel.java

License:Open Source License

/**
 * @param monitor/*from w w w .ja v  a2  s .  c  o  m*/
 * @throws CoreException
 */
private void populateFromWorkspace(IProgressMonitor monitor) throws CoreException {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IProject[] projects = root.getProjects();
    for (IProject project : projects) {
        try {
            if (!project.isOpen())
                continue;

            IJavaProject javaProject = (IJavaProject) project.getNature(JAVA_NATURE);
            String pluginId = null;

            try {
                Class IFragmentModel = Class.forName("org.eclipse.pde.core.plugin.IFragmentModel");
                Class IPluginModelBase = Class.forName("org.eclipse.pde.core.plugin.IPluginModelBase");
                Class PluginRegistry = Class.forName("org.eclipse.pde.core.plugin.PluginRegistry");
                Class IPluginBase = Class.forName("org.eclipse.pde.core.plugin.IPluginBase");
                Class PluginFragmentModel = Class.forName("org.eclipse.core.runtime.model.PluginFragmentModel");

                // Plugin and fragment projects
                Class pluginModel = (Class) PluginRegistry.getMethod("findModel", IProject.class).invoke(null,
                        project);
                if (pluginModel != null) {
                    // Get plugin id
                    BundleDescription bd = (BundleDescription) IPluginModelBase
                            .getMethod("getBundleDescription").invoke(pluginModel);
                    pluginId = bd.getName();
                    // OSGi bundle name
                    if (pluginId == null) {
                        Object pluginBase = IPluginModelBase.getMethod("getPluginBase").invoke(pluginModel);
                        pluginId = (String) IPluginBase.getMethod("getId").invoke(pluginBase); // non-OSGi
                        // plug-in id
                    }

                    boolean isFragment = IFragmentModel.isInstance(pluginModel);
                    if (isFragment) {
                        Object pfm = IFragmentModel.getMethod("getFragment");
                        pluginId = (String) PluginFragmentModel.getMethod("getPluginId").invoke(pfm);
                    }

                    // Look for additional 'nl' resources
                    IFolder nl = project.getFolder("nl"); //$NON-NLS-1$
                    if (isFragment && nl.exists()) {
                        IResource[] members = nl.members();
                        for (IResource member : members) {
                            if (member instanceof IFolder) {
                                IFolder langFolder = (IFolder) member;
                                String language = langFolder.getName();

                                // Collect property files
                                IFile[] propertyFiles = collectPropertyFiles(langFolder);
                                for (IFile file : propertyFiles) {
                                    // Compute path name
                                    IPath path = file.getProjectRelativePath();
                                    String country = ""; //$NON-NLS-1$
                                    String packageName = null;
                                    int segmentCount = path.segmentCount();
                                    if (segmentCount > 1) {
                                        StringBuilder builder = new StringBuilder();

                                        // Segment 0: 'nl'
                                        // Segment 1: language code
                                        // Segment 2: (country code)
                                        int begin = 2;
                                        if (segmentCount > 2 && isCountry(path.segment(2))) {
                                            begin = 3;
                                            country = path.segment(2);
                                        }

                                        for (int i = begin; i < segmentCount - 1; i++) {
                                            if (i > begin)
                                                builder.append('.');
                                            builder.append(path.segment(i));
                                        }
                                        packageName = builder.toString();
                                    }

                                    String baseName = getBaseName(file.getName());

                                    ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId,
                                            packageName, baseName);
                                    addBundle(family, getLocale(language, country), file);
                                }
                            }
                        }
                    }

                    // Collect property files
                    if (isFragment || javaProject == null) {
                        IFile[] propertyFiles = collectPropertyFiles(project);
                        for (IFile file : propertyFiles) {
                            IPath path = file.getProjectRelativePath();
                            int segmentCount = path.segmentCount();

                            if (segmentCount > 0 && path.segment(0).equals("nl")) //$NON-NLS-1$
                                continue; // 'nl' resource have been
                            // processed
                            // above

                            // Guess package name
                            String packageName = null;
                            if (segmentCount > 1) {
                                StringBuilder builder = new StringBuilder();
                                for (int i = 0; i < segmentCount - 1; i++) {
                                    if (i > 0)
                                        builder.append('.');
                                    builder.append(path.segment(i));
                                }
                                packageName = builder.toString();
                            }

                            String baseName = getBaseName(file.getName());
                            String language = getLanguage(file.getName());
                            String country = getCountry(file.getName());

                            ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId,
                                    packageName, baseName);
                            addBundle(family, getLocale(language, country), file);
                        }
                    }

                }
            } catch (Throwable e) {
                // MessagesEditorPlugin.log(e);
            }

            // Look for resource bundles in Java packages (output folders,
            // e.g. 'bin', will be ignored)
            if (javaProject != null) {
                IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);
                for (IClasspathEntry entry : classpathEntries) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        IPath path = entry.getPath();
                        IFolder folder = workspace.getRoot().getFolder(path);
                        IFile[] propertyFiles = collectPropertyFiles(folder);

                        for (IFile file : propertyFiles) {
                            String name = file.getName();
                            String baseName = getBaseName(name);
                            String language = getLanguage(name);
                            String country = getCountry(name);
                            IPackageFragment pf = javaProject
                                    .findPackageFragment(file.getParent().getFullPath());
                            String packageName = pf.getElementName();

                            ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId,
                                    packageName, baseName);

                            addBundle(family, getLocale(language, country), file);
                        }
                    } else if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        IPackageFragmentRoot[] findPackageFragmentRoots = javaProject
                                .findPackageFragmentRoots(entry);
                        for (IPackageFragmentRoot packageFragmentRoot : findPackageFragmentRoots) {
                            IJavaElement[] children = packageFragmentRoot.getChildren();
                            for (IJavaElement child : children) {
                                IPackageFragment pf = (IPackageFragment) child;
                                Object[] nonJavaResources = pf.getNonJavaResources();

                                for (Object resource : nonJavaResources) {
                                    if (resource instanceof IJarEntryResource) {
                                        IJarEntryResource jarEntryResource = (IJarEntryResource) resource;
                                        String name = jarEntryResource.getName();
                                        if (name.endsWith(PROPERTIES_SUFFIX)) {
                                            String baseName = getBaseName(name);
                                            String language = getLanguage(name);
                                            String country = getCountry(name);
                                            String packageName = pf.getElementName();

                                            ResourceBundleFamily family = getOrCreateFamily(project.getName(),
                                                    pluginId, packageName, baseName);

                                            addBundle(family, getLocale(language, country), jarEntryResource);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Collect non-Java resources
                Object[] nonJavaResources = javaProject.getNonJavaResources();
                ArrayList<IFile> files = new ArrayList<IFile>();
                for (Object resource : nonJavaResources) {
                    if (resource instanceof IContainer) {
                        IContainer container = (IContainer) resource;
                        collectPropertyFiles(container, files);
                    } else if (resource instanceof IFile) {
                        IFile file = (IFile) resource;
                        String name = file.getName();
                        if (isIgnoredFilename(name))
                            continue;
                        if (name.endsWith(PROPERTIES_SUFFIX)) {
                            files.add(file);
                        }
                    }
                }
                for (IFile file : files) {

                    // Convert path to package name format
                    IPath path = file.getProjectRelativePath();
                    String packageName = null;
                    int segmentCount = path.segmentCount();
                    if (segmentCount > 1) {
                        StringBuilder builder = new StringBuilder();
                        for (int i = 0; i < segmentCount - 1; i++) {
                            if (i > 0)
                                builder.append('.');
                            builder.append(path.segment(i));
                        }
                        packageName = builder.toString();
                    }

                    String baseName = getBaseName(file.getName());
                    String language = getLanguage(file.getName());
                    String country = getCountry(file.getName());

                    ResourceBundleFamily family = getOrCreateFamily(project.getName(), pluginId, packageName,
                            baseName);
                    addBundle(family, getLocale(language, country), file);
                }

            }
        } catch (Exception e) {
            MessagesEditorPlugin.log(e);
        }
    }
}

From source file:org.eclipse.pde.ui.tests.imports.ImportAsSourceTestCase.java

License:Open Source License

private boolean checkSourceFolder(IJavaProject jProject) throws JavaModelException {
    IClasspathEntry[] entries = jProject.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        if (entries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE)
            return true;
    }/*from   w  w  w . j a v a  2s. com*/
    return false;
}

From source file:org.eclipse.qvt.declarative.execution.ui.launching.configuration.DeclarativeQVTFileFetcher.java

License:Open Source License

private void collect(List<IJavaProject> javaProjects) {
    for (IJavaProject javaProject : javaProjects) {
        try {/*from   w w w .j  a va 2s .  com*/
            IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
            for (IClasspathEntry classpathEntry : classpathEntries) {
                if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    visit(classpathEntry.getPath());
                }
            }
        } catch (JavaModelException e) {
        }
    }
}

From source file:org.eclipse.qvt.declarative.execution.ui.launching.configuration.DeclarativeQVTMainTab.java

License:Open Source License

private static IFile getASTFile(IFile sourceFile) {
    IJavaProject javaProject = JavaCore.create(sourceFile.getProject());
    IClasspathEntry srcContainer = null;
    IPath currentTransformationPath = sourceFile.getFullPath();
    try {/*from  w  w w . j  a  va2s  .  c  o m*/
        for (IClasspathEntry classpathEntry : javaProject.getRawClasspath()) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath classPathEntryPath = classpathEntry.getPath();
                if (classPathEntryPath.isPrefixOf(currentTransformationPath)) {
                    srcContainer = classpathEntry;
                }
            }
        }
        if (srcContainer != null) {
            IPath relativeTransformationPath = currentTransformationPath
                    .removeFirstSegments(srcContainer.getPath().segmentCount());
            IPath binPath = srcContainer.getOutputLocation();
            IPath ASTPath = binPath.append(relativeTransformationPath).removeFileExtension()
                    .addFileExtension("eqvtrelation");
            IFile ASTFile = getWorkspaceRoot().getFile(ASTPath);
            return ASTFile;
        }

    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:org.eclipse.qvt.declarative.execution.ui.launching.configuration.DeclarativeQVTMainTab.java

License:Open Source License

private static IFile getSourceFile(String executablePath) {
    try {/*  w  w w  .j  ava  2  s  . c  om*/
        IFile executableFile = getFileFromURI(executablePath);
        IJavaProject javaProject = JavaCore.create(executableFile.getProject());
        IPath executableRelativePath = executableFile.getFullPath()
                .removeFirstSegments(javaProject.getOutputLocation().segmentCount());
        IPath sourceRelativePath = executableRelativePath.removeFileExtension().removeFileExtension()
                .addFileExtension("qvtr");
        for (IClasspathEntry classpathEntry : javaProject.getRawClasspath()) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath classPathPath = classpathEntry.getPath();
                IFolder classPathFolder = getWorkspaceRoot().getFolder(classPathPath);
                IFile searchedFile = classPathFolder.getFile(sourceRelativePath);
                if (searchedFile.exists()) {
                    return searchedFile;
                }
            }
        }
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:org.eclipse.qvt.declarative.execution.ui.launching.configuration.DeclarativeQVTMainTab.java

License:Open Source License

private String getTraceabilityMM(String absoluteExecutablePath) {
    IPath path = new Path(absoluteExecutablePath);
    String projectName = path.segment(0);
    IProject project = getWorkspaceRoot().getProject(projectName);
    try {/*from  w  w  w. java 2 s . com*/
        for (IClasspathEntry classpathEntry : JavaCore.create(project).getRawClasspath()) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath sourcePath = classpathEntry.getPath();
                if (sourcePath.isPrefixOf(path)) {
                    IPath sourceFolderRelativePath = path.makeRelativeTo(sourcePath);
                    String traceabilityMetamodelFileName = "T"
                            + sourceFolderRelativePath.removeFileExtension().lastSegment() + ".ecore";
                    IPath result = classpathEntry.getOutputLocation();
                    result = result.append(sourceFolderRelativePath.removeLastSegments(1));
                    result = result.append(traceabilityMetamodelFileName);
                    return URI.createPlatformResourceURI(result.toString(), true).toString();
                }
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:org.eclipse.qvt.declarative.relations.atlvm.ATLVMExecutor.java

License:Open Source License

private static IFile getExecutableFile(IFile sourceFile, String direction) {
    IJavaProject javaProject = JavaCore.create(sourceFile.getProject());
    IClasspathEntry srcContainer = null;
    IPath currentTransformationPath = sourceFile.getFullPath();
    try {//from w  w  w. ja va 2  s. c  om
        for (IClasspathEntry classpathEntry : javaProject.getRawClasspath()) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                IPath classPathEntryPath = classpathEntry.getPath();
                if (classPathEntryPath.isPrefixOf(currentTransformationPath)) {
                    srcContainer = classpathEntry;
                }
            }
        }
        IPath relativeTransformationPath = currentTransformationPath
                .removeFirstSegments(srcContainer.getPath().segmentCount());
        IPath binPath = srcContainer.getOutputLocation();
        IPath relativeExecutablePath = binPath.append(relativeTransformationPath).removeFileExtension()
                .addFileExtension(direction).addFileExtension("asm");
        IFile executableFile = ResourcesPlugin.getWorkspace().getRoot().getFile(relativeExecutablePath);
        return executableFile;
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}