Class ClassName

java.lang.Object
com.squareup.javapoet.TypeName
com.squareup.javapoet.ClassName
All Implemented Interfaces:
Comparable<ClassName>

public final class ClassName extends TypeName implements Comparable<ClassName>
A fully-qualified class name for top-level and member classes.
  • Field Details

    • OBJECT

      public static final ClassName OBJECT
    • names

      final List<String> names
      From top to bottom. This will be ["java.util", "Map", "Entry"] for Map.Entry.
    • canonicalName

      final String canonicalName
  • Constructor Details

  • Method Details

    • annotated

      public ClassName annotated(List<AnnotationSpec> annotations)
      Overrides:
      annotated in class TypeName
    • withoutAnnotations

      public TypeName withoutAnnotations()
      Overrides:
      withoutAnnotations in class TypeName
    • packageName

      public String packageName()
      Returns the package name, like "java.util" for Map.Entry.
    • enclosingClassName

      public ClassName enclosingClassName()
      Returns the enclosing class, like Map for Map.Entry. Returns null if this class is not nested in another class.
    • topLevelClassName

      public ClassName topLevelClassName()
      Returns the top class in this nesting group. Equivalent to chained calls to enclosingClassName() until the result's enclosing class is null.
    • nestedClass

      public ClassName nestedClass(String name)
      Returns a new ClassName instance for the specified name as nested inside this class.
    • simpleNames

      public List<String> simpleNames()
    • peerClass

      public ClassName peerClass(String name)
      Returns a class that shares the same enclosing package or class. If this class is enclosed by another class, this is equivalent to enclosingClassName().nestedClass(name). Otherwise it is equivalent to get(packageName(), name).
    • simpleName

      public String simpleName()
      Returns the simple name of this class, like "Entry" for Map.Entry.
    • get

      public static ClassName get(Class<?> clazz)
    • bestGuess

      public static ClassName bestGuess(String classNameString)
      Returns a new ClassName instance for the given fully-qualified class name string. This method assumes that the input is ASCII and follows typical Java style (lowercase package names, UpperCamelCase class names) and may produce incorrect results or throw IllegalArgumentException otherwise. For that reason, get(Class) and get(Class) should be preferred as they can correctly create ClassName instances without such restrictions.
    • get

      public static ClassName get(String packageName, String simpleName, String... simpleNames)
      Returns a class name created from the given parts. For example, calling this with package name "java.util" and simple names "Map", "Entry" yields Map.Entry.
    • get

      public static ClassName get(TypeElement element)
      Returns the class name for element.
    • isClassOrInterface

      private static boolean isClassOrInterface(Element e)
    • getPackage

      private static PackageElement getPackage(Element type)
    • compareTo

      public int compareTo(ClassName o)
      Specified by:
      compareTo in interface Comparable<ClassName>
    • emit

      CodeWriter emit(CodeWriter out) throws IOException
      Overrides:
      emit in class TypeName
      Throws:
      IOException