edu.rice.cs.cunit.classFile
Class ClassFileTools

java.lang.Object
  extended by edu.rice.cs.cunit.classFile.ClassFileTools

public class ClassFileTools
extends java.lang.Object

Tools for dealing with class files.

Author:
Mathias Ricken

Nested Class Summary
static class ClassFileTools.ClassFileToolsTest
          Test cases.
static class ClassFileTools.ClassLocation
          Helper class for findClassFile.
 
Constructor Summary
ClassFileTools()
           
 
Method Summary
static boolean classNameMatches(java.lang.String className, java.lang.String... classNamePatterns)
          Returns true if the specified class name matches one of the patterns.
static ClassFileTools.ClassLocation findClassFile(java.lang.String className, java.util.List<java.lang.String> classPath)
          Find a class.
static ClassFile findClassInFiles(java.lang.String className, java.util.Set<java.lang.String> filesToConsider)
          Find a class in the files to consider and return its class file info.
static void generateDependencyFixPoint(java.util.Set<java.lang.String> classNames, java.util.List<java.lang.String> classPath, java.util.Set<java.lang.String> classesUsed, ILambda<java.lang.Object,ClassFileTools.ClassLocation> processClassNameLambda, ILambda<java.lang.Object,java.lang.String> processNotFoundLambda)
          Generate the class dependency fix point.
static java.lang.String getAccessString(short flags)
          Return a string that represents the access flags set.
static java.lang.Class<?> getClassFromType(java.lang.String type)
          Get the Class instance for the given type.
static java.lang.Class<?> getClassFromType(java.lang.String type, boolean initialize, java.lang.ClassLoader loader)
          Get the Class instance for the given type.
static java.lang.String getClassName(java.lang.String s)
          Return class name in Java form.
static java.lang.String getClassNameFromType(java.lang.String type)
          Get the class/primitive name for the given type.
static java.util.Set<java.lang.String> getClassNamesUsed(ClassFile cf)
          Returns a set of class names used in a class files.
static java.util.Set<java.lang.String> getClassNamesUsed(java.lang.String s)
          Return set of class names used in this mangled name.
 java.util.Set<java.lang.String> getDependencyFixPoint(java.util.Set<java.lang.String> classNames, java.util.List<java.lang.String> classPath)
          Return the class dependency fix point.
static java.lang.String getNextSignature(java.lang.String sig)
          Return the next descriptor from a string of concatenated signatures.
static char getPrimitiveTypeChar(java.lang.String primTypeName)
          Returns the type character associated with a primitive type name, or 0 if there was an error.
static java.util.List<java.lang.String> getSignatures(java.lang.String sig)
          Return a list of signatures.
static java.lang.String getTypeString(java.lang.String typeString, java.lang.String varName)
          Translate a type descriptor descriptor and a variable name into a Java declaration.
static boolean isPrimitive(java.lang.String typeString)
          Return true if this is a primitive type or an array of primitive types.
static ClassFile loadClassFromStream(java.io.InputStream is)
          Load class file info from the stream.
private static void processAnnotation(AAnnotationsAttributeInfo.Annotation a, java.util.HashSet<java.lang.String> classesUsed)
          Helper for getClassNamesUsed that processes an annotation.
private static void processMemberValue(AAnnotationsAttributeInfo.Annotation.AMemberValue mv, java.util.HashSet<java.lang.String> classesUsed)
          Helper for processAnnotation that processes an annotation member value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassFileTools

public ClassFileTools()
Method Detail

getAccessString

public static java.lang.String getAccessString(short flags)
Return a string that represents the access flags set.

Parameters:
flags - access flags
Returns:
string with access flags as words

getTypeString

public static java.lang.String getTypeString(java.lang.String typeString,
                                             java.lang.String varName)
Translate a type descriptor descriptor and a variable name into a Java declaration.

Parameters:
typeString - type descriptor descriptor
varName - variable name
Returns:
declaration

getPrimitiveTypeChar

public static char getPrimitiveTypeChar(java.lang.String primTypeName)
Returns the type character associated with a primitive type name, or 0 if there was an error.

Parameters:
primTypeName - primitive type name, e.g. "void" or "byte"
Returns:
primitive type character, e.g. 'V' or 'B', or 0 if the type name was not recognized

isPrimitive

public static boolean isPrimitive(java.lang.String typeString)
Return true if this is a primitive type or an array of primitive types.

Parameters:
typeString - type descriptor descriptor
Returns:
true if primitive type or array of primitive types

getSignatures

public static java.util.List<java.lang.String> getSignatures(java.lang.String sig)
Return a list of signatures.

Parameters:
sig - concatenated signatures
Returns:
list of signatures

getNextSignature

public static java.lang.String getNextSignature(java.lang.String sig)
Return the next descriptor from a string of concatenated signatures. For example, if the descriptor was "[BII", this method would return "II".

Parameters:
sig - concatenated signatures
Returns:
next descriptor

getClassName

public static java.lang.String getClassName(java.lang.String s)
Return class name in Java form.

Parameters:
s - mangled class name
Returns:
Java class name

getClassNamesUsed

public static java.util.Set<java.lang.String> getClassNamesUsed(java.lang.String s)
Return set of class names used in this mangled name. Primitives are not included.

Parameters:
s - mangled class name
Returns:
set of Java class names used

getClassNamesUsed

public static java.util.Set<java.lang.String> getClassNamesUsed(ClassFile cf)
Returns a set of class names used in a class files. Primitives are not included.

Parameters:
cf - class file
Returns:
set of class names used

processAnnotation

private static void processAnnotation(AAnnotationsAttributeInfo.Annotation a,
                                      java.util.HashSet<java.lang.String> classesUsed)
Helper for getClassNamesUsed that processes an annotation.

Parameters:
a - annotation
classesUsed - set of classes used

processMemberValue

private static void processMemberValue(AAnnotationsAttributeInfo.Annotation.AMemberValue mv,
                                       java.util.HashSet<java.lang.String> classesUsed)
Helper for processAnnotation that processes an annotation member value.

Parameters:
mv - member value
classesUsed - set of classes used

findClassFile

public static ClassFileTools.ClassLocation findClassFile(java.lang.String className,
                                                         java.util.List<java.lang.String> classPath)
Find a class.

Parameters:
className - class to find
classPath - list with class path entries
Returns:
class location

generateDependencyFixPoint

public static void generateDependencyFixPoint(java.util.Set<java.lang.String> classNames,
                                              java.util.List<java.lang.String> classPath,
                                              java.util.Set<java.lang.String> classesUsed,
                                              ILambda<java.lang.Object,ClassFileTools.ClassLocation> processClassNameLambda,
                                              ILambda<java.lang.Object,java.lang.String> processNotFoundLambda)
Generate the class dependency fix point.

Parameters:
classNames - list of class names
classPath - list of class path entries
classesUsed - set of used classes
processClassNameLambda - lambda to apply class names to
processNotFoundLambda - lambda to apply to missing class names to NOTE: The ClassLocation's input stream will get closed after the lambda has been invoked. That means it should not be stored and used later.

getDependencyFixPoint

public java.util.Set<java.lang.String> getDependencyFixPoint(java.util.Set<java.lang.String> classNames,
                                                             java.util.List<java.lang.String> classPath)
Return the class dependency fix point.

Parameters:
classNames - class names
classPath - class path entries
Returns:
class dependency fix point

findClassInFiles

public static ClassFile findClassInFiles(java.lang.String className,
                                         java.util.Set<java.lang.String> filesToConsider)
Find a class in the files to consider and return its class file info.

Parameters:
className - name of class to find
filesToConsider - set of file names (*.class and *.jar) to consider during the search.
Returns:
class file info or null if not found

loadClassFromStream

public static ClassFile loadClassFromStream(java.io.InputStream is)
                                     throws java.io.IOException
Load class file info from the stream.

Parameters:
is - input stream
Returns:
class file info
Throws:
java.io.IOException

classNameMatches

public static boolean classNameMatches(java.lang.String className,
                                       java.lang.String... classNamePatterns)
Returns true if the specified class name matches one of the patterns. ! = make sure class name does NOT match this pattern (prefix only) ? = one character
* = zero or more characters except '.' *** = zero or more characters including '.' java.**.lang [abc] = Either a, b or c [a-mA-M] = A letter from the ranges a-m or A-M (inclusive) The entire class name has to match the pattern, i.e. the regex characters ^ and $ are implicit.

Parameters:
className - class name
classNamePatterns - variable argument list of patterns
Returns:
true if the specified class name matches one of the patterns

getClassFromType

public static java.lang.Class<?> getClassFromType(java.lang.String type)
                                           throws java.lang.ClassNotFoundException
Get the Class instance for the given type. The type can be a class formatted "Lfoo/bar/MyClass;", a primitive type formatted "I", or an array formatted "[I" or "[Lfoo/bar/MyClass;".

Parameters:
type - type string
Returns:
Class instance
Throws:
java.lang.ClassNotFoundException

getClassFromType

public static java.lang.Class<?> getClassFromType(java.lang.String type,
                                                  boolean initialize,
                                                  java.lang.ClassLoader loader)
                                           throws java.lang.ClassNotFoundException
Get the Class instance for the given type. The type can be a class formatted "Lfoo/bar/MyClass;", a primitive type formatted "I", or an array formatted "[I" or "[Lfoo/bar/MyClass;".

Parameters:
type - type string
initialize - whether the class needs to be initialized
loader - the class loader
Returns:
Class instance
Throws:
java.lang.ClassNotFoundException

getClassNameFromType

public static java.lang.String getClassNameFromType(java.lang.String type)
Get the class/primitive name for the given type. The type can be a class formatted "Lfoo/bar/MyClass;", a primitive type formatted "I", or an array formatted "[I" or "[Lfoo/bar/MyClass;".

Parameters:
type - type string
Returns:
type name