Package serp.bytecode

Class Project

java.lang.Object
serp.bytecode.Project
All Implemented Interfaces:
VisitAcceptor

public class Project extends 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
  • Constructor Details

    • Project

      public Project()
      Default constructor.
    • Project

      public Project(String name)
      Construct a named project.
  • Method Details

    • getName

      public 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(String name)
      Load a class with the given name.
      See Also:
    • loadClass

      public BCClass loadClass(String name, ClassLoader loader)
      Load the bytecode for the class with the given name. If a BCClass with the given name already exists in this project, it will be returned. Otherwise, a new BCClass 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 package
      loader - 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:
      RuntimeException - on parse error
    • loadClass

      public BCClass loadClass(Class type)
      Load the bytecode for the given class. If a BCClass 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 new BCClass. If the given class is an array or primitive type, the returned instance will act accordingly.
      Parameters:
      type - the class to parse
      Throws:
      RuntimeException - on parse error
    • loadClass

      public BCClass loadClass(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 new BCClass will be created from the given bytecode.
      Throws:
      RuntimeException - on parse error
    • loadClass

      public BCClass loadClass(File classFile, 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 new BCClass will be created from the given bytecode.
      Throws:
      RuntimeException - on parse error
    • loadClass

      public BCClass loadClass(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 new BCClass will be created from the given bytecode.
      Throws:
      RuntimeException - on parse error
    • loadClass

      public BCClass loadClass(InputStream in, 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 new BCClass will be created from the given bytecode.
      Throws:
      RuntimeException - on parse error
    • loadClass

      public BCClass loadClass(BCClass bc)
      Import the given bytecode from another project. If a BCClass with the same name already exists in this project, it will be returned. Otherwise, a new BCClass will be created from the information in the given class.
    • clear

      public void clear()
      Clears all classes from this project.
    • removeClass

      public boolean removeClass(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(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(String type)
      Return true if the project already contains the given class.
    • containsClass

      public boolean containsClass(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 a BCVisitor, 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 interface VisitAcceptor