List of usage examples for java.lang.reflect Field getType
public Class<?> getType()
From source file:app.core.PersistenceTest.java
private static <T extends PersistentObject> T buildRecursively(T newInstance) throws Exception { Set<ConstraintViolation<T>> constraintViolations = validate(newInstance); if (constraintViolations.isEmpty()) return newInstance; // the instance is finished building for (ConstraintViolation constraintViolation : constraintViolations) { Object missingProperty;//from ww w.j a v a 2s . co m Field violatedField = ReflectionUtils.getField(constraintViolation.getRootBeanClass(), constraintViolation.getPropertyPath().toString()); if (NotNull.class .isAssignableFrom(constraintViolation.getConstraintDescriptor().getAnnotation().getClass())) { if (PersistentObject.class.isAssignableFrom(violatedField.getType())) { // similar to instanceof missingProperty = newInstance((Class<PersistentObject>) violatedField.getType()); //recursion here ((PersistentObject) missingProperty).save(); // save the dependency } else { missingProperty = violatedField.getType().newInstance(); // just create a non-null property } ReflectionUtils.setFieldValue(newInstance, violatedField.getName(), missingProperty); } else if (Length.class .isAssignableFrom(constraintViolation.getConstraintDescriptor().getAnnotation().getClass())) { // TODO: implement correctly ReflectionUtils.setFieldValue(newInstance, violatedField.getName(), "0123456789"); } // TODO: support more annotations } return buildRecursively(newInstance); }
From source file:com.adobe.acs.commons.mcp.util.AnnotatedFieldDeserializer.java
@SuppressWarnings("squid:S3776") private static void parseInput(Object target, ValueMap input, Field field) throws ReflectiveOperationException, ParseException { FormField inputAnnotation = field.getAnnotation(FormField.class); Object value;//from w w w . jav a 2s. c om if (input.get(field.getName()) == null) { if (inputAnnotation != null && inputAnnotation.required()) { if (field.getType() == Boolean.class || field.getType() == Boolean.TYPE) { value = false; } else { throw new NullPointerException("Required field missing: " + field.getName()); } } else { return; } } else { value = input.get(field.getName()); } if (hasMultipleValues(field.getType())) { parseInputList(target, serializeToStringArray(value), field); } else { Object val = value; if (value.getClass().isArray()) { val = ((Object[]) value)[0]; } if (val instanceof RequestParameter) { /** Special case handling uploaded files; Method call ~ copied from parseInputValue(..) **/ if (field.getType() == RequestParameter.class) { FieldUtils.writeField(field, target, val, true); } else { try { FieldUtils.writeField(field, target, ((RequestParameter) val).getInputStream(), true); } catch (IOException ex) { LOG.error("Unable to get InputStream for uploaded file [ {} ]", ((RequestParameter) val).getName(), ex); } } } else { parseInputValue(target, String.valueOf(val), field); } } }
From source file:gr.abiss.calipso.jpasearch.specifications.GenericSpecifications.java
protected static void addPredicate(final Class clazz, Root<Persistable> root, CriteriaBuilder cb, LinkedList<Predicate> predicates, String[] propertyValues, String propertyName) { // dot notation only supports toOne.toOne.id if (propertyName.contains(".")) { // LOGGER.info("addPredicate, property name is a path: " + // propertyName); predicates/*from ww w.j a v a2 s . c o m*/ .add(anyToOneToOnePredicateFactory.getPredicate(root, cb, propertyName, null, propertyValues)); } else {// normal single step predicate Field field = GenericSpecifications.getField(clazz, propertyName); if (field != null) { // LOGGER.info("addPredicate, property: " + propertyName); Class fieldType = field.getType(); IPredicateFactory predicateFactory = getPredicateFactoryForClass(field); // LOGGER.info("addPredicate, predicateFactory: " + // predicateFactory); if (predicateFactory != null) { predicates .add(predicateFactory.getPredicate(root, cb, propertyName, fieldType, propertyValues)); } } } }
From source file:com.palantir.ptoss.cinch.core.BindingContext.java
private static List<ObjectFieldMethod> getParameterlessMethods(Object object, Field field) { List<ObjectFieldMethod> methods = Lists.newArrayList(); for (Method method : field.getType().getDeclaredMethods()) { if (method.getParameterTypes().length == 0 && Reflections.isMethodPublic(method)) { methods.add(new ObjectFieldMethod(object, field, method)); }/* ww w. jav a2s . c om*/ } return methods; }
From source file:com.plusub.lib.annotate.JsonParserUtils.java
/** * ?set??/*www.j av a 2 s . c o m*/ * <p>Title: getSetMethodName * <p>Description: * @param field * @return */ private static String getSetMethodName(Field field) { Class clazz = field.getType(); String name = field.getName(); StringBuilder sb = new StringBuilder(); //boolean ?set? //private boolean ishead; setIshead //private boolean isHead; setHead //private boolean head; setHead if (clazz.equals(Boolean.class) || clazz.getName().equals("boolean")) { sb.append("set"); if (name.startsWith("is")) { if (name.length() > 2) { //private boolean isHead; setHead if (Character.isUpperCase(name.charAt(2))) { sb.append(name.substring(2)); } else {//private boolean ishead; setIshead sb.append(name.toUpperCase().charAt(0)); sb.append(name.substring(1)); } } else { sb.append(name.toUpperCase().charAt(0)); sb.append(name.substring(1)); } } else {//private boolean head; setHead sb.append(name.toUpperCase().charAt(0)); sb.append(name.substring(1)); } } else { sb.append("set"); sb.append(name.toUpperCase().charAt(0)); sb.append(name.substring(1)); } return sb.toString(); }
From source file:com.qumoon.commons.BeanMapper.java
public static <T extends com.google.protobuf.GeneratedMessage> T convertBean2Proto( com.google.protobuf.GeneratedMessage.Builder message, Object srcObject) throws Exception { for (Field srcField : srcObject.getClass().getDeclaredFields()) { Object value = PropertyUtils.getProperty(srcObject, srcField.getName()); if (value == null) { continue; }/*from w ww. j a v a2 s.c om*/ Descriptors.FieldDescriptor fd = message.getDescriptorForType().findFieldByName(srcField.getName()); if (null == fd) { continue; } if (srcField.getType() == BigDecimal.class) { message.setField(fd, ((BigDecimal) value).toString()); continue; } else { if (srcField.getType() == Date.class) { message.setField(fd, ((Date) value).getTime()); continue; } else { message.setField(fd, value); } } } return (T) message.build(); }
From source file:edu.mit.csail.sdg.alloy4.Terminal.java
private static A4Options.SatSolver getSolver(String name) { if (getAvaliableSatSolvers().contains(name)) { for (Field f : A4Options.SatSolver.class.getFields()) { if (f.getName().equals(name) && A4Options.SatSolver.class.isAssignableFrom(f.getType())) { try { return (A4Options.SatSolver) f.get(null); } catch (Exception e) { e.printStackTrace(); }//from ww w.jav a 2s. c om } } } return null; }
From source file:kina.utils.UtilMongoDB.java
/** * converts from an entity class with kina's anotations to BsonObject. * * @param t an instance of an object of type T to convert to BSONObject. * @param <T> the type of the object to convert. * @return the provided object converted to BSONObject. * @throws IllegalAccessException//from w w w. j a v a 2 s . c om * @throws InstantiationException * @throws InvocationTargetException */ public static <T extends KinaType> BSONObject getBsonFromObject(T t) throws IllegalAccessException, InstantiationException, InvocationTargetException { Field[] fields = filterKinaFields(t.getClass()); BSONObject bson = new BasicBSONObject(); for (Field field : fields) { Method method = Utils.findGetter(field.getName(), t.getClass()); Object object = method.invoke(t); if (object != null) { if (Collection.class.isAssignableFrom(field.getType())) { Collection c = (Collection) object; Iterator iterator = c.iterator(); List<BSONObject> innerBsonList = new ArrayList<>(); while (iterator.hasNext()) { innerBsonList.add(getBsonFromObject((KinaType) iterator.next())); } bson.put(kinaFieldName(field), innerBsonList); } else if (KinaType.class.isAssignableFrom(field.getType())) { bson.put(kinaFieldName(field), getBsonFromObject((KinaType) object)); } else { bson.put(kinaFieldName(field), object); } } } return bson; }
From source file:com.ghy.common.util.reflection.ReflectionUtils.java
/** * , private/protected, ??setter./*w w w .j a va2 s . c om*/ */ public static void setFieldValue(final Object obj, final String fieldName, final Object value) { Field field = getAccessibleField(obj, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } System.out.println("-----field-----" + field.getType()); try { field.set(obj, value); } catch (IllegalAccessException e) { logger.error("??:{}", e.getMessage()); } }
From source file:com.aw.support.reflection.AttributeAccessor.java
/** * @param target/*from w w w . j a v a2s.co m*/ * @param attrName * @return */ public static void set(Object target, String attrName, Object value) { Field field = null; try { Class cls = target.getClass(); field = getField(cls, attrName); field.setAccessible(true); field.set(target, value); } catch (IllegalArgumentException e) { e.printStackTrace(); throw new IllegalArgumentException("Error setting the value of the attribute:<" + attrName + "> of object:<" + target + "> check: Attribute Type:<" + field.getType() + "> value Type: <" + value.getClass() + ">"); } catch (Throwable e) { e.printStackTrace(); throw new IllegalStateException( "Error setting the value of the attribute:<" + attrName + "> of object:<" + target + ">"); } }