Class ASTUtils


  • public class ASTUtils
    extends java.lang.Object
    Collection of AST-related static utils that didn't really fit into the main class.
    • Constructor Summary

      Constructors 
      Constructor Description
      ASTUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String descriptorFromExecutableElement​(javax.lang.model.element.ExecutableElement m, javax.annotation.processing.ProcessingEnvironment env)
      Builds a method descriptor from the given ExecutableElement.
      static java.lang.String descriptorFromType​(javax.lang.model.type.TypeMirror t, javax.annotation.processing.ProcessingEnvironment env)
      Builds a type descriptor from the given TypeMirror.
      static <T extends javax.lang.model.element.Element>
      java.util.List<T>
      findAnnotatedElement​(javax.lang.model.element.Element parent, java.lang.Class<? extends java.lang.annotation.Annotation> ann)
      Finds, among the methods of a class cl, the one annotated with ann, and tries to build an Element from it.
      static javax.lang.model.element.Element findMember​(ClassContainer parent, java.lang.String name, java.lang.String descr, boolean strict, boolean field, javax.annotation.processing.ProcessingEnvironment env)
      Finds a member given the name, the container class and (if it's a method) the descriptor.
      static javax.lang.model.element.ExecutableElement findOverloadedMethod​(javax.lang.model.element.TypeElement context, javax.lang.model.element.ExecutableElement method, javax.annotation.processing.ProcessingEnvironment env)
      Tries to find the method being overloaded by the given ExecutableElement.
      static javax.lang.model.element.ExecutableElement findSyntheticBridge​(javax.lang.model.element.TypeElement context, javax.lang.model.element.ExecutableElement method, javax.annotation.processing.ProcessingEnvironment env)
      Tries to find the "synthetic bridge" generated by the compiler for a certain overridden methods.
      static ftbsc.lll.mapper.data.ClassData getClassData​(java.lang.String name, ftbsc.lll.mapper.utils.Mapper mapper)
      Gets the ClassData corresponding to the given internal name, or creates a false one with the same, non-obfuscated name twice.
      static ftbsc.lll.mapper.data.FieldData getFieldData​(java.lang.String parent, java.lang.String name, ftbsc.lll.mapper.utils.Mapper mapper)
      Gets the FieldData corresponding to the field matching the given name and parent, or creates a dummy one with fake data if no valid mapping is found.
      static ftbsc.lll.mapper.data.MethodData getMethodData​(java.lang.String parent, java.lang.String name, java.lang.String descriptor, ftbsc.lll.mapper.utils.Mapper mapper)
      Gets the MethodData corresponding to the method matching the given name, parent and descriptor, or creates a dummy one with fake data if no valid mapping is found.
      static ProxyType getProxyType​(javax.lang.model.element.VariableElement v)
      Utility method for finding out what type of proxy a field is.
      static <T extends java.lang.annotation.Annotation>
      javax.lang.model.type.TypeMirror
      getTypeFromAnnotation​(T ann, java.util.function.Function<T,​java.lang.Class<?>> classFunction, javax.annotation.processing.ProcessingEnvironment env)
      Safely extracts a Class from an annotation and gets a TypeMirror representing it.
      static java.lang.String internalNameFromType​(javax.lang.model.type.TypeMirror type, javax.annotation.processing.ProcessingEnvironment env)
      Gets the internal name from a TypeMirror.
      static int mapModifier​(javax.lang.model.element.Modifier m)
      Maps a Modifier to its reflective Modifier equivalent.
      static int mapModifiers​(java.util.Collection<javax.lang.model.element.Modifier> modifiers)
      Takes in a Collection of AST Modifiers and returns them mapped to their reflective integer equivalent.
      static boolean shouldValidate​(java.lang.String name)
      Checks whether a certain class name is valid, and whether the processor is able to validate its existence.
      • Methods inherited from class java.lang.Object

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

      • ASTUtils

        public ASTUtils()
    • Method Detail

      • findAnnotatedElement

        public static <T extends javax.lang.model.element.Element> java.util.List<T> findAnnotatedElement​(javax.lang.model.element.Element parent,
                                                                                                          java.lang.Class<? extends java.lang.annotation.Annotation> ann)
        Finds, among the methods of a class cl, the one annotated with ann, and tries to build an Element from it.
        Type Parameters:
        T - the type of Element to use
        Parameters:
        parent - the parent Element to the desired element
        ann - the Class corresponding to the desired annotation
        Returns:
        a List of Elements annotated with the given annotation
        Since:
        0.2.0
      • mapModifier

        public static int mapModifier​(javax.lang.model.element.Modifier m)
        Maps a Modifier to its reflective Modifier equivalent.
        Parameters:
        m - the Modifier to map
        Returns:
        an integer representing the modifier
        Since:
        0.2.0
        See Also:
        Modifier
      • mapModifiers

        public static int mapModifiers​(java.util.Collection<javax.lang.model.element.Modifier> modifiers)
        Takes in a Collection of AST Modifiers and returns them mapped to their reflective integer equivalent.
        Parameters:
        modifiers - the Modifiers
        Returns:
        an integer value representing them
        Since:
        0.5.0
      • getTypeFromAnnotation

        public static <T extends java.lang.annotation.Annotation> javax.lang.model.type.TypeMirror getTypeFromAnnotation​(T ann,
                                                                                                                         java.util.function.Function<T,​java.lang.Class<?>> classFunction,
                                                                                                                         javax.annotation.processing.ProcessingEnvironment env)
        Safely extracts a Class from an annotation and gets a TypeMirror representing it.
        Type Parameters:
        T - the type of the annotation carrying the information
        Parameters:
        ann - the annotation containing the class
        classFunction - the annotation function returning the class
        env - the ProcessingEnvironment to perform the operation in
        Returns:
        a TypeMirror representing the requested Class
        Since:
        0.3.0
      • internalNameFromType

        public static java.lang.String internalNameFromType​(javax.lang.model.type.TypeMirror type,
                                                            javax.annotation.processing.ProcessingEnvironment env)
        Gets the internal name from a TypeMirror.
        Parameters:
        type - the TypeMirror in question
        env - the ProcessingEnvironment to perform the operation in
        Returns:
        the internal name at compile time, or null if it wasn't a qualifiable
        Since:
        0.5.1
      • descriptorFromType

        public static java.lang.String descriptorFromType​(javax.lang.model.type.TypeMirror t,
                                                          javax.annotation.processing.ProcessingEnvironment env)
        Builds a type descriptor from the given TypeMirror.
        Parameters:
        t - the TypeMirror representing the desired type
        env - the ProcessingEnvironment to perform the operation in
        Returns:
        a String containing the relevant descriptor
      • descriptorFromExecutableElement

        public static java.lang.String descriptorFromExecutableElement​(javax.lang.model.element.ExecutableElement m,
                                                                       javax.annotation.processing.ProcessingEnvironment env)
        Builds a method descriptor from the given ExecutableElement.
        Parameters:
        m - the ExecutableElement for the method
        env - the ProcessingEnvironment to perform the operation in
        Returns:
        a String containing the relevant descriptor
      • getClassData

        public static ftbsc.lll.mapper.data.ClassData getClassData​(java.lang.String name,
                                                                   ftbsc.lll.mapper.utils.Mapper mapper)
        Gets the ClassData corresponding to the given internal name, or creates a false one with the same, non-obfuscated name twice.
        Parameters:
        name - the internal name of the class to convert
        mapper - the Mapper to use, may be null
        Returns:
        the fully qualified class name
        Since:
        0.6.1
      • getMethodData

        public static ftbsc.lll.mapper.data.MethodData getMethodData​(java.lang.String parent,
                                                                     java.lang.String name,
                                                                     java.lang.String descriptor,
                                                                     ftbsc.lll.mapper.utils.Mapper mapper)
        Gets the MethodData corresponding to the method matching the given name, parent and descriptor, or creates a dummy one with fake data if no valid mapping is found.
        Parameters:
        parent - the internal name of the parent class
        name - the name of the member
        descriptor - the descriptor of the method
        mapper - the Mapper to use, may be null
        Returns:
        the fully qualified class name
        Since:
        0.6.1
      • getFieldData

        public static ftbsc.lll.mapper.data.FieldData getFieldData​(java.lang.String parent,
                                                                   java.lang.String name,
                                                                   ftbsc.lll.mapper.utils.Mapper mapper)
        Gets the FieldData corresponding to the field matching the given name and parent, or creates a dummy one with fake data if no valid mapping is found.
        Parameters:
        parent - the internal name of the parent class
        name - the name of the member
        mapper - the Mapper to use, may be null
        Returns:
        the fully qualified class name
        Since:
        0.6.1
      • findMember

        public static javax.lang.model.element.Element findMember​(ClassContainer parent,
                                                                  java.lang.String name,
                                                                  java.lang.String descr,
                                                                  boolean strict,
                                                                  boolean field,
                                                                  javax.annotation.processing.ProcessingEnvironment env)
        Finds a member given the name, the container class and (if it's a method) the descriptor.
        Parameters:
        parent - the ClassContainer representing the parent
        name - the name to search for
        descr - the descriptor to search for, or null if it's not a method
        strict - whether to perform lookup in strict mode (see Target.strict() for more information)
        field - whether the member being searched is a field
        env - the ProcessingEnvironment to perform the operation in
        Returns:
        the desired member, if it exists
        Throws:
        AmbiguousDefinitionException - if it finds more than one candidate
        TargetNotFoundException - if it finds no valid candidate
        Since:
        0.3.0
      • findOverloadedMethod

        public static javax.lang.model.element.ExecutableElement findOverloadedMethod​(javax.lang.model.element.TypeElement context,
                                                                                      javax.lang.model.element.ExecutableElement method,
                                                                                      javax.annotation.processing.ProcessingEnvironment env)
        Tries to find the method being overloaded by the given ExecutableElement. In case of multiple layers of overloading, it finds the original one. In case of no overloading, it returns the given method.
        Parameters:
        context - the TypeElement representing the parent class
        method - an ExecutableElement representing the overloading method
        env - the ProcessingEnvironment to perform the operation in
        Returns:
        the original overloaded method, or the given method if it was not found
        Since:
        0.5.2
      • findSyntheticBridge

        public static javax.lang.model.element.ExecutableElement findSyntheticBridge​(javax.lang.model.element.TypeElement context,
                                                                                     javax.lang.model.element.ExecutableElement method,
                                                                                     javax.annotation.processing.ProcessingEnvironment env)
                                                                              throws TargetNotFoundException
        Tries to find the "synthetic bridge" generated by the compiler for a certain overridden methods. A "bridge" only exists in cases where type erasure is involved (i.e. when the method being overridden uses a generic parameter that is not preserved in the overriding method).
        Parameters:
        context - the TypeElement representing the parent class
        method - an ExecutableElement stub representing the overloading method
        env - the ProcessingEnvironment to perform the operation in
        Returns:
        the "bridge"
        Throws:
        TargetNotFoundException - if the method in question was not overriding anything, or if the method it was overriding does not require a bridge
        Since:
        0.5.2
      • getProxyType

        public static ProxyType getProxyType​(javax.lang.model.element.VariableElement v)
        Utility method for finding out what type of proxy a field is. It will fail if the return type is not a known type of proxy.
        Parameters:
        v - the annotated VariableElement
        Returns:
        the ProxyType for the element
        Throws:
        NotAProxyException - if it's neither
        Since:
        0.4.0
      • shouldValidate

        public static boolean shouldValidate​(java.lang.String name)
                                      throws InvalidClassNameException
        Checks whether a certain class name is valid, and whether the processor is able to validate its existence.
        Parameters:
        name - the name to validate
        Returns:
        true if it's a valid class name, false if it's an anonymous class identifier
        Throws:
        InvalidClassNameException - if an invalid name was provided
        Since:
        0.7.0