edu.cmu.cs.crystal.internal
Class WorkspaceUtilities

java.lang.Object
  extended by edu.cmu.cs.crystal.internal.WorkspaceUtilities

public class WorkspaceUtilities
extends Object

A collection of methods used to extract useful data from the workspace. These methods are used by the framework and should not be used by users of the framework. You can access must of the data collected from these methods via the Crystal class.

Author:
David Dickey

Constructor Summary
WorkspaceUtilities()
           
 
Method Summary
static List<ICompilationUnit> collectCompilationUnits(IJavaElement javaElement)
          A recursive traversal of the IJavaModel starting from the given element to collect all ICompilationUnits.
static List<ITypeRoot> collectCompilationUnits(IJavaElement javaElement, boolean includeArchives)
          A recursive traversal of the IJavaModel starting from the given element to collect all ITypeRoots, optionally including .class files available in libraries.
static List<ICompilationUnit> findCompilationUnits(List<String> files)
          Returns the list of compilation units for a given list of file names.
static ASTNode getASTNodeFromCompilationUnit(ITypeRoot compUnit)
          Gets the root ASTNode for a compilation unit, with bindings on.
static Option<TypeDeclaration> getDeclNodeFromType(IType type)
          Given an IType from the model, this method will return the ast node associated with that type, or null if it doesn't exist.
static Option<MethodDeclaration> getMethodDeclFromModel(IMethod method)
          Returns the AST node associated with the given model element, which in this case is a method.
static String getWorkspaceRelativeName(IJavaElement element)
          Walks up the Java model hierarchy and separates the names of encountered elements by forward slashes
static Map<ICompilationUnit,ASTNode> parseCompilationUnits(List<ICompilationUnit> compilationUnits)
          Goes through a list of compilation units and parses them.
static Map<String,ASTNode> scanForBindings(ICompilationUnit compUnit, ASTNode node)
           
static Map<String,ASTNode> scanForBindings(Map<ICompilationUnit,ASTNode> compilationUnitToASTNode)
           
static List<ICompilationUnit> scanForCompilationUnits()
          Traverses the workspace for CompilationUnits.
static List<ITypeRoot> scanForCompilationUnits(boolean includeClassfiles)
          Traverses the workspace for CompilationUnits and (optionally) class files.
static List<MethodDeclaration> scanForMethodDeclarations(Map<ICompilationUnit,ASTNode> compilationUnitToASTNode)
          Collects all top level methods from CompilationUnits.
static List<MethodDeclaration> scanForMethodDeclarationsFromAST(ASTNode node)
          Collects all top level methods from an AST including embedded methods.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WorkspaceUtilities

public WorkspaceUtilities()
Method Detail

scanForCompilationUnits

public static List<ICompilationUnit> scanForCompilationUnits()
Traverses the workspace for CompilationUnits.

Returns:
the list of all CompilationUnits in the workspace or null if no comp units were found.

collectCompilationUnits

public static List<ICompilationUnit> collectCompilationUnits(IJavaElement javaElement)
A recursive traversal of the IJavaModel starting from the given element to collect all ICompilationUnits. Each compilation unit corresponds to each java file.

Parameters:
javaElement - a node in the IJavaModel that will be traversed
Returns:
a list of compilation units or null if no comp units are found

scanForCompilationUnits

public static List<ITypeRoot> scanForCompilationUnits(boolean includeClassfiles)
Traverses the workspace for CompilationUnits and (optionally) class files.

Parameters:
includeClassfiles - include compilation units availabile as .class files
Returns:
the list of all CompilationUnits in the workspace or null if no comp units were found.

collectCompilationUnits

public static List<ITypeRoot> collectCompilationUnits(IJavaElement javaElement,
                                                      boolean includeArchives)
A recursive traversal of the IJavaModel starting from the given element to collect all ITypeRoots, optionally including .class files available in libraries. If the given element is a type root it will be included no matter what. Each compilation unit corresponds to each java file.

Parameters:
javaElement - a node in the IJavaModel that will be traversed
includeArchives - whether to descend into binary libraries of .class files
Returns:
a list of compilation units or null if no comp units are found

parseCompilationUnits

public static Map<ICompilationUnit,ASTNode> parseCompilationUnits(List<ICompilationUnit> compilationUnits)
Goes through a list of compilation units and parses them. The act of parsing creates the AST structures from the source code.

Parameters:
compilationUnits - the list of compilation units to parse
Returns:
the mapping from compilation unit to the AST roots of each

scanForMethodDeclarations

public static List<MethodDeclaration> scanForMethodDeclarations(Map<ICompilationUnit,ASTNode> compilationUnitToASTNode)
Collects all top level methods from CompilationUnits. (Embedded Methods are currently not collected.)

Parameters:
compilationUnitToASTNode - the mapping of CompilationUnits to preparsed ASTNodes
Returns:
the list of all top level methods within the CompilationUnits

scanForMethodDeclarationsFromAST

public static List<MethodDeclaration> scanForMethodDeclarationsFromAST(ASTNode node)
Collects all top level methods from an AST including embedded methods.

Parameters:
node - the root of an AST
Returns:
all top level methods within the AST

scanForBindings

public static Map<String,ASTNode> scanForBindings(Map<ICompilationUnit,ASTNode> compilationUnitToASTNode)

scanForBindings

public static Map<String,ASTNode> scanForBindings(ICompilationUnit compUnit,
                                                  ASTNode node)

findCompilationUnits

public static List<ICompilationUnit> findCompilationUnits(List<String> files)
Returns the list of compilation units for a given list of file names. All compilation units that contain one of the given strings are returned.

Parameters:
files - List of file names to search for. They will be compared to the result of getWorkspaceRelativeName(IJavaElement).
Returns:
List of compilation units for a given list of file names.

getWorkspaceRelativeName

public static String getWorkspaceRelativeName(IJavaElement element)
Walks up the Java model hierarchy and separates the names of encountered elements by forward slashes

Parameters:
element -
Returns:
Symbolic name of the given Java element relative to the workspace root

getASTNodeFromCompilationUnit

public static ASTNode getASTNodeFromCompilationUnit(ITypeRoot compUnit)
Gets the root ASTNode for a compilation unit, with bindings on.

Parameters:
compUnit - never null
Returns:
the root ASTNode for a compilation unit, with bindings on.
Throws:
IllegalStateException - if compUnit doesn't have a source attachment
See Also:
ASTParser.createAST(org.eclipse.core.runtime.IProgressMonitor)

getDeclNodeFromType

public static Option<TypeDeclaration> getDeclNodeFromType(IType type)
Given an IType from the model, this method will return the ast node associated with that type, or null if it doesn't exist.

Returns:
NONE if the ast node associated with this type could not be found.

getMethodDeclFromModel

public static Option<MethodDeclaration> getMethodDeclFromModel(IMethod method)
Returns the AST node associated with the given model element, which in this case is a method.

Parameters:
method -
Returns: