/
ClassTypeConverter.java
39 lines (31 loc) · 1.42 KB
/
ClassTypeConverter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.blamejared.crafttweaker_annotation_processors.processors.document.conversion.element;
import javax.lang.model.type.MirroredTypeException;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import java.lang.annotation.Annotation;
import java.util.function.Function;
public class ClassTypeConverter {
private final Elements elementUtils;
public ClassTypeConverter(Elements elementUtils) {
this.elementUtils = elementUtils;
}
public <T extends Annotation> TypeMirror getTypeMirror(T annotation, Function<T, Class<?>> classGetter) {
try {
return getNativeTypeFromClass(classGetter.apply(annotation));
} catch(MirroredTypeException exception) {
return getFromMirroredTypeException(exception);
} catch(TypeNotPresentException exception) {
return getFromTypeNotPresentException(exception);
}
}
private TypeMirror getFromTypeNotPresentException(TypeNotPresentException exception) {
final String typeName = exception.typeName();
return elementUtils.getTypeElement(typeName).asType();
}
private TypeMirror getFromMirroredTypeException(MirroredTypeException exception) {
return exception.getTypeMirror();
}
private TypeMirror getNativeTypeFromClass(Class<?> cls) {
return elementUtils.getTypeElement(cls.getCanonicalName()).asType();
}
}