Class ClassTool
java.lang.Object
org.apache.velocity.tools.generic.SafeConfig
org.apache.velocity.tools.generic.ClassTool
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.
- Since:
- VelocityTools 2.0
- Version:
- $Id: ClassTool.java 463298 2006-10-12 16:10:32Z henning $
- Author:
- Nathan Bubna
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
ClassTool.CallableSub<T extends ClassTool.CallableSub>
static class
A simplified wrapping interface for inspecting features of aConstructor
in an inspected Class.static class
A simplified wrapping interface for inspecting features of aField
in an inspected Class.static class
A simplified wrapping interface for inspecting features of aMethod
in an inspected Class.static class
ClassTool.Sub<T extends ClassTool.Sub>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected List<ClassTool.ConstructorSub>
protected List<ClassTool.FieldSub>
static final String
protected org.apache.velocity.runtime.log.Log
protected List<ClassTool.MethodSub>
static final String
private boolean
protected Class
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, OLD_LOCK_CONFIG_KEY, SAFE_MODE_KEY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate void
protected void
configure
(ValueParser values) Does the actual configuration.Returns theAnnotation
s of the Class being inspected.Returns aList
ofClassTool.ConstructorSub
s for each constructor declared constructor in the inspected class.Returns aList
ofClassTool.FieldSub
s for each field declared field in the inspected class.Returns the fully-qualified name for the inspected Class.Returns aList
ofClassTool.MethodSub
s for each method declared method in the inspected class.getName()
Returns the simple name (i.e.Returns the name of the package to which the inspected Class belongs.boolean
Returns the current showDeprecated setting.getSuper()
Returns a new ClassTool instance that is inspecting the superclass of the Class being inspected by this instance.getType()
Returns theClass
being inspected by this instance.getTypes()
Returns a new ClassTool instance that is inspecting the the specifiedClass
.Returns a new ClassTool instance that is inspecting the Class of the specifiedObject
.Returns a new ClassTool instance that is inspecting the Class with the specified name.boolean
Returns true if the inspected Class is declared abstract.boolean
Returns true if the inspected Class has been deprecated.protected static boolean
isDeprecated
(AnnotatedElement element) boolean
isFinal()
Returns true if the inspected Class is declared final.boolean
Returns true if the inspected Class is an interface.boolean
Returns true if the inspected Class is declared private.boolean
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
boolean
Returns true if a call to newInstance() on the Class being inspected is successful; otherwise returns false.private Class
toString()
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
Field Details
-
INSPECT_KEY
- See Also:
-
SHOW_DEPRECATED_KEY
- See Also:
-
log
protected org.apache.velocity.runtime.log.Log log -
type
-
methods
-
constructors
-
fields
-
showDeprecated
private boolean showDeprecated
-
-
Constructor Details
-
ClassTool
public ClassTool()Creates an instance with target type ofObject
. -
ClassTool
Creates a new instance that inspects the specified type and otherwise shares the configuration values of the specified "parent" ClassTool instance.
-
-
Method Details
-
configure
Description copied from class:SafeConfig
Does the actual configuration. This is protected, so subclasses may share the same ValueParser and call configure at any time, while preventing templates from doing so when configure(Map) is locked.- Overrides:
configure
in classSafeConfig
-
toClass
-
setType
-
isDeprecated
-
getShowDeprecated
public boolean getShowDeprecated()Returns the current showDeprecated setting. -
getType
Returns theClass
being inspected by this instance. -
inspect
Returns a new ClassTool instance that is inspecting the Class with the specified name. If the specified Class cannot be found, then this will returnnull
. All other configuration settings will be copied to the new instance. -
inspect
Returns a new ClassTool instance that is inspecting the Class of the specifiedObject
. If the specified object is null, then this will returnnull
. All other configuration settings will be copied to the new instance. -
getSuper
Returns a new ClassTool instance that is inspecting the superclass of the Class being inspected by this instance. If the current inspectee has no super class, then this will returnnull
. All other configuration settings will be copied to the new instance. -
inspect
Returns a new ClassTool instance that is inspecting the the specifiedClass
. If the specified class is null, then this will returnnull
. All other configuration settings will be copied to the new instance. IfSafeConfig.isSafeMode()
istrue
and the specified Class is not declaredpublic
, then this will returnnull
. -
getPackage
Returns the name of the package to which the inspected Class belongs. -
getName
Returns the simple name (i.e. full name with package name removed) of the inspected Class. -
getFullName
Returns the fully-qualified name for the inspected Class. -
supportsNewInstance
public boolean supportsNewInstance()Returns true if a call to newInstance() on the Class being inspected is successful; otherwise returns false. Unlike calling newInstance() directly from a template, this will not throw an Exception if it fails, as all Exceptions are caught. -
isDeprecated
public boolean isDeprecated()Returns true if the inspected Class has been deprecated. -
isPublic
public boolean isPublic()Returns true if the inspected Class is declared public. -
isProtected
public boolean isProtected()Returns true if the inspected Class is declared protected. -
isPrivate
public boolean isPrivate()Returns true if the inspected Class is declared private. -
isStatic
public boolean isStatic()Returns true if the inspected Class is an inner class that has been declared static or is a standard outer class.. -
isFinal
public boolean isFinal()Returns true if the inspected Class is declared final. -
isInterface
public boolean isInterface()Returns true if the inspected Class is an interface. -
isStrict
public boolean isStrict()Returns true if the inspected Class is declared strictfp (uses strict floating point math). -
isAbstract
public boolean isAbstract()Returns true if the inspected Class is declared abstract. -
getMethods
Returns aList
ofClassTool.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. -
getConstructors
Returns aList
ofClassTool.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. -
getFields
Returns aList
ofClassTool.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. -
getTypes
-
addType
-
getAnnotations
Returns theAnnotation
s of the Class being inspected. -
toString
-