Search in sources :

Example 21 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project qi4j-sdk by Qi4j.

the class ClassesTest method givenGenericTypeWithWildCardWhenGetRawClassThenCorrectTypeIsReturned.

@Test
public void givenGenericTypeWithWildCardWhenGetRawClassThenCorrectTypeIsReturned() throws NoSuchMethodException {
    Type returnType = Generics.class.getMethod("wildcard").getGenericReturnType();
    Type wildcardType = ((ParameterizedType) returnType).getActualTypeArguments()[0];
    assertThat("Return type is A", Classes.RAW_CLASS.map(wildcardType), equalTo((Class) A.class));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Test(org.junit.Test)

Example 22 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project qi4j-sdk by Qi4j.

the class InjectedFieldsModel method addModel.

private void addModel(Class fragmentClass, Field field, Annotation injectionAnnotation) {
    Type genericType = field.getGenericType();
    if (genericType instanceof ParameterizedType) {
        genericType = new ParameterizedTypeInstance(((ParameterizedType) genericType).getActualTypeArguments(), ((ParameterizedType) genericType).getRawType(), ((ParameterizedType) genericType).getOwnerType());
        for (int i = 0; i < ((ParameterizedType) genericType).getActualTypeArguments().length; i++) {
            Type type = ((ParameterizedType) genericType).getActualTypeArguments()[i];
            if (type instanceof TypeVariable) {
                type = Classes.resolveTypeVariable((TypeVariable) type, field.getDeclaringClass(), fragmentClass);
                ((ParameterizedType) genericType).getActualTypeArguments()[i] = type;
            }
        }
    }
    boolean optional = DependencyModel.isOptional(injectionAnnotation, field.getAnnotations());
    DependencyModel dependencyModel = new DependencyModel(injectionAnnotation, genericType, fragmentClass, optional, field.getAnnotations());
    InjectedFieldModel injectedFieldModel = new InjectedFieldModel(field, dependencyModel);
    this.fields.add(injectedFieldModel);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable)

Example 23 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project qi4j-sdk by Qi4j.

the class ServiceInjectionProviderFactory method newInjectionProvider.

@Override
@SuppressWarnings("unchecked")
public InjectionProvider newInjectionProvider(Resolution resolution, DependencyModel dependencyModel) throws InvalidInjectionException {
    // TODO This could be changed to allow multiple @Qualifier annotations
    Annotation qualifierAnnotation = first(filter(Specifications.translate(Annotations.type(), hasAnnotation(Qualifier.class)), iterable(dependencyModel.annotations())));
    Specification<ServiceReference<?>> serviceQualifier = null;
    if (qualifierAnnotation != null) {
        Qualifier qualifier = qualifierAnnotation.annotationType().getAnnotation(Qualifier.class);
        try {
            serviceQualifier = qualifier.value().newInstance().qualifier(qualifierAnnotation);
        } catch (Exception e) {
            throw new InvalidInjectionException("Could not instantiate qualifier serviceQualifier", e);
        }
    }
    if (dependencyModel.rawInjectionType().equals(Iterable.class)) {
        Type iterableType = ((ParameterizedType) dependencyModel.injectionType()).getActualTypeArguments()[0];
        if (Classes.RAW_CLASS.map(iterableType).equals(ServiceReference.class)) {
            // @Service Iterable<ServiceReference<MyService<Foo>> serviceRefs
            Type serviceType = ((ParameterizedType) iterableType).getActualTypeArguments()[0];
            return new IterableServiceReferenceProvider(serviceType, serviceQualifier);
        } else {
            // @Service Iterable<MyService<Foo>> services
            return new IterableServiceProvider(iterableType, serviceQualifier);
        }
    } else if (dependencyModel.rawInjectionType().equals(ServiceReference.class)) {
        // @Service ServiceReference<MyService<Foo>> serviceRef
        Type referencedType = ((ParameterizedType) dependencyModel.injectionType()).getActualTypeArguments()[0];
        return new ServiceReferenceProvider(referencedType, serviceQualifier);
    } else {
        // @Service MyService<Foo> service
        return new ServiceProvider(dependencyModel.injectionType(), serviceQualifier);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) Qualifier(org.qi4j.api.service.qualifier.Qualifier) Annotations.hasAnnotation(org.qi4j.api.util.Annotations.hasAnnotation) Annotation(java.lang.annotation.Annotation) InvalidInjectionException(org.qi4j.bootstrap.InvalidInjectionException) NoSuchServiceException(org.qi4j.api.service.NoSuchServiceException) ServiceReference(org.qi4j.api.service.ServiceReference)

Example 24 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project qi4j-sdk by Qi4j.

the class ValueTypeFactory method newValueType.

@SuppressWarnings({ "raw", "unchecked" })
public ValueType newValueType(Type type, Class declaringClass, Class compositeType, LayerModel layer, ModuleModel module) {
    ValueType valueType = null;
    if (CollectionType.isCollection(type)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type collectionType = pt.getActualTypeArguments()[0];
            if (collectionType instanceof TypeVariable) {
                TypeVariable collectionTypeVariable = (TypeVariable) collectionType;
                collectionType = Classes.resolveTypeVariable(collectionTypeVariable, declaringClass, compositeType);
            }
            ValueType collectedType = newValueType(collectionType, declaringClass, compositeType, layer, module);
            valueType = new CollectionType(Classes.RAW_CLASS.map(type), collectedType);
        } else {
            valueType = new CollectionType(Classes.RAW_CLASS.map(type), newValueType(Object.class, declaringClass, compositeType, layer, module));
        }
    } else if (MapType.isMap(type)) {
        if (type instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) type;
            Type keyType = pt.getActualTypeArguments()[0];
            if (keyType instanceof TypeVariable) {
                TypeVariable keyTypeVariable = (TypeVariable) keyType;
                keyType = Classes.resolveTypeVariable(keyTypeVariable, declaringClass, compositeType);
            }
            ValueType keyedType = newValueType(keyType, declaringClass, compositeType, layer, module);
            Type valType = pt.getActualTypeArguments()[1];
            if (valType instanceof TypeVariable) {
                TypeVariable valueTypeVariable = (TypeVariable) valType;
                valType = Classes.resolveTypeVariable(valueTypeVariable, declaringClass, compositeType);
            }
            ValueType valuedType = newValueType(valType, declaringClass, compositeType, layer, module);
            valueType = new MapType(Classes.RAW_CLASS.map(type), keyedType, valuedType);
        } else {
            valueType = new MapType(Classes.RAW_CLASS.map(type), newValueType(Object.class, declaringClass, compositeType, layer, module), newValueType(Object.class, declaringClass, compositeType, layer, module));
        }
    } else if (ValueCompositeType.isValueComposite(type)) {
        // Find ValueModel in module/layer/used layers
        ValueModel model = new ValueFinder(layer, module, Classes.RAW_CLASS.map(type)).getFoundModel();
        if (model == null) {
            if (type.equals(ValueComposite.class)) {
                // Create default model
                MixinsModel mixinsModel = new MixinsModel();
                Iterable valueComposite = (Iterable) Iterables.iterable(ValueComposite.class);
                ValueStateModel valueStateModel = new ValueStateModel(new PropertiesModel(), new AssociationsModel(), new ManyAssociationsModel(), new NamedAssociationsModel());
                model = new ValueModel(valueComposite, Visibility.application, new MetaInfo(), mixinsModel, valueStateModel, new CompositeMethodsModel(mixinsModel));
            } else {
                throw new InvalidApplicationException("[" + module.name() + "] Could not find ValueComposite of type " + type);
            }
        }
        return model.valueType();
    } else if (EnumType.isEnum(type)) {
        valueType = new EnumType(Classes.RAW_CLASS.map(type));
    } else {
        valueType = new ValueType(Classes.RAW_CLASS.map(type));
    }
    return valueType;
}
Also used : MixinsModel(org.qi4j.runtime.composite.MixinsModel) ValueModel(org.qi4j.runtime.value.ValueModel) ValueType(org.qi4j.api.type.ValueType) NamedAssociationsModel(org.qi4j.runtime.association.NamedAssociationsModel) MetaInfo(org.qi4j.api.common.MetaInfo) ValueComposite(org.qi4j.api.value.ValueComposite) MapType(org.qi4j.api.type.MapType) ParameterizedType(java.lang.reflect.ParameterizedType) ManyAssociationsModel(org.qi4j.runtime.association.ManyAssociationsModel) ValueCompositeType(org.qi4j.api.type.ValueCompositeType) EnumType(org.qi4j.api.type.EnumType) MapType(org.qi4j.api.type.MapType) CollectionType(org.qi4j.api.type.CollectionType) ValueType(org.qi4j.api.type.ValueType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) CompositeMethodsModel(org.qi4j.runtime.composite.CompositeMethodsModel) TypeVariable(java.lang.reflect.TypeVariable) EnumType(org.qi4j.api.type.EnumType) CollectionType(org.qi4j.api.type.CollectionType) InvalidApplicationException(org.qi4j.api.common.InvalidApplicationException) ValueStateModel(org.qi4j.runtime.value.ValueStateModel) PropertiesModel(org.qi4j.runtime.property.PropertiesModel) ManyAssociationsModel(org.qi4j.runtime.association.ManyAssociationsModel) AssociationsModel(org.qi4j.runtime.association.AssociationsModel) NamedAssociationsModel(org.qi4j.runtime.association.NamedAssociationsModel)

Example 25 with ParameterizedType

use of java.lang.reflect.ParameterizedType in project qi4j-sdk by Qi4j.

the class AbstractSQLStartup method appendColumnDefinitionsForProperty.

private void appendColumnDefinitionsForProperty(TableElementListBuilder builder, QNameInfo qNameInfo) {
    Type finalType = qNameInfo.getFinalType();
    if (finalType instanceof ParameterizedType) {
        finalType = ((ParameterizedType) finalType).getRawType();
    }
    Class<?> finalClass = (Class<?>) finalType;
    SQLDataType sqlType = null;
    String valueRefTableName = null;
    String valueRefTablePKColumnName = null;
    if (qNameInfo.isFinalTypePrimitive()) {
        if (this._customizableTypes.keySet().contains(finalClass) && qNameInfo.getPropertyDescriptor().accessor().isAnnotationPresent(SQLTypeInfo.class)) {
            sqlType = this._customizableTypes.get(finalClass).customizeType(finalClass, qNameInfo.getPropertyDescriptor().accessor().getAnnotation(SQLTypeInfo.class));
        } else if (Enum.class.isAssignableFrom(finalClass)) {
            // Enum - reference the lookup table
            sqlType = this._primitiveTypes.get(Integer.class);
            valueRefTableName = ENUM_LOOKUP_TABLE_NAME;
            valueRefTablePKColumnName = ENUM_LOOKUP_TABLE_PK_COLUMN_NAME;
        } else {
            // Primitive type, default sqlType
            sqlType = this._primitiveTypes.get(finalClass);
        }
        if (sqlType == null) {
            throw new InternalError("Could not find sql type for java type [" + finalType + "]");
        }
    } else {
        // Value composite - just need used class
        sqlType = this._primitiveTypes.get(Integer.class);
        valueRefTableName = USED_CLASSES_TABLE_NAME;
        valueRefTablePKColumnName = USED_CLASSES_TABLE_PK_COLUMN_NAME;
    }
    SQLVendor vendor = this._vendor;
    DefinitionFactory d = vendor.getDefinitionFactory();
    TableReferenceFactory t = vendor.getTableReferenceFactory();
    builder.addTableElement(d.createColumnDefinition(QNAME_TABLE_VALUE_COLUMN_NAME, sqlType, qNameInfo.getCollectionDepth() > 0)).addTableElement(d.createTableConstraintDefinition(d.createUniqueConstraintBuilder().setUniqueness(UniqueSpecification.PRIMARY_KEY).addColumns(ALL_QNAMES_TABLE_PK_COLUMN_NAME, ENTITY_TABLE_PK_COLUMN_NAME).createExpression()));
    if (valueRefTableName != null && valueRefTablePKColumnName != null) {
        builder.addTableElement(d.createTableConstraintDefinition(d.createForeignKeyConstraintBuilder().addSourceColumns(QNAME_TABLE_VALUE_COLUMN_NAME).setTargetTableName(t.tableName(this._state.schemaName().get(), valueRefTableName)).addTargetColumns(valueRefTablePKColumnName).setOnUpdate(ReferentialAction.CASCADE).setOnDelete(ReferentialAction.RESTRICT).createExpression(), ConstraintCharacteristics.NOT_DEFERRABLE));
    }
}
Also used : SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) SQLTypeInfo(org.qi4j.index.sql.support.api.SQLTypeInfo) ParameterizedType(java.lang.reflect.ParameterizedType) BigInteger(java.math.BigInteger) TableReferenceFactory(org.sql.generation.api.grammar.factories.TableReferenceFactory) QNameType(org.qi4j.index.sql.support.common.QNameInfo.QNameType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ObjectType(org.sql.generation.api.grammar.manipulation.ObjectType) SQLDataType(org.sql.generation.api.grammar.common.datatypes.SQLDataType) SQLVendor(org.sql.generation.api.vendor.SQLVendor) DefinitionFactory(org.sql.generation.api.grammar.factories.DefinitionFactory)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)605 Type (java.lang.reflect.Type)456 GenericArrayType (java.lang.reflect.GenericArrayType)203 WildcardType (java.lang.reflect.WildcardType)162 TypeVariable (java.lang.reflect.TypeVariable)126 ArrayList (java.util.ArrayList)79 Method (java.lang.reflect.Method)60 Test (org.junit.Test)55 List (java.util.List)45 Field (java.lang.reflect.Field)43 Map (java.util.Map)39 HashMap (java.util.HashMap)32 Collection (java.util.Collection)18 Annotation (java.lang.annotation.Annotation)13 ImmutableList (com.google.common.collect.ImmutableList)12 TypeLiteral (com.google.inject.TypeLiteral)12 HashSet (java.util.HashSet)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 GenericType (javax.ws.rs.core.GenericType)10 MediaType (javax.ws.rs.core.MediaType)10