use of javax.lang.model.type.MirroredTypesException 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;
}
Aggregations