Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004, 2008 Richard Hoefter and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: Richard Hoefter (richard.hoefter@web.de) - initial API and implementation, bug 95298, bug 192726, bug 201180 IBM Corporation - nlsing and incorporating into Eclipse, bug 108276 /
 
 
 package org.eclipse.ant.internal.ui.datatransfer;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
Class to inspect classpath of an Eclipse project.
 
 public class EclipseClasspath
 {
     protected List srcDirs = new ArrayList();
     protected List classDirs = new ArrayList();
     protected List inclusionLists = new ArrayList();
     protected List exclusionLists = new ArrayList();
   
     protected Map variable2valueMap = new LinkedHashMap();
     protected List rawClassPathEntries = new ArrayList();
     protected List rawClassPathEntriesAbsolute = new ArrayList();
  
     private IJavaProject project;
     
     private static Map userLibraryCache = new HashMap();

    
Initialize object with classpath of given project.
 
     public EclipseClasspath(IJavaProject projectthrows JavaModelException
     {
         this. = project;
         handle(project.getRawClasspath()); 
     }
  
    
Initialize object with runtime classpath of given launch configuration.

Parameters:
project project that contains given launch configuration conf
conf launch configuration
bootstrap if true only bootstrap entries are added, if false only non-bootstrap entries are added
 
     public EclipseClasspath(IJavaProject projectILaunchConfiguration confboolean bootstrap)
         throws CoreException
     {
         this. = project;
         
         // convert IRuntimeClasspathEntry to IClasspathEntry
         IRuntimeClasspathEntry[] runtimeEntries;
         // see AbstractJavaLaunchConfigurationDelegate
         runtimeEntries = JavaRuntime.computeUnresolvedRuntimeClasspath(conf);
         List classpathEntries = new ArrayList(runtimeEntries.length);
         for (int i = 0; i < runtimeEntries.lengthi++)
         {
             IRuntimeClasspathEntry entry = runtimeEntries[i];
             if (  bootstrap && (entry.getClasspathProperty()  == .) ||
                 ! bootstrap && (entry.getClasspathProperty()  != .))
             {
                 // NOTE: See AbstractJavaLaunchConfigurationDelegate.getBootpathExt()
                 //       for an alternate bootclasspath detection
                 if (entry.getClass().getName().equals("org.eclipse.jdt.internal.launching.VariableClasspathEntry")) //$NON-NLS-1$
                 {
                     IClasspathEntry e = convertVariableClasspathEntry(entry);
                     if (e != null)
                     {
                         classpathEntries.add(e);
                     }
                }
                else if (entry.getClass().getName().equals("org.eclipse.jdt.internal.launching.DefaultProjectClasspathEntry")) //$NON-NLS-1$
                {
                    IClasspathEntry e = JavaCore.newProjectEntry(entry.getPath());
                    classpathEntries.add(e);
                }
                else if (entry.getClasspathEntry() != null)
                {
                    classpathEntries.add(entry.getClasspathEntry());
                }               
            }
            else if (bootstrap && entry.toString().startsWith(.))
            {
                classpathEntries.add(entry.getClasspathEntry());
            }
            else if (bootstrap && entry.toString().startsWith(.))
            {
                classpathEntries.add(entry.getClasspathEntry());
            }
        }
        IClasspathEntry[] entries =
            (IClasspathEntry[]) classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]);
        handle(entries); 
    }
    private void handle(IClasspathEntry[] entriesthrows JavaModelException
    {
        for (int i = 0; i < entries.lengthi++)
        {
            handleSources(entries[i]);
            handleVariables(entries[i]);
            handleJars(entries[i]);
            handleLibraries(entries[i]);
            handleProjects(entries[i]);
        }
    }
    private void handleSources(IClasspathEntry entrythrows JavaModelException
    {
        String projectRoot = ExportUtil.getProjectRoot();
        String defaultClassDir = .getOutputLocation().toString();
        String defaultClassDirAbsolute = ExportUtil.resolve(.getOutputLocation());
        if (entry.getContentKind() == . &&
            entry.getEntryKind() == .)
        {
            // found source path
            IPath srcDirPath = entry.getPath();
            IPath classDirPath = entry.getOutputLocation();
            String srcDir = handleLinkedResource(srcDirPath);
            ExportUtil.removeProjectRoot((srcDirPath != null) ? srcDirPath.toString() : projectRoot.getProject());
            String classDir = ExportUtil.removeProjectRoot((classDirPath != null) ? classDirPath.toString() : defaultClassDir.getProject());
            .add(srcDir);
            .add(classDir);
            String classDirAbsolute = (classDirPath != null) ? ExportUtil.resolve(classDirPath) : defaultClassDirAbsolute;
            .add(classDir);
            .add(classDirAbsolute);
            IPath[] inclusions = entry.getInclusionPatterns();                   
            List inclusionList = new ArrayList();
            for (int j = 0; j < inclusions.lengthj++)
            {
                if (inclusions[j] != null)
                {
                    inclusionList.add(ExportUtil.removeProjectRoot(inclusions[j].toString(), .getProject()));
                }
            }
            .add(inclusionList);
            IPath[] exclusions = entry.getExclusionPatterns();
            List exclusionList = new ArrayList();
            for (int j = 0; j < exclusions.lengthj++)
            {
                if (exclusions[j] != null)
                {
                    exclusionList.add(ExportUtil.removeProjectRoot(exclusions[j].toString(), .getProject()));
                }
            }
            .add(exclusionList);
        }
    }
    
    
Check if given source path is a linked resource. Add values to variable2valueMap accordingly.

Parameters:
srcDirPath source dir as IPath
Returns:
source directory with reference, e.g. ${MYPATH}/src, if it is no link, orginal source dir is returned
    private String handleLinkedResource(IPath srcDirPath)
    {
        String projectRoot = ExportUtil.getProjectRoot();
        String srcDir = ExportUtil.removeProjectRoot((srcDirPath != null) ? srcDirPath.toString() : projectRoot.getProject());
        if (srcDirPath == null)
        {
            return srcDir;
        }
        IFile file;
        try
        {
            file = ResourcesPlugin.getWorkspace().getRoot().getFile(srcDirPath);
        }
        catch (IllegalArgumentException e)
        {
            return srcDir;
        }
        if (file.isLinked())
        {
            String pathVariable = file.getRawLocation().segment(0).toString();
            IPath pathVariableValue = file.getWorkspace().getPathVariableManager().getValue(pathVariable);
            if (pathVariableValue != null)
            {
                // path variable was used
                String pathVariableExtension = file.getRawLocation().removeFirstSegments(1).toString(); // Bug 192726
                String relativePath = ExportUtil.getRelativePath(pathVariableValue.toString(),
                        projectRoot);
                .put(pathVariable + ".pathvariable"relativePath); //$NON-NLS-1$
                .put(srcDir + ".link"//$NON-NLS-1$
                        "${" + pathVariable + ".pathvariable}/" + pathVariableExtension); //$NON-NLS-1$  //$NON-NLS-2$
            }
            else
            {
                String relativePath = ExportUtil.getRelativePath(file.getLocation() + ""//$NON-NLS-1$
                        projectRoot);
                .put(srcDir + ".link"relativePath); //$NON-NLS-1$
            }
            srcDir = "${" + srcDir + ".link}"//$NON-NLS-1$ //$NON-NLS-2$
        }
        return srcDir;
    }
    private void handleJars(IClasspathEntry entry)
    {
        if (entry.getContentKind() == . &&
            entry.getEntryKind() == .)
        {
            String jarFile = entry.getPath().toString();
            StringBuffer jarFileBuffer = new StringBuffer();
            StringBuffer jarFileAbsoluteBuffer = new StringBuffer();
            String jarFileAbsolute = ExportUtil.resolve(entry.getPath());
            if (jarFileAbsolute == null)
            {
                jarFileAbsolute = jarFile// jarFile was already absolute
                if (handleSubProjectClassesDirectory(jarFilejarFileBufferjarFileAbsoluteBuffer))
                {
                    jarFile = jarFileBuffer.toString();
                    jarFileAbsolute = jarFileAbsoluteBuffer.toString();
                }
            }
            String jarFileOld = jarFile;
            jarFile = ExportUtil.removeProjectRoot(jarFile.getProject());
            if (jarFile.equals(jarFileOld))
            {
                if (handleSubProjectClassesDirectory(jarFilejarFileBufferjarFileAbsoluteBuffer))
                {
                    jarFile = jarFileBuffer.toString();
                    jarFileAbsolute = jarFileAbsoluteBuffer.toString();
                }
            }
            .add(jarFile);
            .add(jarFileAbsolute);
        }
    }

    
Checks if file is a class directory of a subproject and fills string buffers with resolved values.

Parameters:
file file to check
jarFile filled with file location with variable reference ${project.location}, which is also added to variable2valueMap
jarFileAbsolute filled with absolute file location
Returns:
true if file is a classes directory
    private boolean handleSubProjectClassesDirectory(String fileStringBuffer jarFileStringBuffer jarFileAbsolute)
    {
        // class directory of a subproject?
        if (file != null && file.indexOf('/') == 0)
        {
            int i = file.indexOf('/', 1);
            i = (i != -1) ? i : file.length(); 
            String subproject = file.substring(1, i);
            IJavaProject javaproject = ExportUtil.getJavaProjectByName(subproject);
            if (javaproject != null)
            {
                jarFile.setLength(0);
                jarFileAbsolute.setLength(0);
                String location = javaproject.getProject().getName() + ".location"//$NON-NLS-1$
                jarFileAbsolute.append(ExportUtil.replaceProjectRoot(filejavaproject.getProject(), ExportUtil.getProjectRoot(javaproject)));
                jarFile.append(ExportUtil.replaceProjectRoot(filejavaproject.getProject(), "${" + location + "}")); //$NON-NLS-1$ //$NON-NLS-2$
                String projectRoot= ExportUtil.getProjectRoot();
                String relativePath = ExportUtil.getRelativePath(ExportUtil.getProjectRoot(javaproject),
                        projectRoot);
                .put(locationrelativePath);
                return true;
            }
        }
        return false;
    }
    private void handleVariables(IClasspathEntry entry)
    {
        if (entry.getContentKind() == . &&
            entry.getEntryKind() == .)
        {
            // found variable
            String e = entry.getPath().toString();
            int index = e.indexOf('/');
            if (index == -1)
            {
                index = e.indexOf('\\');
            }
            String variable = e;
            String path = ""//$NON-NLS-1$
            if (index != -1)
            {
                variable = e.substring(0, index);
                path = e.substring(index);
            }
            IPath value = JavaCore.getClasspathVariable(variable);
            if (value != null)
            {
                String projectRoot = ExportUtil.getProjectRoot();
                String relativePath = ExportUtil.getRelativePath(value.toString(),
                        projectRoot);
                .put(variablerelativePath);
            }
            else if (.get(variable) == null)
            {
                // only add empty value, if variable is new 
                .put(variable""); //$NON-NLS-1$
            }
            .add(value + path);
            .add("${" + variable + "}" + path); //$NON-NLS-1$ //$NON-NLS-2$
        }
    }
    
    private void handleLibraries(IClasspathEntry entrythrows JavaModelException
    {
        if (entry.getContentKind() == . &&
            entry.getEntryKind() == .)
        {
            // found library
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), );
            if (container == null) {
                // jar missing (project not compile clean)
                return;
            }
            String jar = entry.getPath().toString();
            String refName;
            if (jar.startsWith(.))
            {
                // JRE System Library
                refName = "${jre.container}"//$NON-NLS-1$
            }
            else if (jar.startsWith(.))
            {
                // User Library
                String libraryName = container.getDescription();
                refName = "${" + libraryName + ".userclasspath}"//$NON-NLS-1$ //$NON-NLS-2$
                if (container.getKind() == .)
                {
                    refName = "${" + libraryName + ".bootclasspath}"//$NON-NLS-1$ //$NON-NLS-2$
                }
            }
            else
            {
                // Library dependencies: e.g. Plug-in Dependencies
                String libraryName = container.getDescription();
                refName = "${" + libraryName + ".libraryclasspath}"//$NON-NLS-1$ //$NON-NLS-2$
            }
            .put(refNamecontainer);
            .add(refName);
            .add(refName);
            .add(refName);
            .add(refName);
            .add(new ArrayList());
            .add(new ArrayList());                
        }
    }
    
    private void handleProjects(IClasspathEntry entry)
    {
        if (entry.getContentKind() == . &&
            entry.getEntryKind() == .)
        {
            // found required project on build path
            String subProjectRoot = entry.getPath().toString();
            IJavaProject subProject = ExportUtil.getJavaProject(subProjectRoot);
            if (subProject == null)
            {
                // project was not loaded in workspace
                AntUIPlugin.log("project is not loaded in workspace: " + subProjectRootnull); //$NON-NLS-1$
                return;
            }
            // only add an indicator that this is a project reference
            String classpathRef = "${" + subProject.getProject().getName() + ".classpath}"//$NON-NLS-1$ //$NON-NLS-2$
            .add(classpathRef);
            .add(classpathRef);
            .add(classpathRef);
            .add(classpathRef);
            .add(new ArrayList());
            .add(new ArrayList());
        }
    }

    
Get runtime classpath items for given project separated with path separator.
    public static String getClasspath(IJavaProject projectthrows CoreException
    {
        List items = getClasspathList(project);
        return ExportUtil.toString(items.);
    }

    
Get runtime classpath items for given project.
    public static List getClasspathList(IJavaProject projectthrows CoreException
    {
        String[] classpath = JavaRuntime.computeDefaultRuntimeClassPath(project);
        return Arrays.asList(classpath);
    }
    
    
Check if given string is a reference.
    public static boolean isReference(String s)
    {
        return isProjectReference(s) || isUserLibraryReference(s) ||
            isUserSystemLibraryReference(s) || isLibraryReference(s) ||
            isJreReference(s);
        // NOTE: A linked resource is no reference
    }

    
Check if given string is a project reference.
    public static boolean isProjectReference(String s)
    {
        return s.startsWith("${") && s.endsWith(".classpath}"); //$NON-NLS-1$ //$NON-NLS-2$ 
    }

    
Resolves given project reference to a project.

Returns:
null if project is not resolvable
    public static IJavaProject resolveProjectReference(String s)
    {
        String name = ExportUtil.removePrefixAndSuffix(s"${"".classpath}"); //$NON-NLS-1$ //$NON-NLS-2$
        return ExportUtil.getJavaProjectByName(name);  
    }

    
Check if given string is a user library reference.
    public static boolean isUserLibraryReference(String s)
    {
        return s.startsWith("${") && s.endsWith(".userclasspath}"); //$NON-NLS-1$ //$NON-NLS-2$ 
    }

    
Check if given string is a user system library reference. This library is added to the compiler boot classpath.
    public static boolean isUserSystemLibraryReference(String s)
    {
        return s.startsWith("${") && s.endsWith(".bootclasspath}"); //$NON-NLS-1$ //$NON-NLS-2$ 
    }

    
Check if given string is a library reference. e.g. Plug-in dependencies are library references.
    public static boolean isLibraryReference(String s)
    {
        return s.startsWith("${") && s.endsWith(".libraryclasspath}"); //$NON-NLS-1$ //$NON-NLS-2$ 
    }

    
Check if given string is a JRE reference.
    public static boolean isJreReference(String s)
    {
        return s.equals("${jre.container}"); //$NON-NLS-1$
    }   
    
    
Resolves given user (system) library or plugin reference to its container.

NOTE: The library can only be resolved if an EclipseClasspath object was created which had a reference to this library. The class holds an internal cache to circumvent that UserLibraryManager is an internal class.

Returns:
null if library is not resolvable
    {
        return (IClasspathContainer.get(s);
    }
    
    
Check if given string is a linked resource.
    public static boolean isLinkedResource(String s)
    {
        return s.startsWith("${") && s.endsWith(".link}"); //$NON-NLS-1$ //$NON-NLS-2$ 
    }

    
Get source folder name of a linked resource.

    public static String getLinkedResourceName(String s)
    {
        return ExportUtil.removePrefixAndSuffix(s"${"".link}"); //$NON-NLS-1$ //$NON-NLS-2$ 
    }
    
    
Resolves given linked resource to an absolute file location.
    {
        String name = ExportUtil.removePrefixAndSuffix(s"${""}"); //$NON-NLS-1$ //$NON-NLS-2$
        String value = (String.get(name);
        String suffix = ".pathvariable}"//$NON-NLS-1$
        int i = value.indexOf(suffix);
        if (i != -1)
        {
            // path variable
            String pathVariable = value.substring(0, i + suffix.length() - 1);
            pathVariable = ExportUtil.removePrefix(pathVariable"${"); //$NON-NLS-1$
            return (String.get(pathVariable) + value.substring(i + suffix.length());
        }
        return value;
    }

    
Convert a VariableClasspathEntry to a IClasspathEntry.

This is a workaround as entry.getClasspathEntry() returns null.

    {
        try
        {
            Document doc = ExportUtil.parseXmlString(entry.getMemento());
            Element element = (Elementdoc.getElementsByTagName("memento").item(0); //$NON-NLS-1$
            String variableString = element.getAttribute("variableString"); //$NON-NLS-1$
            ExportUtil.addVariable(variableString, ExportUtil.getProjectRoot());
            // remove ${...} from string to be conform for handleVariables()
            variableString = ExportUtil.removePrefix(variableString"${");//$NON-NLS-1$
            int i = variableString.indexOf('}');
            if (i != -1)
            {
                variableString = variableString.substring(0, i)
                    + variableString.substring(i + 1);                
            }
            IPath path = new Path(variableString);
            return JavaCore.newVariableEntry(pathnullnull);
        }
        catch (Exception e)
        {
            AntUIPlugin.log(e);
            return null;
        }
    }
New to GrepCode? Check out our FAQ X