Package ftbsc.lll.processor.utils
Class ASTUtils
- java.lang.Object
-
- ftbsc.lll.processor.utils.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 givenExecutableElement
.static java.lang.String
descriptorFromType(javax.lang.model.type.TypeMirror t, javax.annotation.processing.ProcessingEnvironment env)
Builds a type descriptor from the givenTypeMirror
.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 anElement
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 givenExecutableElement
.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 theClassData
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 theFieldData
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 theMethodData
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.TypeMirrorgetTypeFromAnnotation(T ann, java.util.function.Function<T,java.lang.Class<?>> classFunction, javax.annotation.processing.ProcessingEnvironment env)
Safely extracts aClass
from an annotation and gets aTypeMirror
representing it.static java.lang.String
internalNameFromType(javax.lang.model.type.TypeMirror type, javax.annotation.processing.ProcessingEnvironment env)
Gets the internal name from aTypeMirror
.static int
mapModifier(javax.lang.model.element.Modifier m)
Maps aModifier
to its reflectiveModifier
equivalent.static int
mapModifiers(java.util.Collection<javax.lang.model.element.Modifier> modifiers)
Takes in aCollection
of ASTModifier
s 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.
-
-
-
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 anElement
from it.- Type Parameters:
T
- the type ofElement
to use- Parameters:
parent
- the parentElement
to the desired elementann
- theClass
corresponding to the desired annotation- Returns:
- a
List
ofElement
s annotated with the given annotation - Since:
- 0.2.0
-
mapModifier
public static int mapModifier(javax.lang.model.element.Modifier m)
Maps aModifier
to its reflectiveModifier
equivalent.- Parameters:
m
- theModifier
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 aCollection
of ASTModifier
s and returns them mapped to their reflective integer equivalent.- Parameters:
modifiers
- theModifier
s- 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 aClass
from an annotation and gets aTypeMirror
representing it.- Type Parameters:
T
- the type of the annotation carrying the information- Parameters:
ann
- the annotation containing the classclassFunction
- the annotation function returning the classenv
- theProcessingEnvironment
to perform the operation in- Returns:
- a
TypeMirror
representing the requestedClass
- 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 aTypeMirror
.- Parameters:
type
- theTypeMirror
in questionenv
- theProcessingEnvironment
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 givenTypeMirror
.- Parameters:
t
- theTypeMirror
representing the desired typeenv
- theProcessingEnvironment
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 givenExecutableElement
.- Parameters:
m
- theExecutableElement
for the methodenv
- theProcessingEnvironment
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 theClassData
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 convertmapper
- theMapper
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 theMethodData
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 classname
- the name of the memberdescriptor
- the descriptor of the methodmapper
- theMapper
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 theFieldData
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 classname
- the name of the membermapper
- theMapper
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
- theClassContainer
representing the parentname
- the name to search fordescr
- the descriptor to search for, or null if it's not a methodstrict
- whether to perform lookup in strict mode (seeTarget.strict()
for more information)field
- whether the member being searched is a fieldenv
- theProcessingEnvironment
to perform the operation in- Returns:
- the desired member, if it exists
- Throws:
AmbiguousDefinitionException
- if it finds more than one candidateTargetNotFoundException
- 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 givenExecutableElement
. In case of multiple layers of overloading, it finds the original one. In case of no overloading, it returns the given method.- Parameters:
context
- theTypeElement
representing the parent classmethod
- anExecutableElement
representing the overloading methodenv
- theProcessingEnvironment
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
- theTypeElement
representing the parent classmethod
- anExecutableElement
stub representing the overloading methodenv
- theProcessingEnvironment
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 annotatedVariableElement
- 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
-
-