use of javax.lang.model.type.MirroredTypeException in project requery by requery.
the class EntityType method builderType.
@Override
public Optional<TypeMirror> builderType() {
Optional<Entity> entityAnnotation = annotationOf(Entity.class);
if (entityAnnotation.isPresent()) {
Entity entity = entityAnnotation.get();
Elements elements = processingEnvironment.getElementUtils();
TypeMirror mirror = null;
try {
// easiest way to get the class TypeMirror
Class<?> builderClass = entity.builder();
if (builderClass != void.class) {
mirror = elements.getTypeElement(builderClass.getName()).asType();
}
} catch (MirroredTypeException typeException) {
mirror = typeException.getTypeMirror();
}
if (mirror != null && mirror.getKind() != TypeKind.VOID) {
return Optional.of(mirror);
}
}
if (builderFactoryMethod().isPresent()) {
return Optional.of(builderFactoryMethod().get().getReturnType());
}
return ElementFilter.typesIn(element().getEnclosedElements()).stream().filter(element -> element.getSimpleName().toString().contains("Builder")).map(Element::asType).filter(Objects::nonNull).filter(type -> type.getKind() != TypeKind.VOID).findFirst();
}
use of javax.lang.model.type.MirroredTypeException in project butterknife by JakeWharton.
the class ButterKnifeProcessor method parseRClass.
private void parseRClass(String respectivePackageName, String rClass) {
Element element;
try {
element = elementUtils.getTypeElement(rClass);
} catch (MirroredTypeException mte) {
element = typeUtils.asElement(mte.getTypeMirror());
}
JCTree tree = (JCTree) trees.getTree(element);
if (tree != null) {
// tree can be null if the references are compiled types and not source
IdScanner idScanner = new IdScanner(symbols, elementUtils.getPackageOf(element).getQualifiedName().toString(), respectivePackageName);
tree.accept(idScanner);
} else {
parseCompiledR(respectivePackageName, (TypeElement) element);
}
}
use of javax.lang.model.type.MirroredTypeException in project dukescript-presenters by dukescript.
the class JUnitTestMethodsProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (Element e : roundEnv.getElementsAnnotatedWith(JUnitTestMethods.class)) {
JUnitTestMethods ju = e.getAnnotation(JUnitTestMethods.class);
if (ju == null) {
continue;
}
PackageElement pkg = processingEnv.getElementUtils().getPackageOf(e);
try {
TypeMirror sn;
try {
ju.superclass().getName();
throw new IllegalStateException();
} catch (MirroredTypeException ex) {
sn = ex.getTypeMirror();
}
JavaFileObject sf = processingEnv.getFiler().createSourceFile(pkg.toString() + "." + ju.name(), e);
Writer w = sf.openWriter();
w.append("package ").append(pkg.toString()).append(";\n");
w.append("abstract class ").append(ju.name()).append(" extends ").append(sn.toString()).append(ju.generics()).append(" {\n");
Element esn = processingEnv.getTypeUtils().asElement(sn);
for (Element ee : esn.getEnclosedElements()) {
if (ee.getKind() != ElementKind.CONSTRUCTOR) {
continue;
}
ExecutableElement constructor = (ExecutableElement) ee;
w.append(" ").append(ju.name()).append("(");
String sep = "";
for (VariableElement ve : constructor.getParameters()) {
final TypeMirror vet = processingEnv.getTypeUtils().erasure(ve.asType());
w.append(sep).append(vet.toString()).append(" ").append(ve.getSimpleName());
sep = ", ";
}
w.append(") {\n");
w.append(" super(");
sep = "";
for (VariableElement ve : constructor.getParameters()) {
w.append(sep).append(ve.getSimpleName());
sep = ", ";
}
w.append(");\n");
w.append(" }\n");
}
final TypeMirror ko;
try {
ju.annotatedBy().getName();
throw new IllegalStateException();
} catch (MirroredTypeException ex) {
ko = ex.getTypeMirror();
}
final List<? extends TypeMirror> tests;
try {
ju.tests();
throw new IllegalStateException();
} catch (MirroredTypesException ex) {
tests = ex.getTypeMirrors();
}
for (TypeMirror test : tests) {
Element et = processingEnv.getTypeUtils().asElement(test);
for (Element ee : et.getEnclosedElements()) {
if (ee.getKind() != ElementKind.METHOD) {
continue;
}
boolean found = false;
for (AnnotationMirror am : ee.getAnnotationMirrors()) {
if (processingEnv.getTypeUtils().isSameType(am.getAnnotationType(), ko)) {
found = true;
break;
}
}
if (!found) {
continue;
}
String name = ee.getSimpleName().toString();
;
if (!name.startsWith("test")) {
name = "test" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
}
w.append(" public void " + name + "() {\n");
w.append(" throw new IllegalStateException(\"" + name + "\");\n");
w.append(" }\n");
}
}
w.append("}\n");
w.close();
} catch (IOException ex) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, ex.getMessage(), e);
}
}
return true;
}
use of javax.lang.model.type.MirroredTypeException in project LoganSquare by bluelinelabs.
the class JsonFieldProcessor method processJsonFieldAnnotation.
private void processJsonFieldAnnotation(Element element, Map<String, JsonObjectHolder> jsonObjectMap, Elements elements, Types types) {
if (!isJsonFieldFieldAnnotationValid(element, elements)) {
return;
}
TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
JsonObjectHolder objectHolder = jsonObjectMap.get(TypeUtils.getInjectedFQCN(enclosingElement, elements));
JsonFieldHolder fieldHolder = objectHolder.fieldMap.get(element.getSimpleName().toString());
if (fieldHolder == null) {
fieldHolder = new JsonFieldHolder();
objectHolder.fieldMap.put(element.getSimpleName().toString(), fieldHolder);
}
JsonField annotation = element.getAnnotation(JsonField.class);
TypeMirror typeConverterType;
try {
typeConverterType = mProcessingEnv.getElementUtils().getTypeElement(annotation.typeConverter().getCanonicalName()).asType();
} catch (MirroredTypeException mte) {
typeConverterType = mte.getTypeMirror();
}
String[] fieldName = annotation.name();
JsonIgnore ignoreAnnotation = element.getAnnotation(JsonIgnore.class);
boolean shouldParse = ignoreAnnotation == null || ignoreAnnotation.ignorePolicy() == IgnorePolicy.SERIALIZE_ONLY;
boolean shouldSerialize = ignoreAnnotation == null || ignoreAnnotation.ignorePolicy() == IgnorePolicy.PARSE_ONLY;
String error = fieldHolder.fill(element, elements, types, fieldName, typeConverterType, objectHolder, shouldParse, shouldSerialize);
if (!TextUtils.isEmpty(error)) {
error(element, error);
}
ensureTypeConverterClassValid(typeConverterType, elements, types);
}
use of javax.lang.model.type.MirroredTypeException in project androidannotations by androidannotations.
the class AnnotationHelper method extractAnnotationParameter.
@SuppressWarnings("unchecked")
public <T> T extractAnnotationParameter(Element element, String annotationName, String methodName) {
Annotation annotation;
try {
annotation = element.getAnnotation((Class<? extends Annotation>) Class.forName(annotationName));
} catch (ClassNotFoundException e) {
throw new RuntimeException("Could not load annotation class " + annotationName, e);
}
Method method;
try {
method = annotation.getClass().getMethod(methodName);
return (T) method.invoke(annotation);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof MirroredTypeException) {
MirroredTypeException cause = (MirroredTypeException) e.getCause();
return (T) cause.getTypeMirror();
} else {
throw new RuntimeException(e);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations