Class DefaultJavaType

java.lang.Object
com.thoughtworks.qdox.model.impl.DefaultJavaType
All Implemented Interfaces:
JavaAnnotatedElement, JavaClass, JavaGenericDeclaration, JavaModel, JavaType, Serializable
Direct Known Subclasses:
DefaultJavaParameterizedType, DefaultJavaTypeVariable, DefaultJavaWildcardType

public class DefaultJavaType extends Object implements JavaClass, JavaType, Serializable
The default implementation for JavaType
Since:
2.0
See Also:
  • Field Details

    • name

      protected final String name
    • fullName

      protected String fullName
    • dimensions

      private int dimensions
    • typeResolver

      private TypeResolver typeResolver
  • Constructor Details

    • DefaultJavaType

      DefaultJavaType(String name, TypeResolver typeResolver)
    • DefaultJavaType

      DefaultJavaType(String fullName, String name, int dimensions, TypeResolver typeResolver)
    • DefaultJavaType

      DefaultJavaType(String fullName, int dimensions)
      Should only be used by primitives, since they don't have a classloader.
      Parameters:
      fullName - the name of the primitive
      dimensions - number of dimensions
    • DefaultJavaType

      DefaultJavaType(String fullName)
      Should only be used by primitives and wildcard, since they don't have a classloader.
      Parameters:
      fullName - the name of the primitive or ?
  • Method Details

    • getBinaryName

      public String getBinaryName()
      The class or interface must be named by its binary name, which must meet the following constraints:
      • The binary name of a top level type is its canonical name.
      • The binary name of a member type consists of the binary name of its immediately enclosing type, followed by $, followed by the simple name of the member.
      Specified by:
      getBinaryName in interface JavaType
      Returns:
      the binary name
      See Also:
    • getSimpleName

      public String getSimpleName()
      Equivalent of Class.getSimpleName().
      Specified by:
      getSimpleName in interface JavaClass
      Returns:
      the simple name of the underlying class as given in the source code.
    • getFullyQualifiedName

      public String getFullyQualifiedName()
      Every primitive type, named package, top level class, and top level interface has a fully qualified name:
      • The fully qualified name of a primitive type is the keyword for that primitive type, namely byte, short, char, int, long, float, double, or boolean.
      • The fully qualified name of a named package that is not a subpackage of a named package is its simple name.
      • The fully qualified name of a named package that is a subpackage of another named package consists of the fully qualified name of the containing package, followed by ".", followed by the simple (member) name of the subpackage.
      • The fully qualified name of a top level class or top level interface that is declared in an unnamed package is the simple name of the class or interface.
      • The fully qualified name of a top level class or top level interface that is declared in a named package consists of the fully qualified name of the package, followed by ".", followed by the simple name of the class or interface.
      Each member class, member interface, and array type may have a fully qualified name:
      • A member class or member interface M of another class or interface C has a fully qualified name if and only if C has a fully qualified name.
      • In that case, the fully qualified name of M consists of the fully qualified name of C, followed by ".", followed by the simple name of M.
      • An array type has a fully qualified name if and only if its element type has a fully qualified name.
      • In that case, the fully qualified name of an array type consists of the fully qualified name of the component type of the array type followed by "[]".
      Some examples how names will be translated
       Object > java.lang.Object
       java.util.List > java.util.List
       ?  > ?
       T  > T
       anypackage.Outer.Inner > anypackage.Outer.Inner
       String[][] > java.lang.String[][]
       
      Specified by:
      getFullyQualifiedName in interface JavaType
      Returns:
      the fully qualified name, never null
      See Also:
    • getComponentType

      public JavaClass getComponentType()
      Equivalent of Class.getComponentType() If this type is an array, return its component type
      Specified by:
      getComponentType in interface JavaClass
      Returns:
      the type of array if it's one, otherwise null
    • getValue

      public String getValue()
      If there's a reference to this class, use the value used in the code. Otherwise return the simple name. When including all imports, you should be safe to use this method. This won't return generics, so it's java1.4 safe. Examples:
        private String fieldA;             // getValue() will return "String"
        private java.lang.String fieldA;   // getValue() will return "java.lang.String"
        private List>String> aList;  // getValue() will return "List"
       
      Specified by:
      getValue in interface JavaType
      Returns:
      the name of the class as used in the source
    • getGenericValue

      public String getGenericValue()
      A java5+ representation of the class. When including all imports, you should be safe to use this method. Examples:
        private String fieldA;             // getValue() will return "String"
        private java.lang.String fieldA;   // getValue() will return "java.lang.String"
        private List>String> aList;  // getValue() will return "List>String>"
       
      Specified by:
      getGenericValue in interface JavaType
      Returns:
      the generic name of the class as used in the source
    • getGenericValue

      protected static <D extends JavaGenericDeclaration> String getGenericValue(JavaType base, List<JavaTypeVariable<D>> typeVariableList)
    • getActualTypeArguments

      private static List<JavaType> getActualTypeArguments(JavaType base)
    • getResolvedValue

      protected static <D extends JavaGenericDeclaration> String getResolvedValue(JavaType base, List<JavaTypeVariable<D>> typeParameters)
    • resolve

      protected static <D extends JavaGenericDeclaration> JavaTypeVariable<D> resolve(JavaType base, List<JavaTypeVariable<D>> typeParameters)
    • isResolved

      protected boolean isResolved()
    • isArray

      public boolean isArray()
      Specified by:
      isArray in interface JavaClass
      Returns:
      true if this JavaClass is an array, otherwise false
    • getDimensions

      public int getDimensions()
      Returns the depth of this array, 0 if it's not an array
      Specified by:
      getDimensions in interface JavaClass
      Returns:
      The depth of this array, at least 0
    • toString

      public String toString()
      Equivalent of Class.toString(). Converts the object to a string.
      Specified by:
      toString in interface JavaClass
      Overrides:
      toString in class Object
      Returns:
      a string representation of this type.
      See Also:
    • toGenericString

      public String toGenericString()
      Returns getGenericValue() extended with the array information
       Object > java.lang.Object
       Object[] > java.lang.Object[]
       List<Object> > java.lang.List<java.lang.Object>
       Outer.Inner > Outer$Inner
       Outer.Inner<Object>[][] > Outer$Inner<java.lang.Object>[][] 
       
      Specified by:
      toGenericString in interface JavaType
      Returns:
      a generic string representation of this type.
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • resolveRealClass

      private JavaClass resolveRealClass()
    • isA

      public boolean isA(JavaType type)
      Parameters:
      type - the type to match with
      Returns:
      true if this type if of type, otherwise false
      Since:
      1.3
    • isPrimitive

      public boolean isPrimitive()
      Equivalent of Class.isPrimitive()
      Specified by:
      isPrimitive in interface JavaClass
      Returns:
      true if this class represents a primitive, otherwise false
    • isPrimitive

      private static boolean isPrimitive(String value)
    • isVoid

      public boolean isVoid()
      Specified by:
      isVoid in interface JavaClass
      Returns:
      true if this JavaClass is a void, otherwise false
    • resolve

      protected static JavaType resolve(JavaType base, JavaClass declaringClass, JavaClass callingClass)
      Consider the following example
        public abstract class AbstractClass<T> 
        {
          private T value;
          
          public AbstractClass( T value ) { this.value = value; }
          
          public T getValue() { return value; }
        }
        
        public class ConcreteClass extends AbstractClass<String>
        {
          public ConcreteClass( String s ) { super( s ); }
        }
        

      We want to know the resolved returnType when calling ConcreteClass.getValue(). The expected type is String.

      • this would be T
      • declaringClass would be AbstractClass, since that's where T is used
      • callingClass would be ConcreteClass
      Parameters:
      base - the base
      declaringClass - the declaring class
      callingClass - the calling class
      Returns:
      the resolved type
    • getDimensions

      private static int getDimensions(JavaType type)
    • getTypeVariableIndex

      private static int getTypeVariableIndex(JavaClass declaringClass, String fqn)
    • getGenericFullyQualifiedName

      public String getGenericFullyQualifiedName()
      The fully qualified name with generic information.
      Specified by:
      getGenericFullyQualifiedName in interface JavaType
      Returns:
      the generic fully qualified name
    • getGenericCanonicalName

      public String getGenericCanonicalName()
      The canonical name with generic information.
      Specified by:
      getGenericCanonicalName in interface JavaType
      Returns:
      the generic canonical name
    • getResolvedGenericValue

      protected static <D extends JavaGenericDeclaration> String getResolvedGenericValue(JavaType base, List<JavaTypeVariable<D>> typeParameters)
    • getResolvedGenericFullyQualifiedName

      protected static <D extends JavaGenericDeclaration> String getResolvedGenericFullyQualifiedName(JavaType base, List<JavaTypeVariable<D>> typeParameters)
    • getResolvedFullyQualifiedName

      protected static <D extends JavaGenericDeclaration> String getResolvedFullyQualifiedName(JavaType base, List<JavaTypeVariable<D>> typeParameters)
    • getSource

      public JavaSource getSource()
      The compilation unit, which includes the imports, the public and anonymous classes
      Specified by:
      getSource in interface JavaClass
      Returns:
      the JavaSource of this element
    • getLineNumber

      public int getLineNumber()
      The line number where this element started
      Specified by:
      getLineNumber in interface JavaModel
      Returns:
      the line number of this element
    • isInterface

      public boolean isInterface()
      (API description of Class.isInterface())

      Determines if the specified Class object represents an interface type.

      Specified by:
      isInterface in interface JavaClass
      Returns:
      true if this object represents an interface, otherwise false
    • getAnnotations

      public List<JavaAnnotation> getAnnotations()
      Specified by:
      getAnnotations in interface JavaAnnotatedElement
      Returns:
      a list of Annotations, never null
    • isEnum

      public boolean isEnum()
      (API description of Class.isEnum())

      Returns true if and only if this class was declared as an enum in the source code.

      Specified by:
      isEnum in interface JavaClass
      Returns:
      true if this object represents an enum, otherwise false
    • getComment

      public String getComment()
      Retrieve the javadoc comment of this annotated element. This is the part between /** and the */, but without the doclet tags
      Specified by:
      getComment in interface JavaAnnotatedElement
      Returns:
      the comment, otherwise null
    • getTags

      public List<DocletTag> getTags()
      Retrieve all defined doclet tags.
      Specified by:
      getTags in interface JavaAnnotatedElement
      Returns:
      a list of DocletTags, never null
    • isAnnotation

      public boolean isAnnotation()
      (API description of Class.isAnnotation())

      Returns true if this Class object represents an annotation type. Note that if this method returns true, JavaClass.isInterface() would also return true, as all annotation types are also interfaces.

      Specified by:
      isAnnotation in interface JavaClass
      Returns:
      true if this object represents an annotation, otherwise false
    • getTagsByName

      public List<DocletTag> getTagsByName(String name)
      Retrieve all doclettags with a specific name.
      Specified by:
      getTagsByName in interface JavaAnnotatedElement
      Parameters:
      name - the name of the doclet tag
      Returns:
      a list of doclettags, never null
    • getTagByName

      public DocletTag getTagByName(String name)
      Retrieve the doclettag by the specified name. If there are more than one tags, only return the first one.
      Specified by:
      getTagByName in interface JavaAnnotatedElement
      Parameters:
      name - the name of the doclettag trying to retrieve
      Returns:
      the first doclettag matching the name, otherwise null
    • getSuperClass

      public JavaType getSuperClass()
      Specified by:
      getSuperClass in interface JavaClass
    • getSuperJavaClass

      public JavaClass getSuperJavaClass()
      Shorthand for getSuperClass().getJavaClass() with null checking.
      Specified by:
      getSuperJavaClass in interface JavaClass
      Returns:
      the super class as JavaClass
    • getImplements

      public List<JavaType> getImplements()
      Specified by:
      getImplements in interface JavaClass
    • getInterfaces

      public List<JavaClass> getInterfaces()
      Equivalent of Class.getInterfaces() Determines the interfaces implemented by the class or interface represented by this object.
      Specified by:
      getInterfaces in interface JavaClass
      Returns:
      a list of interfaces, never null
    • getNamedParameter

      public String getNamedParameter(String tagName, String parameterName)
      Convenience method for getTagByName(String).getNamedParameter(String) that also checks for null tag.
      Specified by:
      getNamedParameter in interface JavaAnnotatedElement
      Parameters:
      tagName - the tag name
      parameterName - the parameter name
      Returns:
      the value of the matching parameter, otherwise null
    • getCodeBlock

      public String getCodeBlock()
      Specified by:
      getCodeBlock in interface JavaClass
      Specified by:
      getCodeBlock in interface JavaModel
      Returns:
      the codeblock
    • getTypeParameters

      public <D extends JavaGenericDeclaration> List<JavaTypeVariable<D>> getTypeParameters()
      Specified by:
      getTypeParameters in interface JavaGenericDeclaration
      Type Parameters:
      D - the type
      Returns:
      a list of typeParameters, never null
    • getParentSource

      public JavaSource getParentSource()
      Specified by:
      getParentSource in interface JavaClass
    • getPackage

      public JavaPackage getPackage()
      Equivalent of Class.getPackage()
      Specified by:
      getPackage in interface JavaClass
      Returns:
      the package
    • getPackageName

      public String getPackageName()
      If this class has a package, the packagename will be returned. Otherwise an empty String.
      Specified by:
      getPackageName in interface JavaClass
      Returns:
      the name of the package, otherwise an empty String
    • isInner

      public boolean isInner()
      Specified by:
      isInner in interface JavaClass
      Returns:
      true if this class is an inner class, otherwise false
    • getInitializers

      public List<JavaInitializer> getInitializers()
      A list if JavaInitializer, either static or instance initializers.
      Specified by:
      getInitializers in interface JavaClass
      Returns:
      a List of initializers
    • getMethods

      public List<JavaMethod> getMethods()
      Equivalent of Class.getMethods()
      Specified by:
      getMethods in interface JavaClass
      Returns:
      the methods declared or overridden in this class
    • getConstructors

      public List<JavaConstructor> getConstructors()
      Specified by:
      getConstructors in interface JavaClass
      Returns:
      the list of constructors
    • getConstructor

      public JavaConstructor getConstructor(List<JavaType> parameterTypes)
      Specified by:
      getConstructor in interface JavaClass
      Parameters:
      parameterTypes - the parameter types of the constructor, can be null
      Returns:
      the matching constructor, otherwise null
    • getConstructor

      public JavaConstructor getConstructor(List<JavaType> parameterTypes, boolean varArg)
      Specified by:
      getConstructor in interface JavaClass
      Parameters:
      parameterTypes - the parameter types of the constructor, can be null
      varArg - define is the constructor has varArgs
      Returns:
      the matching constructor, otherwise null
    • getMethods

      public List<JavaMethod> getMethods(boolean superclasses)
      Return declared methods and optionally the inherited methods
      Specified by:
      getMethods in interface JavaClass
      Parameters:
      superclasses - true if inherited methods should be returned as well
      Returns:
      all methods
    • getMethodBySignature

      public JavaMethod getMethodBySignature(String name, List<JavaType> parameterTypes)
      Specified by:
      getMethodBySignature in interface JavaClass
      Parameters:
      name - the name of the method
      parameterTypes - the parameter types of the method, can be null.
      Returns:
      the matching method, otherwise null
    • getMethod

      public JavaMethod getMethod(String name, List<JavaType> parameterTypes, boolean varArgs)
      This should be the signature for getMethodBySignature.
      Specified by:
      getMethod in interface JavaClass
      Parameters:
      name - the name of the method
      parameterTypes - the parameter types of the method, can be null
      varArgs - define if the method has varArgs
      Returns:
      the matching method, otherwise null
    • getMethodBySignature

      public JavaMethod getMethodBySignature(String name, List<JavaType> parameterTypes, boolean superclasses)
      Specified by:
      getMethodBySignature in interface JavaClass
      Parameters:
      name - the name of the method
      parameterTypes - the parameter types of the method, can be null
      superclasses - to define if superclasses should be included as well
      Returns:
      the matching method, otherwise null
    • getMethodBySignature

      public JavaMethod getMethodBySignature(String name, List<JavaType> parameterTypes, boolean superclasses, boolean varArg)
      Specified by:
      getMethodBySignature in interface JavaClass
      Parameters:
      name - the name of the method
      parameterTypes - the parameter types of the method, can be null
      superclasses - true if inherited methods should be matched as well
      varArg - define if the method has varArgs
      Returns:
      the matching method, otherwise null
    • getMethodsBySignature

      public List<JavaMethod> getMethodsBySignature(String name, List<JavaType> parameterTypes, boolean superclasses)
      Specified by:
      getMethodsBySignature in interface JavaClass
      Parameters:
      name - the name of the method
      parameterTypes - the parameter types of the method, can be null
      superclasses - true if inherited methods should be matched as well
      Returns:
      the matching methods, otherwise null
    • getMethodsBySignature

      public List<JavaMethod> getMethodsBySignature(String name, List<JavaType> parameterTypes, boolean superclasses, boolean varArg)
      Specified by:
      getMethodsBySignature in interface JavaClass
      Parameters:
      name - the name of the method
      parameterTypes - the parameter types of the method, can be null
      superclasses - true if inherited methods should be matched as well
      varArg - define if the method has varArgs
      Returns:
      the matching methods, otherwise null
    • getFields

      public List<JavaField> getFields()
      Equivalent of Class.getFields()
      Specified by:
      getFields in interface JavaClass
      Returns:
      a list of fiels, never null
    • getFieldByName

      public JavaField getFieldByName(String name)
      Equivalent of Class.getField(String), where this method can resolve every field
      Specified by:
      getFieldByName in interface JavaClass
      Parameters:
      name - the name of the field
      Returns:
      the field
    • getEnumConstants

      public List<JavaField> getEnumConstants()
      Specified by:
      getEnumConstants in interface JavaClass
      Returns:
      a List of enum constants if this class is an enum, otherwise null
    • getEnumConstantByName

      public JavaField getEnumConstantByName(String name)
      Specified by:
      getEnumConstantByName in interface JavaClass
      Parameters:
      name - the name of the enum constant
      Returns:
      the enumConstant matching the name, otherwise null
    • getNestedClasses

      public List<JavaClass> getNestedClasses()
      Specified by:
      getNestedClasses in interface JavaClass
      Returns:
      a list of declared classes, never null
    • getNestedClassByName

      public JavaClass getNestedClassByName(String name)
      Specified by:
      getNestedClassByName in interface JavaClass
    • isA

      public boolean isA(String fullClassName)
      Specified by:
      isA in interface JavaClass
      Parameters:
      fullClassName - the FQN to match with
      Returns:
      true if this is of type FQN, otherwise false
    • isA

      public boolean isA(JavaClass javaClass)
      Specified by:
      isA in interface JavaClass
      Parameters:
      javaClass - the JavaClass to match with
      Returns:
      true if this is of type javaClass, otherwise false
    • getBeanProperties

      public List<BeanProperty> getBeanProperties()
      Gets bean properties without looking in superclasses or interfaces.
      Specified by:
      getBeanProperties in interface JavaClass
      Returns:
      the bean properties
    • getBeanProperties

      public List<BeanProperty> getBeanProperties(boolean superclasses)
      Specified by:
      getBeanProperties in interface JavaClass
      Parameters:
      superclasses - to define if superclasses should be included as well
      Returns:
      the bean properties
    • getBeanProperty

      public BeanProperty getBeanProperty(String propertyName)
      Gets bean property without looking in superclasses or interfaces.
      Specified by:
      getBeanProperty in interface JavaClass
      Parameters:
      propertyName - the name of the property
      Returns:
      the bean property
    • getBeanProperty

      public BeanProperty getBeanProperty(String propertyName, boolean superclasses)
      Specified by:
      getBeanProperty in interface JavaClass
      Parameters:
      propertyName - the name of the property
      superclasses - to define if superclasses should be included as well
      Returns:
      the bean property
    • getDerivedClasses

      public List<JavaClass> getDerivedClasses()
      Equivalent of Class.getClasses() Gets the known derived classes. That is, subclasses or implementing classes.
      Specified by:
      getDerivedClasses in interface JavaClass
      Returns:
      the derived classes
    • getTagsByName

      public List<DocletTag> getTagsByName(String name, boolean superclasses)
      Specified by:
      getTagsByName in interface JavaClass
    • getJavaClassLibrary

      public ClassLibrary getJavaClassLibrary()
      Specified by:
      getJavaClassLibrary in interface JavaClass
    • getName

      public String getName()
      Equivalent of Class.getName().
      Specified by:
      getName in interface JavaClass
      Returns:
      the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.
    • getCanonicalName

      public String getCanonicalName()
      Equivalent of (@link Class.getCanonicalName().
      Specified by:
      getCanonicalName in interface JavaType
      Returns:
      the canonical name of this class
    • getModifiers

      public List<String> getModifiers()
      Equivalent of Class.getModifiers() This does not follow the java-api The Class.getModifiers() returns an int, which should be decoded with the Modifier. This method will return a list of strings representing the modifiers. If this member was extracted from a source, it will keep its order. Otherwise if will be in the preferred order of the java-api.
      Specified by:
      getModifiers in interface JavaClass
      Returns:
      all modifiers is this member
    • isPublic

      public boolean isPublic()
      (API description of Modifier.isPublic(int))

      Return true if the class includes the public modifier, false otherwise.

      Specified by:
      isPublic in interface JavaClass
      Returns:
      true if class has the public modifier, otherwise false
    • isProtected

      public boolean isProtected()
      (API description of Modifier.isProtected(int))

      Return true if the class includes the protected modifier, false otherwise.

      Specified by:
      isProtected in interface JavaClass
      Returns:
      true if class has the protected modifier, otherwise false
    • isPrivate

      public boolean isPrivate()
      (API description of Modifier.isPrivate(int))

      Return true if the class includes the private modifier, false otherwise.

      Specified by:
      isPrivate in interface JavaClass
      Returns:
      true if class has the private modifier, otherwise false
    • isFinal

      public boolean isFinal()
      (API description of Modifier.isFinal(int))

      Return true if the class includes the final modifier, false otherwise.

      Specified by:
      isFinal in interface JavaClass
      Returns:
      true if class has the final modifier, otherwise false
    • isStatic

      public boolean isStatic()
      (API description of Modifier.isStatic(int))

      Return true if the class includes the static modifier, false otherwise.

      Specified by:
      isStatic in interface JavaClass
      Returns:
      true if class the static modifier, otherwise false
    • isAbstract

      public boolean isAbstract()
      (API description of Modifier.isAbstract(int)) Return true if the class includes the abstract modifier, false otherwise.
      Specified by:
      isAbstract in interface JavaClass
      Returns:
      true if class has the abstract modifier, otherwise false
    • getDeclaringClass

      public JavaClass getDeclaringClass()
      Specified by:
      getDeclaringClass in interface JavaClass