Class ClassTool


  • @DefaultKey("class")
    public class ClassTool
    extends SafeConfig

    This tool is meant to simplify reflective lookup of information about a Class and its Fields, Methods, and Constructors. 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
    • Constructor Summary

      Constructors 
      Modifier Constructor 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.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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 Annotations of the Class being inspected.
      java.util.List<ClassTool.ConstructorSub> getConstructors()
      Returns a List of ClassTool.ConstructorSubs for each constructor declared constructor in the inspected class.
      java.util.List<ClassTool.FieldSub> getFields()
      Returns a List of ClassTool.FieldSubs 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.MethodSubs 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 Classes 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()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ClassTool

        public ClassTool()
        Creates an instance with target type of Object.
      • 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.
    • Method Detail

      • configure

        protected void configure​(ValueParser values)
        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 class SafeConfig
      • toClass

        private java.lang.Class toClass​(java.lang.String name)
      • setType

        protected void setType​(java.lang.Class type)
      • isDeprecated

        protected static boolean isDeprecated​(java.lang.reflect.AnnotatedElement element)
      • getShowDeprecated

        public boolean getShowDeprecated()
        Returns the current showDeprecated setting.
      • getType

        public java.lang.Class getType()
        Returns the Class being inspected by this instance.
      • 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 return null. All other configuration settings will be copied to the new instance.
      • inspect

        public ClassTool inspect​(java.lang.Object obj)
        Returns a new ClassTool instance that is inspecting the Class of the specified Object. If the specified object is null, then this will return null. All other configuration settings will be copied to the new instance.
      • 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 return null. All other configuration settings will be copied to the new instance.
      • inspect

        public ClassTool inspect​(java.lang.Class type)
        Returns a new ClassTool instance that is inspecting the the specified 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.
      • getPackage

        public java.lang.String getPackage()
        Returns the name of the package to which the inspected Class belongs.
      • getName

        public java.lang.String getName()
        Returns the simple name (i.e. full name with package name removed) of the inspected Class.
      • getFullName

        public java.lang.String 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

        public java.util.List<ClassTool.MethodSub> getMethods()
        Returns a List of ClassTool.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.
      • getConstructors

        public java.util.List<ClassTool.ConstructorSub> getConstructors()
        Returns a List of ClassTool.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.
      • getFields

        public java.util.List<ClassTool.FieldSub> getFields()
        Returns a List of ClassTool.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.
      • getTypes

        public java.util.Set<java.lang.Class> getTypes()
        Returns a Set of all Classes that are part of the signatures (i.e. parameters or return types) of the inspected Class's methods, constructors and fields.
      • addType

        private void addType​(java.util.Set<java.lang.Class> types,
                             java.lang.Class type)
      • getAnnotations

        public java.util.List<java.lang.annotation.Annotation> getAnnotations()
        Returns the Annotations of the Class being inspected.
      • toString

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