Class TypeUtils


  • public class TypeUtils
    extends java.lang.Object
    Utilities methods related to reflection.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  TypeUtils.TypeInstantiator<T>
      A TypeToken that removes an ugly cast in the common cases of getting a known type.
    • Constructor Summary

      Constructors 
      Constructor Description
      TypeUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkNoInheritanceRelationships​(com.google.common.collect.ImmutableSet<java.lang.Class<?>> resourceClasses)  
      static <T> java.lang.Class<T> getClassFromString​(java.lang.String className, java.lang.Class<T> expectedSuperType)
      Returns the class referred to by a fully qualified class name string.
      static <T> com.google.common.collect.ImmutableMap<java.lang.String,​T> getTypesafeEnumMapping​(java.lang.Class<T> clazz)
      Aggregates enum "values" in a typesafe enum pattern into a string->field map.
      static java.util.function.Predicate<java.lang.Class<?>> hasAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
      Returns a predicate that tests whether classes are annotated with the given annotation.
      static <T> T instantiate​(java.lang.Class<? extends T> clazz)  
      static <T,​U>
      T
      instantiate​(java.lang.Class<? extends T> clazz, U arg1)
      Instantiate a class with the specified constructor argument.
      • Methods inherited from class java.lang.Object

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

      • TypeUtils

        public TypeUtils()
    • Method Detail

      • instantiate

        public static <T> T instantiate​(java.lang.Class<? extends T> clazz)
      • instantiate

        public static <T,​U> T instantiate​(java.lang.Class<? extends T> clazz,
                                                U arg1)
        Instantiate a class with the specified constructor argument.

        Because we use arg1's type to lookup the constructor, this only works if arg1's class is exactly the same type as the constructor argument. Subtypes are not allowed.

      • getClassFromString

        public static <T> java.lang.Class<T> getClassFromString​(java.lang.String className,
                                                                java.lang.Class<T> expectedSuperType)
        Returns the class referred to by a fully qualified class name string.

        Throws an error if the loaded class is not assignable from the expected super type class.

      • getTypesafeEnumMapping

        public static <T> com.google.common.collect.ImmutableMap<java.lang.String,​T> getTypesafeEnumMapping​(java.lang.Class<T> clazz)
        Aggregates enum "values" in a typesafe enum pattern into a string->field map.
      • hasAnnotation

        public static java.util.function.Predicate<java.lang.Class<?>> hasAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
        Returns a predicate that tests whether classes are annotated with the given annotation.
      • checkNoInheritanceRelationships

        public static void checkNoInheritanceRelationships​(com.google.common.collect.ImmutableSet<java.lang.Class<?>> resourceClasses)