本文整理匯總了Java中java.lang.reflect.Field.getGenericType方法的典型用法代碼示例。如果您正苦於以下問題:Java Field.getGenericType方法的具體用法?Java Field.getGenericType怎麽用?Java Field.getGenericType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.lang.reflect.Field
的用法示例。
在下文中一共展示了Field.getGenericType方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getFieldGenricType
import java.lang.reflect.Field; //導入方法依賴的package包/類
@SuppressWarnings("rawtypes")
public static Class getFieldGenricType(final Field field, final int index) {
Assert.notNull(field, "Parameter 'field' must be not null!");
Assert.state(index > -1, "Parameter 'index' must be > -1!");
Type type = field.getGenericType();
if(type instanceof ParameterizedType){
ParameterizedType ptype = (ParameterizedType)type;
type = ptype.getActualTypeArguments()[index];
if(type instanceof ParameterizedType){
return (Class)((ParameterizedType) type).getRawType();
}else{
return (Class) type;
}
}else{
return (Class) type;
}
}
示例2: isFieldPrintable
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static boolean isFieldPrintable(Field field)
{
boolean isPrintable = false;
Type fieldType = field.getGenericType();
if (fieldType instanceof ParameterizedType)
{
Type[] declaredGenericTypes = ((ParameterizedType) fieldType).getActualTypeArguments();
for (Type type : declaredGenericTypes)
{
isPrintable = ClassTypeHelper.isPrintableClass((Class<?>) type);
if (!isPrintable)
{
break;
}
}
}
return isPrintable;
}
示例3: CollectionProperty
import java.lang.reflect.Field; //導入方法依賴的package包/類
public CollectionProperty(EntityMetadata metadata, Field field) {
super(metadata, field);
Collection collection = field.getAnnotation(Collection.class);
if(!java.util.Collection.class.isAssignableFrom(field.getType())){
throw new HibatisException("Collection屬性必須為集合類型");
}
this.isLazy = collection.lazy();
this.isIn = collection.in();
this.cacheable = collection.cacheable();
ParameterizedType type = (ParameterizedType) field.getGenericType();
this.referenceType = (Class<?>) type.getActualTypeArguments()[0];
this.rawType = field.getType();
if(this.rawType != List.class){
throw new HibatisException("@Collection隻支持java.util.List類型");
}
this.setProperties(collection.property());
this.setReferences(collection.reference());
String[] orderBy = collection.orderBy();
if(orderBy != null){
sorts = new Sort[orderBy.length];
for (int i = 0; i < sorts.length; i++) {
String order = orderBy[i];
sorts[i] = Sort.parse(order);
}
}
}
示例4: check
import java.lang.reflect.Field; //導入方法依賴的package包/類
@Override
public final boolean check(Field field) {
if (field == null)
return false;
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type rawType = parameterizedType.getRawType();
if (rawType.equals(collectionType)) {
return checkGenericType(parameterizedType);
} else
return false;
}
return false;
}
示例5: toListGenericsAlias
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 給List中的泛型取別名
*
* @param xStream
* @param obj
*/
private static void toListGenericsAlias(XStream xStream, Object obj) {
/** 得到所有的fields **/
Field[] fs = obj.getClass().getDeclaredFields();
for (Field f : fs) {
/** 得到field的class及類型全路徑 **/
Class<?> fieldClazz = f.getType();
/** 如果是List類型,得到其類的類型 **/
if (fieldClazz.isAssignableFrom(List.class)) {
/** 獲取類的類型 **/
Type fc = f.getGenericType();
if (fc == null)
continue;
/** 是否是參數化泛型類型 **/
if (fc instanceof ParameterizedType) {
/** 得到泛型裏的class類型對象 **/
ParameterizedType pt = (ParameterizedType) fc;
Class<?> genericClazz = (Class<?>) pt.getActualTypeArguments()[0];
xStream.alias(genericClazz.getSimpleName(), genericClazz);
}
}
}
}
示例6: getGenericTypeClass
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Get the generic type class of List or Set. If there's no generic type of
* List or Set return null.
*
* @param field
* A generic type field.
* @return The generic type of List or Set.
*/
protected Class<?> getGenericTypeClass(Field field) {
Type genericType = field.getGenericType();
if (genericType != null) {
if (genericType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericType;
return (Class<?>) parameterizedType.getActualTypeArguments()[0];
}
}
return null;
}
示例7: convertCollectionFromProtobufs
import java.lang.reflect.Field; //導入方法依賴的package包/類
private static Object convertCollectionFromProtobufs(Field field,
Collection<?> collectionOfProtobufs)
throws JException, InstantiationException, IllegalAccessException {
if (collectionOfProtobufs.isEmpty()) {
return collectionOfProtobufs;
}
final ParameterizedType listType = (ParameterizedType) field.getGenericType();
final Class<?> collectionClazzType = (Class<?>) listType.getActualTypeArguments()[0];
final ProtobufEntity protoBufEntityAnno =
ProtobufSerializerUtils.getProtobufEntity(collectionClazzType);
final Object first = collectionOfProtobufs.toArray()[0];
if (!(first instanceof Message) && protoBufEntityAnno == null) {
return collectionOfProtobufs;
}
final Collection<Object> newCollectionOfValues = new ArrayList<>();
for (Object protobufValue : collectionOfProtobufs) {
if (!(protobufValue instanceof Message)) {
throw new ProtobufException(
"Collection contains an object of type " + protobufValue.getClass()
+ " which is not an instanceof GeneratedMessage, can not (de)serialize this");
}
newCollectionOfValues
.add(serializeFromProtobufEntity((Message) protobufValue, collectionClazzType));
}
return newCollectionOfValues;
}
示例8: getConvertedType
import java.lang.reflect.Field; //導入方法依賴的package包/類
private Class<?> getConvertedType(String fieldName) {
try {
Field field = clazz.getField(fieldName);
if (field.getType().equals(Optional.class)) {
ParameterizedType type = (ParameterizedType) field.getGenericType();
return converters.getConvertedType((Class<?>) type.getActualTypeArguments()[0]);
} else {
return converters.getConvertedType(field.getType());
}
} catch (NoSuchFieldException e) {
throw new FieldAccessException(fieldName, clazz.getName());
}
}
示例9: getFieldGenericType
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static Class<?>[] getFieldGenericType(Field field) {
Type genericFieldType = field.getGenericType();
if (genericFieldType instanceof ParameterizedType) {
ParameterizedType aType = (ParameterizedType) genericFieldType;
Type[] fieldArgTypes = aType.getActualTypeArguments();
return (Class<?>[]) fieldArgTypes;
}
return null;
}
示例10: main
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException {
for (TypeVariable<Class<Generic>> var : Generic.class.getTypeParameters()) {
System.out.println(var.getName());
Type bound = var.getBounds()[0];
System.out.println(((Class<?>) bound).getName().equals(AA.class.getName()));
}
Field f = Generic.class.getField("f");
ParameterizedType fieldType = (java.lang.reflect.ParameterizedType)f.getGenericType();
checkOneParameterType(fieldType, Generic.class, AA.class);
ParameterizedType methodReturnType =
(ParameterizedType) Generic.class.getMethod("get").getGenericReturnType();
checkOneParameterType(methodReturnType, Generic.class, AA.class);
}
示例11: getType
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* Reflection to get the type of a given field, even nested or in a superclass.
* @param rootClass
* @param attributePath field name like "surname" or even a path like "friend.name"
* @return
* @throws NoSuchFieldException if the field is not found in the class hierarchy
* @throws SecurityException
*/
public Class getType(Class rootClass, String attributePath) throws NoSuchFieldException, SecurityException {
if (StringUtils.isBlank(attributePath)) {
return rootClass;
}
String attributeName = StringUtils.substringBefore(attributePath, ".");
Field field = null;
NoSuchFieldException exception = null;
while (field==null && rootClass!=null) {
try {
field = rootClass.getDeclaredField(attributeName);
} catch (NoSuchFieldException e) {
if (exception==null) {
exception=e;
}
rootClass = rootClass.getSuperclass();
}
}
if (field==null) {
if (exception!=null) {
throw exception;
} else {
throw new NoSuchFieldException("No field " + attributeName + " found in hierarchy");
}
}
Class attributeType = field.getType();
// If it's a list, look for the list type
if (attributeType == java.util.List.class) {
// TODO check if the attributeType is an instance of java.util.Collection
ParameterizedType parameterizedType = (ParameterizedType)field.getGenericType();
if (parameterizedType!=null) {
Type[] types = parameterizedType.getActualTypeArguments();
if (types.length==1) {
attributeType = (Class<?>) types[0];
}
}
}
return getType(attributeType, StringUtils.substringAfter(attributePath, "."));
}
示例12: getGenericType
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 獲取域的泛型類型,如果不帶泛型返回null
*
* @param f
* @return
*/
public static Class<?> getGenericType(Field f) {
Type type = f.getGenericType();
if (type instanceof ParameterizedType) {
type = ((ParameterizedType) type).getActualTypeArguments()[0];
if (type instanceof Class<?>) return (Class<?>) type;
} else if (type instanceof Class<?>) return (Class<?>) type;
return null;
}
示例13: getFieldType
import java.lang.reflect.Field; //導入方法依賴的package包/類
public static Type getFieldType(Class<?> clazz, String fieldName) {
try {
Field field = clazz.getField(fieldName);
return field.getGenericType();
} catch (Exception ex) {
return null;
}
}
示例14: load
import java.lang.reflect.Field; //導入方法依賴的package包/類
public boolean load() {
if (!this.configFile.exists()) return false;
Config cfg = new Config(configFile, Config.YAML);
for (Field field : this.getClass().getDeclaredFields()) {
if (field.getName().equals("configFile")) continue;
if (skipSave(field)) continue;
String path = getPath(field);
if (path == null) continue;
if (path.isEmpty()) continue;
field.setAccessible(true);
try {
if (field.getType() == int.class || field.getType() == Integer.class)
field.set(this, cfg.getInt(path, field.getInt(this)));
else if (field.getType() == boolean.class || field.getType() == Boolean.class)
field.set(this, cfg.getBoolean(path, field.getBoolean(this)));
else if (field.getType() == long.class || field.getType() == Long.class)
field.set(this, cfg.getLong(path, field.getLong(this)));
else if (field.getType() == double.class || field.getType() == Double.class)
field.set(this, cfg.getDouble(path, field.getDouble(this)));
else if (field.getType() == String.class)
field.set(this, cfg.getString(path, (String) field.get(this)));
else if (field.getType() == ConfigSection.class)
field.set(this, cfg.getSection(path));
else if (field.getType() == List.class) {
Type genericFieldType = field.getGenericType();
if (genericFieldType instanceof ParameterizedType) {
ParameterizedType aType = (ParameterizedType) genericFieldType;
Class fieldArgClass = (Class) aType.getActualTypeArguments()[0];
if (fieldArgClass == Integer.class) field.set(this, cfg.getIntegerList(path));
else if (fieldArgClass == Boolean.class) field.set(this, cfg.getBooleanList(path));
else if (fieldArgClass == Double.class) field.set(this, cfg.getDoubleList(path));
else if (fieldArgClass == Character.class) field.set(this, cfg.getCharacterList(path));
else if (fieldArgClass == Byte.class) field.set(this, cfg.getByteList(path));
else if (fieldArgClass == Float.class) field.set(this, cfg.getFloatList(path));
else if (fieldArgClass == Short.class) field.set(this, cfg.getFloatList(path));
else if (fieldArgClass == String.class) field.set(this, cfg.getStringList(path));
} else field.set(this, cfg.getList(path)); // Hell knows what's kind of List was found :)
} else
throw new IllegalStateException("SimpleConfig did not supports class: " + field.getType().getName() + " for config field " + configFile.getName());
} catch (Exception e) {
Server.getInstance().getLogger().logException(e);
return false;
}
}
return true;
}
示例15: parseValue
import java.lang.reflect.Field; //導入方法依賴的package包/類
/**
* 參數解析 (支持:Byte、Boolean、String、Short、Integer、Long、Float、Double、Date)
*
* @param field
* @param value
* @return
*/
public static Object parseValue(Field field, String value) {
Class<?> fieldType = field.getType();
// parse list item
if (field.getGenericType() instanceof ParameterizedType) {
ParameterizedType fieldGenericType = (ParameterizedType) field.getGenericType();
if (fieldGenericType.getRawType().equals(List.class)) {
Type gtATA = fieldGenericType.getActualTypeArguments()[0];
fieldType = (Class<?>) gtATA;
}
}
PageFieldSelect apiRequestParam = field.getAnnotation(PageFieldSelect.class);
if(value==null || value.trim().length()==0)
return null;
value = value.trim();
if (Byte.class.equals(fieldType) || Byte.TYPE.equals(fieldType)) {
return parseByte(value);
} else if (Boolean.class.equals(fieldType) || Boolean.TYPE.equals(fieldType)) {
return parseBoolean(value);
}/* else if (Character.class.equals(fieldType) || Character.TYPE.equals(fieldType)) {
return value.toCharArray()[0];
}*/ else if (String.class.equals(fieldType)) {
return value;
} else if (Short.class.equals(fieldType) || Short.TYPE.equals(fieldType)) {
return parseShort(value);
} else if (Integer.class.equals(fieldType) || Integer.TYPE.equals(fieldType)) {
return parseInt(value);
} else if (Long.class.equals(fieldType) || Long.TYPE.equals(fieldType)) {
return parseLong(value);
} else if (Float.class.equals(fieldType) || Float.TYPE.equals(fieldType)) {
return parseFloat(value);
} else if (Double.class.equals(fieldType) || Double.TYPE.equals(fieldType)) {
return parseDouble(value);
} else if (Date.class.equals(fieldType)) {
return parseDate(apiRequestParam, value);
} else {
throw new RuntimeException("request illeagal type, type must be Integer not int Long not long etc, type=" + fieldType);
}
}