List of usage examples for java.lang.reflect Field getModifiers
public int getModifiers()
From source file:com.hortonworks.registries.common.util.ReflectionHelper.java
/** * Given a class, this method returns a map of names of all the instance (non static) fields to type. * if the class has any super class it also includes those fields. * @param clazz , not null/* w ww . j av a2 s . co m*/ * @return */ public static Map<String, Class> getFieldNamesToTypes(Class clazz) { Field[] declaredFields = clazz.getDeclaredFields(); Map<String, Class> instanceVariableNamesToTypes = new HashMap<>(); for (Field field : declaredFields) { if (!Modifier.isStatic(field.getModifiers())) { LOG.trace("clazz {} has field {} with type {}", clazz.getName(), field.getName(), field.getType().getName()); instanceVariableNamesToTypes.put(field.getName(), field.getType()); } else { LOG.trace("clazz {} has field {} with type {}, which is static so ignoring", clazz.getName(), field.getName(), field.getType().getName()); } } if (!clazz.getSuperclass().equals(Object.class)) { instanceVariableNamesToTypes.putAll(getFieldNamesToTypes(clazz.getSuperclass())); } return instanceVariableNamesToTypes; }
From source file:com.agimatec.validation.jsr303.util.SecureActions.java
private static void setAccessibility(Field field) { if (!Modifier.isPublic(field.getModifiers()) || (Modifier.isPublic(field.getModifiers()) && Modifier.isAbstract(field.getModifiers()))) { field.setAccessible(true);/*from w w w . j av a2 s . com*/ } }
From source file:de.cosmocode.palava.salesforce.sync.NullFieldCollector.java
private static void collect(Set<String> nullFields, Object object) { LOG.trace("Collecting null fields on {}", object); final Field[] fields = object.getClass().getDeclaredFields(); for (Field field : fields) { if (Modifier.isStatic(field.getModifiers())) continue; if ("fieldsToNull".equals(field.getName())) continue; try {// www . ja v a 2s . c om final boolean accessible = field.isAccessible(); field.setAccessible(true); final Object value = field.get(object); field.setAccessible(accessible); if (value == null) { continue; } else if (value instanceof JAXBElement<?>) { final JAXBElement<?> jaxb = JAXBElement.class.cast(value); if (jaxb.getValue() == null) { nullFields.add(nameOf(field)); } } } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } } LOG.trace("Null fields on {}: {}", object, nullFields); }
From source file:ch.algotrader.util.FieldUtil.java
/** * Returns all non-static / non-transient Fields including Fields defined by superclasses of the defined {@code type} *///w w w.ja va2 s . co m public static List<Field> getAllFields(Class<?> type) { List<Field> fields = new ArrayList<>(); while (true) { for (Field field : type.getDeclaredFields()) { if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) { setAccessible(field); fields.add(field); } } type = type.getSuperclass(); if (type == Object.class || type == null) { break; } } return fields; }
From source file:com.abiquo.model.util.ModelTransformer.java
public static <T> void transform(final Class sourceClass, final Class<T> targetClass, final Object source, final T target) throws Exception { Field[] transportFields = sourceClass.getDeclaredFields(); Class superClass = sourceClass.getSuperclass(); while (!superClass.getSimpleName().equalsIgnoreCase("SingleResourceTransportDto")) { transportFields = (Field[]) ArrayUtils.addAll(transportFields, superClass.getDeclaredFields()); superClass = superClass.getSuperclass(); }/*from www .j a v a 2 s .c om*/ for (Field field : transportFields) { int modifiers = field.getModifiers(); if (!Modifier.isTransient(modifiers) && !Modifier.isStatic(modifiers)) { String name = field.getName(); try { if (fieldExist(name, targetClass) && fieldExist(name, source.getClass()) || getterExist(name, source.getClass()) && setterExist(name, targetClass, field.getType())) { Object value = getter(name, source.getClass()).invoke(source, new Object[0]); if (setterExist(name, targetClass, field.getType())) { setter(name, targetClass, field.getType()).invoke(target, new Object[] { value }); } } } catch (InvocationTargetException e) { // Ignore invalid field } } } }
From source file:io.tilt.minka.utils.Defaulter.java
private static List<Field> getStaticDefaults(Class<?> clas) { final Field[] declaredFields = clas.getDeclaredFields(); final List<Field> staticFields = new ArrayList<Field>(); for (Field field : declaredFields) { if (Modifier.isStatic(field.getModifiers())) { staticFields.add(field);/* w w w .ja v a 2 s . co m*/ } } return staticFields; }
From source file:com.xhsoft.framework.common.utils.ReflectUtil.java
/** * <p>Description:setFieldValue</p> * @param target/* w w w . j a va 2 s. com*/ * @param fname * @param ftype * @param fvalue * @return void */ @SuppressWarnings("unchecked") public static void setFieldValue(Object target, String fname, Class ftype, Object fvalue) { if (target == null || fname == null || "".equals(fname) || (fvalue != null && !ftype.isAssignableFrom(fvalue.getClass()))) { return; } Class clazz = target.getClass(); try { Method method = clazz .getDeclaredMethod("set" + Character.toUpperCase(fname.charAt(0)) + fname.substring(1), ftype); if (!Modifier.isPublic(method.getModifiers())) { method.setAccessible(true); } method.invoke(target, fvalue); } catch (Exception me) { try { Field field = clazz.getDeclaredField(fname); if (!Modifier.isPublic(field.getModifiers())) { field.setAccessible(true); } field.set(target, fvalue); } catch (Exception fe) { if (logger.isDebugEnabled()) { logger.debug(fe); } } } }
From source file:com.taobao.adfs.database.tdhsocket.client.protocol.TDHSProtocolBinary.java
private static void encodeRequest(Request o, ByteArrayOutputStream out, String charestName) throws IllegalAccessException, IOException, TDHSEncodeException { for (Field f : o.getClass().getDeclaredFields()) { if (!Modifier.isPublic(f.getModifiers())) { f.setAccessible(true);/*from w ww. j a v a2 s. c o m*/ } Object v = f.get(o); if (v instanceof Request) { encodeRequest((Request) v, out, charestName); } else if (f.getName().startsWith("_")) { writeObjectToStream(v, out, f.getName(), charestName); } } }
From source file:edu.temple.cis3238.wiki.parser.TagsFromContentParser.java
/** * * @param f//from ww w. j av a 2 s.co m * @return */ public static boolean isStaticField(Field f) { return Modifier.isStatic(f.getModifiers()); }
From source file:net.buffalo.protocal.util.ClassUtil.java
private static HashMap getFieldMap(Class cl) { HashMap fieldMap = new HashMap(); for (; cl != null; cl = cl.getSuperclass()) { Field[] fields = cl.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (Modifier.isTransient(field.getModifiers()) || Modifier.isStatic(field.getModifiers())) continue; field.setAccessible(true);//www . j a v a 2 s .c o m fieldMap.put(field.getName(), field); } } return fieldMap; }