List of usage examples for java.lang.reflect Field getName
public String getName()
From source file:com.neusoft.mid.clwapi.tools.JacksonUtils.java
/** * @param obj// w w w .java2s.com * @param f * @throws IllegalAccessException * @throws JacksonEncoderException */ private static Object jsonCoder(Object obj, Field f) throws IllegalAccessException, JacksonEncoderException { Object o = FieldUtils.readField(f, obj); if (o instanceof String) { FieldUtils.writeField(f, obj, jsonCoder((String) o, true)); } else if (o instanceof Collection<?>) { logger.debug("Field [" + f.getName() + "] is " + o.getClass()); Collection<?> c = (Collection<?>) o; Iterator<?> it = c.iterator(); while (it.hasNext()) { jsonEncoder(it.next()); } } else if (o instanceof Map<?, ?>) { logger.debug("Field [" + f.getName() + "] is " + o.getClass()); Set<?> entries = ((Map<?, ?>) o).entrySet(); Iterator<?> it = entries.iterator(); while (it.hasNext()) { Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) it.next(); logger.debug("Key is [" + entry.getKey() + "]"); entry.setValue(jsonEncoder(entry.getValue())); } } else if (o instanceof Integer || o instanceof Byte || o instanceof Boolean || o instanceof Long || o instanceof Double || o instanceof Character || o instanceof Short || o instanceof Float || o instanceof Number) { return obj; } else { throw new JacksonEncoderException("??" + f.getClass()); } return obj; }
From source file:com.github.gekoh.yagen.util.FieldInfo.java
private static List<FieldInfo> convertFields(List<FieldInfo> fields, Class baseEntity) { for (Field field : baseEntity.getDeclaredFields()) { FieldInfo fi;/*from w ww . ja v a2 s .co m*/ Class type = field.getType(); String name = field.getName(); Column column = field.getAnnotation(Column.class); if (field.isAnnotationPresent(Embedded.class)) { if (field.isAnnotationPresent(AttributeOverride.class)) { fi = new FieldInfo(type, name, field.getAnnotation(AttributeOverride.class)); } else { fi = new FieldInfo(type, name, field.getAnnotation(AttributeOverrides.class)); } } else if (field.isAnnotationPresent(Enumerated.class)) { fi = new FieldInfo(type, name, true, column); } else if (column != null && !field.isAnnotationPresent(CollectionTable.class)) { if (type.isPrimitive()) { if (type.equals(Boolean.TYPE)) { type = Boolean.class; } else if (type.equals(Long.TYPE)) { type = Long.class; } else if (type.equals(Integer.TYPE)) { type = Integer.class; } else if (type.equals(Short.TYPE)) { type = Short.class; } else if (type.equals(Byte.TYPE)) { type = Byte.class; } else if (type.equals(Double.TYPE)) { type = Double.class; } else if (type.equals(Float.TYPE)) { type = Float.class; } else if (type.equals(Character.TYPE)) { type = Character.class; } } fi = new FieldInfo(type, name, false, column); } else if ((field.isAnnotationPresent(ManyToOne.class) && !field.isAnnotationPresent(JoinTable.class)) || (field.isAnnotationPresent(OneToOne.class) && StringUtils.isEmpty(field.getAnnotation(OneToOne.class).mappedBy()))) { String columnName = field.isAnnotationPresent(JoinColumn.class) ? field.getAnnotation(JoinColumn.class).name() : field.getName(); fi = getIdFieldInfo(type, name, columnName); } else if (!field.isAnnotationPresent(Transient.class) && (Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type)) && (field.isAnnotationPresent(JoinColumn.class) || field.isAnnotationPresent(JoinTable.class) || field.isAnnotationPresent(CollectionTable.class))) { fi = new FieldInfo(type, name); } else { continue; } if (field.isAnnotationPresent(Type.class)) { fi.addAnnotation(field.getAnnotation(Type.class)); } fi.setField(field); fields.add(fi); } return fields; }
From source file:Main.java
public static LinkedHashMap<String, String> convertBeans(Object bean) { if (bean == null) return null; try {/*from w w w .j a v a 2 s. c o m*/ LinkedHashMap<String, String> returnMap = new LinkedHashMap<String, String>(); Class<? extends Object> clazz = bean.getClass(); List<Field> fleids = new ArrayList<Field>(); for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) { fleids.addAll(Arrays.asList(c.getDeclaredFields())); } for (Field field : fleids) { String value = ""; field.setAccessible(true); try { Object result = field.get(bean); if (result == null) continue; value = result.toString(); } catch (IllegalAccessException e) { e.printStackTrace(); } // MLogUtil.e("field.getName() "+field.getName()); // MLogUtil.e("value "+value); returnMap.put(field.getName(), value); field.setAccessible(false); } return returnMap; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static Collection<Field> getDeepDeclaredFields(Class<?> c) { if (_reflectedFields.containsKey(c)) { return _reflectedFields.get(c); }//from w ww. j a v a2s. co m Collection<Field> fields = new ArrayList<Field>(); Class<?> curr = c; while (curr != null) { try { Field[] local = curr.getDeclaredFields(); for (Field field : local) { if (!field.isAccessible()) { try { field.setAccessible(true); } catch (Exception ignored) { } } int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers) && !field.getName().startsWith("this$") && !Modifier.isTransient(modifiers)) { fields.add(field); } } } catch (ThreadDeath t) { throw t; } catch (Throwable ignored) { } curr = curr.getSuperclass(); } _reflectedFields.put(c, fields); return fields; }
From source file:Main.java
public static Map<String, String> convertBean(Object bean) { if (bean == null) return null; try {/*ww w . j a v a 2 s . c o m*/ Class<? extends Object> clazz = bean.getClass(); Map<String, String> returnMap = new HashMap<String, String>(); List<Field> fleids = new ArrayList<Field>(); for (Class<?> c = clazz; c != Object.class; c = c.getSuperclass()) { fleids.addAll(Arrays.asList(c.getDeclaredFields())); } for (Field field : fleids) { String value = ""; field.setAccessible(true); try { Object result = field.get(bean); if (result == null) continue; value = result.toString(); } catch (IllegalAccessException e) { e.printStackTrace(); } // MLogUtil.e("field.getName() "+field.getName()); // MLogUtil.e("value "+value); returnMap.put(field.getName(), value); field.setAccessible(false); } return returnMap; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:io.github.wysohn.triggerreactor.tools.ReflectionUtil.java
/** * extract all possible field values. Primitive types will be 'copied,' and * reference types can be referenced.// w w w .java 2 s. co m * * @param e * @return */ public static Map<String, Object> extractVariables(Object e) { Map<String, Object> map = new HashMap<String, Object>(); Class<?> clazz = e.getClass(); for (Field field : getAllFields(new ArrayList<Field>(), clazz)) { field.setAccessible(true); try { map.put(field.getName(), field.get(e)); } catch (IllegalArgumentException | IllegalAccessException e1) { e1.printStackTrace(); } } return map; }
From source file:com.taobao.itest.spring.context.SpringContextManager.java
public static void registerBeanDefinition(Field field, ApplicationContext applicationContext) { @SuppressWarnings("deprecation") RootBeanDefinition beanDefinition = new RootBeanDefinition(field.getType(), true); beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME); DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) (applicationContext .getAutowireCapableBeanFactory()); defaultListableBeanFactory.registerBeanDefinition(field.getName(), beanDefinition); }
From source file:io.github.eternalbits.compactvd.CompactVD.java
private static void dump(Object obj, String in) { for (Field fld : obj.getClass().getDeclaredFields()) { try {/*from w w w . ja va 2 s .co m*/ if (!Modifier.isPrivate(fld.getModifiers())) { if (fld.getAnnotation(Deprecated.class) == null) { if (!fld.getType().isAssignableFrom(List.class)) { System.out.println(in + fld.getName() + ": " + fld.get(obj)); } else { int i = 0; for (Object item : (List<?>) fld.get(obj)) { System.out.println(in + fld.getName() + "[" + i + "]"); dump(item, in + " "); i++; } } } } } catch (IllegalArgumentException | IllegalAccessException e) { e.printStackTrace(); } } }
From source file:com.ykun.commons.utils.excel.ExcelUtils.java
/** * ???headersExcelField//from www .j a v a2s .co m * * @param <T> the type parameter * @param list ?? * @return List headers */ public static <T> List<String> getHeaders(List<T> list) { List<String> headers = new ArrayList<String>(); if (list != null && list.size() > 0) { T t = list.get(0); Field[] fields = t.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; ExcelField excelField = field.getAnnotation(ExcelField.class); if (excelField != null && !excelField.ignore()) { headers.add(excelField.value()); } else if (excelField != null && excelField.ignore()) { } else { headers.add(field.getName()); } } } return headers; }
From source file:com.ghy.common.util.reflection.ReflectionUtils.java
/** * ?string/*from w ww . ja v a2 s . c o m*/ * @param object * @param map * @throws Exception * @throws NoSuchMethodException * @throws SecurityException */ public static void fromObtoMap(Object object, Map<String, String> map) throws SecurityException, NoSuchMethodException, Exception { if (object != null) {//if (object!=null ) ----begin // Class<?> clz = object.getClass(); // ?Field Field[] fields = clz.getDeclaredFields(); for (Field field : fields) {// --for() begin // String if (field.getGenericType().toString().equals("class java.lang.String")) { // type???"class "???? // gettet String methodName = field.getName(); Method m = (Method) object.getClass().getMethod("get" + getMethodName(methodName)); String val = (String) m.invoke(object);// getter? if (val != null) { map.put(methodName, val); } } } } }