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

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

Introduction

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

Prototype

IClasspathAttribute[] getExtraAttributes();

Source Link

Document

Returns the extra classpath attributes for this classpath entry.

Usage

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
 * Firstly, add library to the Java build path if it's not there already,
 * then mark the entry as being on the aspect path
 * @param project/* w w  w .  j av a2 s. c  om*/
 * @param path
 */
private static void addAttribute(IProject project, String jarPath, int eKind, IClasspathAttribute attribute) {
    IJavaProject jp = JavaCore.create(project);

    try {
        IClasspathEntry[] cp = jp.getRawClasspath();
        int cpIndex = getIndexInBuildPathEntry(cp, jarPath);
        if (cpIndex >= 0) { // already on classpath
            // add attribute to classpath entry
            // if it doesn't already exist
            IClasspathEntry pathAdd = cp[cpIndex];
            // only add attribute if this element is not already on the path
            if (isAspectPathAttribute(attribute) ? !isOnAspectpath(pathAdd) : !isOnInpath(pathAdd)) {
                IClasspathAttribute[] attributes = pathAdd.getExtraAttributes();
                IClasspathAttribute[] newattrib = new IClasspathAttribute[attributes.length + 1];
                System.arraycopy(attributes, 0, newattrib, 0, attributes.length);
                newattrib[attributes.length] = attribute;
                switch (pathAdd.getEntryKind()) {
                case IClasspathEntry.CPE_LIBRARY:
                    pathAdd = JavaCore.newLibraryEntry(pathAdd.getPath(), pathAdd.getSourceAttachmentPath(),
                            pathAdd.getSourceAttachmentRootPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_VARIABLE:
                    pathAdd = JavaCore.newVariableEntry(pathAdd.getPath(), pathAdd.getSourceAttachmentPath(),
                            pathAdd.getSourceAttachmentRootPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_CONTAINER:
                    pathAdd = JavaCore.newContainerEntry(pathAdd.getPath(), pathAdd.getAccessRules(), newattrib,
                            pathAdd.isExported());
                    break;

                case IClasspathEntry.CPE_PROJECT:
                    pathAdd = JavaCore.newProjectEntry(pathAdd.getPath(), pathAdd.getAccessRules(), true,
                            newattrib, pathAdd.isExported());
                    break;
                }

                cp[cpIndex] = pathAdd;
                jp.setRawClasspath(cp, null);
            }
        } else {
            addEntryToJavaBuildPath(jp, attribute, jarPath, eKind);
        }
    } catch (JavaModelException e) {
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

public static String getRestriction(IClasspathEntry pathEntry, String attributeName) {
    IClasspathAttribute[] attributes = pathEntry.getExtraAttributes();
    for (int i = 0; i < attributes.length; i++) {
        IClasspathAttribute attribute = attributes[i];
        if (attribute.getName().equals(attributeName)) {
            String extraStr = attribute.getValue();
            if (extraStr != null) {
                return extraStr;
            }/*www  . j av a2 s.c  o  m*/
        }
    }

    return null;
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
 * adds the classpath attribute to the entry with the default value if it doesn't already exist
 * else does nothing/*  w  ww .  j a va 2 s  .  c  om*/
 */
public static IClasspathEntry ensureHasAttribute(IClasspathEntry curr, String attributeName,
        String defaultVal) {
    int index = indexOfAttribute(curr.getExtraAttributes(), attributeName);
    if (index < 0) {
        IClasspathAttribute[] attrs = curr.getExtraAttributes();
        // must create a new entry with more extra attributes
        IClasspathAttribute newAttr = JavaCore.newClasspathAttribute(attributeName, defaultVal);
        IClasspathAttribute[] newAttrs;
        if (attrs == null || attrs.length == 0) {
            newAttrs = new IClasspathAttribute[] { newAttr };
        } else {
            newAttrs = new IClasspathAttribute[attrs.length + 1];
            System.arraycopy(attrs, 0, newAttrs, 0, attrs.length);
            newAttrs[attrs.length] = newAttr;
        }
        return copyContainerEntry(curr, newAttrs);
    } else {
        return curr;
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
 * adds the classpath attribute to the entry with the default value if it doesn't already exist
 * else does nothing/*from w w w .  j  av  a 2  s . c  om*/
 */
public static IClasspathEntry ensureHasNoAttribute(IClasspathEntry curr, String attributeName) {
    int index = indexOfAttribute(curr.getExtraAttributes(), attributeName);
    if (index < 0) {
        return curr;
    } else {
        IClasspathAttribute[] attrs = curr.getExtraAttributes();
        // must create a new entry with more extra attributes
        IClasspathAttribute[] newAttrs = new IClasspathAttribute[attrs.length - 1];
        for (int i = 0, j = 0; i < newAttrs.length; i++) {
            if (i != index) {
                newAttrs[j] = attrs[i];
                j++;
            }
        }
        return copyContainerEntry(curr, newAttrs);
    }
}

From source file:org.eclipse.ajdt.core.AspectJCorePreferences.java

License:Open Source License

/**
 * Adds the classpath restriction to the given classpath entry.
 * Returns the new classpath entry/*  w w w  . j  av  a  2s . c om*/
 */
public static IClasspathEntry updatePathRestrictions(IClasspathEntry entry, String restrictionStr,
        String restrictionKind) {
    IClasspathAttribute[] attrs = entry.getExtraAttributes();
    int index = indexOfAttribute(attrs, restrictionKind);
    IClasspathAttribute newAttr = JavaCore.newClasspathAttribute(restrictionKind, restrictionStr);
    if (index >= 0) {
        // just replace
        attrs[index] = newAttr;
    } else {
        // must create a new entry with more extra attributes
        IClasspathAttribute[] newAttrs;
        if (attrs == null || attrs.length == 0) {
            newAttrs = new IClasspathAttribute[] { newAttr };
        } else {
            newAttrs = new IClasspathAttribute[attrs.length + 1];
            System.arraycopy(attrs, 0, newAttrs, 0, attrs.length);
            newAttrs[attrs.length] = newAttr;
        }
        entry = copyContainerEntry(entry, newAttrs);
    }
    return entry;
}

From source file:org.eclipse.ajdt.internal.ui.AspectJProjectPropertiesPage.java

License:Open Source License

private void addContainerToAttribute(IClasspathEntry classpathEntry, IClasspathAttribute attribute,
        IClasspathContainer container) {
    // find the attribute
    IClasspathAttribute[] attributes = classpathEntry.getExtraAttributes();
    for (int i = 0; i < attributes.length; i++) {
        if (attributes[i].getName().equals(attribute.getName())) {
            attributes[i] = new ClasspathAttribute(attribute.getName(), container.getPath().toPortableString());
        }//  w w  w  .ja va 2s. c o m
    }
}

From source file:org.eclipse.ajdt.internal.ui.AspectJProjectPropertiesPage.java

License:Open Source License

private boolean hasClasspathAttribute(IClasspathEntry entry, IClasspathAttribute attribute) {
    IClasspathAttribute[] allAttributes = entry.getExtraAttributes();
    for (int i = 0; i < allAttributes.length; i++) {
        if (allAttributes[i].getName().equals(attribute.getName())) {
            return true;
        }/*from  ww w  .j a  va2s. co m*/
    }
    return false;
}

From source file:org.eclipse.ajdt.internal.ui.wizards.PathBlock.java

License:Open Source License

private IClasspathContainer getClasspathContainer(IClasspathEntry classpathEntry) {
    IClasspathAttribute[] attributes = classpathEntry.getExtraAttributes();
    for (int i = 0; i < attributes.length; i++) {
        if (AspectJCorePreferences.isAspectPathAttribute(attributes[i])
                || AspectJCorePreferences.isInPathAttribute(attributes[i])) {
            // check to make sure this isn't the standard attribute, but one that is
            // enhanced with the container it came from.
            // When these attributes were created, the value was set to be the classpath container it came from
            if (attributes[i].getValue() != null && !attributes[i].getValue().equals(attributes[i].getName())) {
                try {
                    return JavaCore.getClasspathContainer(new Path(attributes[i].getValue()), fCurrJProject);
                } catch (JavaModelException e) {
                }//from w  ww .j  a v a  2s.  c o m
            }
        }
    }
    return null;
}

From source file:org.eclipse.andmore.internal.project.AndroidClasspathContainerInitializer.java

License:Open Source License

@Override
public void requestClasspathContainerUpdate(IPath containerPath, IJavaProject project,
        IClasspathContainer containerSuggestion) throws CoreException {
    AndmoreAndroidPlugin plugin = AndmoreAndroidPlugin.getDefault();

    synchronized (Sdk.getLock()) {
        boolean sdkIsLoaded = plugin.getSdkLoadStatus() == LoadStatus.LOADED;

        // check if the project has a valid target.
        IAndroidTarget target = null;//from  w  w  w .  j a  va 2 s  .  c  o m
        if (sdkIsLoaded) {
            target = Sdk.getCurrent().getTarget(project.getProject());
        }
        if (sdkIsLoaded && target != null) {
            String[] paths = getTargetPaths(target);
            IPath android_lib = new Path(paths[CACHE_INDEX_JAR]);
            IClasspathEntry[] entries = containerSuggestion.getClasspathEntries();
            for (int i = 0; i < entries.length; i++) {
                IClasspathEntry entry = entries[i];
                if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                    IPath entryPath = entry.getPath();

                    if (entryPath != null) {
                        if (entryPath.equals(android_lib)) {
                            IPath entrySrcPath = entry.getSourceAttachmentPath();
                            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                            if (entrySrcPath != null) {
                                ProjectHelper.saveStringProperty(root, getAndroidSourceProperty(target),
                                        entrySrcPath.toString());
                            } else {
                                ProjectHelper.saveStringProperty(root, getAndroidSourceProperty(target), null);
                            }
                            IClasspathAttribute[] extraAttributtes = entry.getExtraAttributes();
                            if (extraAttributtes.length == 0) {
                                ProjectHelper.saveStringProperty(root, PROPERTY_ANDROID_API, NULL_API_URL);
                            }
                            for (int j = 0; j < extraAttributtes.length; j++) {
                                IClasspathAttribute extraAttribute = extraAttributtes[j];
                                String value = extraAttribute.getValue();
                                if ((value == null || value.trim().length() == 0)
                                        && IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME
                                                .equals(extraAttribute.getName())) {
                                    value = NULL_API_URL;
                                }
                                if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME
                                        .equals(extraAttribute.getName())) {
                                    ProjectHelper.saveStringProperty(root, PROPERTY_ANDROID_API, value);

                                }
                            }
                        }
                    }
                }
            }
            rebindClasspathEntries(project.getJavaModel(), containerPath);
        }
    }
}

From source file:org.eclipse.andmore.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*/, AndmoreAndroidPlugin.getOutStream()));

    String errorMessage = null;/*w  w  w  .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) {
                                    AndmoreAndroidPlugin.log(e, "Failed to process 'doc' attribute for %s",
                                            jarProperties.getAbsolutePath());
                                }
                            }
                        }

                    } catch (FileNotFoundException e) {
                        // shouldn't happen since we check upfront
                    } catch (IOException e) {
                        AndmoreAndroidPlugin.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();
        AndmoreAndroidPlugin.printErrorToConsole(iProject, (Object[]) e.getDetails());
    } catch (Sha1Exception e) {
        errorMessage = e.getMessage();
    }

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

    return entries;
}