protected void updateContainerAttributes(
     IProject project,
     IClasspathAttribute attributeToAdd,
     String attributeToDelete,
     IProgressMonitor monitor)
     throws JavaModelException {
   IJavaProject javaProject = JavaCore.create(project);
   if (javaProject == null) return;
   IClasspathEntry[] cp = javaProject.getRawClasspath();
   for (int i = 0; i < cp.length; i++) {
     if (IClasspathEntry.CPE_CONTAINER == cp[i].getEntryKind()
         && BuildPathManager.isMaven2ClasspathContainer(cp[i].getPath())) {
       LinkedHashMap<String, IClasspathAttribute> attrs =
           new LinkedHashMap<String, IClasspathAttribute>();
       for (IClasspathAttribute attr : cp[i].getExtraAttributes()) {
         if (!attr.getName().equals(attributeToDelete)) {
           attrs.put(attr.getName(), attr);
         }
       }
       attrs.put(attributeToAdd.getName(), attributeToAdd);
       IClasspathAttribute[] newAttrs =
           attrs.values().toArray(new IClasspathAttribute[attrs.size()]);
       cp[i] =
           JavaCore.newContainerEntry(
               cp[i].getPath(), cp[i].getAccessRules(), newAttrs, cp[i].isExported());
       break;
     }
   }
   javaProject.setRawClasspath(cp, monitor);
 }
 private static boolean hasExtraAttribute(IClasspathEntry entry, String expectedAttribute) {
   for (IClasspathAttribute cpa : entry.getExtraAttributes()) {
     if (expectedAttribute.equals(cpa.getName())) {
       return true;
     }
   }
   return false;
 }
Exemplo n.º 3
0
  public static URL getLibraryJavadocLocation(IClasspathEntry entry) {
    if (entry == null) {
      throw new IllegalArgumentException("Entry must not be null"); // $NON-NLS-1$
    }

    int kind = entry.getEntryKind();
    if (kind != IClasspathEntry.CPE_LIBRARY && kind != IClasspathEntry.CPE_VARIABLE) {
      throw new IllegalArgumentException(
          "Entry must be of kind CPE_LIBRARY or CPE_VARIABLE"); //$NON-NLS-1$
    }

    IClasspathAttribute[] extraAttributes = entry.getExtraAttributes();
    for (int i = 0; i < extraAttributes.length; i++) {
      IClasspathAttribute attrib = extraAttributes[i];
      if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attrib.getName())) {
        return parseURL(attrib.getValue());
      }
    }
    return null;
  }
  protected IClasspathEntry createClasspathEntry(
      IPath entryPath, IPath sourcePath, String javadocURL) {
    IPath sourceRootPath = null;
    IAccessRule[] rules = new IAccessRule[] {};
    IClasspathAttribute[] attrs = new IClasspathAttribute[] {};

    final ClasspathDecorations dec =
        cpDecorations.getDecorations(
            getDecorationManagerKey(javaProject.getProject(), getPath().toString()),
            entryPath.toString());

    if (dec != null) {
      sourcePath = dec.getSourceAttachmentPath();
      sourceRootPath = dec.getSourceAttachmentRootPath();
      attrs = dec.getExtraAttributes();
    }

    if (javadocURL != null) {
      if (CoreUtil.empty(attrs)) {
        attrs = new IClasspathAttribute[] {newJavadocAttr(javadocURL)};
      } else {
        List<IClasspathAttribute> newAttrs = new ArrayList<IClasspathAttribute>();

        for (IClasspathAttribute attr : attrs) {
          if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attr.getName())) {
            newAttrs.add(newJavadocAttr(javadocURL));
          } else {
            newAttrs.add(attr);
          }
        }

        attrs = newAttrs.toArray(new IClasspathAttribute[0]);
      }
    }

    return JavaCore.newLibraryEntry(entryPath, sourcePath, sourceRootPath, rules, attrs, false);
  }
  /**
   * Initializes the ProjectProperties object.
   *
   * @param project
   * @throws JavaModelException
   * @throws CoreException
   */
  public void refresh() throws JavaModelException, CoreException {

    classPath = new StringBuffer();
    inPath = new StringBuffer();
    sourceFiles = new ArrayList();

    javaProject = JavaCore.create(project);
    String projectLocalPrefix = File.separator + project.getName();

    /*
     * get paths
     */
    this.projectLocation = project.getLocation().removeLastSegments(1).toOSString();
    this.outputPath = javaProject.getOutputLocation().toOSString();
    this.sourcePaths = new ArrayList<String>();

    /*
     * get source files
     */
    IClasspathEntry[] classPathEntries = javaProject.getResolvedClasspath(false);

    for (int i = 0; i < classPathEntries.length; i++) {
      if (classPathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
        // 1st segment of the path has to be removed because it is added
        // again by findMember ending in duplicated first segment in the path
        getAllSourceFiles(
            project.findMember(classPathEntries[i].getPath().removeFirstSegments(1)),
            this.sourceFiles);
        if (!this.sourcePaths.contains(classPathEntries[i].getPath().toOSString())) {
          this.sourcePaths.add(classPathEntries[i].getPath().toOSString());
        }
      } else if (classPathEntries[i].getEntryKind() == IClasspathEntry.CPE_LIBRARY) {

        if (this.classPath.length() > 0) {
          this.classPath.append(File.pathSeparator);
        }

        String cp = classPathEntries[i].getPath().toOSString();
        if (cp.startsWith(projectLocalPrefix)) {
          cp = this.projectLocation + classPathEntries[i].getPath().toOSString();
        }

        // add the lib to inPath if specified in the .classpath file
        // as an inpath resource
        for (IClasspathAttribute attr : classPathEntries[i].getExtraAttributes()) {
          if (attr.getName().equals("inpath") && attr.getValue().equals("true")) {
            if (this.inPath.length() > 0) {
              this.inPath.append(File.pathSeparator);
            }
            this.inPath.append(cp);
            break;
          }
        }

        this.classPath.append(cp);
      }
    }

    //        IFile inpathFile = project.getFile("inpath.properties");
    //        if(inpathFile != null) {
    //        	Properties inpathProp = new Properties();
    //        	try {
    //				inpathProp.load(inpathFile.getContents());
    //				for(Object prop : inpathProp.keySet()) {
    //					if(inPath.length() > 0) {
    //						inPath.append(File.pathSeparator);
    //					}
    //
    //	inPath.append(this.projectLocation).append(projectLocalPrefix).append(File.separator).append(inpathProp.get(prop));
    //				}
    //			} catch (IOException e) {
    //				// TODO Auto-generated catch block
    //				e.printStackTrace();
    //			}
    //        }

  }