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

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

Introduction

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

Prototype

public static IClasspathEntry newLibraryEntry(IPath path, IPath sourceAttachmentPath,
        IPath sourceAttachmentRootPath, IAccessRule[] accessRules, IClasspathAttribute[] extraAttributes,
        boolean isExported) 

Source Link

Document

Creates and returns a new classpath entry of kind CPE_LIBRARY for the JAR or folder identified by the given absolute path.

Usage

From source file:at.bestsolution.efxclipse.tooling.jdt.core.internal.BuildPathSupport.java

License:Open Source License

public static IClasspathEntry getJavaFXLibraryEntry(IJavaProject project) {
    IPath[] paths = getFxJarPath(project);
    if (paths != null) {

        IPath jarLocationPath = paths[0];
        IPath javadocLocation = paths[1];
        IPath fxSource = paths[3];/*www  .jav  a2  s.c  om*/

        IClasspathAttribute[] attributes;
        IAccessRule[] accessRules = {};
        if (javadocLocation == null || !javadocLocation.toFile().exists()) {
            attributes = new IClasspathAttribute[] { JavaCore.newClasspathAttribute(
                    IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, WEB_JAVADOC_LOCATION) };
        } else {
            attributes = new IClasspathAttribute[] {
                    JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
                            javadocLocation.toFile().toURI().toString()) };
        }

        if (jarLocationPath.toFile().exists()) {
            return JavaCore.newLibraryEntry(jarLocationPath, fxSource, null, accessRules, attributes, false);
        }
    }

    return null;
}

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

License:Open Source License

private IClasspathEntry getEntry(IVMInstall vm, BundleDescription project) {
    IPath[] paths = BuildPathSupport.getFxJarPath(vm);
    if (paths == null) {
        return null;
    } else {// w  w  w. j a v  a 2  s  .  co m
        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 JavaCore.newLibraryEntry(paths[0], null, null, l.toArray(new IAccessRule[0]), extraAttributes,
                false);
    }
}

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   w  w  w  .j a  v  a  2 s  .c om*/
    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:cn.ieclipse.adt.ext.jdt.AormClasspathContainerInitializer.java

License:Apache License

public static IClasspathEntry[] getClasspathEntries() {
    ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    Bundle bundle = AormPlugin.getDefault().getBundle();
    Enumeration<URL> urls = bundle.findEntries(RESOURCE_LIB, "*.jar", false);
    ArrayList<Path> paths = new ArrayList<Path>();
    if (urls != null) {
        while (urls.hasMoreElements()) {
            URL url = (URL) urls.nextElement();
            try {
                url = FileLocator.resolve(url);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();//from w w w  .  ja va2s  .  c o  m
            }
            Path path = new Path(url.getPath());
            paths.add(path);
        }
    }

    Path libPath = getLibPath(AORM_NAME, paths);
    if (libPath != null) {
        // Path docPath = getJarPath(AORM_NAME + ".doc", paths);
        Path sourcePath = getLibPath(AORM_NAME_SOURCE, paths);
        IClasspathEntry entry = JavaCore.newLibraryEntry(libPath, sourcePath, null, null, null, true);
        entries.add(entry);
    }
    return entries.toArray(new IClasspathEntry[entries.size()]);
}

From source file:cn.ieclipse.aorm.eclipse.jdt.AormClasspathContainerInitializer.java

License:Apache License

public static IClasspathEntry[] getClasspathEntries() {
    ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    Bundle bundle = AormPlugin.getDefault().getBundle();
    Enumeration<URL> urls = bundle.findEntries(RESOURCE_LIB, "*.jar", false);
    ArrayList<Path> paths = new ArrayList<Path>();
    if (urls != null) {
        while (urls.hasMoreElements()) {
            URL url = (URL) urls.nextElement();
            try {
                url = FileLocator.resolve(url);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();/*  w  w  w  . ja  v a  2  s. c  om*/
            }
            Path path = new Path(url.getPath());
            paths.add(path);
        }
    }

    Path libPath = getJarPath(AORM_NAME, paths);
    if (libPath != null) {
        Path docPath = getJarPath(AORM_NAME + ".doc", paths);
        Path sourcePath = getJarPath(AORM_NAME + ".source", paths);
        IClasspathEntry entry = JavaCore.newLibraryEntry(libPath, sourcePath, null, null, null, true);

        entries.add(entry);
    }
    return entries.toArray(new IClasspathEntry[entries.size()]);
}

From source file:com.amazonaws.eclipse.android.sdk.classpath.AndroidSdkClasspathContainer.java

License:Open Source License

/**
 * Loads the JDT classpath entries for the AWS SDK for Android from the
 * specified SDK install and returns them as a list.
 *
 * @param sdkInstall//from   w  w w.  jav  a  2  s  . co m
 *            The SDK install from which to load the classpath entries.
 *
 * @return A list of the JDT classpath entries for the AWS SDK for Android
 *         at the specified install base.
 */
private List<IClasspathEntry> loadSdkClasspathEntries(AbstractSdkInstall sdkInstall) {
    List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    IPath sdkJarPath = null, sdkSourceJarPath = null;

    try {
        File sdkJar = sdkInstall.getSdkJar();
        sdkJarPath = project.getLocation().append("libs").append(sdkJar.getName());
    } catch (FileNotFoundException e) {
        // The SDK has been deleted, there's no classpath entries anymore.
        return new ArrayList<IClasspathEntry>();
    }
    IClasspathAttribute externalJavadocPath = JavaCore.newClasspathAttribute(
            IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, sdkInstall.getJavadocURL());
    if (externalJavadocPath.getValue() != null) {
        classpathEntries.add(JavaCore.newLibraryEntry(sdkJarPath, sdkSourceJarPath, null, new IAccessRule[0],
                new IClasspathAttribute[] { externalJavadocPath }, true));
    } else {
        classpathEntries.add(JavaCore.newLibraryEntry(sdkJarPath, sdkSourceJarPath, null, new IAccessRule[0],
                new IClasspathAttribute[0], true));
    }

    for (File jarFile : sdkInstall.getThirdPartyJars()) {
        IPath thirdPartyJarPath = new Path(jarFile.getAbsolutePath());
        classpathEntries.add(JavaCore.newLibraryEntry(thirdPartyJarPath, thirdPartyJarPath, null, true));
    }

    return classpathEntries;
}

From source file:com.amazonaws.eclipse.lambda.project.classpath.LambdaRuntimeClasspathContainer.java

License:Open Source License

private IClasspathEntry loadRuntimeClasspathEntry(LambdaRuntimeLibraryComponent component) {
    IPath classJarPath = new Path(component.getClassJarFile().getAbsolutePath());

    IClasspathAttribute attrs[] = new IClasspathAttribute[0];
    if (component.getJavadocJarFile() != null) {
        attrs = new IClasspathAttribute[] {
                JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
                        constructJavadocLocationAttributeValue(component.getJavadocJarFile())) };
    }/*from   w  w w .  ja v a 2s .  c  om*/

    return JavaCore.newLibraryEntry(classJarPath, null, null, new IAccessRule[0], attrs, true);
}

From source file:com.amazonaws.eclipse.sdk.ui.classpath.AwsClasspathContainer.java

License:Open Source License

/**
 * Loads the JDT classpath entries for the AWS SDK for Java from the specified
 * SDK install and returns them as a list.
 *
 * @param sdkInstall//from  w  ww.j ava  2  s .  c o m
 *            The SDK install from which to load the classpath entries.
 *
 * @return A list of the JDT classpath entries for the AWS SDK for Java at the
 *         specified install base.
 */
private List<IClasspathEntry> loadSdkClasspathEntries(SdkInstall sdkInstall) {
    List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    IPath sdkJarPath, sdkSourceJarPath;

    try {
        sdkJarPath = new Path(sdkInstall.getSdkJar().getAbsolutePath());
        sdkSourceJarPath = new Path(sdkInstall.getSdkSourceJar().getAbsolutePath());
    } catch (FileNotFoundException e) {
        // The SDK has been deleted, there's no classpath entries anymore.
        return new ArrayList<IClasspathEntry>();
    }
    IClasspathAttribute externalJavadocPath = JavaCore.newClasspathAttribute(
            IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, sdkInstall.getJavadocURL());
    if (externalJavadocPath.getValue() != null) {
        classpathEntries.add(JavaCore.newLibraryEntry(sdkJarPath, sdkSourceJarPath, null, new IAccessRule[0],
                new IClasspathAttribute[] { externalJavadocPath }, true));
    } else {
        classpathEntries.add(JavaCore.newLibraryEntry(sdkJarPath, sdkSourceJarPath, null, new IAccessRule[0],
                new IClasspathAttribute[0], true));
    }

    for (File jarFile : sdkInstall.getThirdPartyJars()) {
        IPath thirdPartyJarPath = new Path(jarFile.getAbsolutePath());
        classpathEntries.add(JavaCore.newLibraryEntry(thirdPartyJarPath, thirdPartyJarPath, null, true));
    }

    return classpathEntries;
}

From source file:com.android.ide.eclipse.adt.internal.project.AndroidClasspathContainerInitializer.java

License:Open Source License

/**
 * Generates an array of {@link IClasspathEntry} from a set of paths.
 * @see #getTargetPaths(IAndroidTarget)/*from w w w  .  j a  v a 2  s . co  m*/
 */
private static IClasspathEntry[] createClasspathEntriesFromPaths(String[] paths, IAndroidTarget target) {
    ArrayList<IClasspathEntry> list = new ArrayList<IClasspathEntry>();

    // First, we create the IClasspathEntry for the framework.
    // now add the android framework to the class path.
    // create the path object.
    IPath androidLib = new Path(paths[CACHE_INDEX_JAR]);

    IPath androidSrc = null;
    String androidSrcOsPath = null;
    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    if (target != null) {
        androidSrcOsPath = ProjectHelper.loadStringProperty(root, getAndroidSourceProperty(target));
    }
    if (androidSrcOsPath != null && androidSrcOsPath.trim().length() > 0) {
        androidSrc = new Path(androidSrcOsPath);
    }
    if (androidSrc == null) {
        androidSrc = new Path(paths[CACHE_INDEX_SRC]);
        File androidSrcFile = new File(paths[CACHE_INDEX_SRC]);
        if (!androidSrcFile.isDirectory()) {
            androidSrc = null;
        }
    }

    if (androidSrc == null && target != null) {
        Bundle bundle = getSourceBundle();

        if (bundle != null) {
            AndroidVersion version = target.getVersion();
            String apiString = version.getApiString();
            String sourcePath = apiString + SOURCES_ZIP;
            URL sourceURL = bundle.getEntry(sourcePath);
            if (sourceURL != null) {
                URL url = null;
                try {
                    url = FileLocator.resolve(sourceURL);
                } catch (IOException ignore) {
                }
                if (url != null) {
                    androidSrcOsPath = url.getFile();
                    if (new File(androidSrcOsPath).isFile()) {
                        androidSrc = new Path(androidSrcOsPath);
                    }
                }
            }
        }
    }

    // create the java doc link.
    String androidApiURL = ProjectHelper.loadStringProperty(root, PROPERTY_ANDROID_API);
    String apiURL = null;
    if (androidApiURL != null && testURL(androidApiURL)) {
        apiURL = androidApiURL;
    } else {
        if (testURL(paths[CACHE_INDEX_DOCS_URI])) {
            apiURL = paths[CACHE_INDEX_DOCS_URI];
        } else if (testURL(ANDROID_API_REFERENCE)) {
            apiURL = ANDROID_API_REFERENCE;
        }
    }

    IClasspathAttribute[] attributes = null;
    if (apiURL != null && !NULL_API_URL.equals(apiURL)) {
        IClasspathAttribute cpAttribute = JavaCore
                .newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, apiURL);
        attributes = new IClasspathAttribute[] { cpAttribute };
    }
    // create the access rule to restrict access to classes in
    // com.android.internal
    IAccessRule accessRule = JavaCore.newAccessRule(new Path("com/android/internal/**"), //$NON-NLS-1$
            IAccessRule.K_NON_ACCESSIBLE);

    IClasspathEntry frameworkClasspathEntry = JavaCore.newLibraryEntry(androidLib, androidSrc, // source attachment path
            null, // default source attachment root path.
            new IAccessRule[] { accessRule }, attributes, false // not exported.
    );

    list.add(frameworkClasspathEntry);

    // now deal with optional libraries
    if (paths.length >= 5) {
        String docPath = paths[CACHE_INDEX_OPT_DOCS_URI];
        int i = 4;
        while (i < paths.length) {
            Path jarPath = new Path(paths[i++]);

            attributes = null;
            if (docPath.length() > 0) {
                attributes = new IClasspathAttribute[] { JavaCore
                        .newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, docPath) };
            }

            IClasspathEntry entry = JavaCore.newLibraryEntry(jarPath, null, // source attachment path
                    null, // default source attachment root path.
                    null, attributes, false // not exported.
            );
            list.add(entry);
        }
    }

    if (apiURL != null) {
        ProjectHelper.saveStringProperty(root, PROPERTY_ANDROID_API, apiURL);
    }
    if (androidSrc != null && target != null) {
        ProjectHelper.saveStringProperty(root, getAndroidSourceProperty(target), androidSrc.toOSString());
    }
    return list.toArray(new IClasspathEntry[list.size()]);
}

From source file:com.android.ide.eclipse.adt.internal.project.LibraryClasspathContainerInitializer.java

License:Open Source License

private static List<IClasspathEntry> convertJarsToClasspathEntries(final IProject iProject,
        Set<File> jarFiles) {
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>(jarFiles.size());

    // and process the jar files list, but first sanitize it to remove dups.
    JarListSanitizer sanitizer = new JarListSanitizer(
            iProject.getFolder(SdkConstants.FD_OUTPUT).getLocation().toFile(),
            new AndroidPrintStream(iProject, null /*prefix*/, AdtPlugin.getOutStream()));

    String errorMessage = null;/* www. j a  v  a2 s.  c  o  m*/

    try {
        List<File> sanitizedList = sanitizer.sanitize(jarFiles);

        for (File jarFile : sanitizedList) {
            if (jarFile instanceof CPEFile) {
                CPEFile cpeFile = (CPEFile) jarFile;
                IClasspathEntry e = cpeFile.getClasspathEntry();

                entries.add(JavaCore.newLibraryEntry(e.getPath(), e.getSourceAttachmentPath(),
                        e.getSourceAttachmentRootPath(), e.getAccessRules(), e.getExtraAttributes(),
                        true /*isExported*/));
            } else {
                String jarPath = jarFile.getAbsolutePath();

                IPath sourceAttachmentPath = null;
                IClasspathAttribute javaDocAttribute = null;

                File jarProperties = new File(jarPath + DOT_PROPERTIES);
                if (jarProperties.isFile()) {
                    Properties p = new Properties();
                    InputStream is = null;
                    try {
                        p.load(is = new FileInputStream(jarProperties));

                        String value = p.getProperty(ATTR_SRC);
                        if (value != null) {
                            File srcPath = getFile(jarFile, value);

                            if (srcPath.exists()) {
                                sourceAttachmentPath = new Path(srcPath.getAbsolutePath());
                            }
                        }

                        value = p.getProperty(ATTR_DOC);
                        if (value != null) {
                            File docPath = getFile(jarFile, value);
                            if (docPath.exists()) {
                                try {
                                    javaDocAttribute = JavaCore.newClasspathAttribute(
                                            IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME,
                                            docPath.toURI().toURL().toString());
                                } catch (MalformedURLException e) {
                                    AdtPlugin.log(e, "Failed to process 'doc' attribute for %s",
                                            jarProperties.getAbsolutePath());
                                }
                            }
                        }

                    } catch (FileNotFoundException e) {
                        // shouldn't happen since we check upfront
                    } catch (IOException e) {
                        AdtPlugin.log(e, "Failed to read %s", jarProperties.getAbsolutePath());
                    } finally {
                        if (is != null) {
                            try {
                                is.close();
                            } catch (IOException e) {
                                // ignore
                            }
                        }
                    }
                }

                if (javaDocAttribute != null) {
                    entries.add(JavaCore.newLibraryEntry(new Path(jarPath), sourceAttachmentPath,
                            null /*sourceAttachmentRootPath*/, new IAccessRule[0],
                            new IClasspathAttribute[] { javaDocAttribute }, true /*isExported*/));
                } else {
                    entries.add(JavaCore.newLibraryEntry(new Path(jarPath), sourceAttachmentPath,
                            null /*sourceAttachmentRootPath*/, true /*isExported*/));
                }
            }
        }
    } catch (DifferentLibException e) {
        errorMessage = e.getMessage();
        AdtPlugin.printErrorToConsole(iProject, (Object[]) e.getDetails());
    } catch (Sha1Exception e) {
        errorMessage = e.getMessage();
    }

    processError(iProject, errorMessage, AdtConstants.MARKER_DEPENDENCY, true /*outputToConsole*/);

    return entries;
}