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

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

Introduction

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

Prototype

int CPE_PROJECT

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

Click Source Link

Document

Entry kind constant describing a classpath entry identifying a required project.

Usage

From source file:apet.utils.ClasspathUtils.java

License:Open Source License

/**
 * Returns the Eclipse Project class loader using project build path
 * @param javaProject Eclipse Java Project
 * @return A class loader using project build path
 * @throws Exception If the project is no valid
 */// w w  w .  jav  a 2s  . c  o  m
public static ClassLoader getProjectClassLoader(IJavaProject javaProject) throws Exception {
    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    String wsPath = javaProject.getProject().getLocation().toPortableString();
    String firstEntryLocation = javaProject.getPath().toPortableString();
    int idx = wsPath.indexOf(firstEntryLocation);

    URL[] urls = null;
    int i = 0;

    //System.out.println("ClassLoader " + wsPath);
    //System.out.println("ClassLoader " + firstEntryLocation);

    String output = javaProject.getOutputLocation().toPortableString();
    urls = new URL[entries.length + 1];

    if (idx != -1) {
        wsPath = wsPath.substring(0, idx);
    } else {
        output = output.substring(firstEntryLocation.length());
    }
    urls[i++] = new File(wsPath + output).toURL();

    //System.out.println("ClassLoader " + output);

    String fullPath = null;

    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IResource project = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            String projectPath = JavaCore.create(project.getProject()).getOutputLocation().toPortableString();
            fullPath = wsPath + projectPath;
        } else {
            Object resource = JavaModel.getTarget(entry.getPath(), true);

            if (resource instanceof IResource) {
                IResource iFile = (IResource) resource;
                fullPath = iFile.getLocation().toPortableString();
            } else if (resource instanceof File) {
                File file = (File) resource;
                fullPath = file.getAbsolutePath();
            }
        }

        urls[i++] = new File(fullPath).toURL();
        //System.out.println(fullPath);
    }

    URLClassLoader classLoader = new URLClassLoader(urls, String.class.getClassLoader());

    /*for (int j = 0; j < urls.length; j ++) {
       System.out.println(urls[j]);
    }*/

    return classLoader;
}

From source file:apet.utils.ClasspathUtils.java

License:Open Source License

/**
 * Returns a String with all libraries defined in build path
 * @param javaProject Eclipse Java Project
 * @return a String with all libraries defined in build path
 * @throws Exception If the project is no valid
 *//*from  w  w  w  .ja va2s.  com*/
public static String getStringClasspath(IJavaProject javaProject) throws Exception {

    IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    String wsPath = javaProject.getProject().getLocation().toPortableString();
    String firstEntryLocation = javaProject.getPath().toPortableString();
    int idx = wsPath.indexOf(firstEntryLocation);

    String[] urls = new String[entries.length + 1];
    String fullPath = null;
    int i = 0;
    String output = javaProject.getOutputLocation().toPortableString();

    if (idx != -1) {
        wsPath = wsPath.substring(0, idx);
    } else {
        output = output.substring(firstEntryLocation.length());
    }
    urls[i++] = wsPath + output;

    for (IClasspathEntry entry : entries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            IResource project = ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath());
            String projectPath = JavaCore.create(project.getProject()).getOutputLocation().toPortableString();
            fullPath = wsPath + projectPath;
        } else {
            Object resource = JavaModel.getTarget(entry.getPath(), true);

            if (resource instanceof IResource) {
                IResource iFile = (IResource) resource;
                fullPath = iFile.getLocation().toPortableString();
            } else if (resource instanceof File) {
                File file = (File) resource;
                fullPath = file.getAbsolutePath();
            }
        }

        urls[i++] = fullPath;
    }

    StringBuffer buffer = new StringBuffer();
    for (String url : urls) {
        buffer.append(url);
        buffer.append(":");
    }

    return buffer.toString().substring(0, buffer.length() - 1);

}

From source file:at.bestsolution.efxclipse.robovm.RobovmSetupHandler.java

License:Open Source License

private void resolveDataProject(IJavaProject project, Set<IPath> listProjectSourceDirs,
        Set<IPath> listRefProjectSourceDirs, Set<IPath> listRefLibraries) {
    try {//from w w  w .j  a  va2 s . co m
        IClasspathEntry[] entries = project.getRawClasspath();
        for (IClasspathEntry e : entries) {
            if (e.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(e.getPath().lastSegment());
                if (p.exists()) {
                    resolveDataProject(JavaCore.create(p), listRefProjectSourceDirs, listRefProjectSourceDirs,
                            listRefLibraries);
                }
            } else if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                listRefLibraries.add(e.getPath());
            } else if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                listProjectSourceDirs.add(e.getPath());
            } else if (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                String start = e.getPath().segment(0);
                // TODO remove hard coded strings
                if (!"org.eclipse.jdt.launching.JRE_CONTAINER".equals(start)
                        && !"org.eclipse.fx.ide.jdt.core.JAVAFX_CONTAINER".equals(start)) {
                    IClasspathContainer cpe = JavaCore.getClasspathContainer(e.getPath(), project);
                    IClasspathEntry[] cpEntries = cpe.getClasspathEntries();
                    for (IClasspathEntry tmp : cpEntries) {
                        if (tmp.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                            listRefLibraries.add(tmp.getPath());
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:at.bestsolution.efxclipse.tooling.jdt.ui.internal.handler.AbstractAntHandler.java

License:Open Source License

private void resolveDataProject(IJavaProject project, Set<IPath> listProjectSourceDirs,
        Set<IPath> listRefProjectSourceDirs, Set<IPath> listRefLibraries) {
    try {/*from  w w w .j  a va  2s. c  o m*/
        IClasspathEntry[] entries = project.getRawClasspath();
        for (IClasspathEntry e : entries) {
            if (e.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(e.getPath().lastSegment());
                if (p.exists()) {
                    resolveDataProject(JavaCore.create(p), listRefProjectSourceDirs, listRefProjectSourceDirs,
                            listRefLibraries);
                }
            } else if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                listRefLibraries.add(e.getPath());
            } else if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                listProjectSourceDirs.add(e.getPath());
            } else if (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                String start = e.getPath().segment(0);
                // TODO remove hard coded strings
                if (!"org.eclipse.jdt.launching.JRE_CONTAINER".equals(start)
                        && !"at.bestsolution.efxclipse.tooling.jdt.core.JAVAFX_CONTAINER".equals(start)) {
                    IClasspathContainer cpe = JavaCore.getClasspathContainer(e.getPath(), project);
                    IClasspathEntry[] cpEntries = cpe.getClasspathEntries();
                    for (IClasspathEntry tmp : cpEntries) {
                        if (tmp.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                            listRefLibraries.add(tmp.getPath());
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:at.bestsolution.efxclipse.tooling.ui.preview.LivePreviewSynchronizer.java

License:Open Source License

private void resolveDataProject(IJavaProject project, Set<IPath> outputPath, Set<IPath> listRefLibraries) {
    try {/*from ww  w  . jav  a 2s .  c o  m*/
        IClasspathEntry[] entries = project.getRawClasspath();
        outputPath.add(project.getOutputLocation());
        for (IClasspathEntry e : entries) {
            if (e.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(e.getPath().lastSegment());
                if (p.exists()) {
                    resolveDataProject(JavaCore.create(p), outputPath, listRefLibraries);
                }
            } else if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                listRefLibraries.add(e.getPath());
            } else if ("org.eclipse.pde.core.requiredPlugins".equals(e.getPath().toString())) {
                IClasspathContainer cpContainer = JavaCore.getClasspathContainer(e.getPath(), project);
                for (IClasspathEntry cpEntry : cpContainer.getClasspathEntries()) {
                    if (cpEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                        IProject p = ResourcesPlugin.getWorkspace().getRoot()
                                .getProject(cpEntry.getPath().lastSegment());
                        if (p.exists()) {
                            resolveDataProject(JavaCore.create(p), outputPath, listRefLibraries);
                        }
                    } else if (cpEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                        listRefLibraries.add(cpEntry.getPath());
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

License:Open Source License

public CPListElement(Object parent, IJavaProject project, int entryKind, IPath path, boolean newElement,
        IResource res, IPath linkTarget) {
    fProject = project;/*from ww  w  .j av a 2 s . c om*/

    fEntryKind = entryKind;
    fPath = path;
    fOrginalPath = newElement ? null : path;
    fLinkTarget = linkTarget;
    fOrginalLinkTarget = linkTarget;
    fChildren = new ArrayList<Object>();
    fResource = res;
    fIsExported = false;

    fIsMissing = false;
    fCachedEntry = null;
    fParentContainer = parent;

    switch (entryKind) {
    case IClasspathEntry.CPE_SOURCE:
        createAttributeElement(OUTPUT, null, true);
        createAttributeElement(INCLUSION, new Path[0], true);
        createAttributeElement(EXCLUSION, new Path[0], true);
        createAttributeElement(NATIVE_LIB_PATH, null, false);
        createAttributeElement(IGNORE_OPTIONAL_PROBLEMS, null, false);
        break;
    case IClasspathEntry.CPE_LIBRARY:
    case IClasspathEntry.CPE_VARIABLE:
        createAttributeElement(SOURCEATTACHMENT, null, true);
        createAttributeElement(JAVADOC, null, false);
        createAttributeElement(SOURCE_ATTACHMENT_ENCODING, null, false);
        createAttributeElement(NATIVE_LIB_PATH, null, false);
        createAttributeElement(ACCESSRULES, new IAccessRule[0], true);
        break;
    case IClasspathEntry.CPE_PROJECT:
        createAttributeElement(ACCESSRULES, new IAccessRule[0], true);
        createAttributeElement(COMBINE_ACCESSRULES, Boolean.FALSE, true); // not rendered
        createAttributeElement(NATIVE_LIB_PATH, null, false);
        break;
    case IClasspathEntry.CPE_CONTAINER:
        createAttributeElement(ACCESSRULES, new IAccessRule[0], true);
        try {
            IClasspathContainer container = JavaCore.getClasspathContainer(fPath, fProject);
            if (container != null) {
                IClasspathEntry[] entries = container.getClasspathEntries();
                if (entries != null) { // catch invalid container implementation
                    for (int i = 0; i < entries.length; i++) {
                        IClasspathEntry entry = entries[i];
                        if (entry != null) {
                            CPListElement curr = createFromExisting(this, entry, fProject);
                            fChildren.add(curr);
                        } else {
                            JavaPlugin.logErrorMessage("Null entry in container '" + fPath + "'"); //$NON-NLS-1$//$NON-NLS-2$
                        }
                    }
                } else {
                    JavaPlugin.logErrorMessage("container returns null as entries: '" + fPath + "'"); //$NON-NLS-1$//$NON-NLS-2$
                }
            }
        } catch (JavaModelException e) {
        }
        createAttributeElement(NATIVE_LIB_PATH, null, false);
        break;
    default:
    }
}

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);//from www.  j  a v  a2 s  .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:at.bestsolution.javafx.ide.jdt.internal.jdt.CPListElement.java

License:Open Source License

public Object[] getChildren(boolean hideOutputFolder) {
    if (hideOutputFolder && fEntryKind == IClasspathEntry.CPE_SOURCE) {
        return getFilteredChildren(new String[] { OUTPUT });
    }//from  www  .  j  a va2  s . com
    /*if (isInContainer(JavaRuntime.JRE_CONTAINER)) {
       return getFilteredChildren(new String[] { COMBINE_ACCESSRULES, NATIVE_LIB_PATH });
    }*/
    if (fEntryKind == IClasspathEntry.CPE_PROJECT) {
        return getFilteredChildren(new String[] { COMBINE_ACCESSRULES });
    }
    return getFilteredChildren(new String[0]);
}

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

License:Open Source License

public static CPListElement create(Object parent, IClasspathEntry curr, boolean newElement,
        IJavaProject project) {/*from   w  w  w .  j  av a  2  s.com*/
    IPath path = curr.getPath();
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    // get the resource
    IResource res = null;
    boolean isMissing = false;
    IPath linkTarget = null;

    switch (curr.getEntryKind()) {
    case IClasspathEntry.CPE_CONTAINER:
        try {
            isMissing = project != null && (JavaCore.getClasspathContainer(path, project) == null);
        } catch (JavaModelException e) {
            isMissing = true;
        }
        break;
    case IClasspathEntry.CPE_VARIABLE:
        IPath resolvedPath = JavaCore.getResolvedVariablePath(path);
        isMissing = root.findMember(resolvedPath) == null && !resolvedPath.toFile().exists();
        break;
    case IClasspathEntry.CPE_LIBRARY:
        res = root.findMember(path);
        if (res == null) {
            if (!ArchiveFileFilter.isArchivePath(path, true)) {
                if (root.getWorkspace().validatePath(path.toString(), IResource.FOLDER).isOK()
                        && root.getProject(path.segment(0)).exists()) {
                    res = root.getFolder(path);
                }
            }

            IPath rawPath = path;
            if (project != null) {
                IPackageFragmentRoot[] roots = project.findPackageFragmentRoots(curr);
                if (roots.length == 1)
                    rawPath = roots[0].getPath();
            }
            isMissing = !rawPath.toFile().exists(); // look for external JARs and folders
        } else if (res.isLinked()) {
            linkTarget = res.getLocation();
        }
        break;
    case IClasspathEntry.CPE_SOURCE:
        path = path.removeTrailingSeparator();
        res = root.findMember(path);
        if (res == null) {
            if (root.getWorkspace().validatePath(path.toString(), IResource.FOLDER).isOK()) {
                res = root.getFolder(path);
            }
            isMissing = true;
        } else if (res.isLinked()) {
            linkTarget = res.getLocation();
        }
        break;
    case IClasspathEntry.CPE_PROJECT:
        res = root.findMember(path);
        isMissing = (res == null);
        break;
    }
    CPListElement elem = new CPListElement(parent, project, curr.getEntryKind(), path, newElement, res,
            linkTarget);
    elem.setExported(curr.isExported());
    elem.setAttribute(SOURCEATTACHMENT, curr.getSourceAttachmentPath());
    elem.setAttribute(OUTPUT, curr.getOutputLocation());
    elem.setAttribute(EXCLUSION, curr.getExclusionPatterns());
    elem.setAttribute(INCLUSION, curr.getInclusionPatterns());
    elem.setAttribute(ACCESSRULES, curr.getAccessRules());
    elem.setAttribute(COMBINE_ACCESSRULES, new Boolean(curr.combineAccessRules()));

    IClasspathAttribute[] extraAttributes = curr.getExtraAttributes();
    for (int i = 0; i < extraAttributes.length; i++) {
        IClasspathAttribute attrib = extraAttributes[i];
        CPListElementAttribute attribElem = elem.findAttributeElement(attrib.getName());
        if (attribElem == null) {
            elem.createAttributeElement(attrib.getName(), attrib.getValue(), false);
        } else {
            attribElem.setValue(attrib.getValue());
        }
    }

    elem.setIsMissing(isMissing);
    return elem;
}

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

License:Open Source License

public static void insert(CPListElement element, List<CPListElement> cpList) {
    int length = cpList.size();
    CPListElement[] elements = cpList.toArray(new CPListElement[length]);
    int i = 0;/*from  w  w w .  j  a v a  2 s  .  c  o  m*/
    while (i < length && elements[i].getEntryKind() != element.getEntryKind()) {
        i++;
    }
    if (i < length) {
        i++;
        while (i < length && elements[i].getEntryKind() == element.getEntryKind()) {
            i++;
        }
        cpList.add(i, element);
        return;
    }

    switch (element.getEntryKind()) {
    case IClasspathEntry.CPE_SOURCE:
        cpList.add(0, element);
        break;
    case IClasspathEntry.CPE_CONTAINER:
    case IClasspathEntry.CPE_LIBRARY:
    case IClasspathEntry.CPE_PROJECT:
    case IClasspathEntry.CPE_VARIABLE:
    default:
        cpList.add(element);
        break;
    }
}