Package serp.bytecode
Class Project
- java.lang.Object
-
- serp.bytecode.Project
-
- All Implemented Interfaces:
VisitAcceptor
public class Project extends java.lang.Object implements VisitAcceptor
The Project represents a working set of classes. It caches parsed bytecode and is responsible for bytecode class creation. Currently changes made in one class are not reflected in other classes, though this will be an option in the future.Bytecode that has been parsed is held in a cache so that retrieving a class with the same name multiple times always returns the same
BCClass
instance.A future goal is to eventually have facilities for traversing jars or directory structures to find classes that meet a given criteria (such as implementing a given interface, etc) and to perform operations on entire projects, similar to aspect-oriented programming.
- Author:
- Abe White
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
acceptVisit(BCVisitor visit)
Accept a visit from aBCVisitor
, calling the appropriate methods to notify the visitor that it has entered this entity, and to provide it with the proper callbacks for each sub-entity owned by this one.void
clear()
Clears all classes from this project.boolean
containsClass(java.lang.Class type)
Return true if the project already contains the given class.boolean
containsClass(java.lang.String type)
Return true if the project already contains the given class.boolean
containsClass(BCClass type)
Return true if the project already contains the given class.BCClass[]
getClasses()
Return all loaded classes in the project.java.lang.String
getName()
Return the project name, or null if unset.NameCache
getNameCache()
Return the name cache, which includes utilities for converting names from internal to external form and vice versa.BCClass
loadClass(java.io.File classFile)
Load the bytecode from the given class file.BCClass
loadClass(java.io.File classFile, java.lang.ClassLoader loader)
Load the bytecode from the given class file.BCClass
loadClass(java.io.InputStream in)
Load the bytecode from the given stream.BCClass
loadClass(java.io.InputStream in, java.lang.ClassLoader loader)
Load the bytecode from the given stream.BCClass
loadClass(java.lang.Class type)
Load the bytecode for the given class.BCClass
loadClass(java.lang.String name)
Load a class with the given name.BCClass
loadClass(java.lang.String name, java.lang.ClassLoader loader)
Load the bytecode for the class with the given name.BCClass
loadClass(BCClass bc)
Import the given bytecode from another project.boolean
removeClass(java.lang.Class type)
Remove a class from this project.boolean
removeClass(java.lang.String type)
Remove a class from this project.boolean
removeClass(BCClass type)
Remove a class from this project.
-
-
-
Method Detail
-
getName
public java.lang.String getName()
Return the project name, or null if unset.
-
getNameCache
public NameCache getNameCache()
Return the name cache, which includes utilities for converting names from internal to external form and vice versa.
-
loadClass
public BCClass loadClass(java.lang.String name)
Load a class with the given name.- See Also:
loadClass(String,ClassLoader)
-
loadClass
public BCClass loadClass(java.lang.String name, java.lang.ClassLoader loader)
Load the bytecode for the class with the given name. If aBCClass
with the given name already exists in this project, it will be returned. Otherwise, a newBCClass
will be created with the given name and returned. If the name represents an existing type, the returned instance will contain the parsed bytecode for that type. If the name is of a primitive or array type, the returned instance will act accordingly.- Parameters:
name
- the name of the class, including packageloader
- the class loader to use to search for an existing class with the given name; if null defaults to the context loader of the current thread- Throws:
java.lang.RuntimeException
- on parse error
-
loadClass
public BCClass loadClass(java.lang.Class type)
Load the bytecode for the given class. If aBCClass
with the name of the given class already exists in this project, it will be returned. Otherwise, the bytecode of the given class will be parsed and returned as a newBCClass
. If the given class is an array or primitive type, the returned instance will act accordingly.- Parameters:
type
- the class to parse- Throws:
java.lang.RuntimeException
- on parse error
-
loadClass
public BCClass loadClass(java.io.File classFile)
Load the bytecode from the given class file. If this project already contains the class in the given file, it will be returned. Otherwise a newBCClass
will be created from the given bytecode.- Throws:
java.lang.RuntimeException
- on parse error
-
loadClass
public BCClass loadClass(java.io.File classFile, java.lang.ClassLoader loader)
Load the bytecode from the given class file. If this project already contains the class in the given file, it will be returned. Otherwise a newBCClass
will be created from the given bytecode.- Throws:
java.lang.RuntimeException
- on parse error
-
loadClass
public BCClass loadClass(java.io.InputStream in)
Load the bytecode from the given stream. If this project already contains the class in the given stream, it will be returned. Otherwise a newBCClass
will be created from the given bytecode.- Throws:
java.lang.RuntimeException
- on parse error
-
loadClass
public BCClass loadClass(java.io.InputStream in, java.lang.ClassLoader loader)
Load the bytecode from the given stream. If this project already contains the class in the given stream, it will be returned. Otherwise a newBCClass
will be created from the given bytecode.- Throws:
java.lang.RuntimeException
- on parse error
-
clear
public void clear()
Clears all classes from this project.
-
removeClass
public boolean removeClass(java.lang.String type)
Remove a class from this project. After removal, the result of any further operations on the class is undefined.- Returns:
- true if the class belonged to this project, false otherwise
-
removeClass
public boolean removeClass(java.lang.Class type)
Remove a class from this project. After removal, the result of any further operations on the class is undefined.- Returns:
- true if the class belonged to this project, false otherwise
-
removeClass
public boolean removeClass(BCClass type)
Remove a class from this project. After removal, the result of any further operations on the class is undefined.- Returns:
- true if the class belonged to this project, false otherwise
-
getClasses
public BCClass[] getClasses()
Return all loaded classes in the project.
-
containsClass
public boolean containsClass(java.lang.String type)
Return true if the project already contains the given class.
-
containsClass
public boolean containsClass(java.lang.Class type)
Return true if the project already contains the given class.
-
containsClass
public boolean containsClass(BCClass type)
Return true if the project already contains the given class.
-
acceptVisit
public void acceptVisit(BCVisitor visit)
Description copied from interface:VisitAcceptor
Accept a visit from aBCVisitor
, calling the appropriate methods to notify the visitor that it has entered this entity, and to provide it with the proper callbacks for each sub-entity owned by this one.- Specified by:
acceptVisit
in interfaceVisitAcceptor
-
-