Search in sources :

Example 21 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project liferay-ide by liferay.

the class AbstractLiferayComponentTemplate method createResorcesFolder.

protected void createResorcesFolder(IProject project) throws CoreException {
    IFolder resourceFolder = liferayProject.getSourceFolder("resources");
    if (FileUtil.exists(resourceFolder)) {
        return;
    }
    IJavaProject javaProject = JavaCore.create(project);
    List<IClasspathEntry> existingRawClasspath = Arrays.asList(javaProject.getRawClasspath());
    List<IClasspathEntry> newRawClasspath = new ArrayList<>();
    IClasspathAttribute[] attributes = { JavaCore.newClasspathAttribute("FROM_GRADLE_MODEL", "true") };
    IPath path = project.getFullPath().append("src/main/resources");
    IClasspathEntry resourcesEntry = JavaCore.newSourceEntry(path, new IPath[0], new IPath[0], null, attributes);
    newRawClasspath.add(resourcesEntry);
    for (IClasspathEntry entry : existingRawClasspath) {
        newRawClasspath.add(entry);
    }
    javaProject.setRawClasspath(newRawClasspath.toArray(new IClasspathEntry[0]), new NullProgressMonitor());
    project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) IFolder(org.eclipse.core.resources.IFolder)

Example 22 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project webtools.servertools by eclipse.

the class RuntimeClasspathProviderDelegate method save.

/**
 * Save source attachment info.
 */
private synchronized void save() {
    List<SourceAttachmentUpdate> srcAttachments = sourceAttachments;
    if (srcAttachments == null)
        return;
    String id = extensionId;
    String filename = JavaServerPlugin.getInstance().getStateLocation().append(id + ".xml").toOSString();
    try {
        XMLMemento memento = XMLMemento.createWriteRoot("classpath");
        Iterator iterator = srcAttachments.iterator();
        while (iterator.hasNext()) {
            SourceAttachmentUpdate sau = (SourceAttachmentUpdate) iterator.next();
            IMemento child = memento.createChild("source-attachment");
            child.putString("runtime-id", sau.runtimeId);
            if (sau.entry != null)
                child.putString("entry", sau.entry.toPortableString());
            if (sau.sourceAttachmentPath != null)
                child.putString("source-attachment-path", sau.sourceAttachmentPath.toPortableString());
            if (sau.sourceAttachmentRootPath != null)
                child.putString("source-attachment-root-path", sau.sourceAttachmentRootPath.toPortableString());
            if (sau.attributes != null) {
                for (IClasspathAttribute attr : sau.attributes) {
                    IMemento attrChild = child.createChild("attribute");
                    attrChild.putString("name", attr.getName());
                    attrChild.putString("value", attr.getValue());
                }
            }
        }
        memento.saveToFile(filename);
    } catch (Exception e) {
        if (Trace.SEVERE) {
            Trace.trace(Trace.STRING_SEVERE, "Error saving source path info", e);
        }
    }
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) XMLMemento(org.eclipse.jst.server.core.internal.XMLMemento) IMemento(org.eclipse.jst.server.core.internal.IMemento)

Example 23 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project webtools.servertools by eclipse.

the class ModuleTraverser method traverseWebComponentLocalEntries.

private static void traverseWebComponentLocalEntries(WorkbenchComponent comp, IModuleVisitor visitor, IProgressMonitor monitor) throws CoreException {
    IProject warProject = StructureEdit.getContainingProject(comp);
    if (warProject == null || !warProject.hasNature(JavaCore.NATURE_ID)) {
        return;
    }
    IJavaProject project = JavaCore.create(warProject);
    List res = comp.getResources();
    for (Iterator itorRes = res.iterator(); itorRes.hasNext(); ) {
        ComponentResource childComp = (ComponentResource) itorRes.next();
        IClasspathEntry cpe = getClasspathEntry(project, childComp.getSourcePath());
        if (cpe == null)
            continue;
        visitor.visitWebResource(childComp.getRuntimePath(), getOSPath(warProject, project, cpe.getOutputLocation()));
    }
    // Include tagged classpath entries
    Map classpathDeps = getComponentClasspathDependencies(project, true);
    for (Iterator iterator = classpathDeps.keySet().iterator(); iterator.hasNext(); ) {
        IClasspathEntry entry = (IClasspathEntry) iterator.next();
        IClasspathAttribute attrib = (IClasspathAttribute) classpathDeps.get(entry);
        boolean isClassFolder = isClassFolderEntry(entry);
        String rtFolder = attrib.getValue();
        if (rtFolder == null) {
            if (isClassFolder) {
                rtFolder = "/WEB-INF/classes";
            } else {
                rtFolder = "/WEB-INF/lib";
            }
        }
        IPath entryPath = entry.getPath();
        IResource entryRes = ResourcesPlugin.getWorkspace().getRoot().findMember(entryPath);
        if (entryRes != null) {
            entryPath = entryRes.getLocation();
        }
        // TODO Determine if different handling is needed for some use cases
        if (isClassFolder) {
            visitor.visitWebResource(new Path(rtFolder), getOSPath(warProject, project, entry.getPath()));
        } else {
            visitor.visitArchiveComponent(new Path(rtFolder), entryPath);
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IProject(org.eclipse.core.resources.IProject) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IJavaProject(org.eclipse.jdt.core.IJavaProject) ComponentResource(org.eclipse.wst.common.componentcore.internal.ComponentResource) Iterator(java.util.Iterator) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IResource(org.eclipse.core.resources.IResource)

Example 24 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project webtools.servertools by eclipse.

the class ModuleTraverser method getComponentClasspathDependencies.

/*
	 * Derived from ClasspathDependencyUtil.getComponentClasspathDependencies()
	 */
private static Map getComponentClasspathDependencies(final IJavaProject javaProject, final boolean isWebApp) throws CoreException {
    // get the raw entries
    final Map referencedRawEntries = getRawComponentClasspathDependencies(javaProject);
    final Map<IClasspathEntry, IClasspathAttribute> validRawEntries = new HashMap<IClasspathEntry, IClasspathAttribute>();
    // filter out non-valid referenced raw entries
    final Iterator i = referencedRawEntries.keySet().iterator();
    while (i.hasNext()) {
        final IClasspathEntry entry = (IClasspathEntry) i.next();
        final IClasspathAttribute attrib = (IClasspathAttribute) referencedRawEntries.get(entry);
        if (isValid(entry, attrib, isWebApp, javaProject.getProject())) {
            validRawEntries.put(entry, attrib);
        }
    }
    // if we have no valid raw entries, return empty map
    if (validRawEntries.isEmpty()) {
        return Collections.EMPTY_MAP;
    }
    // XXX Would like to replace the code below with use of a public JDT API that returns
    // the raw IClasspathEntry for a given resolved IClasspathEntry (see see https://bugs.eclipse.org/bugs/show_bug.cgi?id=183995)
    // The code must currently leverage IPackageFragmentRoot to determine this
    // mapping and, because IPackageFragmentRoots do not maintain IClasspathEntry data, a prior
    // call is needed to getResolvedClasspath() and the resolved IClasspathEntries have to be stored in a Map from IPath-to-IClasspathEntry to
    // support retrieval using the resolved IPackageFragmentRoot
    // retrieve the resolved classpath
    final IClasspathEntry[] entries = javaProject.getResolvedClasspath(true);
    final Map<IPath, IClasspathEntry> pathToResolvedEntry = new HashMap<IPath, IClasspathEntry>();
    // might be a child of a classpath container.
    for (int j = 0; j < entries.length; j++) {
        IClasspathAttribute attrib = checkForComponentDependencyAttribute(entries[j], DEPENDECYATTRIBUTETYPE_CLASSPATH_COMPONENT_NONDEPENDENCY);
        // If not a non-dependency (i.e. not excluded), then add this entry.
        if (attrib == null) {
            pathToResolvedEntry.put(entries[j].getPath(), entries[j]);
        }
    }
    final Map<IClasspathEntry, IClasspathAttribute> referencedEntries = new LinkedHashMap<IClasspathEntry, IClasspathAttribute>();
    // grab all IPackageFragmentRoots
    final IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
    for (int j = 0; j < roots.length; j++) {
        final IPackageFragmentRoot root = roots[j];
        final IClasspathEntry rawEntry = root.getRawClasspathEntry();
        // is the raw entry valid?
        IClasspathAttribute attrib = validRawEntries.get(rawEntry);
        if (attrib == null) {
            continue;
        }
        final IPath pkgFragPath = root.getPath();
        final IClasspathEntry resolvedEntry = pathToResolvedEntry.get(pkgFragPath);
        // If the resolvedEntry is not present for this path, then it was excluded above due to being a non-dependency
        if (resolvedEntry == null) {
            continue;
        }
        final IClasspathAttribute resolvedAttrib = checkForComponentDependencyAttribute(resolvedEntry, DEPENDECYATTRIBUTETYPE_DEPENDENCY_OR_NONDEPENDENCY);
        // dependency attribute for it to be included
        if (resolvedAttrib == null || resolvedAttrib.getName().equals(CLASSPATH_COMPONENT_DEPENDENCY)) {
            // filter out resolved entry if it doesn't pass the validation rules
            if (isValid(resolvedEntry, resolvedAttrib != null ? resolvedAttrib : attrib, isWebApp, javaProject.getProject())) {
                if (resolvedAttrib != null) {
                    // if there is an attribute on the sub-entry, use that
                    attrib = resolvedAttrib;
                }
                referencedEntries.put(resolvedEntry, attrib);
            }
        }
    }
    return referencedEntries;
}
Also used : IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 25 with IClasspathAttribute

use of org.eclipse.jdt.core.IClasspathAttribute in project webtools.servertools by eclipse.

the class ModuleTraverser method checkForComponentDependencyAttribute.

/*
	 * Derived from ClasspathDependencyUtil.checkForComponentDependencyAttribute()
	 */
private static IClasspathAttribute checkForComponentDependencyAttribute(final IClasspathEntry entry, final int attributeType) {
    if (entry == null) {
        return null;
    }
    final IClasspathAttribute[] attributes = entry.getExtraAttributes();
    for (int i = 0; i < attributes.length; i++) {
        final IClasspathAttribute attribute = attributes[i];
        final String name = attribute.getName();
        if (name.equals(CLASSPATH_COMPONENT_DEPENDENCY)) {
            if (attributeType == DEPENDECYATTRIBUTETYPE_DEPENDENCY_OR_NONDEPENDENCY || attributeType == DEPENDECYATTRIBUTETYPE_CLASSPATH_COMPONENT_DEPENDENCY) {
                return attribute;
            }
        } else if (name.equals(CLASSPATH_COMPONENT_NON_DEPENDENCY)) {
            if (attributeType == DEPENDECYATTRIBUTETYPE_DEPENDENCY_OR_NONDEPENDENCY || attributeType == DEPENDECYATTRIBUTETYPE_CLASSPATH_COMPONENT_NONDEPENDENCY) {
                return attribute;
            }
        }
    }
    return null;
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute)

Aggregations

IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)52 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)37 IPath (org.eclipse.core.runtime.IPath)26 ArrayList (java.util.ArrayList)17 IJavaProject (org.eclipse.jdt.core.IJavaProject)16 Path (org.eclipse.core.runtime.Path)11 HashMap (java.util.HashMap)9 CoreException (org.eclipse.core.runtime.CoreException)9 IAccessRule (org.eclipse.jdt.core.IAccessRule)9 File (java.io.File)6 LinkedHashMap (java.util.LinkedHashMap)6 IProject (org.eclipse.core.resources.IProject)6 Map (java.util.Map)5 IFolder (org.eclipse.core.resources.IFolder)5 Bundle (org.osgi.framework.Bundle)5 Iterator (java.util.Iterator)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 JavaModelException (org.eclipse.jdt.core.JavaModelException)4 URL (java.net.URL)3 IClasspathContainer (org.eclipse.jdt.core.IClasspathContainer)3