Class Util

  • public final class Util
    extends Object
    A random assortment of methods to help with implementing the Java API checks made public so that extenders don't have to reinvent the wheel.
    Lukas Krejci
    • Method Detail

      • isSameType

        public static boolean isSameType​(@Nonnull
                                         TypeMirror t1,
                                         TypeMirror t2)
        To be used to compare types from different compilations (which are not comparable by standard means in Types). This just compares the type names.
        t1 - first type
        t2 - second type
        true if the types have the same fqn, false otherwise
      • toHumanReadableString

        public static String toHumanReadableString​(@Nonnull
                                                   AnnotatedConstruct construct)
        Constructs a human readable representation of the supplied element or type mirror. Note that in some cases the representation might be "surprising". Especially for ExecutableType for which there is no way of getting reliably at the method name or the type declaring the method.
        construct - the element or type mirror to render
        a human readable representation of the construct
      • toUniqueString

        public static String toUniqueString​(@Nonnull
                                            TypeMirror t)
        Represents the type mirror as a string in such a way that it can be used for equality comparisons.
        t - type to convert to string
        the string representation of the type that is fit for equality comparisons
      • getAllSuperClasses

        public static List<TypeMirror> getAllSuperClasses​(@Nonnull
                                                          Types types,
                                                          TypeMirror type)
        Returns all the super classes of given type. I.e. the returned list does NOT contain any interfaces 0 * the class or tis superclasses implement.
        types - the Types instance of the compilation environment from which the type comes from
        type - the type
        the list of super classes
      • isSubtype

        public static boolean isSubtype​(@Nonnull
                                        TypeMirror type,
                                        List<? extends TypeMirror> superTypes,
                                        Types typeEnvironment)
        Checks whether given type is a sub type or is equal to one of the provided types. Note that this does not require the type to come from the same type "environment" or compilation as the super types.
        type - the type to check
        superTypes - the list of supposed super types
        typeEnvironment - the environment in which the type lives
        true if type a sub type of one of the provided super types, false otherwise.
      • keyAnnotationAttributesByName

        public static Map<String,​Map.Entry<? extends ExecutableElement,​? extends AnnotationValue>> keyAnnotationAttributesByName​(@Nonnull
                                                                                                                                             Map<? extends ExecutableElement,​? extends AnnotationValue> attributes)
        Extracts the names of the attributes from the executable elements that represents them in the given map and returns a map keyed by those names.

        I.e. while representing annotation attributes on an annotation type by executable elements is technically correct it is more convenient to address them simply by their names, which, in case of annotation types, are unique (i.e. you cannot overload an annotation attribute, because they cannot have method parameters).

        attributes - the attributes as obtained by AnnotationMirror.getElementValues()
        the equivalent of the supplied map keyed by attribute names instead of the full-blown executable elements
      • findTypeByBinaryName

        public static TypeElement findTypeByBinaryName​(Elements elements,
                                                       String binaryName)
        Tries to find a type element using the provided Elements helper given its binary name. Note that this might NOT be able to find some classes if there are conflicts in the canonical names (but that theoretically cannot happen because the compiler should refuse to compile code with conflicting canonical names).
        elements - the elements instance to search the classpath
        binaryName - the binary name of the class
        the type element with given binary name