org.apache.velocity.tools.generic
@DefaultKey(value="class") public class ClassTool extends SafeConfig
This tool is meant to simplify reflective lookup of information about
a Class
and its Field
s, Method
s, and Constructor
s.
This is ideally aimed at those wishing to generate documentation, demo code, or
other content based on runtime reflection of a specified Class or Classes. It was not
designed with reflective execution of code in mind and thus provides no facilities
for code execution, nor direct access to the actual methods, constructors or fields
of the class being inspected.
Example tools.xml config: <tools> <toolbox scope="application"> <tool class="org.apache.velocity.tools.generic.ClassTool" inspect="com.org.Foo"/> </toolbox> </tools>
If no Class to be inspected is specified, the default is java.lang.Object.
Modifier and Type | Class and Description |
---|---|
static class |
ClassTool.CallableSub<T extends ClassTool.CallableSub> |
static class |
ClassTool.ConstructorSub
A simplified wrapping interface for inspecting features
of a
Constructor in an inspected Class. |
static class |
ClassTool.FieldSub
A simplified wrapping interface for inspecting features
of a
Field in an inspected Class. |
static class |
ClassTool.MethodSub
A simplified wrapping interface for inspecting features
of a
Method in an inspected Class. |
static class |
ClassTool.Sub<T extends ClassTool.Sub> |
Modifier and Type | Field and Description |
---|---|
protected java.util.List<ClassTool.ConstructorSub> |
constructors |
protected java.util.List<ClassTool.FieldSub> |
fields |
static java.lang.String |
INSPECT_KEY |
protected Log |
log |
protected java.util.List<ClassTool.MethodSub> |
methods |
static java.lang.String |
SHOW_DEPRECATED_KEY |
private boolean |
showDeprecated |
protected java.lang.Class |
type |
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY
Modifier | Constructor and Description |
---|---|
|
ClassTool()
Creates an instance with target type of
Object . |
protected |
ClassTool(ClassTool tool,
java.lang.Class type)
Creates a new instance that inspects the specified type
and otherwise shares the configuration values of the specified "parent"
ClassTool instance.
|
Modifier and Type | Method and Description |
---|---|
private void |
addType(java.util.Set<java.lang.Class> types,
java.lang.Class type) |
protected void |
configure(ValueParser values)
Does the actual configuration.
|
java.util.List<java.lang.annotation.Annotation> |
getAnnotations()
Returns the
Annotation s of the Class being inspected. |
java.util.List<ClassTool.ConstructorSub> |
getConstructors()
Returns a
List of ClassTool.ConstructorSub s for each
constructor declared constructor in the inspected class. |
java.util.List<ClassTool.FieldSub> |
getFields()
Returns a
List of ClassTool.FieldSub s for each
field declared field in the inspected class. |
java.lang.String |
getFullName()
Returns the fully-qualified name for the inspected Class.
|
java.util.List<ClassTool.MethodSub> |
getMethods()
Returns a
List of ClassTool.MethodSub s for each
method declared method in the inspected class. |
java.lang.String |
getName()
Returns the simple name (i.e.
|
java.lang.String |
getPackage()
Returns the name of the package to which the inspected Class belongs.
|
boolean |
getShowDeprecated()
Returns the current showDeprecated setting.
|
ClassTool |
getSuper()
Returns a new ClassTool instance that is inspecting the
superclass of the Class being inspected by this instance.
|
java.lang.Class |
getType()
Returns the
Class being inspected by this instance. |
java.util.Set<java.lang.Class> |
getTypes()
Returns a
Set of all Class es that are
part of the signatures (i.e. |
ClassTool |
inspect(java.lang.Class type)
Returns a new ClassTool instance that is inspecting the
the specified
Class . |
ClassTool |
inspect(java.lang.Object obj)
Returns a new ClassTool instance that is inspecting the
Class of the specified
Object . |
ClassTool |
inspect(java.lang.String name)
Returns a new ClassTool instance that is inspecting the
Class with the specified name.
|
boolean |
isAbstract()
Returns true if the inspected Class is declared abstract.
|
boolean |
isDeprecated()
Returns true if the inspected Class has been deprecated.
|
protected static boolean |
isDeprecated(java.lang.reflect.AnnotatedElement element) |
boolean |
isFinal()
Returns true if the inspected Class is declared final.
|
boolean |
isInterface()
Returns true if the inspected Class is an interface.
|
boolean |
isPrivate()
Returns true if the inspected Class is declared private.
|
boolean |
isProtected()
Returns true if the inspected Class is declared protected.
|
boolean |
isPublic()
Returns true if the inspected Class is declared public.
|
boolean |
isStatic()
Returns true if the inspected Class is an inner class
that has been declared static or is a standard outer class..
|
boolean |
isStrict()
Returns true if the inspected Class is declared strictfp
(uses strict floating point math).
|
protected void |
setType(java.lang.Class type) |
boolean |
supportsNewInstance()
Returns true if a call to newInstance() on the Class being
inspected is successful; otherwise returns false.
|
private java.lang.Class |
toClass(java.lang.String name) |
java.lang.String |
toString() |
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
public static final java.lang.String INSPECT_KEY
public static final java.lang.String SHOW_DEPRECATED_KEY
protected Log log
protected java.lang.Class type
protected java.util.List<ClassTool.MethodSub> methods
protected java.util.List<ClassTool.ConstructorSub> constructors
protected java.util.List<ClassTool.FieldSub> fields
private boolean showDeprecated
public ClassTool()
Object
.protected ClassTool(ClassTool tool, java.lang.Class type)
protected void configure(ValueParser values)
SafeConfig
configure
in class SafeConfig
private java.lang.Class toClass(java.lang.String name)
protected void setType(java.lang.Class type)
protected static boolean isDeprecated(java.lang.reflect.AnnotatedElement element)
public boolean getShowDeprecated()
public java.lang.Class getType()
Class
being inspected by this instance.public ClassTool inspect(java.lang.String name)
null
. All other
configuration settings will be copied to the new instance.public ClassTool inspect(java.lang.Object obj)
Object
. If the specified object
is null, then this will return null
. All other
configuration settings will be copied to the new instance.public ClassTool getSuper()
null
. All other
configuration settings will be copied to the new instance.public ClassTool inspect(java.lang.Class type)
Class
. If the specified class
is null, then this will return null
. All other
configuration settings will be copied to the new instance.
If SafeConfig.isSafeMode()
is true
and the specified Class
is not declared public
, then this will return
null
.public java.lang.String getPackage()
public java.lang.String getName()
public java.lang.String getFullName()
public boolean supportsNewInstance()
public boolean isDeprecated()
public boolean isPublic()
public boolean isProtected()
public boolean isPrivate()
public boolean isStatic()
public boolean isFinal()
public boolean isInterface()
public boolean isStrict()
public boolean isAbstract()
public java.util.List<ClassTool.MethodSub> getMethods()
List
of ClassTool.MethodSub
s for each
method declared method in the inspected class. However,
in safe mode (which *is* the default), this will only return
the public methods. You must configure safe mode to be off
to receive a list of all methods.public java.util.List<ClassTool.ConstructorSub> getConstructors()
List
of ClassTool.ConstructorSub
s for each
constructor declared constructor in the inspected class. However,
in safe mode (which *is* the default), this will only return
the public constructors. You must configure safe mode to be off
to receive a list of all constructors.public java.util.List<ClassTool.FieldSub> getFields()
List
of ClassTool.FieldSub
s for each
field declared field in the inspected class. However,
in safe mode (which *is* the default), this will only return
the public fields. You must configure safe mode to be off
to receive a list of all fields.public java.util.Set<java.lang.Class> getTypes()
Set
of all Class
es that are
part of the signatures (i.e. parameters or return types)
of the inspected Class's methods, constructors and fields.private void addType(java.util.Set<java.lang.Class> types, java.lang.Class type)
public java.util.List<java.lang.annotation.Annotation> getAnnotations()
Annotation
s of the Class being inspected.public java.lang.String toString()
toString
in class java.lang.Object
Copyright (c) 2003-2007 Apache Software Foundation