Search in sources :

Example 21 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project requery by requery.

the class EntityType method addAnnotationElement.

@Override
public void addAnnotationElement(TypeElement annotationElement, Element annotatedElement) {
    String qualifiedName = annotationElement.getQualifiedName().toString();
    Class<? extends Annotation> type;
    try {
        type = Class.forName(qualifiedName).asSubclass(Annotation.class);
    } catch (ClassNotFoundException e) {
        return;
    }
    switch(annotatedElement.getKind()) {
        case CLASS:
        case INTERFACE:
            annotations().put(type, annotatedElement.getAnnotation(type));
            break;
        case FIELD:
            if (annotatedElement.getModifiers().contains(Modifier.STATIC) || annotatedElement.getModifiers().contains(Modifier.FINAL)) {
                // check if this a requery annotation
                String packageName = Entity.class.getPackage().getName();
                if (annotationElement.getQualifiedName().toString().startsWith(packageName)) {
                    processingEnvironment.getMessager().printMessage(Diagnostic.Kind.ERROR, annotationElement.getQualifiedName() + " not applicable to static or final member", annotatedElement);
                }
            } else {
                VariableElement element = (VariableElement) annotatedElement;
                Optional<AttributeMember> attribute = computeAttribute(element);
                Annotation annotation = annotatedElement.getAnnotation(type);
                attribute.ifPresent(a -> a.annotations().put(type, annotation));
            }
            break;
        case METHOD:
            ExecutableElement element = (ExecutableElement) annotatedElement;
            Annotation annotation = annotatedElement.getAnnotation(type);
            if (ListenerAnnotations.all().anyMatch(a -> a.equals(type))) {
                ListenerMethod listener = listeners.computeIfAbsent(element, key -> new ListenerMethod(element));
                listener.annotations().put(type, annotation);
            } else if (isMethodProcessable(element)) {
                Optional<AttributeMember> attribute = computeAttribute(element);
                attribute.ifPresent(a -> a.annotations().put(type, annotation));
            }
            break;
    }
}
Also used : 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) Optional(java.util.Optional) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) Annotation(java.lang.annotation.Annotation)

Example 22 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project requery by requery.

the class EntityType method factoryArguments.

@Override
public List<String> factoryArguments() {
    List<String> names = new ArrayList<>();
    ExecutableElement method = factoryMethod().orElseThrow(IllegalStateException::new);
    // TODO need more validation here
    // now match the builder fields to the parameters...
    Map<Element, AttributeDescriptor> map = new LinkedHashMap<>(attributes);
    for (VariableElement parameter : method.getParameters()) {
        // straight forward case type and name are the same
        Element matched = null;
        for (Map.Entry<Element, AttributeDescriptor> entry : map.entrySet()) {
            AttributeDescriptor attribute = entry.getValue();
            String fieldName = attribute.fieldName();
            if (fieldName.equalsIgnoreCase(parameter.getSimpleName().toString())) {
                names.add(fieldName);
                matched = entry.getKey();
            }
        }
        if (matched != null) {
            map.remove(matched);
        }
    }
    // didn't work likely because the parameter names are missing
    if (names.isEmpty()) {
        // for kotlin data classes add processable element field names in order
        if (isUnimplementable()) {
            ElementFilter.methodsIn(element().getEnclosedElements()).stream().filter(this::isMethodProcessable).forEach(getter -> names.addAll(map.entrySet().stream().filter(entry -> entry.getKey().equals(getter)).map(entry -> entry.getValue().fieldName()).collect(Collectors.toList())));
        } else {
            for (Map.Entry<Element, AttributeDescriptor> entry : map.entrySet()) {
                names.add(0, entry.getValue().fieldName());
            }
        }
    }
    return names;
}
Also used : 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) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 23 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project roboguice by roboguice.

the class GuiceAnnotationProcessor method addMethodOrConstructorToAnnotationDatabase.

private void addMethodOrConstructorToAnnotationDatabase(String annotationClassName, Element injectionPoint) {
    String injectionPointName = injectionPoint.getSimpleName().toString();
    for (VariableElement variable : ((ExecutableElement) injectionPoint).getParameters()) {
        String parameterTypeName = getTypeName((TypeElement) ((DeclaredType) variable.asType()).asElement());
        bindableClasses.add(parameterTypeName);
        injectionPointName += ":" + parameterTypeName;
    }
    TypeElement typeElementRequiringScanning = (TypeElement) injectionPoint.getEnclosingElement();
    String typeElementName = getTypeName(typeElementRequiringScanning);
    //System.out.printf("Type: %s, injection: %s \n",typeElementName, injectionPointName);
    if (injectionPointName.startsWith("<init>")) {
        addToInjectedConstructors(annotationClassName, typeElementName, injectionPointName);
    } else {
        addToInjectedMethods(annotationClassName, typeElementName, injectionPointName);
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) String(java.lang.String) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 24 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project robolectric by robolectric.

the class ShadowProviderGeneratorTest method element.

@NotNull
private ExecutableElement element(String reset) {
    ExecutableElement resetterExecutable = mock(ExecutableElement.class);
    Name mock = mock(Name.class);
    when(mock.toString()).thenReturn(reset);
    when(resetterExecutable.getSimpleName()).thenReturn(mock);
    return resetterExecutable;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) Name(javax.lang.model.element.Name) NotNull(org.jetbrains.annotations.NotNull)

Example 25 with ExecutableElement

use of javax.lang.model.element.ExecutableElement in project robolectric by robolectric.

the class ShadowProviderGeneratorTest method resettersAreOnlyCalledIfSdkMatches.

@Test
public void resettersAreOnlyCalledIfSdkMatches() throws Exception {
    HashMap<TypeElement, ExecutableElement> resetters = new HashMap<>();
    resetters.put(type("ShadowThing", 19, 20), element("reset19To20"));
    resetters.put(type("ShadowThing", -1, 18), element("resetMax18"));
    resetters.put(type("ShadowThing", 21, -1), element("resetMin21"));
    when(model.getResetters()).thenReturn(resetters);
    generator.generate("the.package", new PrintWriter(writer));
    assertThat(writer.toString()).contains("if (org.robolectric.RuntimeEnvironment.getApiLevel() >= 19 && org.robolectric.RuntimeEnvironment.getApiLevel() <= 20) ShadowThing.reset19To20();");
    assertThat(writer.toString()).contains("if (org.robolectric.RuntimeEnvironment.getApiLevel() >= 21) ShadowThing.resetMin21();");
    assertThat(writer.toString()).contains("if (org.robolectric.RuntimeEnvironment.getApiLevel() <= 18) ShadowThing.resetMax18();");
}
Also used : HashMap(java.util.HashMap) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

ExecutableElement (javax.lang.model.element.ExecutableElement)341 TypeElement (javax.lang.model.element.TypeElement)154 TypeMirror (javax.lang.model.type.TypeMirror)96 VariableElement (javax.lang.model.element.VariableElement)85 Element (javax.lang.model.element.Element)69 Test (org.junit.Test)41 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)32 DeclaredType (javax.lang.model.type.DeclaredType)30 ArrayList (java.util.ArrayList)26 JBlock (com.helger.jcodemodel.JBlock)25 JInvocation (com.helger.jcodemodel.JInvocation)20 MethodDeclaration (com.google.devtools.j2objc.ast.MethodDeclaration)18 JVar (com.helger.jcodemodel.JVar)18 Map (java.util.Map)18 IJExpression (com.helger.jcodemodel.IJExpression)16 HashSet (java.util.HashSet)16 ElementValidation (org.androidannotations.ElementValidation)15 Block (com.google.devtools.j2objc.ast.Block)13 JMethod (com.helger.jcodemodel.JMethod)13 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)11