Search in sources :

Example 1 with MirroredTypesException

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;
}
Also used : MirroredTypesException(javax.lang.model.type.MirroredTypesException) PackageElement(javax.lang.model.element.PackageElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) IOException(java.io.IOException) MirroredTypeException(javax.lang.model.type.MirroredTypeException) JavaFileObject(javax.tools.JavaFileObject) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeMirror(javax.lang.model.type.TypeMirror) PackageElement(javax.lang.model.element.PackageElement) Writer(java.io.Writer)

Aggregations

IOException (java.io.IOException)1 Writer (java.io.Writer)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 PackageElement (javax.lang.model.element.PackageElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 MirroredTypeException (javax.lang.model.type.MirroredTypeException)1 MirroredTypesException (javax.lang.model.type.MirroredTypesException)1 TypeMirror (javax.lang.model.type.TypeMirror)1 JavaFileObject (javax.tools.JavaFileObject)1