/**The IClassPathEntry is the one found in the code snippet above. */ private void setJavadocLocationAttribute(IJavaProject project, IClasspathEntry entry, String text) { assert ripped != null; FoundClassPathEntry found = JavaProjects.findClassPathEntry(project, entry); System.out.println("Setting. Found: " + found); JavaProjects.updateFoundClassPath(found, new IFunction1<IClasspathEntry, IClasspathEntry>() { @Override public IClasspathEntry apply(IClasspathEntry from) throws Exception { IClasspathAttribute[] extraAttributes = from.getExtraAttributes(); List<IClasspathAttribute> newAttributes = new ArrayList<IClasspathAttribute>(); boolean found = false; IClasspathAttribute newClasspathAttribute = JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, text); for (IClasspathAttribute oldAttribute : extraAttributes) if (oldAttribute.getName().equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) { newAttributes.add(newClasspathAttribute); found = true; } else newAttributes.add(oldAttribute); if (!found) newAttributes.add(newClasspathAttribute); IClasspathEntry newLibraryEntry = JavaCore.newLibraryEntry(from.getPath(), from.getSourceAttachmentPath(), from.getSourceAttachmentRootPath(), from.getAccessRules(), newAttributes.toArray(new IClasspathAttribute[0]), from.isExported()); return newLibraryEntry; } }); } public class FoundClassPathEntry { public static enum FoundIn { NOT_FOUND, FOUND_IN_RAW, FOUND_IN_LIBRARY } public final IJavaProject javaProject; public final FoundIn foundIn; public final IClasspathEntry rawClassPath; public final IClasspathEntry classPathEntry; public final IClasspathContainer container; public FoundClassPathEntry(IJavaProject javaProject, FoundIn foundIn, IClasspathEntry classPathEntry) { this.javaProject = javaProject; this.foundIn = foundIn; this.rawClassPath = classPathEntry; this.container = null; this.classPathEntry = classPathEntry; } public FoundClassPathEntry(IJavaProject javaProject, FoundIn foundIn, IClasspathEntry rawClassPath, IClasspathContainer container, IClasspathEntry classPathEntry) { this.javaProject = javaProject; this.foundIn = foundIn; this.rawClassPath = rawClassPath; this.container = container; this.classPathEntry = classPathEntry; } @Override public String toString() { return "FoundClassPathEntry [project=" + javaProject.getElementName() + ", foundIn=" + foundIn + "]"; } } public class JavaProjects { public static FoundClassPathEntry findClassPathEntry(IJavaProject project, IClasspathEntry entry) { try { IClasspathEntry[] rawClasspaths = project.getRawClasspath(); for (IClasspathEntry rawClassPath : rawClasspaths) { if (rawClassPath == entry) return new FoundClassPathEntry(project, FoundIn.FOUND_IN_RAW, entry); else if (rawClassPath.getEntryKind() == IClasspathEntry.CPE_CONTAINER) { IClasspathContainer container = JavaCore.getClasspathContainer(rawClassPath.getPath(), project); for (IClasspathEntry contained : container.getClasspathEntries()) { if (contained == entry) return new FoundClassPathEntry(project, FoundIn.FOUND_IN_LIBRARY, rawClassPath, container, entry); } } } return new FoundClassPathEntry(project, FoundIn.NOT_FOUND, entry); } catch (JavaModelException e) { throw WrappedException.wrap(e); } } public static void updateFoundClassPath(final FoundClassPathEntry found, final IFunction1<IClasspathEntry, IClasspathEntry> mutator) { try { switch (found.foundIn) { case FOUND_IN_LIBRARY: ClasspathContainerInitializer initializer = JavaCore.getClasspathContainerInitializer(found.container.getPath().segment(0)); final IClasspathEntry[] newEntries = ArrayHelper.map(IClasspathEntry.class, found.container.getClasspathEntries(), new IFunction1<IClasspathEntry, IClasspathEntry>() { @Override public IClasspathEntry apply(IClasspathEntry from) throws Exception { if (from == found.classPathEntry) return mutator.apply(from); else return from; } }); IClasspathContainer containerSuggestion = new IClasspathContainer() { @Override public IPath getPath() { return found.container.getPath(); } @Override public int getKind() { return found.container.getKind(); } @Override public String getDescription() { return found.container.getDescription(); } @Override public IClasspathEntry[] getClasspathEntries() { return newEntries; } }; initializer.requestClasspathContainerUpdate(found.container.getPath(), found.javaProject, containerSuggestion); break; default: throw new IllegalStateException(found.toString()); } } catch (Exception e) { throw WrappedException.wrap(e); } } } public class ArrayHelper { public static <From, To> To[] map(Class<To> clazz, From[] old, IFunction1<From, To> mutator) { try { @SuppressWarnings({ "unchecked" }) To[] result = (To[]) Array.newInstance(clazz, old.length); for (int i = 0; i < old.length; i++) result[i] = mutator.apply(old[i]); return result; } catch (Exception e) { throw WrappedException.wrap(e); } } }