List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:org.romaframework.aspect.core.CoreAspect.java
public void configField(SchemaField iField) { if (iField.getFeature(CoreFieldFeatures.EXPAND)) { expandField(iField, iField.getEntity()); } else if (iField.isSettedFeature(CoreFieldFeatures.EXPAND) && !iField.getFeature(CoreFieldFeatures.EXPAND)) { unexpandField(iField);/*from w w w. j a v a 2 s . c o m*/ } if (iField instanceof SchemaFieldReflection) { SchemaFieldReflection ref = (SchemaFieldReflection) iField; SchemaClass[] embeddedTypeGenerics = null; Type fieldType = null; if (ref.getGetterMethod() != null) { if ((fieldType = ref.getGetterMethod().getGenericReturnType()) == null) { fieldType = ref.getGetterMethod().getReturnType(); } } if (fieldType == null && ref.getField() != null) { if ((fieldType = ref.getField().getGenericType()) == null) { fieldType = ref.getField().getType(); } } if (fieldType != null && fieldType instanceof ParameterizedType) { ParameterizedType ownerType = SchemaHelper .resolveParameterizedType((Type) ref.getEntity().getSchemaClass().getLanguageType()); ParameterizedType pt = (ParameterizedType) fieldType; if (pt.getActualTypeArguments() != null) { embeddedTypeGenerics = new SchemaClass[pt.getActualTypeArguments().length]; int i = 0; for (Type argType : pt.getActualTypeArguments()) embeddedTypeGenerics[i++] = Roma.schema() .getSchemaClassIfExist(SchemaHelper.resolveClassFromType(argType, ownerType)); } ref.setEmbeddedTypeGenerics(embeddedTypeGenerics); if (embeddedTypeGenerics != null && embeddedTypeGenerics.length > 0) ref.setEmbeddedType(embeddedTypeGenerics[0]); } else if (fieldType instanceof Class<?>) { Class<?> cls = (Class<?>) fieldType; if (cls.isArray()) ref.setEmbeddedLanguageType(cls.getComponentType()); } } }
From source file:cn.bzvs.excel.export.base.ExportBase.java
/** * ??/*from w w w . j a va 2 s . c o m*/ * * @param exclusions * @param targetId ID * @param fields * @throws Exception */ public void getAllExcelField(String[] exclusions, String targetId, Field[] fields, List<ExcelExportEntity> excelParams, Class<?> pojoClass, List<Method> getMethods) throws Exception { List<String> exclusionsList = exclusions != null ? Arrays.asList(exclusions) : null; ExcelExportEntity excelEntity; // ??filed for (int i = 0; i < fields.length; i++) { Field field = fields[i]; // ?collection,?java,? if (PoiPublicUtil.isNotUserExcelUserThis(exclusionsList, field, targetId)) { continue; } // Excel ??? if (field.getAnnotation(Excel.class) != null) { excelParams.add(createExcelExportEntity(field, targetId, pojoClass, getMethods)); } else if (PoiPublicUtil.isCollection(field.getType())) { ExcelCollection excel = field.getAnnotation(ExcelCollection.class); ParameterizedType pt = (ParameterizedType) field.getGenericType(); Class<?> clz = (Class<?>) pt.getActualTypeArguments()[0]; List<ExcelExportEntity> list = new ArrayList<ExcelExportEntity>(); getAllExcelField(exclusions, StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, PoiPublicUtil.getClassFields(clz), list, clz, null); excelEntity = new ExcelExportEntity(); excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null)); excelEntity.setOrderNum( Integer.valueOf(PoiPublicUtil.getValueByTargetId(excel.orderNum(), targetId, "0"))); excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); excelEntity.setList(list); excelParams.add(excelEntity); } else { List<Method> newMethods = new ArrayList<Method>(); if (getMethods != null) { newMethods.addAll(getMethods); } newMethods.add(PoiReflectorUtil.fromCache(pojoClass).getGetMethod(field.getName())); ExcelEntity excel = field.getAnnotation(ExcelEntity.class); getAllExcelField(exclusions, StringUtils.isNotEmpty(excel.id()) ? excel.id() : targetId, PoiPublicUtil.getClassFields(field.getType()), excelParams, field.getType(), newMethods); } } }
From source file:com.msopentech.odatajclient.proxy.api.impl.AbstractInvocationHandler.java
protected Object functionImport(final Operation annotation, final Method method, final Object[] args, final URI target, final com.msopentech.odatajclient.engine.metadata.edm.v3.FunctionImport funcImp) throws InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { // 1. invoke params (if present) final Map<String, ODataValue> parameters = new HashMap<String, ODataValue>(); if (!ArrayUtils.isEmpty(args)) { final Annotation[][] parAnnots = method.getParameterAnnotations(); final Class<?>[] parTypes = method.getParameterTypes(); for (int i = 0; i < args.length; i++) { if (!(parAnnots[i][0] instanceof Parameter)) { throw new IllegalArgumentException("Paramter " + i + " is not annotated as @Param"); }//from w ww . j av a2s . c o m final Parameter parAnnot = (Parameter) parAnnots[i][0]; if (!parAnnot.nullable() && args[i] == null) { throw new IllegalArgumentException( "Parameter " + parAnnot.name() + " is not nullable but a null value was provided"); } final ODataValue paramValue = args[i] == null ? null : EngineUtils.getODataValue(client, containerHandler.getFactory().getMetadata(), new EdmV3Type(containerHandler.getFactory().getMetadata(), parAnnot.type()), args[i]); parameters.put(parAnnot.name(), paramValue); } } // 2. IMPORTANT: flush any pending change *before* invoke if this operation is side effecting if (annotation.isSideEffecting()) { new Container(client, containerHandler.getFactory()).flush(); } // 3. invoke final ODataInvokeResult result = client.getInvokeRequestFactory() .getInvokeRequest(target, containerHandler.getFactory().getMetadata(), funcImp, parameters) .execute().getBody(); // 3. process invoke result if (StringUtils.isBlank(annotation.returnType())) { return ClassUtils.returnVoid(); } final EdmType edmType = new EdmV3Type(containerHandler.getFactory().getMetadata(), annotation.returnType()); if (edmType.isEnumType()) { throw new UnsupportedOperationException("Usupported enum type " + edmType.getTypeExpression()); } if (edmType.isSimpleType() || edmType.isComplexType()) { return EngineUtils.getValueFromProperty(containerHandler.getFactory().getMetadata(), (ODataProperty) result, method.getGenericReturnType()); } if (edmType.isEntityType()) { if (edmType.isCollection()) { final ParameterizedType collType = (ParameterizedType) method.getReturnType() .getGenericInterfaces()[0]; final Class<?> collItemType = (Class<?>) collType.getActualTypeArguments()[0]; return getEntityCollection(collItemType, method.getReturnType(), null, (ODataEntitySet) result, target, false); } else { return getEntityProxy((ODataEntity) result, null, null, method.getReturnType(), false); } } throw new IllegalArgumentException("Could not process the functionImport information"); }
From source file:io.mandrel.common.schema.SchemaTest.java
public void inspect(int level, Type clazz, String name) { if (level > 6) return;// w ww . j ava2 s . co m if (clazz instanceof Class && clazz.equals(LinkFilter.class)) return; if (clazz instanceof Class && !((Class<?>) clazz).isEnum() && ((Class<?>) clazz).getPackage() != null && ((Class<?>) clazz).getPackage().getName().startsWith("io.mandrel")) { int newLevel = level + 1; List<Field> fields = new ArrayList<Field>(); Class<?> i = ((Class<?>) clazz); while (i != null && i != Object.class) { fields.addAll(Arrays.asList(i.getDeclaredFields())); i = i.getSuperclass(); } for (Field field : fields) { Class<?> fieldType = field.getType(); String text; if (!field.isAnnotationPresent(JsonIgnore.class) && !Modifier.isStatic(field.getModifiers())) { if (List.class.equals(fieldType) || Map.class.equals(fieldType)) { Type type = field.getGenericType(); if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; for (Type paramType : pType.getActualTypeArguments()) { if (paramType instanceof Class && NamedDefinition.class.isAssignableFrom((Class) paramType)) { text = field.getName() + "(container of " + paramType + " oneOf)"; System.err.println( StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); Class<? extends NamedDefinition> nd = (Class<? extends NamedDefinition>) field .getType(); Map<String, ? extends NamedDefinition> map = NamedProviders.get(nd); map.forEach((k, v) -> { String text2 = k; System.err.println(StringUtils.leftPad(text2, text2.length() + newLevel * 5, "\t- ")); inspect(newLevel, v.getClass(), field.getName()); }); } else if (paramType instanceof ParameterizedType && NamedDefinition.class .isAssignableFrom((Class) ((ParameterizedType) paramType).getRawType())) { text = field.getName() + "(container of " + paramType + " oneOf2)"; System.err.println( StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); Class<? extends NamedDefinition> nd = (Class<? extends NamedDefinition>) ((ParameterizedType) paramType) .getRawType(); Map<String, ? extends NamedDefinition> map = NamedProviders.get(nd); map.forEach((k, v) -> { String text2 = k; System.err.println(StringUtils.leftPad(text2, text2.length() + newLevel * 5, "\t- ")); inspect(newLevel, v.getClass(), field.getName()); }); } else if (paramType instanceof WildcardType) { for (Type wildType : ((WildcardType) paramType).getUpperBounds()) { if (wildType instanceof Class && NamedDefinition.class.isAssignableFrom((Class) wildType)) { text = field.getName() + "(container of " + wildType + " oneOf)"; System.err.println(StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); Class<? extends NamedDefinition> nd = (Class<? extends NamedDefinition>) field .getType(); Map<String, ? extends NamedDefinition> map = NamedProviders.get(nd); map.forEach((k, v) -> { String text2 = k; System.err.println(StringUtils.leftPad(text2, text2.length() + newLevel * 5, "\t- ")); inspect(newLevel, v.getClass(), field.getName()); }); } else if (wildType instanceof ParameterizedType && NamedDefinition.class.isAssignableFrom( (Class) ((ParameterizedType) wildType).getRawType())) { text = field.getName() + "(container of " + wildType + " oneOf2)"; System.err.println(StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); Class<? extends NamedDefinition> nd = (Class<? extends NamedDefinition>) ((ParameterizedType) wildType) .getRawType(); Map<String, ? extends NamedDefinition> map = NamedProviders.get(nd); map.forEach((k, v) -> { String text2 = k; System.err.println(StringUtils.leftPad(text2, text2.length() + newLevel * 5, "\t- ")); inspect(newLevel, v.getClass(), field.getName()); }); } } } else { text = field.getName() + "(container of " + paramType + ")"; System.err.println( StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); inspect(newLevel, paramType, ""); } } } } else { if (NamedDefinition.class.isAssignableFrom(field.getType())) { text = field.getName() + " oneOf"; System.err.println(StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); Class<? extends NamedDefinition> nd = (Class<? extends NamedDefinition>) field .getType(); Map<String, ? extends NamedDefinition> map = NamedProviders.get(nd); map.forEach((k, v) -> { String text2 = k; System.err.println( StringUtils.leftPad(text2, text2.length() + newLevel * 5, "\t- ")); inspect(newLevel, v.getClass(), field.getName()); }); } else { text = field.getName() + (field.getType().isPrimitive() || field.getType().equals(String.class) || field.getType().equals(LocalDateTime.class) ? " (" + field.getType().getName() + ")" : ""); System.err.println(StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); inspect(newLevel, fieldType, field.getName()); } } // JsonSubTypes subtype = // fieldType.getAnnotation(JsonSubTypes.class); // if (subtype != null) { // int subLevel = level + 2; // text = "subtypes:"; // System.err.println(StringUtils.leftPad(text, // text.length() + subLevel * 5, "\t- ")); // for (JsonSubTypes.Type type : subtype.value()) { // text = "subtype:" + type.name(); // System.err.println(StringUtils.leftPad(text, // text.length() + (subLevel + 1) * 5, "\t- ")); // inspect((subLevel + 1), type.value(), type.name()); // } // } } } JsonSubTypes subtype = ((Class<?>) clazz).getAnnotation(JsonSubTypes.class); if (subtype != null) { int subLevel = level + 1; String text = "subtypes:"; System.err.println(StringUtils.leftPad(text, text.length() + subLevel * 5, "\t- ")); for (JsonSubTypes.Type type : subtype.value()) { text = "subtype:" + type.name(); System.err.println(StringUtils.leftPad(text, text.length() + (subLevel + 1) * 5, "\t- ")); inspect((subLevel + 1), type.value(), type.name()); } } } }
From source file:com.is.rest.client.RestMethodCache.java
@SuppressWarnings("unchecked") private void parseMethod() { Type type = ResponseTypeUtil.parseResponseType(method); if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; rawContainer = parameterizedType.getRawType(); containerTarget = parameterizedType.getActualTypeArguments()[0]; } else if (!(type instanceof TypeVariable)) { targetType = type;//from w ww . jav a 2 s . c om } if (method.isAnnotationPresent(GET.class)) { requestType = RequestType.GET; path = method.getAnnotation(GET.class).value(); } else if (method.isAnnotationPresent(POST.class)) { requestType = RequestType.POST; path = method.getAnnotation(POST.class).value(); } else if (method.isAnnotationPresent(PUT.class)) { requestType = RequestType.PUT; path = method.getAnnotation(PUT.class).value(); } else if (method.isAnnotationPresent(PATCH.class)) { requestType = RequestType.PATCH; path = method.getAnnotation(PATCH.class).value(); } else if (method.isAnnotationPresent(DELETE.class)) { requestType = RequestType.DELETE; path = method.getAnnotation(DELETE.class).value(); } else if (method.isAnnotationPresent(HEAD.class)) { requestType = RequestType.HEAD; path = method.getAnnotation(HEAD.class).value(); } else { throw new IllegalStateException( "No http annotation (GET, POST, PUT, DELETE, PATCH, HEAD) found on method" + method); } }
From source file:com.wavemaker.runtime.service.ElementType.java
/** * Create an ElementType with one level of children (populated by looking at the bean properties of javaType). This * method should not be used recursively. *///from w w w . ja va2 s.co m public ElementType(String name, Class<?> javaType, boolean isList) { this(name, javaType.getName(), isList); PropertyDescriptor[] pds; pds = PropertyUtils.getPropertyDescriptors(javaType); List<ElementType> elements = new ArrayList<ElementType>(pds.length); for (PropertyDescriptor pd : pds) { if (pd.getName().equals("class")) { continue; } if (pd.getReadMethod() == null && pd.getWriteMethod() == null) { continue; } Class<?> klass; Type type; if (pd.getReadMethod() != null) { klass = pd.getReadMethod().getReturnType(); type = pd.getReadMethod().getGenericReturnType(); } else { klass = pd.getWriteMethod().getParameterTypes()[0]; type = pd.getWriteMethod().getGenericParameterTypes()[0]; } ElementType element; if (klass.isArray()) { element = new ElementType(pd.getName(), klass.getComponentType().getName(), true); } else if (Collection.class.isAssignableFrom(klass) && type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; Type aType = ptype.getActualTypeArguments()[0]; element = new ElementType(pd.getName(), ((Class<?>) aType).getName(), true); } else { element = new ElementType(pd.getName(), klass.getName()); } elements.add(element); } this.properties = elements; }
From source file:org.yukung.daguerreo.domain.repository.BasicJooqRepository.java
@SuppressWarnings("unchecked") @PostConstruct//from w ww . j a va2s. c o m private void init() { Type type = this.getClass().getGenericSuperclass(); ParameterizedType pt = (ParameterizedType) type; Class<?> tableClass = (Class<?>) pt.getActualTypeArguments()[1]; try { table = (T) tableClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); // TODO later } entityClass = (Class<E>) pt.getActualTypeArguments()[2]; mapper = dsl.configuration().recordMapperProvider().provide(table.recordType(), entityClass); }
From source file:nl.luminis.test.util.annotations.HierarchyDiscovery.java
private Type resolveTypeParameter(ParameterizedType type, Type beanType, TypeVariable<?> typeVariable) { // step1. raw type Class<?> actualType = (Class<?>) type.getRawType(); TypeVariable<?>[] typeVariables = actualType.getTypeParameters(); Type[] actualTypes = type.getActualTypeArguments(); for (int i = 0; i < typeVariables.length; i++) { if (typeVariables[i].equals(typeVariable) && !actualTypes[i].equals(typeVariable)) { return resolveType(this.type, beanType, actualTypes[i]); }//from w w w. j a v a2s .c o m } // step2. generic super class Type genericSuperType = actualType.getGenericSuperclass(); Type resolvedGenericSuperType = resolveType(genericSuperType, beanType, typeVariable); if (!(resolvedGenericSuperType instanceof TypeVariable<?>)) { return resolvedGenericSuperType; } // step3. generic interfaces if (beanType instanceof ParameterizedType) { for (Type interfaceType : ((Class<?>) ((ParameterizedType) beanType).getRawType()) .getGenericInterfaces()) { Type resolvedType = resolveType(interfaceType, interfaceType, typeVariable); if (!(resolvedType instanceof TypeVariable<?>)) { return resolvedType; } } } // don't resolve type variable return typeVariable; }
From source file:de.dfki.iui.mmds.dialogue.SiamContext.java
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override/*from www . ja v a 2 s .c om*/ public void setLocal(String name, Object value) { if (value != null && value.toString().equals("[data: null]") && has("stateID")) { // value is variable from scxml model String stateID = (String) get("stateID"); value = null; if (DialogueComponent.INSTANCE.idToScxmlState.containsKey(stateID)) { TransitionTarget tt = DialogueComponent.INSTANCE.idToScxmlState.get(stateID); State state = null; if (tt instanceof State) { state = (State) tt; } while (value == null && state != null) { // search for variable in scxml-model and set variable with // default value in context. for (Data data : state.getDatamodel().getData()) { if (data instanceof XData && ((XData) data).getId().equals(name)) { value = ((XData) data).getObject(); if (name.startsWith("_variable$")) { Object defaultValue = null; String variableName = name.split("\\$")[1]; Variable variable = (Variable) value; if (variable.getDefault() != null && !variable.getDefault().isEmpty()) { if (variable.getEmfType() != null && variable.getEmfType() instanceof EDataType) { defaultValue = variable.getEmfType().getEPackage().getEFactoryInstance() .createFromString((EDataType) variable.getEmfType(), variable.getDefault()); } else if (variable.getType() != null && !variable.getType().isEmpty()) { try { defaultValue = siamStateMachine.siamEvaluator.eval(this, variable.getDefault()); } catch (IllegalArgumentException | SecurityException | SCXMLExpressionException e) { final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); e.printStackTrace(printWriter); Logger.getLogger(this.getClass()).warn(String.format( "Cannot create default variable value \"%s\" for variable \"%s\" of type %s!\n%s", variable.getDefault(), variable.getName(), variable.getType(), result.toString())); } if (defaultValue == null) { Logger.getLogger(this.getClass()).warn(String.format( "Cannot create default variable value \"%s\" for variable \"%s\" of type %s!", variable.getDefault(), variable.getName(), variable.getType())); } } else { Logger.getLogger(this.getClass()).warn(String.format( "Cannot create default value for variable \"%s\". The type %s is not serializable.", variable.getName(), variable.getEmfType())); } } super.setLocal(variableName, defaultValue); } break; } } // go up to containing state and search for variable here if (state.eContainer() instanceof State) { state = (State) state.eContainer(); } else { state = null; } } } } else { // type check. Convert BDataType to DataType or vice versa if // necessary. Variable variable = (Variable) get("_variable$" + name); if (variable != null && value != null) { Class variableInstanceClass; if (variable.getEmfType() != null) { variableInstanceClass = variable.getEmfType().getInstanceClass(); } else if (variable.getType() != null && !variable.getType().isEmpty()) { try { variableInstanceClass = DialogueComponent.INSTANCE.projectContext.getBundleContext() .getBundle().loadClass(variable.getType()); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(String.format( "Cannot assign value to variable \"%s\" of type %s. Class \'%s\' does not exist", name, variable.getType(), variable.getType())); } } else throw new IllegalArgumentException( String.format("No type defined for variable \"%s\" in scope of state \"%s\".", variable.getName(), get("stateID"))); if (!(variableInstanceClass.isInstance(value)) && !areMatchingClassTypes(variableInstanceClass, value.getClass())) { boolean success = false; if (value instanceof BDataType) { ParameterizedType type = (ParameterizedType) value.getClass().getGenericSuperclass(); if (((Class) type.getActualTypeArguments()[0]).getName() .equals(variableInstanceClass.getName())) { if (!((BDataType) value).isResolved() && ((BDataType) value).getExpression().isEmpty()) { value = ""; } else { value = ((BDataType) value).getValue(); } success = true; } else if (((Class) type.getActualTypeArguments()[0]).getName() .equals(String.class.getName())) { if (BDataType.class.isAssignableFrom(variableInstanceClass)) { try { value = variableInstanceClass.getMethod("valueOf", String.class).invoke(null, value.toString()); success = true; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } else if (variableInstanceClass.getSuperclass() == BDataType.class) { BDataType newValue; try { newValue = (BDataType) variableInstanceClass.newInstance(); newValue.setValue(value); value = newValue; success = true; } catch (InstantiationException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassCastException e) { success = false; } } if (!success) { String message = String.format( "Cannot assign value of type %s to variable \"%s\" of type %s: Wrong type.", value.getClass().getName(), name, variableInstanceClass.getName()); Logger.getLogger(this.getClass()).error(message); throw new IllegalArgumentException(message); } } } } super.setLocal(name, value); }
From source file:org.apache.hadoop.yarn.webapp.hamlet.HamletGen.java
String getTypeName(Type type) { if (type instanceof Class<?>) { return ((Class<?>) type).getSimpleName(); }//from www . j av a 2s . c om ParameterizedType pt = (ParameterizedType) type; return ((Class<?>) pt.getRawType()).getSimpleName() + "<" + ((Class<?>) pt.getActualTypeArguments()[0]).getSimpleName() + ">"; }