List of usage examples for java.lang.reflect Field getGenericType
public Type getGenericType()
From source file:org.rhq.helpers.perftest.support.jpa.EntityDependencyGraph.java
private Class<?> getRelevantType(Field field, Class<?> declaredTargetEntity) { if (declaredTargetEntity == null || declaredTargetEntity == void.class) { declaredTargetEntity = field.getType(); }//from ww w. j av a 2s.c o m if (isCollection(declaredTargetEntity)) { Type type = field.getGenericType(); declaredTargetEntity = getCollectionTypeParameter(type, 0); } else if (isMap(declaredTargetEntity)) { Type type = field.getGenericType(); declaredTargetEntity = getCollectionTypeParameter(type, 1); } return declaredTargetEntity; }
From source file:org.raml.emitter.RamlEmitter.java
private void dumpSequenceField(StringBuilder dump, int depth, Field field, Object pojo) { if (!List.class.isAssignableFrom(field.getType())) { throw new RuntimeException("Only List can be sequence."); }/* w ww. j av a 2 s .c o m*/ List seq = (List) getFieldValue(field, pojo); if (seq == null || seq.size() == 0) { return; } Type type = field.getGenericType(); if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Type itemType = pType.getActualTypeArguments()[0]; dump.append(indent(depth)).append(alias(field)).append(YAML_MAP_SEP); dumpSequenceItems(dump, depth, seq, itemType); } }
From source file:com.flowpowered.cerealization.config.annotated.AnnotatedObjectConfiguration.java
private void save(ConfigurationNodeSource source, Object object, String[] path, Set<Member> members) throws ConfigurationException { final Set<Method> methods = new HashSet<Method>(); for (Member member : members) { if (member instanceof Method) { final Method method = (Method) member; if (method.isAnnotationPresent(Save.class)) { methods.add(method);//from ww w.j a v a 2 s . c om } continue; } final Field field = (Field) member; field.setAccessible(true); String[] fieldPath = field.getAnnotation(Setting.class).value(); if (fieldPath.length == 0) { fieldPath = new String[] { field.getName() }; } final ConfigurationNode fieldNode = source.getNode(ArrayUtils.addAll(path, fieldPath)); try { fieldNode.setValue(field.getGenericType(), field.get(object)); } catch (IllegalAccessException ex) { throw new ConfigurationException(ex); } } invokeMethods(methods, object, source.getNode(path)); }
From source file:net.firejack.platform.core.utils.Factory.java
private FieldInfo getField(Class<?> clazz, Field field) { FieldInfo info = null;//w ww .j a v a2 s . c o m if (field != null) { info = new FieldInfo(field); if (field.getGenericType() instanceof TypeVariable) { TypeVariable genericType = (TypeVariable) field.getGenericType(); TypeVariable<?>[] parameters = genericType.getGenericDeclaration().getTypeParameters(); for (int i = 0; i < parameters.length; i++) { if (parameters[i].getName().equals(genericType.getName())) { Class parameterClass = getGenericParameterClass(clazz, (Class) genericType.getGenericDeclaration(), i); info.setType(parameterClass); break; } } } if (field.getGenericType() instanceof ParameterizedType) { ParameterizedType genericType = (ParameterizedType) field.getGenericType(); Class<?> arrayType = null; if (genericType.getActualTypeArguments()[0] instanceof TypeVariable) { TypeVariable genericType1 = (TypeVariable) genericType.getActualTypeArguments()[0]; TypeVariable<?>[] parameters = genericType1.getGenericDeclaration().getTypeParameters(); for (int i = 0; i < parameters.length; i++) { if (parameters[i].getName().equals(genericType1.getName())) { arrayType = getGenericParameterClass(clazz, (Class) genericType1.getGenericDeclaration(), i); break; } } } else { arrayType = (Class<?>) genericType.getActualTypeArguments()[0]; } info.setGenericType(arrayType); } } return info; }
From source file:com.flowpowered.cerealization.config.annotated.AnnotatedObjectConfiguration.java
private void load(ConfigurationNodeSource source, Object object, String[] path, Set<Member> members) throws ConfigurationException { final Set<Method> methods = new HashSet<Method>(); for (Member member : members) { if (member instanceof Method) { final Method method = (Method) member; if (method.isAnnotationPresent(Load.class)) { methods.add(method);//from ww w . j a va2s.c o m } continue; } final Field field = (Field) member; field.setAccessible(true); String[] fieldPath = field.getAnnotation(Setting.class).value(); if (fieldPath.length == 0) { fieldPath = new String[] { field.getName() }; } final ConfigurationNode fieldNode = source.getNode(ArrayUtils.addAll(path, fieldPath)); final Object value = fieldNode.getTypedValue(field.getGenericType()); try { if (value != null) { field.set(object, value); } else { fieldNode.setValue(field.getGenericType(), field.get(object)); } } catch (IllegalAccessException ex) { throw new ConfigurationException(ex); } } invokeMethods(methods, object, source.getNode(path)); }
From source file:io.mandrel.OtherTest.java
public void inspect(int level, Type clazz, String name) { if (level > 6) return;// w w w . j a va 2s.c o m if (clazz instanceof Class && !((Class<?>) clazz).isEnum() && ((Class<?>) clazz).getPackage() != null && ((Class<?>) clazz).getPackage().getName().startsWith("io.mandrel")) { int newLevel = level + 1; Field[] fields = ((Class<?>) clazz).getDeclaredFields(); 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; Type paramType = pType.getActualTypeArguments()[pType.getActualTypeArguments().length - 1]; text = field.getName() + "(container of " + paramType + ")"; System.err.println(StringUtils.leftPad(text, text.length() + newLevel * 5, "\t- ")); inspect(newLevel, paramType, ""); } } 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:info.archinnov.achilles.internal.metadata.parsing.PropertyParser.java
private <K, V> Pair<Class<K>, Class<V>> determineMapGenericTypes(Field field) { log.trace("Determine generic types for field Map<K,V> {} of entity class {}", field.getName(), field.getDeclaringClass().getCanonicalName()); Type genericType = field.getGenericType(); ParameterizedType pt = (ParameterizedType) genericType; Type[] actualTypeArguments = pt.getActualTypeArguments(); Class<K> keyClass = getClassFromType(actualTypeArguments[0]); Class<V> valueClass = getClassFromType(actualTypeArguments[1]); return Pair.create(keyClass, valueClass); }
From source file:io.mandrel.common.schema.SchemaTest.java
public void inspect(int level, Type clazz, String name) { if (level > 6) return;//from w w w. ja v a2 s . c o 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.haulmont.chile.core.loader.ChileAnnotationsLoader.java
protected Class getFieldType(Field field) { Type genericType = field.getGenericType(); Class type;//from w w w . j a va 2 s . c om if (genericType instanceof ParameterizedType) { Type[] types = ((ParameterizedType) genericType).getActualTypeArguments(); if (Map.class.isAssignableFrom(field.getType())) type = (Class<?>) types[1]; else type = (Class<?>) types[0]; } else { type = getFieldTypeAccordingAnnotations(field); } if (type == null) throw new IllegalArgumentException("Field " + field + " must either be of parameterized type or have a JPA annotation declaring a targetEntity"); return type; }
From source file:com.github.reinert.jjschema.JsonSchemaGenerator.java
private <T> void processCustomCollection(Class<T> type, ObjectNode schema) throws TypeException { schema.put(TAG_TYPE, TAG_ARRAY);//from w w w . j a v a 2 s . c om Field field = type.getDeclaredFields()[0]; ParameterizedType genericType = (ParameterizedType) field.getGenericType(); Class<?> genericClass = (Class<?>) genericType.getActualTypeArguments()[0]; ObjectNode itemsSchema = generateSchema(genericClass); itemsSchema.remove("$schema"); schema.put("items", itemsSchema); }