List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:org.broadleafcommerce.openadmin.server.dao.provider.metadata.AdvancedCollectionFieldMetadataProvider.java
@Override public FieldProviderResponse addMetadataFromFieldType( AddMetadataFromFieldTypeRequest addMetadataFromFieldTypeRequest, Map<String, FieldMetadata> metadata) { if (!canHandleFieldForTypeMetadata(addMetadataFromFieldTypeRequest, metadata)) { return FieldProviderResponse.NOT_HANDLED; }//from ww w .ja v a 2 s . c o m CollectionMetadata fieldMetadata = (CollectionMetadata) addMetadataFromFieldTypeRequest .getPresentationAttribute(); if (StringUtils.isEmpty(fieldMetadata.getCollectionCeilingEntity())) { ParameterizedType listType = (ParameterizedType) addMetadataFromFieldTypeRequest.getRequestedField() .getGenericType(); Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0]; fieldMetadata.setCollectionCeilingEntity(listClass.getName()); } if (addMetadataFromFieldTypeRequest.getTargetClass() != null) { if (StringUtils.isEmpty(fieldMetadata.getInheritedFromType())) { fieldMetadata.setInheritedFromType(addMetadataFromFieldTypeRequest.getTargetClass().getName()); } if (ArrayUtils.isEmpty(fieldMetadata.getAvailableToTypes())) { fieldMetadata.setAvailableToTypes( new String[] { addMetadataFromFieldTypeRequest.getTargetClass().getName() }); } } // Handle scenarios where the collection metadata is also a foreign key. The {@link BasicFieldMetadata} that has all // of the information about the foreign key will travel along with the built {@link BasicCollectionMetadata} under // the {@link FieldMetadata#getAdditionalMetadata()} field. This is then pulled out within // {@link BasicPersistenceModule#filterOutCollectionMetadata} if (addMetadataFromFieldTypeRequest.getForeignField() != null && addMetadataFromFieldTypeRequest.isPropertyForeignKey()) { FieldInfo info = buildFieldInfo(addMetadataFromFieldTypeRequest.getRequestedField()); BasicFieldMetadata basicMetadata = new BasicFieldMetadata(); basicMetadata.setName(info.getName()); basicMetadata.setExcluded(false); // Don't show this anywhere on the form and ensure it's explicitly not required basicMetadata.setVisibility(VisibilityEnum.HIDDEN_ALL); basicMetadata.setRequired(false); setClassOwnership(addMetadataFromFieldTypeRequest.getReturnedClass(), addMetadataFromFieldTypeRequest.getTargetClass(), metadata, info); Map<String, FieldMetadata> fakedMd = new HashMap<String, FieldMetadata>(); fakedMd.put(addMetadataFromFieldTypeRequest.getRequestedField().getName(), basicMetadata); // Fake out a request and some metadata to pass along as additional metadata within this metadata AddMetadataFromFieldTypeRequest fakedRequest = new AddMetadataFromFieldTypeRequest( addMetadataFromFieldTypeRequest.getRequestedField(), addMetadataFromFieldTypeRequest.getTargetClass(), addMetadataFromFieldTypeRequest.getForeignField(), addMetadataFromFieldTypeRequest.getAdditionalForeignFields(), addMetadataFromFieldTypeRequest.getMergedPropertyType(), addMetadataFromFieldTypeRequest.getComponentProperties(), addMetadataFromFieldTypeRequest.getIdProperty(), addMetadataFromFieldTypeRequest.getPrefix(), addMetadataFromFieldTypeRequest.getRequestedPropertyName(), addMetadataFromFieldTypeRequest.getType(), addMetadataFromFieldTypeRequest.isPropertyForeignKey(), addMetadataFromFieldTypeRequest.getAdditionalForeignKeyIndexPosition(), fakedMd, basicMetadata, addMetadataFromFieldTypeRequest.getExplicitType(), addMetadataFromFieldTypeRequest.getReturnedClass(), addMetadataFromFieldTypeRequest.getDynamicEntityDao()); defaultMetadataProvider.addMetadataFromFieldType(fakedRequest, fakedMd); fieldMetadata.getAdditionalMetadata().put(FOREIGN_KEY_ADDITIONAL_METADATA_KEY, basicMetadata); } metadata.put(addMetadataFromFieldTypeRequest.getRequestedPropertyName(), fieldMetadata); return FieldProviderResponse.HANDLED; }
From source file:com.mycomm.dao.mydao.base.MyDaoSupport.java
public MyDaoSupport() { // ??T/*from www . ja v a 2 s .c om*/ ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); // ??new this.entityClass = (Class<T>) pt.getActualTypeArguments()[0]; // ?? System.out.println("clazz ---> " + entityClass); }
From source file:com.dangdang.ddframe.job.internal.job.dataflow.AbstractDataFlowElasticJob.java
private DataFlowType getDataFlowType() { Class<?> target = getClass(); while (true) { if (!(target.getGenericSuperclass() instanceof ParameterizedType)) { target = target.getSuperclass(); continue; }//from w w w.ja v a 2s . co m ParameterizedType parameterizedType = (ParameterizedType) target.getGenericSuperclass(); if (2 != parameterizedType.getActualTypeArguments().length) { target = target.getSuperclass(); continue; } Type type = parameterizedType.getActualTypeArguments()[1]; if (JobExecutionMultipleShardingContext.class == type) { return DataFlowType.THROUGHPUT; } else if (JobExecutionSingleShardingContext.class == type) { return DataFlowType.SEQUENCE; } else { throw new UnsupportedOperationException(String.format("Cannot support %s", type)); } } }
From source file:info.magnolia.jcr.node2bean.impl.TypeMappingImpl.java
private List<Class<?>> inferGenericTypes(Method method) { List<Class<?>> inferredTypes = new ArrayList<Class<?>>(); Type[] parameterTypes = method.getGenericParameterTypes(); for (Type parameterType : parameterTypes) { if (parameterType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) parameterType; for (Type t : type.getActualTypeArguments()) { if (t instanceof ParameterizedType) { // this the case when parameterized type looks like this: Collection<List<String>> // we care only for raw type List inferredTypes.add((Class<?>) ((ParameterizedType) t).getRawType()); } else { inferredTypes.add((Class<?>) t); }//from w ww . j a va 2 s . co m } } } return inferredTypes; }
From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java
/** * W//from www . j a v a2 s .co m */ public DaoImpl() { ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass(); this.entityClass = (Class<T>) type.getActualTypeArguments()[0]; }
From source file:org.apache.shindig.protocol.conversion.BeanFilter.java
/** * Provide list of all fields for a specific bean * @param bean the class to list fields for * @param depth maximum depth of recursive (mainly for infinite loop protection) *///from www .ja va 2 s . c o m public List<String> getBeanFields(Class<?> bean, int depth) { List<String> fields = Lists.newLinkedList(); for (Method method : bean.getMethods()) { if (method.getName().startsWith("get")) { String fieldName = method.getName().substring(3); fields.add(fieldName); Class<?> returnType = method.getReturnType(); // Get the type of list: if (List.class.isAssignableFrom(returnType)) { ParameterizedType aType = (ParameterizedType) method.getGenericReturnType(); Type[] parameterArgTypes = aType.getActualTypeArguments(); if (parameterArgTypes.length > 0) { returnType = (Class<?>) parameterArgTypes[0]; } else { returnType = null; } } // Get the type of map value if (Map.class.isAssignableFrom(returnType)) { ParameterizedType aType = (ParameterizedType) method.getGenericReturnType(); Type[] parameterArgTypes = aType.getActualTypeArguments(); if (parameterArgTypes.length > 1) { returnType = (Class<?>) parameterArgTypes[1]; } else { returnType = null; } } // Get member fields and append fields using dot notation if (depth > 1 && returnType != null && !returnType.isPrimitive() && !returnType.isEnum() && !BeanDelegator.PRIMITIVE_TYPE_CLASSES.contains(returnType)) { List<String> subFields = getBeanFields(returnType, depth - 1); for (String field : subFields) { fields.add(fieldName + DELIMITER + field); } } } } return fields; }
From source file:com.nesscomputing.jersey.exceptions.GuiceProvisionExceptionMapper.java
private Class<?> getType(Class<?> c) { Class<?> _c = c;// www . j ava 2s . c o m while (_c != Object.class) { Type[] ts = _c.getGenericInterfaces(); for (Type t : ts) { if (t instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) t; if (pt.getRawType() == ExceptionMapper.class) { return getResolvedType(pt.getActualTypeArguments()[0], c, _c); } } } _c = _c.getSuperclass(); } // This statement will never be reached return null; }
From source file:org.blocks4j.reconf.client.constructors.MapConstructor.java
public Object construct(MethodData data) throws Throwable { Class<?> returnClass;/*from ww w . ja v a2 s . c om*/ Type keyType = null; Type valueType = null; if (data.getReturnType() instanceof ParameterizedType) { ParameterizedType parameterized = (ParameterizedType) data.getReturnType(); returnClass = (Class<?>) parameterized.getRawType(); if (parameterized.getActualTypeArguments().length == 1) { Type first = parameterized.getActualTypeArguments()[0]; if (returnClass.getGenericSuperclass() != null && returnClass.getGenericSuperclass() instanceof ParameterizedType) { parameterized = (ParameterizedType) returnClass.getGenericSuperclass(); if (parameterized.getActualTypeArguments().length != 2) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } if (parameterized.getActualTypeArguments()[0] instanceof TypeVariable) { keyType = first; valueType = parameterized.getActualTypeArguments()[1]; } else if (parameterized.getActualTypeArguments()[1] instanceof TypeVariable) { valueType = first; keyType = parameterized.getActualTypeArguments()[0]; } else { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } } } else { keyType = parameterized.getActualTypeArguments()[0]; valueType = parameterized.getActualTypeArguments()[1]; } } else if (data.getReturnType() instanceof Class) { returnClass = (Class<?>) data.getReturnType(); if (returnClass.getGenericSuperclass() != null && returnClass.getGenericSuperclass() instanceof ParameterizedType) { ParameterizedType parameterized = (ParameterizedType) returnClass.getGenericSuperclass(); if (parameterized.getActualTypeArguments().length != 2) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } keyType = parameterized.getActualTypeArguments()[0]; valueType = parameterized.getActualTypeArguments()[1]; } else { keyType = Object.class; valueType = Object.class; } } else { throw new IllegalArgumentException(msg.format("error.return", data.getMethod())); } if (returnClass.isInterface()) { returnClass = getDefaultImplementation(data, returnClass); } Constructor<?> constructor = returnClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY); Map<Object, Object> mapInstance = (Map<Object, Object>) constructor .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY); if (null == data.getValue() || StringUtils.isEmpty(data.getValue())) { return mapInstance; } if ((!(keyType instanceof Class)) || (!StringUtils.startsWith(data.getValue(), "[") || !StringUtils.endsWith(data.getValue(), "]"))) { throw new IllegalArgumentException(msg.format("error.build", data.getValue(), data.getMethod())); } StringParser parser = new StringParser(data); for (Entry<String, String> each : parser.getTokensAsMap().entrySet()) { Object value = ObjectConstructorFactory.get(valueType) .construct(new MethodData(data.getMethod(), valueType, each.getValue())); mapInstance.put(ObjectConstructorFactory.get(keyType) .construct(new MethodData(data.getMethod(), keyType, each.getKey())), value); } return mapInstance; }
From source file:it.unibas.spicy.persistence.object.operators.AnalyzeFields.java
private void generateCollectionOfReferences(ClassTree classTree, IClassNode currentNode, Field field) throws IllegalModelException { if (logger.isDebugEnabled()) logger.debug("Found a collection:" + field); ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType(); // list the actual type arguments Type[] targs = parameterizedType.getActualTypeArguments(); if (targs.length == 0 || targs.length > 1) { throw new IllegalModelException("Collection of non generic type: " + field); }/*from w ww . j av a 2s.c o m*/ Class typeArgumentClass = (Class) targs[0]; String typeArgumentName = typeArgumentClass.getName(); if (logger.isDebugEnabled()) logger.debug("Generic type:" + typeArgumentName); IClassNode argumentTypeNode = classTree.get(typeArgumentName); if (argumentTypeNode == null) { throw new IllegalModelException("Illegal collection of references: " + field); } ReferenceProperty referenceProperty = new ReferenceProperty(field.getName(), field, currentNode, argumentTypeNode, Constants.MANY); currentNode.addReferenceProperty(referenceProperty); classTree.getReferences().add(referenceProperty); }
From source file:io.servicecomb.swagger.invocation.converter.ConverterMgr.java
protected Converter findArrayToCollection(Type src, Type target) { if (src.getClass().equals(Class.class) && ParameterizedType.class.isAssignableFrom(target.getClass())) { Class<?> srcCls = (Class<?>) src; ParameterizedType targetType = (ParameterizedType) target; Class<?> targetCls = (Class<?>) targetType.getRawType(); if (srcCls.isArray() && srcCls.getComponentType().equals(targetType.getActualTypeArguments()[0])) { if (List.class.isAssignableFrom(targetCls)) { return arrayToList; }/*from ww w . j av a2 s. c o m*/ if (Set.class.isAssignableFrom(targetCls)) { return arrayToSet; } } } return null; }