Search in sources :

Example 16 with IClasspathAttribute

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

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>();
    // store in a map from path to entry
    for (int j = 0; j < entries.length; j++) {
        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);
        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 17 with IClasspathAttribute

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

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) {
                // $NON-NLS-1$
                rtFolder = "/WEB-INF/classes";
            } else {
                // $NON-NLS-1$
                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 18 with IClasspathAttribute

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

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)

Example 19 with IClasspathAttribute

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

the class ModuleTraverser method getRawComponentClasspathDependencies.

/*
	 * Derived from ClasspathDependencyUtil.getRawComponentClasspathDependencies()
	 */
private static Map getRawComponentClasspathDependencies(final IJavaProject javaProject) throws CoreException {
    if (javaProject == null) {
        return Collections.EMPTY_MAP;
    }
    final Map<IClasspathEntry, IClasspathAttribute> referencedRawEntries = new HashMap<IClasspathEntry, IClasspathAttribute>();
    final IClasspathEntry[] entries = javaProject.getRawClasspath();
    for (int i = 0; i < entries.length; i++) {
        final IClasspathEntry entry = entries[i];
        final IClasspathAttribute attrib = checkForComponentDependencyAttribute(entry, DEPENDECYATTRIBUTETYPE_CLASSPATH_COMPONENT_DEPENDENCY);
        if (attrib != null) {
            referencedRawEntries.put(entry, attrib);
        }
    }
    return referencedRawEntries;
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 20 with IClasspathAttribute

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

the class ProjectUtil method _fixExtProjectClasspathEntries.

private static void _fixExtProjectClasspathEntries(IProject project) {
    try {
        boolean fixedAttr = false;
        IJavaProject javaProject = JavaCore.create(project);
        List<IClasspathEntry> newEntries = new ArrayList<>();
        IClasspathEntry[] entries = javaProject.getRawClasspath();
        for (IClasspathEntry entry : entries) {
            IClasspathEntry newEntry = null;
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                List<IClasspathAttribute> newAttrs = new ArrayList<>();
                IClasspathAttribute[] attrs = entry.getExtraAttributes();
                if (ListUtil.isNotEmpty(attrs)) {
                    for (IClasspathAttribute attr : attrs) {
                        IClasspathAttribute newAttr = null;
                        if ("owner.project.facets".equals(attr.getName()) && "liferay.plugin".equals(attr.getValue())) {
                            newAttr = JavaCore.newClasspathAttribute(attr.getName(), "liferay.ext");
                            fixedAttr = true;
                        } else {
                            newAttr = attr;
                        }
                        newAttrs.add(newAttr);
                    }
                    newEntry = JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(), entry.getExclusionPatterns(), entry.getOutputLocation(), newAttrs.toArray(new IClasspathAttribute[0]));
                }
            }
            if (newEntry == null) {
                newEntry = entry;
            }
            newEntries.add(newEntry);
        }
        if (fixedAttr) {
            IProgressMonitor monitor = new NullProgressMonitor();
            javaProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[0]), monitor);
            try {
                javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
            } catch (Exception e) {
                ProjectCore.logError(e);
            }
        }
        fixExtProjectSrcFolderLinks(project);
    } catch (Exception ex) {
        ProjectCore.logError("Exception trying to fix Ext project classpath entries.", ex);
    }
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) CoreException(org.eclipse.core.runtime.CoreException) JavaModelException(org.eclipse.jdt.core.JavaModelException) IOException(java.io.IOException)

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