|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.xml.utils.synthetic.Class
org.apache.xml.utils.synthetic.Class is a mutable equivalent of java.lang.Class. Instances represent classes and interfaces in a running Java application, or class descriptions under construction. In the former case, org.apache.xml.utils.synthetic.Class operates as a proxy for the "real" java.lang.Class object; in the latter, it consults data structures defined in the org.apache.xml.utils.synthetic.reflection.* package.
Unlike java.lang.Class, org.apache.xml.utils.synthetic.Class has a pair of factories (fromName and fromClass). It can also be switched from synthetic to proxy operation after construction, by setting the realClass property; this is intended to allow these definitions to be "compiled in place".
For convenient use, org.apache.xml.utils.synthetic.Class implements an extended version of the java.lang.Class API -- but is not a subclass thereof, since java.lang.Class is Final (presumably for security reasons).
DEVELOPMENT NOTE: Methods not yet implemented will throw IllegalStateException
I've added code to convert primitive names into their TYPEs, to accept foo[] as a synonym for [Lfoo, and to generate the right thing on output (getJava[Short]Name). Useful extension for code generation from Java-like source. We may want to factor these and toSource out, making org.apache.xml.utils.synthetic.Class addess only the JVM level and providing subclasses or access tools that handle language syntax (Java source, NetRexx source, etc.)
Field Summary | |
private Class[] |
allclasses
Field allclasses: an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object. |
private Constructor[] |
allconstructors
Field allconstructors: an array containing Constructor objects reflecting all the constructors of the class represented by this Class object. |
private Field[] |
allfields
Field allfields. |
private Method[] |
allmethods
Field allmethods. |
private Class[] |
declaredclasses
Field declaredclasses: an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. |
private Constructor[] |
declaredconstructors
Field declaredconstructors: an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. |
private Field[] |
declaredfields
Field declaredfields. |
private Method[] |
declaredmethods
Field declaredmethods. |
private Class |
declaringclass
Field declaringclass: If this object represents an inner class, the Class object that represents the class that declared it. |
private static Hashtable |
global_classtable
Class descriptions currently existing. |
private Class[] |
innerclasses
Field innerclasses. |
private Class[] |
interfaces
Field interfaces: A list of all interfaces implemented by the class or interface represented by this object. |
private boolean |
isInterface
Field isInterface: True if the Class object represents an interface type. |
(package private) static String[] |
kwd
Field kwd |
private int |
modifiers
Field modifiers: Java language modifiers for this class or interface, encoded in an integer. |
private String |
name
fully-qualified path.classname. |
private Class |
realclass
Actual Java class object. |
private Class |
superclass
Field superclass: If this object represents the class Object, this is null. |
(package private) static int[] |
val
Field val |
Constructor Summary | |
(package private) |
Class(Class realclass)
Construct a synthetic class as proxy/wrapper for an existing Java Class. |
(package private) |
Class(String fullname)
Construct a named-but-empty synthetic Class object. |
Method Summary | |
void |
addExtends(Class newclass)
Adds an "extends" description for the class or interface represented by this Class object |
void |
addImplements(Class newclass)
Adds an "implements" description for the class or interface represented by this Class object |
private void |
addInnerClass(Class newclass)
Declare that this class is an inner class of another. |
static Class |
declareClass(String className)
Start to create a synthetic Class with the given fully-qualified name. |
Constructor |
declareConstructor()
Adds a Constructor description for the class or interface represented by this Class object |
Field |
declareField(String name)
Adds a Field description for the class or interface represented by this Class object |
Class |
declareInnerClass(String className)
Declare a class contained within this class. |
Class |
declareInterface(Class newifce)
State that this class implements a specified interface. |
Method |
declareMethod(String name)
Adds a Method description for the class or interface represented by this Class object |
static Class |
forClass(Class cls)
Returns the synthetic Class object associated with the "real" class specified, creating one if it didn't already exist. |
static Class |
forName(String className)
Returns the synthetic Class object associated with the class with the given fully-qualified name. |
Class |
forNameInContext(String classname)
Like forName, but if the classname doesn't have a package prefix we first attempt to look it up as one of our own inner clases. |
Class[] |
getClasses()
Returns an array containing Class objects representing all the public classes and interfaces that are members of the class represented by this Class object. |
ClassLoader |
getClassLoader()
Determines the class loader for the class. |
Class |
getComponentType()
If this class represents an array type, returns the Class object representing the component type of the array; otherwise returns null. |
Constructor |
getConstructor(Class[] parameterTypes)
Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object. |
Constructor[] |
getConstructors()
Returns an array containing Constructor objects reflecting all the public constructors of the class represented by this Class object. |
Class[] |
getDeclaredClasses()
This method is not implemented in VAJAVA 3.0 Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. |
Constructor |
getDeclaredConstructor(Class[] parameterTypes)
Returns a Constructor object that reflects the specified declared constructor of the class or interface represented by this Class object. |
Constructor[] |
getDeclaredConstructors()
Returns an array of Constructor objects reflecting all the constructors declared by the class represented by this Class object. |
Field |
getDeclaredField(String name)
Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object. |
Field[] |
getDeclaredFields()
Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. |
Method |
getDeclaredMethod(String name,
Class[] parameterTypes)
Returns a Method object that reflects the specified declared method of the class or interface represented by this Class object. |
Method[] |
getDeclaredMethods()
Returns an array of Method objects reflecting all the methods declared by the class or interface represented by this Class object. |
Class |
getDeclaringClass()
This method is not implemented in VAJava 3.0 If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class of which it is a member (its declaring class). |
Field |
getField(String name)
Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object. |
Field[] |
getFields()
Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. |
Class[] |
getInnerClasses()
Fetch a list of classes contained within this class. |
Class[] |
getInterfaces()
Determines the interfaces implemented by the class or interface represented by this object. |
String |
getJavaName()
Like getName, but back-convert array notation escapes. ***** DOESN'T YET HANDLE ARRAYS OF PRIMITIVES! |
String |
getJavaShortName()
Like getShortName, but back-convert array notation escapes. ***** DOESN'T YET HANDLE ARRAYS OF PRIMITIVES! |
Method |
getMethod(String name,
Class[] parameterTypes)
Returns a Method object that reflects the specified public member method of the class or interface represented by this Class object. |
Method[] |
getMethods()
Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and and those inherited from superclasses and superinterfaces. |
int |
getModifiers()
Returns the Java language modifiers for this class or interface, encoded in an integer. |
String |
getName()
Retrieve the fully-qualified classname. |
String |
getPackageName()
Extract the package name for this class. ***** I don't think this handles array classes properly yet. |
Class |
getRealClass()
If this synthetic class is a wrapper for a "real" java.lang.Class -- either because it was instantiated as such or because it has been compiled -- this method will return that class. |
URL |
getResource(String name)
Finds a resource with the specified name. |
InputStream |
getResourceAsStream(String name)
Finds a resource with a given name. |
String |
getShortName()
Extract just the local name of this class, minus the package prefix. ***** I don't think this handles array types properly yet. |
Object[] |
getSigners()
Get the signers of this class. |
Class |
getSuperclass()
If this object represents any class other than the class Object, then the object that represents the superclass of that class is returned. |
boolean |
isArray()
If this Class object represents an array type, returns true, otherwise returns false. |
boolean |
isAssignableFrom(Class cls)
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. |
boolean |
isAssignableFrom(Class cls)
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. |
boolean |
isInstance(Object obj)
This method is the dynamic equivalent of the Java language instanceof operator. |
boolean |
isInterface()
Determines if the specified Class object represents an interface type. |
void |
isInterface(boolean isInterface)
Assert that the specified Class object represents an interface type. |
boolean |
isPrimitive()
Determines if the specified Class object represents a primitive Java type. |
static int |
modifierFromString(String t)
Method modifierFromString |
static int |
modifiersFromString(String s)
Method modifiersFromString |
Object |
newInstance()
Creates a new instance of a class. |
static Class |
reallyDeclareClass(String className)
Start to create a synthetic Class with the given fully-qualified name. |
void |
setModifiers(int modifiers)
Set the Java language modifiers for this class or interface, encoded in an integer. |
void |
setRealClass(Class realclass)
This call is intended to allow an existing org.apache.xml.utils.synthetic.Class to be switched from purely descriptive mode to proxy mode ("reified"). |
void |
setSuperClass(Class superclass)
Set the superclass for this synthetic class. |
void |
setSuperClass(Class superclass)
Set the superclass for this synthetic class. |
private String |
tabset(int depth)
Method tabset |
void |
toSource(OutputStream out,
int depth)
Convenience for writing to, eg, System.out |
void |
toSource(PrintWriter out,
int depth)
Converts the object to a Java code stream. |
String |
toString()
Converts the object to a string. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
private static Hashtable global_classtable
private String name
private Class realclass
private int modifiers
private boolean isInterface
private Class superclass
private Class declaringclass
private Class[] interfaces
private Class[] allclasses
private Class[] declaredclasses
private Constructor[] allconstructors
private Constructor[] declaredconstructors
private Method[] allmethods
private Method[] declaredmethods
private Field[] allfields
private Field[] declaredfields
private Class[] innerclasses
static final int[] val
static final String[] kwd
Constructor Detail |
Class(Class realclass)
Creation date: (12-25-99 12:16:15 PM)
realclass
- java.lang.ClassClass(String fullname)
Creation date: (12-25-99 12:15:23 PM)
fullname
- full name of the class that is synthetized.Method Detail |
public static Class forClass(Class cls)
For example, the following code fragment returns
the runtime Class descriptor for the class named
mypackage.MyClass.
Class t =
Class.forName(java.lang.Class.forName("mypackage.MyClass"))
Note that if the user has manually created a org.apache.xml.utils.synthetic.Class with the same name before this call is issued, that object will be found instead. See also the declareClass call.
We need a better way to declare/define array classes, given a class object (synthetic or not).
cls
- the desired Java class.
public Class forNameInContext(String classname) throws ClassNotFoundException
classname
- the full or partial class name.
ClassNotFoundException
public static Class forName(String className) throws ClassNotFoundException
For example, the following code fragment returns
the runtime Class descriptor for the class named
mypackage.MyClass -- either as a synthetic or as
a standard Java class.
Class t =
Class.forName("mypackage.MyClass")
I've added support for arrays -- assuming any name that ends with ']' is an array. It probably needs to be made smarter, possibly via a subclass of org.apache.xml.utils.synthetic.Class.
className
- the fully qualified name of the desired class.
ClassNotFoundException
- if the class could not be found.public static Class declareClass(String className) throws SynthesisException
className
- the fully qualified name of the desired class.
SynthesisException
- if the class has been reified.public static Class reallyDeclareClass(String className)
className
- the fully qualified name of the desired class.
public Class[] getClasses()
NOTE: In a significant number of existing Java environments, this method is not implemented by the official Class object and always returns an empty array. So if you don't get any useful information from a proxied java.lang.Class, don't be surprised. I'm not sure if someone decided it was a potential security issue, or if Sun was lazy and everyone else followed suit.
ALSO NOTE: The above spec, as taken from java.lang.Class, doesn't provide any good way to distinguish the immediate superclass from all other superclasses. That makes it only marginally useful, which is no doubt one of the reasons folks have declined to implement it.
public ClassLoader getClassLoader()
public Class getComponentType()
NOTE: Since org.apache.xml.utils.synthetic.Class doesn't yet attempt to model array types, this will currently return false unless we are proxying such a type.
public Constructor getConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException, SynthesisException
The constructor to reflect is located by searching all the constructors of the class represented by this Class object for a public constructor with the exactly the same formal parameter types.
parameterTypes
- array of Class
objects that identify the constructor's formal
parameter types, in declared order.
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.
SynthesisException
public Constructor[] getConstructors() throws SecurityException
SecurityException
- if access to the information is denied.public Class[] getDeclaredClasses() throws SecurityException
Returns an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. This includes public, protected, default (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces. Returns an array of length 0 if the class declares no classes or interfaces as members, or if this Class object represents a primitive type.
SecurityException
- if access to the information is denied.public void addExtends(Class newclass) throws SynthesisException
newclass
- The class that this class extends.
SynthesisException
- if the class has been reified.public Constructor getDeclaredConstructor(Class[] parameterTypes) throws NoSuchMethodException, SecurityException
parameterTypes
- array of Class
objects that identify the constructor's formal
parameter types, in declared order.
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.public Constructor declareConstructor() throws SynthesisException
SynthesisException
- if the class has been reified.public Class declareInterface(Class newifce) throws SynthesisException
newifce
- org.apache.xml.utils.synthetic.Class representing the interface we want to add.
SynthesisException
- if the Class isn't an interfacepublic Constructor[] getDeclaredConstructors() throws SecurityException
See The Java Language Specification, section 8.2.
SecurityException
- if access to the information is denied.public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException
name
- String that specifies the simple name of the desired
field.
NoSuchFieldException
- if a field with the specified name is not found.
SecurityException
- if access to the information is denied.public Field declareField(String name) throws SynthesisException
name
- The name of the field.
SynthesisException
- if the class has been reified.public Field[] getDeclaredFields() throws SecurityException
SecurityException
- if access to the information is denied.public Method getDeclaredMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException
name
- String that specifies the simple
name of the desired method.parameterTypes
- array of Class
objects that identify the method's formal parameter
types, in declared order.
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.public Method declareMethod(String name) throws SynthesisException
name
- Name of method.
SynthesisException
- if the class has been reified.public Method[] getDeclaredMethods() throws SecurityException
See The Java Language Specification, section 8.2.
SecurityException
- if access to the information is denied.public Class getDeclaringClass()
If the class or interface represented by this Class object is a member of another class, returns the Class object representing the class of which it is a member (its declaring class). Returns null if this class or interface is not a member of any other class.
private void addInnerClass(Class newclass) throws SynthesisException
newclass
-
SynthesisException
public Class declareInnerClass(String className) throws SynthesisException
***** This requires lookup methods that operate in the context of a specific class, and per-class registries!
className
- Local name of inner class to create. This should _not_ be a
qualified name, unlike the normal forName() call. Its
hierarchy is established by the class within which it is
created.
SynthesisException
- if class could not be created.
SynthesisException
public Class[] getInnerClasses()
public Field getField(String name) throws NoSuchFieldException, SecurityException
The field to be reflected is located by searching all the member fields of the class or interface represented by this Class object for a public field with the specified name.
See The Java Language Specification, sections 8.2 and 8.3.
name
-
NoSuchFieldException
- if a field with the specified name is not
found.
SecurityException
- if access to the information is denied.public Field[] getFields() throws SecurityException
Specifically, if this Class object represents a class, returns the public fields of this class and of all its superclasses. If this Class object represents an interface, returns the fields of this interface and of all its superinterfaces. If this Class object represents an array type or a primitive type, returns an array of length 0.
The implicit length field for array types is not reflected by this method. User code should use the methods of class Array to manipulate arrays.
See The Java Language Specification, sections 8.2 and 8.3.
SecurityException
- if access to the information is denied.public Class[] getInterfaces()
If this object represents a class, the return value is an array containing objects representing all interfaces implemented by the class. The order of the interface objects in the array corresponds to the order of the interface names in the implements clause of the declaration of the class represented by this object.
If this object represents an interface, the array contains objects representing all interfaces extended by the interface. The order of the interface objects in the array corresponds to the order of the interface names in the extends clause of the declaration of the interface represented by this object.
If the class or interface implements no interfaces, the method returns an array of length 0. an array of interfaces implemented by this class.
public void addImplements(Class newclass) throws SynthesisException
newclass
-
SynthesisException
- if the class has been reified.public Method getMethod(String name, Class[] parameterTypes) throws NoSuchMethodException, SecurityException
The method to reflect is located by searching all the member methods of the class or interface represented by this Class object for a public method with the specified name and exactly the same formal parameter types.
See The Java Language Specification, sections 8.2 and 8.4.
name
- parameterTypes
-
NoSuchMethodException
- if a matching method is not found.
SecurityException
- if access to the information is denied.public Method[] getMethods() throws SecurityException
See The Java Language Specification, sections 8.2 and 8.4.
SecurityException
- if access to the information is denied.public int getModifiers()
public void setModifiers(int modifiers) throws SynthesisException
modifiers
-
SynthesisException
public String getName()
public String getJavaName()
public String getShortName()
public String getJavaShortName()
public String getPackageName()
public Class getRealClass()
public void setRealClass(Class realclass) throws SynthesisException
This should have the side-effect of limiting further mutation of the org.apache.xml.utils.synthetic.Class to things which can not be obtained from the real Class object, to avoid "lying" to the user
NOTE: Not all information defined by the Java libraries is in fact available in all Java environments. We assume the calls will work; if they return null or empty lists, there's nothing we can do about it. Note that this may mean that a reified class tells us less about itself than the synthetic description used to generate it.
Creation date: (12-25-99 12:26:01 PM)
realclass
-
SynthesisException
public void setSuperClass(Class superclass) throws SynthesisException
superclass
-
SynthesisException
public void setSuperClass(Class superclass) throws ClassNotFoundException, SynthesisException
superclass
-
ClassNotFoundException
SynthesisException
public URL getResource(String name)
The Class methods delegate to ClassLoader methods, after applying a naming convention: if the resource name starts with "/", it is used as is. Otherwise, the name of the package is prepended, after converting "." to "/".
name
- - the string representing the resource to
be found.
the URL object having the specified name, or
null if no resource with the specified name
is found.public InputStream getResourceAsStream(String name)
The Class methods delegate to ClassLoader methods, after applying a naming convention: if the resource name starts with "/", it is used as is. Otherwise, the name of the package is prepended, after converting "." to "/".
name
- - the string representing the resource to
be found
the InputStream object having the
specified name, or null if no resource with
the specified name is found.public Object[] getSigners()
public Class getSuperclass()
If this object is the one that represents the class Object or this object represents an interface, null is returned. the superclass of the class represented by this object.
public boolean isArray()
public boolean isAssignableFrom(Class cls)
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.
cls
-
NullPointerException
- if the specified Class parameter is null.public boolean isAssignableFrom(Class cls)
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.
cls
-
NullPointerException
- if the specified Class parameter is null.public boolean isInstance(Object obj)
Specifically, if this Class object represents a declared class, returns true if the specified Object argument is an instance of the represented class (or of any of its subclasses); false otherwise. If this Class object represents an array class, returns true if the specified Object argument can be converted to an object of the array type by an identity conversion or by a widening reference conversion; false otherwise. If this Class object represents an interface, returns true if the class or any superclass of the specified Object argument implements this interface; false otherwise. If this Class object represents a primitive type, returns false.
obj
- The object to checkpublic boolean isInterface()
public void isInterface(boolean isInterface) throws SynthesisException
isInterface
-
SynthesisException
public boolean isPrimitive()
There are nine predefined Class objects to represent the eight primitive Java types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double, and void.
These objects may only be accessed via the following public static final variables, and are the only Class objects for which this method returns true.
public Object newInstance() throws InstantiationException, IllegalAccessException
IllegalAccessException
- if the class or initializer is not accessible.
InstantiationException
- if an application tries to instantiate an
abstract class or an interface, or if the
instantiation fails for some other reason.public String toString()
Should this say "synthetic" as well as "class" or "interface"? Or should that be gated on whether we're proxy to a realclass?
toString
in class Object
public void toSource(OutputStream out, int depth)
out
- depth
- public void toSource(PrintWriter out, int depth)
out
- depth
- private String tabset(int depth)
depth
- (tabset) @returnpublic static int modifierFromString(String t)
t
- (modifierFromString) @returnpublic static int modifiersFromString(String s)
s
- (modifiersFromString) @return
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |