Class JClass

java.lang.Object
com.sun.codemodel.JType
com.sun.codemodel.JClass
All Implemented Interfaces:
JGenerable, Comparable<JType>
Direct Known Subclasses:
JDefinedClass, JNullType, JTypeVar

public abstract class JClass extends JType
Represents a Java reference type, such as a class, an interface, an enum, an array type, a parameterized type.

To be exact, this object represents an "use" of a reference type, not necessarily a declaration of it, which is modeled as JDefinedClass.

  • Field Details

    • EMPTY_ARRAY

      protected static final JTypeVar[] EMPTY_ARRAY
      Sometimes useful reusable empty array.
  • Constructor Details

  • Method Details

    • name

      public abstract String name()
      Gets the name of this class.
      Specified by:
      name in class JType
      Returns:
      name of this class, without any qualification. For example, this method returns "String" for java.lang.String.
    • _package

      public abstract JPackage _package()
      Gets the package to which this class belongs. TODO: shall we move move this down?
    • outer

      public JClass outer()
      Returns the class in which this class is nested, or null if this is a top-level class.
    • owner

      public final JCodeModel owner()
      Gets the JCodeModel object to which this object belongs.
      Specified by:
      owner in class JType
    • _extends

      public abstract JClass _extends()
      Gets the super class of this class.
      Returns:
      Returns the JClass representing the superclass of the entity (class or interface) represented by this . Even if no super class is given explicitly or this is not a class, this method still returns for Object. If this JClass represents Object, return null.
    • _implements

      public abstract Iterator<JClass> _implements()
      Iterates all super interfaces directly implemented by this class/interface.
      Returns:
      A non-null valid iterator that iterates all objects that represents those interfaces implemented by this object.
    • typeParams

      public JTypeVar[] typeParams()
      Iterates all the type parameters of this class/interface.

      For example, if this represents Set<T>, this method returns an array that contains single JTypeVar for 'T'.

    • isInterface

      public abstract boolean isInterface()
      Checks if this object represents an interface.
    • isAbstract

      public abstract boolean isAbstract()
      Checks if this class is an abstract class.
    • getPrimitiveType

      public JPrimitiveType getPrimitiveType()
      If this class represents one of the wrapper classes defined in the java.lang package, return the corresponding primitive type. Otherwise null.
    • boxify

      @Deprecated public JClass boxify()
      Deprecated.
      Description copied from class: JType
      If this class is a primitive type, return the boxed class. Otherwise return this.

      For example, for "int", this method returns "java.lang.Integer".

      Specified by:
      boxify in class JType
    • unboxify

      public JType unboxify()
      Description copied from class: JType
      If this class is a wrapper type for a primitive, return the primitive type. Otherwise return this.

      For example, for "java.lang.Integer", this method returns "int".

      Specified by:
      unboxify in class JType
    • erasure

      public JClass erasure()
      Description copied from class: JType
      Returns the erasure of this type.
      Overrides:
      erasure in class JType
    • isAssignableFrom

      public final boolean isAssignableFrom(JClass derived)
      Checks the relationship between two classes.

      This method works in the same way as Class.isAssignableFrom(Class) works. For example, baseClass.isAssignableFrom(derivedClass)==true.

    • getBaseClass

      public final JClass getBaseClass(JClass baseType)
      Gets the parameterization of the given base type.

      For example, given the following

      
       interface Foo<T> extends List<List<T>> {}
       interface Bar extends Foo<String> {}
       
      This method works like this:
      
       getBaseClass( Bar, List ) = List<List<String>
       getBaseClass( Bar, Foo  ) = Foo<String>
       getBaseClass( Foo<? extends Number>, Collection ) = Collection<List<? extends Number>>
       getBaseClass( ArrayList<? extends BigInteger>, List ) = List<? extends BigInteger>
       
      Parameters:
      baseType - The class whose parameterization we are interested in.
      Returns:
      The use of baseType in this type. or null if the type is not assignable to the base type.
    • getBaseClass

      public final JClass getBaseClass(Class<?> baseType)
    • array

      public JClass array()
      Description copied from class: JType
      Create an array type of this type. This method is undefined for primitive void type, which doesn't have any corresponding array representation.
      Specified by:
      array in class JType
      Returns:
      A JClass representing the array type whose element type is this type
    • narrow

      public JClass narrow(Class<?> clazz)
      "Narrows" a generic class to a concrete class by specifying a type argument.

      .narrow(X) builds Set<X> from Set.

    • narrow

      public JClass narrow(Class<?>... clazz)
    • narrow

      public JClass narrow(JClass clazz)
      "Narrows" a generic class to a concrete class by specifying a type argument.

      .narrow(X) builds Set<X> from Set.

    • narrow

      public JClass narrow(JType type)
    • narrow

      public JClass narrow(JClass... clazz)
    • narrow

      public JClass narrow(List<? extends JClass> clazz)
    • getTypeParameters

      public List<JClass> getTypeParameters()
      If this class is parameterized, return the type parameter of the given index.
    • isParameterized

      public final boolean isParameterized()
      Returns true if this class is a parameterized class.
    • wildcard

      public final JClass wildcard()
      Create "? extends T" from T.
      Returns:
      never null
    • substituteParams

      protected abstract JClass substituteParams(JTypeVar[] variables, List<JClass> bindings)
      Substitutes the type variables with their actual arguments.

      For example, when this class is Map<String,Map<V>>, (where V then doing substituteParams( V, Integer ) returns a for Map<String,Map<Integer>>.

      This method needs to work recursively.

    • toString

      public String toString()
      Overrides:
      toString in class JType
    • dotclass

      public final JExpression dotclass()
    • staticInvoke

      public final JInvocation staticInvoke(JMethod method)
      Generates a static method invocation.
    • staticInvoke

      public final JInvocation staticInvoke(String method)
      Generates a static method invocation.
    • staticRef

      public final JFieldRef staticRef(String field)
      Static field reference.
    • staticRef

      public final JFieldRef staticRef(JVar field)
      Static field reference.
    • generate

      public void generate(JFormatter f)