Class ClassTool
- java.lang.Object
-
- org.apache.velocity.tools.generic.SafeConfig
-
- org.apache.velocity.tools.generic.ClassTool
-
@DefaultKey("class") @ValidScope("application") public class ClassTool extends SafeConfig
This tool is meant to simplify reflective lookup of information about a
Classand itsFields,Methods, andConstructors. 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 Classes Modifier and Type Class Description static classClassTool.CallableSub<T extends ClassTool.CallableSub>static classClassTool.ConstructorSubA simplified wrapping interface for inspecting features of aConstructorin an inspected Class.static classClassTool.FieldSubA simplified wrapping interface for inspecting features of aFieldin an inspected Class.static classClassTool.MethodSubA simplified wrapping interface for inspecting features of aMethodin an inspected Class.static classClassTool.Sub<T extends ClassTool.Sub>
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<ClassTool.ConstructorSub>constructorsprotected java.util.List<ClassTool.FieldSub>fieldsstatic java.lang.StringINSPECT_KEYprotected java.util.List<ClassTool.MethodSub>methodsstatic java.lang.StringSHOW_DEPRECATED_KEYprotected java.lang.Classtype-
Fields inherited from class org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY
-
-
Constructor Summary
Constructors Modifier Constructor Description ClassTool()Creates an instance with target type ofObject.protectedClassTool(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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidconfigure(ValueParser values)Configure this tooljava.util.List<java.lang.annotation.Annotation>getAnnotations()Returns theAnnotations of the Class being inspected.java.util.List<ClassTool.ConstructorSub>getConstructors()Returns aListofClassTool.ConstructorSubs for each constructor declared constructor in the inspected class.java.util.List<ClassTool.FieldSub>getFields()Returns aListofClassTool.FieldSubs for each field declared field in the inspected class.java.lang.StringgetFullName()Returns the fully-qualified name for the inspected Class.java.util.List<ClassTool.MethodSub>getMethods()Returns aListofClassTool.MethodSubs for each method declared method in the inspected class.java.lang.StringgetName()Returns the simple name (i.e.java.lang.StringgetPackage()Returns the name of the package to which the inspected Class belongs.booleangetShowDeprecated()Returns the current showDeprecated setting.ClassToolgetSuper()Returns a new ClassTool instance that is inspecting the superclass of the Class being inspected by this instance.java.lang.ClassgetType()Returns theClassbeing inspected by this instance.java.util.Set<java.lang.Class>getTypes()Returns aSetof allClasses that are part of the signatures (i.e.ClassToolinspect(java.lang.Class type)Returns a new ClassTool instance that is inspecting the the specifiedClass.ClassToolinspect(java.lang.Object obj)Returns a new ClassTool instance that is inspecting the Class of the specifiedObject.ClassToolinspect(java.lang.String name)Returns a new ClassTool instance that is inspecting the Class with the specified name.booleanisAbstract()Returns true if the inspected Class is declared abstract.booleanisDeprecated()Returns true if the inspected Class has been deprecated.protected static booleanisDeprecated(java.lang.reflect.AnnotatedElement element)Check if an annotation deprecates its classbooleanisFinal()Returns true if the inspected Class is declared final.booleanisInterface()Returns true if the inspected Class is an interface.booleanisPrivate()Returns true if the inspected Class is declared private.booleanisProtected()Returns true if the inspected Class is declared protected.booleanisPublic()Returns true if the inspected Class is declared public.booleanisStatic()Returns true if the inspected Class is an inner class that has been declared static or is a standard outer class..booleanisStrict()Returns true if the inspected Class is declared strictfp (uses strict floating point math).protected voidsetType(java.lang.Class type)Set the class to inspectbooleansupportsNewInstance()Returns true if a call to newInstance() on the Class being inspected is successful; otherwise returns false.java.lang.StringtoString()-
Methods inherited from class org.apache.velocity.tools.generic.SafeConfig
configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
-
-
-
Field Detail
-
INSPECT_KEY
public static final java.lang.String INSPECT_KEY
- See Also:
- Constant Field Values
-
SHOW_DEPRECATED_KEY
public static final java.lang.String SHOW_DEPRECATED_KEY
- See Also:
- Constant Field Values
-
type
protected java.lang.Class type
-
methods
protected java.util.List<ClassTool.MethodSub> methods
-
constructors
protected java.util.List<ClassTool.ConstructorSub> constructors
-
fields
protected java.util.List<ClassTool.FieldSub> fields
-
-
Constructor Detail
-
ClassTool
public ClassTool()
Creates an instance with target type ofObject.
-
ClassTool
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.- Parameters:
tool- parent class tooltype- class to inspect
-
-
Method Detail
-
configure
protected void configure(ValueParser values)
Configure this tool- Overrides:
configurein classSafeConfig- Parameters:
values- configuration values
-
setType
protected void setType(java.lang.Class type)
Set the class to inspect- Parameters:
type- the class to inspect
-
isDeprecated
protected static boolean isDeprecated(java.lang.reflect.AnnotatedElement element)
Check if an annotation deprecates its class- Parameters:
element- annotation- Returns:
- deprecation status
-
getShowDeprecated
public boolean getShowDeprecated()
Returns the current showDeprecated setting.- Returns:
- flag value
-
getType
public java.lang.Class getType()
Returns theClassbeing inspected by this instance.- Returns:
- inspected class
-
inspect
public ClassTool inspect(java.lang.String name)
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.- Parameters:
name- class name- Returns:
- new class tool
-
inspect
public ClassTool inspect(java.lang.Object obj)
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.- Parameters:
obj- object instance to inspect- Returns:
- new class tool
-
getSuper
public ClassTool 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.- Returns:
- parent class tool
-
inspect
public ClassTool inspect(java.lang.Class type)
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()istrueand the specified Class is not declaredpublic, then this will returnnull.- Parameters:
type- class to inspect- Returns:
- new class tool
-
getPackage
public java.lang.String getPackage()
Returns the name of the package to which the inspected Class belongs.- Returns:
- package name
-
getName
public java.lang.String getName()
Returns the simple name (i.e. full name with package name removed) of the inspected Class.- Returns:
- inspected class simple name
-
getFullName
public java.lang.String getFullName()
Returns the fully-qualified name for the inspected Class.- Returns:
- inspected class name
-
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.- Returns:
- new instances supported or not
-
isDeprecated
public boolean isDeprecated()
Returns true if the inspected Class has been deprecated.- Returns:
- deprecation status
-
isPublic
public boolean isPublic()
Returns true if the inspected Class is declared public.- Returns:
- whether the inspected class is public
-
isProtected
public boolean isProtected()
Returns true if the inspected Class is declared protected.- Returns:
- whether the inspected class is protected
-
isPrivate
public boolean isPrivate()
Returns true if the inspected Class is declared private.- Returns:
- whether the inspected class is 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..- Returns:
- whether the inspected class is static
-
isFinal
public boolean isFinal()
Returns true if the inspected Class is declared final.- Returns:
- whether the inspected class is final
-
isInterface
public boolean isInterface()
Returns true if the inspected Class is an interface.- Returns:
- whether the inspected class is an interface
-
isStrict
public boolean isStrict()
Returns true if the inspected Class is declared strictfp (uses strict floating point math).- Returns:
- whether the inspected class is strictfp
-
isAbstract
public boolean isAbstract()
Returns true if the inspected Class is declared abstract.- Returns:
- whether the inspected class is abstract
-
getMethods
public java.util.List<ClassTool.MethodSub> getMethods()
Returns aListofClassTool.MethodSubs 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.- Returns:
- methods inspectors list
-
getConstructors
public java.util.List<ClassTool.ConstructorSub> getConstructors()
Returns aListofClassTool.ConstructorSubs 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.- Returns:
- constructors inspectors list
-
getFields
public java.util.List<ClassTool.FieldSub> getFields()
Returns aListofClassTool.FieldSubs 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.- Returns:
- fields inspectors list
-
getTypes
public java.util.Set<java.lang.Class> getTypes()
Returns aSetof allClasses that are part of the signatures (i.e. parameters or return types) of the inspected Class's methods, constructors and fields.- Returns:
- referenced classes set
-
getAnnotations
public java.util.List<java.lang.annotation.Annotation> getAnnotations()
Returns theAnnotations of the Class being inspected.- Returns:
- annotation list
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- string representation of inspected class
-
-