Search in sources :

Example 71 with DeclaredType

use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.

the class ValidatorHelper method canBePutInABundle.

public void canBePutInABundle(Element element, ElementValidation valid) {
    TypeMirror typeMirror = element.asType();
    String typeString = element.asType().toString();
    if (!isKnownBundleCompatibleType(typeString)) {
        if (typeMirror instanceof ArrayType) {
            ArrayType arrayType = (ArrayType) element.asType();
            typeMirror = arrayType.getComponentType();
        }
        if (typeMirror.getKind() != TypeKind.NONE) {
            TypeMirror parcelableType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.PARCELABLE).asType();
            TypeMirror serializableType = annotationHelper.typeElementFromQualifiedName("java.io.Serializable").asType();
            if (typeString.startsWith(CanonicalNameConstants.SPARSE_ARRAY)) {
                DeclaredType declaredType = (DeclaredType) typeMirror;
                List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
                if (typeArguments.size() != 1 || !annotationHelper.isSubtype(typeArguments.get(0), parcelableType)) {
                    valid.addError("Unrecognized type. The type argument of SparseArray should implement Parcelable.");
                }
            } else if (!annotationHelper.isSubtype(typeMirror, parcelableType) && !annotationHelper.isSubtype(typeMirror, serializableType) && !parcelerHelper.isParcelType(typeMirror)) {
                valid.addError("Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable or an annotated Parceler bean.");
            }
        }
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 72 with DeclaredType

use of javax.lang.model.type.DeclaredType in project androidannotations by androidannotations.

the class ValidatorHelper method extendsListOfView.

public void extendsListOfView(Element element, ElementValidation valid) {
    DeclaredType elementType = (DeclaredType) element.asType();
    List<? extends TypeMirror> elementTypeArguments = elementType.getTypeArguments();
    TypeMirror viewType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW).asType();
    if (!elementType.toString().equals(CanonicalNameConstants.LIST) && elementTypeArguments.size() == 1 && !annotationHelper.isSubtype(elementTypeArguments.get(0), viewType)) {
        valid.invalidate();
        valid.addError("%s can only be used on a " + CanonicalNameConstants.LIST + " of elements extending " + CanonicalNameConstants.VIEW);
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) DeclaredType(javax.lang.model.type.DeclaredType)

Example 73 with DeclaredType

use of javax.lang.model.type.DeclaredType in project j2objc by google.

the class TranslationUtil method createAnnotation.

public Expression createAnnotation(AnnotationMirror annotationMirror) {
    DeclaredType type = annotationMirror.getAnnotationType();
    TypeElement typeElem = (TypeElement) type.asElement();
    FunctionElement element = new FunctionElement("create_" + nameTable.getFullName(typeElem), type, typeElem);
    FunctionInvocation invocation = new FunctionInvocation(element, type);
    Map<? extends ExecutableElement, ? extends AnnotationValue> values = typeUtil.elementUtil().getElementValuesWithDefaults(annotationMirror);
    for (ExecutableElement member : ElementUtil.getSortedAnnotationMembers(typeElem)) {
        TypeMirror valueType = member.getReturnType();
        element.addParameters(valueType);
        invocation.addArgument(createAnnotationValue(valueType, values.get(member)));
    }
    return invocation;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 74 with DeclaredType

use of javax.lang.model.type.DeclaredType in project tiger by google.

the class ComponentGeneratorProcessor method getMembersInjectorScope.

/**
   * Returns the {@link DeclaredType} of the scope class of the
   * {@link MembersInjector} specified.
   */
private DeclaredType getMembersInjectorScope(DeclaredType membersInjectorType) {
    ExecutableElement scopeElement = null;
    TypeElement membersInjectorTypeElement = elementUtils.getTypeElement(MembersInjector.class.getCanonicalName());
    for (Element element : membersInjectorTypeElement.getEnclosedElements()) {
        if (element.getSimpleName().contentEquals("scope")) {
            scopeElement = (ExecutableElement) element;
        }
    }
    Preconditions.checkNotNull(scopeElement);
    for (AnnotationMirror annotationMirror : membersInjectorType.asElement().getAnnotationMirrors()) {
        if (annotationMirror.getAnnotationType().asElement().equals(elementUtils.getTypeElement(MembersInjector.class.getCanonicalName()))) {
            return (DeclaredType) annotationMirror.getElementValues().get(scopeElement).getValue();
        }
    }
    throw new RuntimeException(String.format("Scope not found for MembersInjector: %s", membersInjectorType));
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) PackageElement(javax.lang.model.element.PackageElement) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) DeclaredType(javax.lang.model.type.DeclaredType)

Example 75 with DeclaredType

use of javax.lang.model.type.DeclaredType in project tiger by google.

the class ComponentGeneratorProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
    if (done) {
        return false;
    }
    System.out.println("pure tiger");
    PackageElement packageElement = elementUtils.getPackageElement(SharedNames.DEPENDENCY_INFORMATION_PACKAGE_NAME);
    if (packageElement == null) {
        return false;
    }
    if (componentTree == null) {
        Map<TypeElement, TypeElement> scopeTree = null;
        Map<TypeElement, String> scopedComponentNames = null;
        for (Element element : packageElement.getEnclosedElements()) {
            for (Element e : element.getEnclosedElements()) {
                if (e.getKind().equals(ElementKind.FIELD)) {
                    Preconditions.checkState(((DeclaredType) e.asType()).asElement().equals(elementUtils.getTypeElement(String.class.getCanonicalName())), String.format("There should be only fields of type String. But got field: %s", e));
                    TypeElement typeElement;
                    java.lang.reflect.Type stringListType = new TypeToken<List<String>>() {
                    }.getType();
                    List<String> collected;
                    String fieldName = e.getSimpleName().toString();
                    String jsonString = (String) ((VariableElement) e).getConstantValue();
                    if (fieldName.equals(SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_SCOPE_DEPENDENCIES)) {
                        collected = gson.fromJson(jsonString, stringListType);
                        if (collected.isEmpty()) {
                            continue;
                        }
                        Set<TypeElement> scopeDependencies = new HashSet<>();
                        for (String elementName : collected) {
                            typeElement = elementUtils.getTypeElement(elementName);
                            scopeDependencies.add(typeElement);
                        }
                        if (scopeTree != null) {
                            messager.printMessage(Kind.ERROR, String.format("Duplicate scopeDependencies. Existing: %s, new: %s", scopeTree, scopeDependencies));
                        }
                        scopeTree = getScopeTree(scopeDependencies);
                    } else if (fieldName.equals(SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_SCOPED_COMPONENT_NAMES)) {
                        collected = gson.fromJson(jsonString, stringListType);
                        if (collected.isEmpty()) {
                            continue;
                        }
                        Set<TypeElement> scopeComponentNameElements = new HashSet<>();
                        for (String elementName : collected) {
                            typeElement = elementUtils.getTypeElement(elementName);
                            scopeComponentNameElements.add(typeElement);
                        }
                        scopedComponentNames = getScopedComponentNames(scopeComponentNameElements);
                    } else if (fieldName.equals(SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_PACKAGE_FOR_GENERATED)) {
                        packageForGenerated = jsonString;
                    }
                }
            // Enclosed elements.
            }
        // Classes
        }
        Preconditions.checkNotNull(packageForGenerated);
        if (scopedComponentNames == null) {
            Set<TypeElement> allScopes = new HashSet<>();
            Preconditions.checkNotNull(scopeTree);
            allScopes.addAll(scopeTree.values());
            allScopes.addAll(scopeTree.keySet());
            scopedComponentNames = getDefaultScopedComponentNames(allScopes);
        }
        componentTree = getComponentTree(scopeTree, scopedComponentNames);
        if (componentTree.isEmpty()) {
        // TODO(freeman): support only scope.
        }
        scopeSizer = new TreeScopeSizer(componentTree, null);
        //    messager.printMessage(Kind.NOTE, String.format("%s componentTree: %s", TAG, componentTree));
        //    messager.printMessage(Kind.NOTE, String.format("%s scopeSizer: %s", TAG, scopeSizer));
        scopeToComponent = getScopeToComponentMap();
    }
    if (componentTree == null) {
        return false;
    }
    for (Element element : packageElement.getEnclosedElements()) {
        for (Element e : element.getEnclosedElements()) {
            if (e.getKind().equals(ElementKind.FIELD)) {
                Preconditions.checkState(((DeclaredType) e.asType()).asElement().equals(elementUtils.getTypeElement(String.class.getCanonicalName())), String.format("There should be only fields of type String. But got field: %s", e));
                TypeElement typeElement;
                DeclaredType elementType;
                java.lang.reflect.Type stringListType = new TypeToken<List<String>>() {
                }.getType();
                List<String> collected;
                String fieldName = e.getSimpleName().toString();
                String jsonString = (String) ((VariableElement) e).getConstantValue();
                if (fieldName.equals(SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_MODULES)) {
                    collected = gson.fromJson(jsonString, stringListType);
                    for (String elementName : collected) {
                        typeElement = elementUtils.getTypeElement(elementName);
                        elementType = (DeclaredType) typeElement.asType();
                        if (Utils.hasProvisionMethod(elementType)) {
                            TypeElement scope = Utils.getModuleScope(elementType);
                            if (scope == null) {
                                unscopedModules.add(typeElement);
                            } else {
                                modules.put(scopeToComponent.get(scope), typeElement);
                            }
                        }
                    }
                } else if (fieldName.equals(SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_MEMBERS_INJECTORS)) {
                    collected = gson.fromJson(jsonString, stringListType);
                    for (String elementName : collected) {
                        typeElement = elementUtils.getTypeElement(elementName);
                        elementType = (DeclaredType) typeElement.asType();
                        DeclaredType scopeClass = getMembersInjectorScope(elementType);
                        TypeElement scope = (TypeElement) scopeClass.asElement();
                        injections.put(scopeToComponent.get(scope), typeElement);
                    }
                } else if (fieldName.equals(SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_SCOPE_DEPENDENCIES)) {
                // Already handled
                } else if (fieldName.equals(SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_SCOPED_COMPONENT_NAMES)) {
                // Already handled
                } else if (fieldName.equals(SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_PACKAGE_FOR_GENERATED)) {
                // Already handled
                } else if (fieldName.equals(SharedNames.DEPENDENCY_INFORMATION_FIELD_NAME_CTOR_INJECTED_CLASSES)) {
                    collected = gson.fromJson(jsonString, stringListType);
                    for (String elementName : collected) {
                        typeElement = elementUtils.getTypeElement(elementName);
                        // typeElement must have scope. See {@link DependencyInformationCollectorProcessor}.
                        TypeElement scope = (TypeElement) Utils.getScopeType(typeElement).asElement();
                        ctorInjectedClasses.put(scopeToComponent.get(scope), typeElement);
                    }
                } else {
                    throw new RuntimeException(String.format("Unexpected field: %s", fieldName));
                }
            }
        }
    // Enclosed elements.
    }
    // Classes
    check();
    if (!allRecoverableErrors.isEmpty()) {
        if (env.processingOver()) {
            for (String err : allRecoverableErrors) {
                messager.printMessage(Kind.ERROR, err);
            }
        }
        // Leave it to next round.
        return false;
    }
    Set<TypeElement> allModules = Sets.newHashSet(modules.values());
    allModules.addAll(unscopedModules);
    NewDependencyCollector dependencyCollector = new NewDependencyCollector(processingEnv);
    Collection<TypeElement> membersInjectors = injections.values();
    Collection<NewDependencyInfo> dependencyInfos = dependencyCollector.collect(allModules, membersInjectors, allRecoverableErrors);
    Set<NewBindingKey> requiredKeys = dependencyCollector.getRequiredKeys(membersInjectors, dependencyInfos);
    //messager.printMessage(Kind.NOTE, String.format("all keys required: %s", requiredKeys));
    NewScopeCalculator newScopeCalculator = new NewScopeCalculator(scopeSizer, dependencyInfos, requiredKeys, processingEnv);
    Preconditions.checkState(newScopeCalculator.initialize().isEmpty());
    NewInjectorGenerator newInjectorGenerator = new NewInjectorGenerator(NewDependencyCollector.collectionToMultimap(dependencyInfos), newScopeCalculator, modules, unscopedModules, injections, componentTree, // TODO(freeman): fill it if componentTree is empty.
    null, packageForGenerated, "Dagger", "Component", processingEnv);
    newInjectorGenerator.generate();
    done = true;
    return false;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) PackageElement(javax.lang.model.element.PackageElement) 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) List(java.util.List) HashSet(java.util.HashSet) TypeElement(javax.lang.model.element.TypeElement) PackageElement(javax.lang.model.element.PackageElement) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

DeclaredType (javax.lang.model.type.DeclaredType)138 TypeElement (javax.lang.model.element.TypeElement)82 TypeMirror (javax.lang.model.type.TypeMirror)75 ExecutableElement (javax.lang.model.element.ExecutableElement)38 Element (javax.lang.model.element.Element)28 VariableElement (javax.lang.model.element.VariableElement)26 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Test (org.junit.Test)19 ArrayType (javax.lang.model.type.ArrayType)15 ArrayList (java.util.ArrayList)14 List (java.util.List)10 Map (java.util.Map)9 AbstractJClass (com.helger.jcodemodel.AbstractJClass)8 HashSet (java.util.HashSet)8 AnnotationValue (javax.lang.model.element.AnnotationValue)8 HashMap (java.util.HashMap)6 TypeParameterElement (javax.lang.model.element.TypeParameterElement)6 Types (javax.lang.model.util.Types)6 Name (javax.lang.model.element.Name)5 PackageElement (javax.lang.model.element.PackageElement)5