Search in sources :

Example 6 with Element

use of javax.lang.model.element.Element in project buck by facebook.

the class TreeBackedTypeParameterElementTest method testAccept.

@Test
public void testAccept() throws IOException {
    compile("class Foo<T> { }");
    TypeParameterElement expectedTypeParameter = elements.getTypeElement("Foo").getTypeParameters().get(0);
    Object expectedResult = new Object();
    Object actualResult = expectedTypeParameter.accept(new SimpleElementVisitor8<Object, Object>() {

        @Override
        protected Object defaultAction(Element e, Object o) {
            return null;
        }

        @Override
        public Object visitTypeParameter(TypeParameterElement actualTypeParameter, Object o) {
            assertSame(expectedTypeParameter, actualTypeParameter);
            return o;
        }
    }, expectedResult);
    assertSame(expectedResult, actualResult);
}
Also used : Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Test(org.junit.Test)

Example 7 with Element

use of javax.lang.model.element.Element in project RoboBinding by RoboBinding.

the class ElementWrapperTest method unsupportedElements.

@DataPoints("unsupportedElements")
public static Element[] unsupportedElements() {
    Elements elements = compilation.getElements();
    PackageElement packageElement = elements.getPackageElement("java.lang");
    TypeElement typeElement = elements.getTypeElement(MethodsAndFields.class.getName());
    VariableElement variableElement = ElementFilter.fieldsIn(typeElement.getEnclosedElements()).get(0);
    return new Element[] { packageElement, variableElement };
}
Also used : TypeElement(javax.lang.model.element.TypeElement) 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) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) Elements(javax.lang.model.util.Elements) FromDataPoints(org.junit.experimental.theories.FromDataPoints) DataPoints(org.junit.experimental.theories.DataPoints)

Example 8 with Element

use of javax.lang.model.element.Element in project RxCache by VictorAlbertos.

the class GetProvidersClass method getMethods.

private List<ProvidersClass.Method> getMethods(Element classElement) throws ValidationException {
    List<? extends Element> enclosedElements = classElement.getEnclosedElements();
    List<ProvidersClass.Method> methods = new ArrayList<>();
    for (Element methodElement : enclosedElements) {
        if (!isAnnotatedWithActionable(methodElement))
            continue;
        if (methodElement.getKind() != ElementKind.METHOD)
            continue;
        Symbol.MethodSymbol methodSymbol = (Symbol.MethodSymbol) methodElement;
        String nameMethod = methodSymbol.getSimpleName().toString();
        Type returnType = methodSymbol.getReturnType();
        if (!returnType.tsym.toString().equals(TypeName.get(Observable.class).toString())) {
            throw new ValidationException(methodSymbol, "Error parsing %s provider. Only Observable<List> type is supported as observable loader", nameMethod);
        }
        Type enclosingTypeObservable = returnType.getTypeArguments().get(0);
        if (!enclosingTypeObservable.tsym.toString().equals(TypeName.get(List.class).toString())) {
            throw new ValidationException(methodSymbol, "Error parsing %s provider. Only Observable<List> type is supported as observable loader", nameMethod);
        }
        List<Symbol.VarSymbol> params = methodSymbol.getParameters();
        boolean hasEvictProvider = hasEvictProvider(params);
        boolean hasEvictDynamicKey = hasEvictDynamicKey(params);
        boolean hasEvictDynamicKeyGroup = hasEvictDynamicKeyGroup(params);
        if (!hasEvictProvider && !hasEvictDynamicKey && !hasEvictDynamicKeyGroup) {
            throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
        }
        if (hasEvictProvider && hasEvictDynamicKey) {
            throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
        }
        if (hasEvictProvider && hasEvictDynamicKeyGroup) {
            throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
        }
        if (hasEvictDynamicKey && hasEvictDynamicKeyGroup) {
            throw new ValidationException(methodElement, "Error parsing %s provider. The provider requires one evicting argument: EvictProvider, EvictDynamicKey or EvictDynamicKeyGroup", nameMethod);
        }
        boolean hasDynamicKey = hasDynamicKey(params);
        boolean hasDynamicKeyGroup = hasDynamicKeyGroup(params);
        methods.add(new ProvidersClass.Method(nameMethod, methodElement, enclosingTypeObservable, hasDynamicKey, hasDynamicKeyGroup));
    }
    return methods;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) Observable(io.reactivex.Observable) Type(com.sun.tools.javac.code.Type) List(java.util.List) ArrayList(java.util.ArrayList)

Example 9 with Element

use of javax.lang.model.element.Element in project epoxy by airbnb.

the class HashCodeValidator method hasHashCodeInClassHierarchy.

private boolean hasHashCodeInClassHierarchy(TypeElement clazz) {
    ExecutableElement methodOnClass = getMethodOnClass(clazz, HASH_CODE_METHOD, typeUtils);
    if (methodOnClass == null) {
        return false;
    }
    Element implementingClass = methodOnClass.getEnclosingElement();
    if (implementingClass.getSimpleName().toString().equals("Object")) {
        // Don't count default implementation on Object class
        return false;
    }
    // correctly :P)
    return true;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement)

Example 10 with Element

use of javax.lang.model.element.Element in project epoxy by airbnb.

the class HashCodeValidator method validateImplementsHashCode.

private void validateImplementsHashCode(TypeMirror mirror) throws EpoxyProcessorException {
    if (TypeName.get(mirror).isPrimitive()) {
        return;
    }
    if (mirror.getKind() == TypeKind.ARRAY) {
        validateArrayType((ArrayType) mirror);
        return;
    }
    if (!(mirror instanceof DeclaredType)) {
        return;
    }
    DeclaredType declaredType = (DeclaredType) mirror;
    Element element = typeUtils.asElement(mirror);
    TypeElement clazz = (TypeElement) element;
    if (isIterableType(clazz)) {
        validateIterableType(declaredType);
        return;
    }
    if (isAutoValueType(element)) {
        return;
    }
    if (isWhiteListedType(element)) {
        return;
    }
    if (!hasHashCodeInClassHierarchy(clazz)) {
        throwError("Attribute does not implement hashCode");
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

Element (javax.lang.model.element.Element)286 TypeElement (javax.lang.model.element.TypeElement)227 ExecutableElement (javax.lang.model.element.ExecutableElement)148 VariableElement (javax.lang.model.element.VariableElement)96 TypeMirror (javax.lang.model.type.TypeMirror)68 PackageElement (javax.lang.model.element.PackageElement)48 ArrayList (java.util.ArrayList)39 DeclaredType (javax.lang.model.type.DeclaredType)30 IOException (java.io.IOException)29 Map (java.util.Map)26 HashSet (java.util.HashSet)23 LinkedHashSet (java.util.LinkedHashSet)22 List (java.util.List)22 Set (java.util.Set)22 Test (org.junit.Test)21 ElementKind (javax.lang.model.element.ElementKind)20 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Elements (javax.lang.model.util.Elements)18 HashMap (java.util.HashMap)16 LinkedHashMap (java.util.LinkedHashMap)15