List of usage examples for java.lang.reflect Field getType
public Class<?> getType()
From source file:Main.java
private static Method getBooleanColumnSetMethod(Class<?> entityType, Field field) { String fieldName = field.getName(); String methodName = null;//from w w w. ja v a 2s. co m if (isStartWithIs(field.getName())) { methodName = "set" + fieldName.substring(2, 3).toUpperCase() + fieldName.substring(3); } else { methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); } try { return entityType.getDeclaredMethod(methodName, field.getType()); } catch (NoSuchMethodException e) { //Logger.d(methodName + " not exist"); } return null; }
From source file:com.github.strawberry.guice.config.ConfigLoader.java
private static Option getFromProperties(Map properties, final Field field, final Config annotation) { Object value = null;/*from w w w . j a v a2s . c om*/ Class<?> fieldType = field.getType(); String pattern = annotation.value(); boolean allowNull = annotation.allowNull(); Set<String> matchingKeys = getKeys(properties, pattern); if (matchingKeys.size() == 1) { String matchingKey = Iterables.getOnlyElement(matchingKeys); if (fieldType.equals(char[].class)) { value = properties.get(matchingKey).toString().toCharArray(); } else if (fieldType.equals(Character[].class)) { value = ArrayUtils.toObject(properties.get(matchingKey).toString().toCharArray()); } else if (fieldType.equals(char.class) || fieldType.equals(Character.class)) { String toConvert = properties.get(matchingKey).toString(); if (toConvert.length() == 1) { value = properties.get(matchingKey).toString().charAt(0); } else { throw ConversionException.of(toConvert, matchingKey, fieldType); } } else if (fieldType.equals(String.class)) { value = properties.get(matchingKey); } else if (fieldType.equals(byte[].class)) { if (properties.containsKey(matchingKey)) { value = properties.get(matchingKey).toString().getBytes(); } else { value = null; } } else if (fieldType.equals(Byte[].class)) { value = ArrayUtils.toObject(properties.get(matchingKey).toString().getBytes()); } else if (fieldType.equals(boolean.class) || fieldType.equals(Boolean.class)) { String toConvert = properties.get(matchingKey).toString(); if (BOOLEAN.matcher(toConvert).matches()) { value = TRUE.matcher(toConvert).matches(); } else { throw ConversionException.of(toConvert, matchingKey, fieldType); } } else if (Map.class.isAssignableFrom(fieldType)) { value = mapOf(field, properties, matchingKey); } else if (Collection.class.isAssignableFrom(fieldType)) { value = collectionOf(field, properties, matchingKey); } else { String toConvert = properties.get(matchingKey).toString(); try { if (fieldType.equals(byte.class) || fieldType.equals(Byte.class)) { value = Byte.parseByte(properties.get(matchingKey).toString()); } else if (fieldType.equals(short.class) || fieldType.equals(Short.class)) { value = Short.parseShort(toConvert); } else if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) { value = Integer.parseInt(toConvert); } else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) { value = Long.parseLong(toConvert); } else if (fieldType.equals(BigInteger.class)) { value = new BigInteger(toConvert); } else if (fieldType.equals(float.class) || fieldType.equals(Float.class)) { value = Float.parseFloat(toConvert); } else if (fieldType.equals(double.class) || fieldType.equals(Double.class)) { value = Double.parseDouble(toConvert); } else if (fieldType.equals(BigDecimal.class)) { value = new BigDecimal(toConvert); } } catch (NumberFormatException exception) { throw ConversionException.of(exception, toConvert, matchingKey, fieldType); } } } else if (matchingKeys.size() > 1) { if (Map.class.isAssignableFrom(fieldType)) { value = nestedMapOf(field, properties, matchingKeys); } else if (Collection.class.isAssignableFrom(fieldType)) { value = nestedCollectionOf(field, properties, matchingKeys); } } else { if (!allowNull) { value = nonNullValueOf(fieldType); } } return Option.fromNull(value); }
From source file:com.github.strawberry.redis.RedisLoader.java
private static Collection<?> nestedCollectionOf(Field field, Jedis jedis, Set<String> redisKeys) { Collection collection = collectionImplementationOf(field.getType()); for (String redisKey : redisKeys) { JedisType jedisType = JedisType.valueOf(jedis.type(redisKey).toUpperCase()); switch (jedisType) { case STRING: { collection.add(jedis.get(redisKey)); }/*w ww . ja va2 s .c o m*/ break; case HASH: { collection.add(jedis.hgetAll(redisKey)); } break; case LIST: { collection.add(jedis.lrange(redisKey, 0, -1)); } break; case SET: { collection.add(jedis.smembers(redisKey)); } break; case ZSET: { collection.add(jedis.zrange(redisKey, 0, -1)); } break; } } return collection; }
From source file:Main.java
private static Method getBooleanColumnSetMethod(Class<?> entityType, Field field) { String fieldName = field.getName(); String methodName = null;/*from w w w . java 2s . c o m*/ if (isStartWithIs(field.getName())) { methodName = "set" + fieldName.substring(2, 3).toUpperCase() + fieldName.substring(3); } else { methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); } try { return entityType.getDeclaredMethod(methodName, field.getType()); } catch (NoSuchMethodException e) { Log.d("getBooleanColumnSetMethod", methodName + " not exist"); } return null; }
From source file:com.github.strawberry.redis.RedisLoader.java
private static Collection<?> collectionOf(Field field, Jedis jedis, String key) { Collection collection = collectionImplementationOf(field.getType()); JedisType jedisType = JedisType.valueOf(jedis.type(key).toUpperCase()); Option<Type> genericType = genericTypeOf(field, 0); switch (jedisType) { case STRING: { collection.add(jedis.get(key));//from w w w. j av a 2 s . c om } break; case HASH: { collection.add(jedis.hgetAll(key)); } break; case LIST: { if (genericType.exists(isAssignableTo(Collection.class)) || genericType.exists(isEqualTo(Object.class))) { collection.add(jedis.lrange(key, 0, -1)); } else { collection.addAll(jedis.lrange(key, 0, -1)); } } break; case SET: { if (genericType.exists(isAssignableTo(Collection.class)) || genericType.exists(isEqualTo(Object.class))) { collection.add(jedis.smembers(key)); } else { collection.addAll(jedis.smembers(key)); } } break; case ZSET: { if (genericType.exists(isAssignableTo(Collection.class)) || genericType.exists(isEqualTo(Object.class))) { collection.add(jedis.zrange(key, 0, -1)); } else { collection.addAll(jedis.zrange(key, 0, -1)); } } break; } return collection; }
From source file:net.ostis.sc.memory.SCKeynodesBase.java
protected static void init(SCSession session, Class<? extends SCKeynodesBase> klass) { if (log.isDebugEnabled()) log.debug("Start retrieving keynodes for " + klass); try {/*from w w w.ja v a 2s. co m*/ // // Search default segment for keynodes // SCSegment defaultSegment = null; { DefaultSegment annotation = klass.getAnnotation(DefaultSegment.class); if (annotation != null) { defaultSegment = session.openSegment(annotation.value()); Validate.notNull(defaultSegment, "Default segment \"{0}\" not found", annotation.value()); klass.getField(annotation.fieldName()).set(null, defaultSegment); } } Field[] fields = klass.getFields(); for (Field field : fields) { int modifiers = field.getModifiers(); if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers)) { Class<?> type = field.getType(); if (type.equals(SCSegment.class)) { // // We have segment field. Load segment by uri. // SegmentURI annotation = field.getAnnotation(SegmentURI.class); if (annotation != null) { String uri = annotation.value(); SCSegment segment = session.openSegment(uri); field.set(null, segment); } else { // May be it already has value? if (log.isWarnEnabled()) { if (field.get(null) == null) log.warn(field + " doesn't have value"); } } } else { if (!(checkKeynode(session, defaultSegment, field) || checkKeynodeURI(session, field) || checkKeynodesNumberPatternURI(session, klass, field))) { if (log.isWarnEnabled()) { if (field.get(null) == null) log.warn(field + " doesn't have annotations and value"); } } } } } } catch (Exception e) { // TODO: handle e.printStackTrace(); } }
From source file:org.oncoblocks.centromere.web.controller.RequestUtils.java
/** * Inspects a {@link Model} class and returns all of the available and acceptable query parameter * definitions, as a map of parameter names and {@link QueryParameterDescriptor} objects. * // www .j a v a 2 s . co m * @param model * @return */ public static Map<String, QueryParameterDescriptor> getAvailableQueryParameters(Class<? extends Model<?>> model, boolean recursive) { Map<String, QueryParameterDescriptor> paramMap = new HashMap<>(); for (Field field : model.getDeclaredFields()) { String fieldName = field.getName(); Class<?> type = field.getType(); if (Collection.class.isAssignableFrom(field.getType())) { ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType(); type = (Class<?>) parameterizedType.getActualTypeArguments()[0]; } if (field.isAnnotationPresent(Ignored.class)) { continue; } else { paramMap.put(fieldName, new QueryParameterDescriptor(fieldName, fieldName, type, Evaluation.EQUALS)); } if (field.isAnnotationPresent(ForeignKey.class)) { if (!recursive) continue; ForeignKey foreignKey = field.getAnnotation(ForeignKey.class); String relField = !"".equals(foreignKey.rel()) ? foreignKey.rel() : fieldName; Map<String, QueryParameterDescriptor> foreignModelMap = getAvailableQueryParameters( foreignKey.model(), false); for (QueryParameterDescriptor descriptor : foreignModelMap.values()) { String newParamName = relField + "." + descriptor.getParamName(); descriptor.setParamName(newParamName); paramMap.put(newParamName, descriptor); } } if (field.isAnnotationPresent(Aliases.class)) { Aliases aliases = field.getAnnotation(Aliases.class); for (Alias alias : aliases.value()) { paramMap.put(alias.value(), new QueryParameterDescriptor(alias.value(), alias.fieldName().equals("") ? fieldName : alias.fieldName(), type, alias.evaluation())); } } else if (field.isAnnotationPresent(Alias.class)) { Alias alias = field.getAnnotation(Alias.class); paramMap.put(alias.value(), new QueryParameterDescriptor(alias.value(), alias.fieldName().equals("") ? fieldName : alias.fieldName(), type, alias.evaluation())); } } return paramMap; }
From source file:at.alladin.rmbt.shared.hstoreparser.HstoreParser.java
/** * // ww w . j a va2 s . c o m * @param f * @param o * @return */ public static Object parseFieldValue(Field f, Object o) { if (o != JSONObject.NULL) { if (f.getType().equals(Integer.class) || f.getType().equals(Integer.TYPE)) { return Integer.parseInt(String.valueOf(o)); } else if (f.getType().equals(String.class)) { return String.valueOf(o); } else if (f.getType().equals(Long.class) || f.getType().equals(Long.TYPE)) { return Long.parseLong(String.valueOf(o)); } else if (f.getType().equals(Boolean.class) || f.getType().equals(Boolean.TYPE)) { return Boolean.parseBoolean(String.valueOf(o)); } else if (f.getType().equals(Float.class) || f.getType().equals(Float.TYPE)) { return Float.parseFloat(String.valueOf(o)); } else if (f.getType().equals(Double.class) || f.getType().equals(Double.TYPE)) { return Double.parseDouble(String.valueOf(o)); } else if (f.getType().equals(Short.class) || f.getType().equals(Short.TYPE)) { return Short.parseShort(String.valueOf(o)); } else { return o; } } return null; }
From source file:com.aw.support.reflection.AttributeAccessor.java
/** * @param target/* w w w .j av a 2 s. c o m*/ * @return */ public static void set(Object target, Field field, Object value) { field.setAccessible(true); try { field.set(target, value); } catch (IllegalAccessException e) { e.printStackTrace(); throw new IllegalArgumentException("Error setting the value of the attribute:<" + field.getName() + "> of object:<" + target + "> check: Attribute Type:<" + field.getType() + "> value Type: <" + value.getClass() + ">"); } }
From source file:com.ms.commons.test.common.ReflectUtil.java
public static Object getValueAccroudBean(Object bean, String field, Object value) { Class<?> clazz = bean.getClass(); try {/*from w w w . j a va 2 s . c om*/ Field f = getDeclaredField(clazz, NamingUtil.dbNameToJavaName(field)); f.setAccessible(true); try { return TypeConvertUtil.convert(f.getType(), value); } catch (IllegalArgumentException e) { throw new UnknowException(e); } } catch (SecurityException e) { throw new UnknowException(e); } catch (NoSuchFieldException e) { throw new JavaFieldNotFoundException(clazz, field); } }