Package serp.bytecode

Class 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
    • Constructor Summary

      Constructors 
      Constructor Description
      Project()
      Default constructor.
      Project​(java.lang.String name)
      Construct a named project.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void acceptVisit​(BCVisitor visit)
      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.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Project

        public Project()
        Default constructor.
      • Project

        public Project​(java.lang.String name)
        Construct a named 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,
                                 java.lang.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:
        java.lang.RuntimeException - on parse error
      • loadClass

        public BCClass loadClass​(java.lang.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:
        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 new BCClass 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 new BCClass 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 new BCClass 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 new BCClass will be created from the given bytecode.
        Throws:
        java.lang.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​(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 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