Search in sources :

Example 56 with VariableElement

use of javax.lang.model.element.VariableElement in project bazel by bazelbuild.

the class ElementUtils method getAllFieldsIn.

/**
     * Return all fields declared in the given type or any superclass/interface.
     * TODO: should this use javax.lang.model.util.Elements.getAllMembers(TypeElement)
     * instead of our own getSuperTypes?
     */
public static List<VariableElement> getAllFieldsIn(TypeElement type) {
    List<VariableElement> fields = new ArrayList<VariableElement>();
    fields.addAll(ElementFilter.fieldsIn(type.getEnclosedElements()));
    List<TypeElement> alltypes = getSuperTypes(type);
    for (TypeElement atype : alltypes) {
        fields.addAll(ElementFilter.fieldsIn(atype.getEnclosedElements()));
    }
    return Collections.<VariableElement>unmodifiableList(fields);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement)

Example 57 with VariableElement

use of javax.lang.model.element.VariableElement in project EventBus by greenrobot.

the class EventBusAnnotationProcessor method checkForSubscribersToSkip.

/**
     * Subscriber classes should be skipped if their class or any involved event class are not visible to the index.
     */
private void checkForSubscribersToSkip(Messager messager, String myPackage) {
    for (TypeElement skipCandidate : methodsByClass.keySet()) {
        TypeElement subscriberClass = skipCandidate;
        while (subscriberClass != null) {
            if (!isVisible(myPackage, subscriberClass)) {
                boolean added = classesToSkip.add(skipCandidate);
                if (added) {
                    String msg;
                    if (subscriberClass.equals(skipCandidate)) {
                        msg = "Falling back to reflection because class is not public";
                    } else {
                        msg = "Falling back to reflection because " + skipCandidate + " has a non-public super class";
                    }
                    messager.printMessage(Diagnostic.Kind.NOTE, msg, subscriberClass);
                }
                break;
            }
            List<ExecutableElement> methods = methodsByClass.get(subscriberClass);
            if (methods != null) {
                for (ExecutableElement method : methods) {
                    String skipReason = null;
                    VariableElement param = method.getParameters().get(0);
                    TypeMirror typeMirror = getParamTypeMirror(param, messager);
                    if (!(typeMirror instanceof DeclaredType) || !(((DeclaredType) typeMirror).asElement() instanceof TypeElement)) {
                        skipReason = "event type cannot be processed";
                    }
                    if (skipReason == null) {
                        TypeElement eventTypeElement = (TypeElement) ((DeclaredType) typeMirror).asElement();
                        if (!isVisible(myPackage, eventTypeElement)) {
                            skipReason = "event type is not public";
                        }
                    }
                    if (skipReason != null) {
                        boolean added = classesToSkip.add(skipCandidate);
                        if (added) {
                            String msg = "Falling back to reflection because " + skipReason;
                            if (!subscriberClass.equals(skipCandidate)) {
                                msg += " (found in super class for " + skipCandidate + ")";
                            }
                            messager.printMessage(Diagnostic.Kind.NOTE, msg, param);
                        }
                        break;
                    }
                }
            }
            subscriberClass = getSuperclass(subscriberClass);
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 58 with VariableElement

use of javax.lang.model.element.VariableElement in project auto by google.

the class AutoAnnotationProcessor method getParameters.

private ImmutableMap<String, Parameter> getParameters(TypeElement annotationElement, ExecutableElement method, Map<String, Member> members, TypeSimplifier typeSimplifier) {
    ImmutableMap.Builder<String, Parameter> parameters = ImmutableMap.builder();
    boolean error = false;
    for (VariableElement parameter : method.getParameters()) {
        String name = parameter.getSimpleName().toString();
        Member member = members.get(name);
        if (member == null) {
            reportError(parameter, "@AutoAnnotation method parameter '%s' must have the same name as a member of %s", name, annotationElement);
            error = true;
        } else {
            TypeMirror parameterType = parameter.asType();
            TypeMirror memberType = member.getTypeMirror();
            if (compatibleTypes(parameterType, memberType)) {
                parameters.put(name, new Parameter(parameterType, typeSimplifier));
            } else {
                reportError(parameter, "@AutoAnnotation method parameter '%s' has type %s but %s.%s has type %s", name, parameterType, annotationElement, name, memberType);
                error = true;
            }
        }
    }
    if (error) {
        throw new AbortProcessingException();
    }
    return parameters.build();
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) VariableElement(javax.lang.model.element.VariableElement) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 59 with VariableElement

use of javax.lang.model.element.VariableElement in project auto by google.

the class AutoAnnotationProcessor method wrapperTypesUsedInCollections.

/**
   * Returns the wrapper types ({@code Integer.class} etc) that are used in collection parameters
   * like {@code List<Integer>}. This is needed because we will emit a helper method for each such
   * type, for example to convert {@code Collection<Integer>} into {@code int[]}.
   */
private Set<Class<?>> wrapperTypesUsedInCollections(ExecutableElement method) {
    TypeElement javaUtilCollection = processingEnv.getElementUtils().getTypeElement(Collection.class.getName());
    ImmutableSet.Builder<Class<?>> usedInCollections = ImmutableSet.builder();
    for (Class<?> wrapper : Primitives.allWrapperTypes()) {
        DeclaredType collectionOfWrapper = typeUtils.getDeclaredType(javaUtilCollection, getTypeMirror(wrapper));
        for (VariableElement parameter : method.getParameters()) {
            if (typeUtils.isAssignable(parameter.asType(), collectionOfWrapper)) {
                usedInCollections.add(wrapper);
                break;
            }
        }
    }
    return usedInCollections.build();
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) TypeElement(javax.lang.model.element.TypeElement) Collection(java.util.Collection) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 60 with VariableElement

use of javax.lang.model.element.VariableElement in project auto by google.

the class MoreTypesTest method testReferencedTypes.

@Test
public void testReferencedTypes() {
    Elements elements = compilationRule.getElements();
    TypeElement testDataElement = elements.getTypeElement(ReferencedTypesTestData.class.getCanonicalName());
    ImmutableMap<String, VariableElement> fieldIndex = FluentIterable.from(ElementFilter.fieldsIn(testDataElement.getEnclosedElements())).uniqueIndex(new Function<VariableElement, String>() {

        @Override
        public String apply(VariableElement input) {
            return input.getSimpleName().toString();
        }
    });
    TypeElement objectElement = elements.getTypeElement(Object.class.getCanonicalName());
    TypeElement stringElement = elements.getTypeElement(String.class.getCanonicalName());
    TypeElement integerElement = elements.getTypeElement(Integer.class.getCanonicalName());
    TypeElement setElement = elements.getTypeElement(Set.class.getCanonicalName());
    TypeElement mapElement = elements.getTypeElement(Map.class.getCanonicalName());
    TypeElement charSequenceElement = elements.getTypeElement(CharSequence.class.getCanonicalName());
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f1").asType())).containsExactly(objectElement);
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f2").asType())).containsExactly(setElement, stringElement);
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f3").asType())).containsExactly(mapElement, stringElement, objectElement);
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f4").asType())).containsExactly(integerElement);
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f5").asType())).containsExactly(setElement);
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f6").asType())).containsExactly(setElement, charSequenceElement);
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f7").asType())).containsExactly(mapElement, stringElement, setElement, charSequenceElement);
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f8").asType())).containsExactly(stringElement);
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f9").asType())).containsExactly(stringElement);
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f10").asType())).isEmpty();
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f11").asType())).isEmpty();
    assertThat(MoreTypes.referencedTypes(fieldIndex.get("f12").asType())).containsExactly(setElement, stringElement);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) TypeElement(javax.lang.model.element.TypeElement) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

VariableElement (javax.lang.model.element.VariableElement)248 ExecutableElement (javax.lang.model.element.ExecutableElement)75 TypeElement (javax.lang.model.element.TypeElement)75 TypeMirror (javax.lang.model.type.TypeMirror)72 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)35 Element (javax.lang.model.element.Element)35 Expression (com.google.devtools.j2objc.ast.Expression)30 SimpleName (com.google.devtools.j2objc.ast.SimpleName)28 JVar (com.helger.jcodemodel.JVar)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)23 ArrayList (java.util.ArrayList)23 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)16 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)16 JInvocation (com.helger.jcodemodel.JInvocation)16 DeclaredType (javax.lang.model.type.DeclaredType)16 JBlock (com.helger.jcodemodel.JBlock)15 AbstractJClass (com.helger.jcodemodel.AbstractJClass)14 SingleVariableDeclaration (com.google.devtools.j2objc.ast.SingleVariableDeclaration)13 Elements (javax.lang.model.util.Elements)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)12