Search in sources :

Example 21 with PrimitiveType

use of javax.lang.model.type.PrimitiveType in project requery by requery.

the class AttributeMember method checkMemberType.

private void checkMemberType(ProcessingEnvironment processingEnvironment, Set<ElementValidator> validators) {
    builderClass = AttributeBuilder.class;
    Types types = processingEnvironment.getTypeUtils();
    TypeMirror mirror = typeMirror();
    isBoolean = mirror.getKind() == TypeKind.BOOLEAN;
    if (mirror.getKind() == TypeKind.DECLARED) {
        TypeElement element = (TypeElement) types.asElement(mirror);
        if (element != null) {
            // only set if the attribute is relational
            if (cardinality != null) {
                isIterable = Mirrors.isInstance(types, element, Iterable.class);
            }
            isMap = Mirrors.isInstance(types, element, Map.class);
            if (isMap && cardinality != null) {
                builderClass = MapAttributeBuilder.class;
            }
            // check for optional compatible types
            String[] names = { Optional.class.getName(), "com.google.common.base.Optional", "java8.util.Optional" };
            for (String name : names) {
                if (Mirrors.isInstance(types, element, name)) {
                    isOptional = true;
                    optionalClassName = name;
                    break;
                }
            }
            isBoolean = Mirrors.isInstance(types, element, Boolean.class);
        }
    }
    if (isIterable) {
        ElementValidator validator = validateCollectionType(processingEnvironment);
        if (validator != null) {
            validators.add(validator);
        }
    } else {
        TypeElement element = null;
        if (mirror.getKind().isPrimitive()) {
            element = types.boxedClass((PrimitiveType) mirror);
        } else if (mirror.getKind() == TypeKind.DECLARED) {
            element = (TypeElement) types.asElement(mirror);
        }
        if (element != null) {
            if (Mirrors.isInstance(types, element, Number.class) || Mirrors.isInstance(types, element, java.util.Date.class) || Mirrors.isInstance(types, element, java.time.temporal.Temporal.class)) {
                type = Type.NUMERIC;
            } else if (Mirrors.isInstance(types, element, String.class)) {
                type = Type.STRING;
            }
        }
    }
}
Also used : Types(javax.lang.model.util.Types) Optional(java.util.Optional) TypeElement(javax.lang.model.element.TypeElement) TypeMirror(javax.lang.model.type.TypeMirror) PrimitiveType(javax.lang.model.type.PrimitiveType) Map(java.util.Map)

Example 22 with PrimitiveType

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

the class DependencyCollector method getDependencyInfoForMethod.

@Nullable
public DependencyInfo getDependencyInfoForMethod(ExecutableElement method, DependencySourceType dependencySourceType, @Nullable TypeElement source, @Nullable CoreInjectorInfo coreInjectorInfo) {
    BindingKey key = getBindingKeyForMethod(method);
    if (key == null) {
        return null;
    }
    // logger.n(TAG + ".getDependencyInfoForMethod: method " + method);
    AnnotationMirror annotation = utils.getQualifier(method);
    List<BindingKey> keys = utils.getDependenciesFromExecutableElement(method);
    // Could be from module or other source like component dependencies.
    ProvisionType provideType = utils.isProvisionMethodInModule(method) ? utils.getProvisionType(method) : UNIQUE;
    if (SET.equals(provideType)) {
        key = BindingKey.get(ParameterizedTypeName.get(ClassName.get(Set.class), key.getTypeName()), annotation);
    } else if (MAP.equals(provideType) && !utils.isMultibindsMethod(method)) {
        AnnotationMirror mapKeyedMirror = Preconditions.checkNotNull(utils.getMapKey(method), String.format("Map binding %s missed MapKey.", method));
        AnnotationMirror mapKeyMirror = utils.getAnnotationMirror(mapKeyedMirror.getAnnotationType().asElement(), MapKey.class);
        AnnotationValue unwrapValue = utils.getAnnotationValue(elements, mapKeyMirror, "unwrapValue");
        if (unwrapValue != null && !((Boolean) unwrapValue.getValue())) {
            logger.e(String.format("MapKey with unwrapValue false is not supported, yet. Biding: %s", method));
        }
        TypeMirror keyTypeMirror = Preconditions.checkNotNull(utils.getElementTypeMirror(mapKeyedMirror, "value"), String.format("Get key type failed for binding %s", method));
        if (keyTypeMirror instanceof PrimitiveType) {
            keyTypeMirror = types.boxedClass((PrimitiveType) keyTypeMirror).asType();
        }
        TypeMirror valueTypeMirror = method.getReturnType();
        AnnotationMirror qualifier = utils.getQualifier(method);
        key = BindingKey.get(ParameterizedTypeName.get(ClassName.get(Map.class), TypeName.get(keyTypeMirror), TypeName.get(valueTypeMirror)), qualifier);
    }
    DependencyInfo result = new DependencyInfo(dependencySourceType, key, Sets.newHashSet(keys), source != null ? source : (TypeElement) method.getEnclosingElement(), method, provideType, coreInjectorInfo);
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) HashSet(java.util.HashSet) Set(java.util.Set) MapKey(dagger.MapKey) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) AnnotationValue(javax.lang.model.element.AnnotationValue) PrimitiveType(javax.lang.model.type.PrimitiveType) Nullable(javax.annotation.Nullable)

Example 23 with PrimitiveType

use of javax.lang.model.type.PrimitiveType in project neo4j by neo4j.

the class TypeMirrorUtils method procedureAllowedTypes.

public final Collection<TypeMirror> procedureAllowedTypes() {
    PrimitiveType bool = primitive(TypeKind.BOOLEAN);
    PrimitiveType longType = primitive(TypeKind.LONG);
    PrimitiveType doubleType = primitive(TypeKind.DOUBLE);
    return asList(bool, boxed(bool), longType, boxed(longType), doubleType, boxed(doubleType), typeMirror(String.class), typeMirror(Number.class), typeMirror(Object.class), typeMirror(Map.class), typeMirror(List.class), typeMirror(Node.class), typeMirror(Relationship.class), typeMirror(Path.class));
}
Also used : Path(org.neo4j.graphdb.Path) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) PrimitiveType(javax.lang.model.type.PrimitiveType) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map)

Aggregations

PrimitiveType (javax.lang.model.type.PrimitiveType)23 TypeMirror (javax.lang.model.type.TypeMirror)11 TypeElement (javax.lang.model.element.TypeElement)9 DeclaredType (javax.lang.model.type.DeclaredType)6 ArrayType (javax.lang.model.type.ArrayType)5 Test (org.junit.Test)5 WildcardType (javax.lang.model.type.WildcardType)4 Types (javax.lang.model.util.Types)4 Map (java.util.Map)3 TypeKind (javax.lang.model.type.TypeKind)3 Set (java.util.Set)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 Elements (javax.lang.model.util.Elements)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 CastExpression (com.google.devtools.j2objc.ast.CastExpression)1 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)1 Expression (com.google.devtools.j2objc.ast.Expression)1 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)1 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)1