Search in sources :

Example 1 with MirroredTypeException

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();
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) Transient(io.requery.Transient) Table(io.requery.Table) Modifier(javax.lang.model.element.Modifier) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Embedded(io.requery.Embedded) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) MirroredTypeException(javax.lang.model.type.MirroredTypeException) PropertyNameStyle(io.requery.PropertyNameStyle) NestingKind(javax.lang.model.element.NestingKind) ElementFilter(javax.lang.model.util.ElementFilter) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) View(io.requery.View) Entity(io.requery.Entity) ElementKind(javax.lang.model.element.ElementKind) ExecutableElement(javax.lang.model.element.ExecutableElement) Cacheable(javax.persistence.Cacheable) Set(java.util.Set) ReadOnly(io.requery.ReadOnly) Element(javax.lang.model.element.Element) Collectors(java.util.stream.Collectors) TypeKind(javax.lang.model.type.TypeKind) Objects(java.util.Objects) SourceVersion(javax.lang.model.SourceVersion) TypeMirror(javax.lang.model.type.TypeMirror) List(java.util.List) Stream(java.util.stream.Stream) Index(javax.persistence.Index) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Annotation(java.lang.annotation.Annotation) Factory(io.requery.Factory) Optional(java.util.Optional) Embeddable(javax.persistence.Embeddable) Entity(io.requery.Entity) TypeMirror(javax.lang.model.type.TypeMirror) Objects(java.util.Objects) Elements(javax.lang.model.util.Elements)

Example 2 with MirroredTypeException

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);
    }
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) JCTree(com.sun.tools.javac.tree.JCTree)

Example 3 with MirroredTypeException

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;
}
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)

Example 4 with MirroredTypeException

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);
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) JsonField(com.bluelinelabs.logansquare.annotation.JsonField) JsonIgnore(com.bluelinelabs.logansquare.annotation.JsonIgnore) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) JsonFieldHolder(com.bluelinelabs.logansquare.processor.JsonFieldHolder) JsonObjectHolder(com.bluelinelabs.logansquare.processor.JsonObjectHolder)

Example 5 with MirroredTypeException

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);
    }
}
Also used : MirroredTypeException(javax.lang.model.type.MirroredTypeException) RInnerClass(org.androidannotations.internal.rclass.RInnerClass) IRInnerClass(org.androidannotations.rclass.IRInnerClass) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) InvocationTargetException(java.lang.reflect.InvocationTargetException) MirroredTypeException(javax.lang.model.type.MirroredTypeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

MirroredTypeException (javax.lang.model.type.MirroredTypeException)6 TypeElement (javax.lang.model.element.TypeElement)5 Element (javax.lang.model.element.Element)4 VariableElement (javax.lang.model.element.VariableElement)4 ExecutableElement (javax.lang.model.element.ExecutableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 JCTree (com.sun.tools.javac.tree.JCTree)2 Annotation (java.lang.annotation.Annotation)2 JsonField (com.bluelinelabs.logansquare.annotation.JsonField)1 JsonIgnore (com.bluelinelabs.logansquare.annotation.JsonIgnore)1 JsonFieldHolder (com.bluelinelabs.logansquare.processor.JsonFieldHolder)1 JsonObjectHolder (com.bluelinelabs.logansquare.processor.JsonObjectHolder)1 Embedded (io.requery.Embedded)1 Entity (io.requery.Entity)1 Factory (io.requery.Factory)1 PropertyNameStyle (io.requery.PropertyNameStyle)1 ReadOnly (io.requery.ReadOnly)1 Table (io.requery.Table)1 Transient (io.requery.Transient)1 View (io.requery.View)1