List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:Main.java
public static Object isEmpty(Object obj, Class clazz) { if (obj == null || clazz == null) { return obj; }// w w w . j av a 2 s .c o m Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { try { String fieldName = field.getName(); if (fieldName == null || fieldName.length() == 0) continue; String fieldMethodName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); String getfieldName = "get" + fieldMethodName; Method getMethod = clazz.getMethod(getfieldName, null); if (getMethod == null) continue; Object value = getMethod.invoke(obj, null); Class type = field.getType(); if (type == String.class) { if (value == null || "null".equals(value.toString()) || value.toString().trim().length() == 0) { value = ""; String setfieldName = "set" + fieldMethodName; Method setMethod = clazz.getMethod(setfieldName, String.class); if (setMethod == null) continue; setMethod.invoke(obj, value); } } else if (type == Integer.class || type == int.class || type == Float.class || type == float.class || type == Double.class || type == double.class) { if (value == null || "null".equals(value.toString()) || value.toString().length() == 0) { String setfieldName = "set" + fieldMethodName; Method setMethod = clazz.getMethod(setfieldName, type); setMethod.invoke(obj, 0); } } } catch (Exception e) { e.printStackTrace(); continue; } } return obj; }
From source file:Main.java
public static Field[] getDeclaredFields(Object paramObject) { Object localObject = null;/*from w w w . j a va 2 s .c o m*/ if (paramObject != null) { return null; } else { try { Class localClass = paramObject.getClass(); localObject = null; if (localClass != null) { Field[] arrayOfField = localClass.getDeclaredFields(); return arrayOfField; } } catch (Exception var4) { ; } return null; } }
From source file:Main.java
/** * Coverter./* w w w . j a va 2 s. c om*/ * * @param object * the object * @return the string */ @SuppressWarnings("unused") public static String coverter(Object object) { if (object instanceof Object[]) { return coverter((Object[]) object); } if (object instanceof Collection) { return coverter((Collection<?>) object); } StringBuilder strBuilder = new StringBuilder(); if (isObject(object)) { Class<? extends Object> clz = object.getClass(); Field[] fields = clz.getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); if (field == null) { continue; } String fieldName = field.getName(); Object value = null; try { value = field.get(object); } catch (IllegalArgumentException e) { continue; } catch (IllegalAccessException e) { continue; } strBuilder.append("<").append(fieldName).append(" className=\"").append(value.getClass().getName()) .append("\">\n"); strBuilder.append(value.toString() + "\n"); strBuilder.append("</").append(fieldName).append(">\n"); } } else if (object == null) { strBuilder.append("null"); } else { strBuilder.append(object.toString()); } return strBuilder.toString(); }
From source file:com.qumoon.commons.BeanMapper.java
public static <T> T convertProto2Bean(com.google.protobuf.GeneratedMessage message, T descObject, Class srcClass) { for (Field srcField : srcClass.getDeclaredFields()) { Descriptors.FieldDescriptor fd = message.getDescriptorForType().findFieldByName(srcField.getName()); if (null == fd) { continue; }// ww w.ja v a 2 s . c o m try { String fieldStrValue = String.valueOf(message.getField(fd)); if (fieldStrValue.isEmpty()) { continue; } if (srcField.getType() == BigDecimal.class) { PropertyUtils.setProperty(descObject, srcField.getName(), new BigDecimal(fieldStrValue)); } else { if (srcField.getType() == Date.class) { PropertyUtils.setProperty(descObject, srcField.getName(), new Date((Long) (message.getField(fd)))); } else { if (srcField.getType() == Byte.class) { PropertyUtils.setProperty(descObject, srcField.getName(), Byte.valueOf(fieldStrValue)); } else { PropertyUtils.setProperty(descObject, srcField.getName(), message.getField(fd)); } } } } catch (Exception e) { logger.error(e.getMessage()); } finally { continue; } } return descObject; }
From source file:com.xhsoft.framework.common.utils.ReflectUtil.java
/** * <p>Description:ReflectionMap?</p> * @param target/* ww w . j a v a2 s .c o m*/ * @return Map<String,Object> * @author wanggq * @since 2009-9-9 */ @SuppressWarnings("unchecked") public static Map<String, Object> getMapFieldData(Object target) { Map<String, Object> map = new HashMap<String, Object>(); Class clazz = target.getClass(); Field[] fields = clazz.getDeclaredFields(); Method[] methods = clazz.getDeclaredMethods(); for (Field field : fields) { String fieldName = field.getName(); if ("messageTypeId".equals(fieldName)) { continue; } String getMethod = "get" + StringUtils.capitalize(fieldName); for (Method method : methods) { if (method.getName().equals(getMethod)) { try { Object ret = method.invoke(target, null); map.put(fieldName, ret); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } return map; }
From source file:Main.java
/** * Get all fields' value and put them to a map. * //from ww w. j a v a 2 s . co m * @param bean * @return Map */ public static Map<String, Object> getFieldValueMap(Object bean) { Class<?> cls = bean.getClass(); Map<String, Object> valueMap = new HashMap<String, Object>(); // Get all fields. Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); Object value = field.get(bean); // if(value == null) { // valueMap.put(field.getName(), ""); // continue; // } valueMap.put(field.getName(), value); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return valueMap; }
From source file:Main.java
/** * Returns all fields declared in the class passed as argument or in its super classes annotated with * the supplied annotation.//from w w w. j av a2 s. c o m */ public static List<Field> getAllDeclaredField(Class<?> clazz, Class<? extends Annotation> annotationClass) { final List<Field> result = new LinkedList<Field>(); for (final Field field : clazz.getDeclaredFields()) { final Object jakeOption = field.getAnnotation(annotationClass); if (jakeOption != null) { result.add(field); } } final Class<?> superClass = clazz.getSuperclass(); if (superClass != null) { result.addAll(getAllDeclaredField(superClass, annotationClass)); } return result; }
From source file:com.jigsforjava.util.ClassUtils.java
private static void getAllDeclaredFields(Class<?> aClass, UnaryFunction function) { for (Field field : aClass.getDeclaredFields()) { try {// ww w . j a va 2s. co m function.perform(field); } catch (FunctionException e) { e.printStackTrace(); } } if (aClass.getSuperclass() != null) { getAllDeclaredFields(aClass.getSuperclass(), function); } }
From source file:springobjectmapper.ReflectionHelper.java
public static List<Field> getFields(Class<?> c) { List<Field> results = new ArrayList<Field>(); do {//from w w w . ja v a 2 s.co m Field[] fields = c.getDeclaredFields(); for (Field field : fields) { if (isEntityField(field)) { results.add(field); } field.setAccessible(true); } c = c.getSuperclass(); } while (c != null); return results; }
From source file:com.evolveum.midpoint.prism.marshaller.XNodeProcessorUtil.java
public static <T> Field findXmlValueField(Class<T> beanClass) { for (Field field : beanClass.getDeclaredFields()) { XmlValue xmlValue = field.getAnnotation(XmlValue.class); if (xmlValue != null) { return field; }// ww w .j av a2 s .c o m } return null; }