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));
}
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);
}
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);
}
}
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;
}
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));
}
}
Aggregations