Class ClassIntrospection<X>

  • Type Parameters:
    X - introspected class

    public final class ClassIntrospection<X>
    extends Object
    Entry point for class introspections.

    Use Introspections.introspect(Class) to get instance of this class.

    • Method Detail

      • fields

        public FieldQuery fields()
        Query for fields of introspected class.
        Returns:
        field query on introspected class.
      • constructors

        public ConstructorQuery<X> constructors()
        Query for constructors of introspected class.
        Returns:
        constructors query on introspected class.
      • methods

        public MethodQuery methods()
        Query for methods of introspected class.
        Returns:
        methods query on introspected class.
      • inheritance

        public InheritanceQuery<X> inheritance()
        Query for implemented/extended interfaces/classes of introspected class.
        Returns:
        inheritance query on introspected class.
      • interfaces

        public InheritanceQuery<X> interfaces()
        Query for implemented interfaces of introspected class.

        This will list transitively implemented interfaces.

        Returns:
        query for interfaces of introspected class.
      • superclasses

        public InheritanceQuery<X> superclasses()
        Query for extended superclasses of introspected class.
        Returns:
        query for superclasses of introspected class.
      • annotations

        public AnnotationQuery<Annotation> annotations()
        Query for runtime-visible annotations on introspected class.
        Returns:
        query for annotations of introspected class.
      • view

        public ClassView<X> view()
        Wrap introspected class in ClassView.
        Returns:
        ClassView of the introspected class
      • isInstantiable

        public boolean isInstantiable()
        Tests if the class is instantiable.

        This checks if there is way to instantiate this class using some of its constructor.

        Returns:
        if the class can be instantiated using constructor
      • instantiate

        public X instantiate()
        Tries to create an instance of this class using parameterless constructor.

        If the construction fails, unchecked exception is thrown.

        This method should be only used when the class is assumed to have parameterless constructor. If this is not the case, and the constructor to use is unknown, use the constructors() to search for suitable one.

        Returns:
        new instance of a class
      • parameterlessConstructor

        public Constructor<X> parameterlessConstructor()
        Gets parameterless constructor for introspected class.

        This method also marks the constructor as Constructor.setAccessible(boolean) for immediate use.

        This method should be only used when the class is assumed to have parameterless constructor. If this is not the case, and the constructor to use is unknown, use the constructors() to search for suitable one.

        Returns:
        parameterless constructor
      • asGeneric

        public <E extends XClass<E> asGeneric()
        Allows cast-less conversion from raw class to either generic form, or parametrized class.

        This function exists, because creating ClassIntrospection from class literal, either by of(java.lang.Class<X>), or Introspections.introspect(Class) will produce introspection with raw type as argument. When provided literal is a generic class, produced type should parameterized with unbounded wildcards, but isn't. This method allows adjusting the type easily.

        Example:

         ClassIntrospection<List> rawIntrospection = ClassIntrospection.of(List.class);
         ClassIntrospection<List<?>> genericntrospection = rawView.adjustWildcards();
         

        This method is equivalent to just casting to parameterized class with wildcards, but without unchecked warning.

        WARNING: This method can be used to cast to inheriting types, i.e. ClassIntrospection<ArrayList<Number>> in previous example. If you are concerned that this might be the case, avoid this method, its only for convenience.

        Type Parameters:
        E - parameterized type to cast to
        Returns:
        casted class introspection