alt.jiapi.reflect
Class JiapiClass

java.lang.Object
  extended by alt.jiapi.reflect.JiapiClass

public class JiapiClass
extends java.lang.Object

This class represents a bytecode of a Class. It contains methods to add and remove JiapiMethods and JiapiFields.

JiapiClass is an abstract class. Concrete classes are loaded using Loader.

Loader loader = new Loader(); JiapiClass clazz = loader.loadClass(className);

Version:
$Revision: 1.38 $ $Date: 2010/04/29 05:54:57 $
Author:
Mika Riekkinen, Joni Suominen
See Also:
Loader

Constructor Summary
JiapiClass(ClassFile classFile)
           
 
Method Summary
 JiapiField addField(int modifiers, java.lang.String type, java.lang.String name)
          Adds a new Field to this JiapiClass.
 JiapiField addField(java.lang.String name)
          Adds a named Field to this JiapiClass.
 void addInterface(JiapiClass interfaceType)
          Adds an interface for a class.
 void addInterface(java.lang.String interfaceType)
          Adds an interface for a class.
 JiapiMethod addMethod(int modifiers, java.lang.String methodName, Signature signature)
          Adds a new method to this class.
 JiapiMethod addMethod(JiapiMethod m)
          Adds a new method to this class.
static JiapiClass createClass(java.lang.String name)
          Create empty JiapiClass.
 void dump(java.io.OutputStream out)
          Dumps bytecodes of this JiapiClass to OutputStream given.
 byte[] getByteCode()
          Get the byte-code of this class.
 ConstantPool getConstantPool()
          Get the ConstantPool related to this JiapiClass.
 JiapiAnnotation[] getDeclaredAnnotations()
          Get all the declared annotations of this Class.
 JiapiField getDeclaredField(java.lang.String name)
          Get a declared field of a class.
 JiapiField[] getDeclaredFields()
          Get all the fields declared by this JiapiClass.
 JiapiMethod getDeclaredMethod(java.lang.String name, JiapiClass[] parameterTypes)
          Get a specific method from a class.
 JiapiMethod getDeclaredMethod(java.lang.String name, java.lang.String[] parameterTypeNames)
          Get a specific method from a class.
 JiapiMethod[] getDeclaredMethods()
          Get all the methods of this JiapiClass.
 JiapiField getField(java.lang.String name)
          Get a field from a class or from any of its superclasses.
 JiapiField[] getFields()
          Get all the Fields of this JiapiClass and its superclasses.
 java.lang.String[] getInterfaceNames()
          Gets all the interfaces, that this class directly implements.
 JiapiClass[] getInterfaceTypes()
          Gets all the interfaces, that this class directly implements.
 Loader getLoader()
          Gets the Loader, that loaded this JiapiClass.
 JiapiMethod getMethod(java.lang.String name, JiapiClass[] parameterTypes)
          Get a specific method from a class or any of its superclasses.
 JiapiMethod getMethod(java.lang.String name, java.lang.String[] parameterTypeNames)
          Get a specific method from a class or any of its superclasses.
 JiapiMethod[] getMethods()
          Get all the methods of this JiapiClass and its superclasses.
 int getModifiers()
          Get modifiers of this JiapiClass.
 java.lang.String getName()
          Get name of a class.
 java.lang.String getPackageName()
          Get the package where this class is defined.
 JiapiClass getSuperclass()
          Gets the super class of this JiapiClass.
 boolean isInterface()
          Tests, whether this JiapiClass is an interface or not.
static void main(java.lang.String[] args)
           
static JiapiClass parseClass(byte[] bytes)
          Parses byte[] for a class definition.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JiapiClass

public JiapiClass(ClassFile classFile)
Method Detail

addField

public JiapiField addField(int modifiers,
                           java.lang.String type,
                           java.lang.String name)
                    throws FieldExistsException
Adds a new Field to this JiapiClass.

Parameters:
modifiers - Modifiers of the field
type - Type of the field to be added. Type is given in simple form, like 'java.lang.Object' or 'int'
name - Name of the field to be added
Returns:
JiapiField which was added
Throws:
FieldExistsException - if a field with a same name already exists

addField

public JiapiField addField(java.lang.String name)
                    throws FieldExistsException
Adds a named Field to this JiapiClass. Field will be public and of type java.lang.Object

Parameters:
name - Name of the field to be added
Returns:
JiapiField which was added
Throws:
FieldExistsException - if a field with a same name already exists

addInterface

public void addInterface(JiapiClass interfaceType)
                  throws java.lang.IllegalArgumentException
Adds an interface for a class. If current class is an interface, it extends the added interface. Otherwise it implements it.

Parameters:
interfaceType - the type of an interface
Throws:
java.lang.IllegalArgumentException - is thrown, if interfaceType is not an interface.

addInterface

public void addInterface(java.lang.String interfaceType)
Adds an interface for a class. If current class is an interface, it extends the added interface. Otherwise it implements it.

Parameters:
interfaceType - the type of an interface

addMethod

public JiapiMethod addMethod(int modifiers,
                             java.lang.String methodName,
                             Signature signature)
                      throws MethodExistsException
Adds a new method to this class. Method, that is added, do not contain any bytecode. It is the responsibility of the developer to provide later some meaningful method body.

Parameters:
modifiers - access modifiers of a class
methodName - name of a method
signature - signature of a method
Returns:
JiapiMethod which was added
Throws:
MethodExistsException - if a method with a same name and parameter signature already exists
See Also:
Modifier

addMethod

public JiapiMethod addMethod(JiapiMethod m)
                      throws MethodExistsException
Adds a new method to this class. The signature is derived from given methods signature.

Parameters:
m - JiapiMethod to be used as method signature
Returns:
JiapiMethod which was added
Throws:
MethodExistsException - if a method with a same name and parameter signature already exists

createClass

public static JiapiClass createClass(java.lang.String name)
Create empty JiapiClass. Created JiapiClass has all internal datastructures initialized so, that when dumped to byte[], it is a valid java class file. It has not methods, fields etc.

Parameters:
name - Name of the JiapiClass to create
Returns:
an instance of JiapiClass

dump

public void dump(java.io.OutputStream out)
          throws java.io.IOException
Dumps bytecodes of this JiapiClass to OutputStream given.

Parameters:
out - OutputStream to use
Throws:
java.io.IOException

getByteCode

public byte[] getByteCode()
Get the byte-code of this class.

Returns:
an array of bytecodes

getConstantPool

public ConstantPool getConstantPool()
Get the ConstantPool related to this JiapiClass.

Returns:
ConstantPool of this JiapiClass

getDeclaredAnnotations

public JiapiAnnotation[] getDeclaredAnnotations()
Get all the declared annotations of this Class.

Returns:
declared annotations

getDeclaredField

public JiapiField getDeclaredField(java.lang.String name)
                            throws java.lang.NoSuchFieldException
Get a declared field of a class.

Parameters:
name - a name of a field
Returns:
a field with a given name
Throws:
java.lang.NoSuchFieldException - if matching field is not found

getDeclaredFields

public JiapiField[] getDeclaredFields()
Get all the fields declared by this JiapiClass. If this JiapiClass has no fields, an array of length 0 is returned. A new array is created each time this method is called, so adding and removing items from array does not reflect such changes back into JiapiClass.

Returns:
an array of fields in this class

getDeclaredMethod

public JiapiMethod getDeclaredMethod(java.lang.String name,
                                     JiapiClass[] parameterTypes)
                              throws java.lang.NoSuchMethodException
Get a specific method from a class.

Parameters:
parameterTypes - An array of JiapiClass, where each element represents a parameter.
Returns:
a method which matches the given name and parameter types
Throws:
java.lang.NoSuchMethodException - if matching method is not found

getDeclaredMethod

public JiapiMethod getDeclaredMethod(java.lang.String name,
                                     java.lang.String[] parameterTypeNames)
                              throws java.lang.NoSuchMethodException
Get a specific method from a class. Parameter type names are given in simple form. This would be the same as used when programming java code. Some examples of simple form: java.lang.Object, int, int[], my.stuff.Thing[]

Example:

   JiapiClass clazz = ...;
   String[] paramTypeNames = new String[] {"java.lang.Object[]", "int"};
   JiapiMethod m = clazz.getMethod("foo", paramTypeNames);
 
Would return a method foo(Object[], int)

Parameters:
name - Name of the method
parameterTypeNames - Type names of the parameters
Returns:
a method which matches the given name and parameter types
Throws:
java.lang.NoSuchMethodException - if matching method is not found

getDeclaredMethods

public JiapiMethod[] getDeclaredMethods()
Get all the methods of this JiapiClass. If this JiapiClass has no methods, an array of length 0 is returned. A new array is created each time this method is called, so adding and removing items from array does not reflect such changes back into JiapiClass.

Returns:
an array of methods in this class

getField

public JiapiField getField(java.lang.String name)
                    throws java.lang.NoSuchFieldException,
                           java.lang.ClassNotFoundException
Get a field from a class or from any of its superclasses.

Parameters:
name - a name of a field
Returns:
a field with a given name
Throws:
java.lang.NoSuchFieldException - if matching field is not found
java.lang.ClassNotFoundException - is thrown if superclass could not be loaded

getFields

public JiapiField[] getFields()
                       throws java.lang.ClassNotFoundException
Get all the Fields of this JiapiClass and its superclasses. All of the fields in this class and superclasses are returned.

Returns:
an array of fields in this class and its superclasses
Throws:
java.lang.ClassNotFoundException - is thrown if superclass could not be loaded
See Also:
getDeclaredFields() for further info

getInterfaceNames

public java.lang.String[] getInterfaceNames()
Gets all the interfaces, that this class directly implements.

Returns:
names of the implemented interfaces

getInterfaceTypes

public JiapiClass[] getInterfaceTypes()
                               throws java.lang.ClassNotFoundException
Gets all the interfaces, that this class directly implements.

Returns:
names of the implemented interfaces
Throws:
java.lang.ClassNotFoundException

getLoader

public Loader getLoader()
Gets the Loader, that loaded this JiapiClass.

Returns:
Loader

getMethod

public JiapiMethod getMethod(java.lang.String name,
                             JiapiClass[] parameterTypes)
                      throws java.lang.NoSuchMethodException,
                             java.lang.ClassNotFoundException
Get a specific method from a class or any of its superclasses.

Parameters:
parameterTypes - An array of JiapiClass, where each element represents a parameter.
Returns:
a method which matches the given name and parameter types
Throws:
java.lang.NoSuchMethodException - if matching method is not found
java.lang.ClassNotFoundException - is thrown if superclass could not be loaded

getMethod

public JiapiMethod getMethod(java.lang.String name,
                             java.lang.String[] parameterTypeNames)
                      throws java.lang.NoSuchMethodException,
                             java.lang.ClassNotFoundException
Get a specific method from a class or any of its superclasses. Parameter type names are given in simple form. This would be the same as used when programming java code. Some examples of simple form: java.lang.Object, int, int[], my.stuff.Thing[]

Example:

   JiapiClass clazz = ...;
   String[] paramTypeNames = new String[] {"java.lang.Object[]", "int"};
   JiapiMethod m = clazz.getMethod("foo", paramTypeNames);
 
Would return a method foo(Object[], int)

Parameters:
name - Name of the method
parameterTypeNames - Type names of the parameters
Returns:
a method which matches the given name and parameter types
Throws:
java.lang.NoSuchMethodException - if matching method is not found
java.lang.ClassNotFoundException - is thrown if superclass could not be loaded

getMethods

public JiapiMethod[] getMethods()
                         throws java.lang.ClassNotFoundException
Get all the methods of this JiapiClass and its superclasses.

Returns:
an array of methods in this class
Throws:
java.lang.ClassNotFoundException - is thrown if superclass could not be loaded
See Also:
getDeclaredMethods() for further info

getModifiers

public int getModifiers()
Get modifiers of this JiapiClass.

Returns:
access modifiers of a class
See Also:
Modifier

getName

public java.lang.String getName()
Get name of a class.

Returns:
name of a class

getPackageName

public java.lang.String getPackageName()
Get the package where this class is defined.

Returns:
a package name of a class, or null if this class does not belong to any package.

getSuperclass

public JiapiClass getSuperclass()
                         throws java.lang.ClassNotFoundException
Gets the super class of this JiapiClass. If this JiapiClass represents java.lang.Object, a null is returned.

Returns:
super class of the class represented by this JiapiClass
Throws:
java.lang.ClassNotFoundException - is thrown, if superclass could not be loaded

isInterface

public boolean isInterface()
Tests, whether this JiapiClass is an interface or not.

Returns:
true, if this JiapiClass represents an interface.

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception

parseClass

public static JiapiClass parseClass(byte[] bytes)
                             throws java.io.IOException
Parses byte[] for a class definition.

Throws:
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2001. Documenation generated August 26 2011.