List of usage examples for java.lang.reflect Field getModifiers
public int getModifiers()
From source file:com.taobao.tdhs.client.protocol.TDHSProtocolBinary.java
protected static void encodeRequest(Request o, ByteArrayOutputStream out, String charsetName) throws IllegalAccessException, IOException, TDHSEncodeException { for (Field f : o.getClass().getDeclaredFields()) { if (!Modifier.isPublic(f.getModifiers())) { f.setAccessible(true);/*from w w w. ja v a 2 s . c om*/ } Object v = f.get(o); if (v instanceof Request) { encodeRequest((Request) v, out, charsetName); } else if (f.getName().startsWith("_")) { writeObjectToStream(v, out, f.getName(), charsetName); } } }
From source file:com.ejisto.modules.web.util.FieldSerializationUtil.java
public static List<MockedField> translateObject(Object object, String containerClassName, String fieldName, String contextPath) {//from w w w . jav a 2 s. c o m if (object == null) { return emptyList(); } if (isBlackListed(object.getClass())) { String fieldType = object.getClass().getName(); MockedField out = buildMockedField(containerClassName, fieldName, contextPath, fieldType); return asList(out); } DefaultSupportedType type = DefaultSupportedType.evaluate(object); if (type != null && type.isPrimitiveOrSimpleValue()) { return asList(getSingleFieldFromDefaultSupportedType(object, type, contextPath, containerClassName, fieldName)); } List<MockedField> out = new ArrayList<MockedField>(); Class<?> objectClass = object.getClass(); String className = objectClass.getName(); for (Field field : objectClass.getDeclaredFields()) { if (!Modifier.isTransient(field.getModifiers()) && !isBlackListed(field.getType())) { out.add(translateField(field, object, className, contextPath)); } } return out; }
From source file:ClassTree.java
/** * Returns a description of the fields of a class. * @param the class to be described/*from ww w . ja v a2s. co m*/ * @return a string containing all field types and names */ public static String getFieldDescription(Class<?> c) { // use reflection to find types and names of fields StringBuilder r = new StringBuilder(); Field[] fields = c.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; if ((f.getModifiers() & Modifier.STATIC) != 0) r.append("static "); r.append(f.getType().getName()); r.append(" "); r.append(f.getName()); r.append("\n"); } return r.toString(); }
From source file:com.googlecode.xtecuannet.framework.catalina.manager.tomcat.constants.Constants.java
public static List<Field> getOnlyFields(Class clazz) { List<Field> salida = new ArrayList<Field>(0); Field[] all = clazz.getDeclaredFields(); for (int i = 0; i < all.length; i++) { Field field = all[i]; logger.debug(Modifier.toString(field.getModifiers()) + "-" + field.getModifiers()); if (field.getModifiers() == Modifier.PRIVATE) { salida.add(field);// w w w .j a v a 2 s . co m } } return salida; }
From source file:Main.java
public static int hashCode(Object target) { if (target == null) return 0; final int prime = 31; int result = 1; Class<?> current = target.getClass(); do {/*w ww .j a v a2 s . c o m*/ for (Field f : current.getDeclaredFields()) { if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers()) || f.isSynthetic()) { continue; } Object self; try { f.setAccessible(true); self = f.get(target); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } if (self == null) { result = prime * result + 0; } else if (self.getClass().isArray()) { if (self.getClass().equals(boolean[].class)) { result = prime * result + Arrays.hashCode((boolean[]) self); } else if (self.getClass().equals(char[].class)) { result = prime * result + Arrays.hashCode((char[]) self); } else if (self.getClass().equals(byte[].class)) { result = prime * result + Arrays.hashCode((byte[]) self); } else if (self.getClass().equals(short[].class)) { result = prime * result + Arrays.hashCode((short[]) self); } else if (self.getClass().equals(int[].class)) { result = prime * result + Arrays.hashCode((int[]) self); } else if (self.getClass().equals(long[].class)) { result = prime * result + Arrays.hashCode((long[]) self); } else if (self.getClass().equals(float[].class)) { result = prime * result + Arrays.hashCode((float[]) self); } else if (self.getClass().equals(double[].class)) { result = prime * result + Arrays.hashCode((double[]) self); } else { result = prime * result + Arrays.hashCode((Object[]) self); } } else { result = prime * result + self.hashCode(); } System.out.println(f.getName() + ": " + result); } current = current.getSuperclass(); } while (!Object.class.equals(current)); return result; }
From source file:org.springmodules.cache.util.Reflections.java
/** * <p>// w w w .j a v a 2 s . c o m * This method uses reflection to build a valid hash code. * </p> * * <p> * It uses <code>AccessibleObject.setAccessible</code> to gain access to * private fields. This means that it will throw a security exception if run * under a security manager, if the permissions are not set up correctly. It * is also not as efficient as testing explicitly. * </p> * * <p> * Transient members will not be used, as they are likely derived fields, * and not part of the value of the <code>Object</code>. * </p> * * <p> * Static fields will not be tested. Superclass fields will be included. * </p> * * @param obj * the object to create a <code>hashCode</code> for * @return the generated hash code, or zero if the given object is * <code>null</code> */ public static int reflectionHashCode(Object obj) { if (obj == null) return 0; Class targetClass = obj.getClass(); if (Objects.isArrayOfPrimitives(obj) || Objects.isPrimitiveOrWrapper(targetClass)) { return Objects.nullSafeHashCode(obj); } if (targetClass.isArray()) { return reflectionHashCode((Object[]) obj); } if (obj instanceof Collection) { return reflectionHashCode((Collection) obj); } if (obj instanceof Map) { return reflectionHashCode((Map) obj); } // determine whether the object's class declares hashCode() or has a // superClass other than java.lang.Object that declares hashCode() Class clazz = (obj instanceof Class) ? (Class) obj : obj.getClass(); Method hashCodeMethod = ReflectionUtils.findMethod(clazz, "hashCode", new Class[0]); if (hashCodeMethod != null) { return obj.hashCode(); } // could not find a hashCode other than the one declared by java.lang.Object int hash = INITIAL_HASH; try { while (targetClass != null) { Field[] fields = targetClass.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; int modifiers = field.getModifiers(); if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) { hash = MULTIPLIER * hash + reflectionHashCode(field.get(obj)); } } targetClass = targetClass.getSuperclass(); } } catch (IllegalAccessException exception) { // ///CLOVER:OFF ReflectionUtils.handleReflectionException(exception); // ///CLOVER:ON } return hash; }
From source file:ReflectionTest.java
/** * Prints all fields of a class//from www . j a v a 2 s . com * @param cl a class */ public static void printFields(Class cl) { Field[] fields = cl.getDeclaredFields(); for (Field f : fields) { Class type = f.getType(); String name = f.getName(); System.out.print(" "); String modifiers = Modifier.toString(f.getModifiers()); if (modifiers.length() > 0) System.out.print(modifiers + " "); System.out.println(type.getName() + " " + name + ";"); } }
From source file:Main.java
public static boolean equals(Object target, Object o) { if (target == o) return true; if (target == null || o == null) return false; if (!target.getClass().equals(o.getClass())) return false; Class<?> current = target.getClass(); do {//from w w w.ja v a 2 s.com for (Field f : current.getDeclaredFields()) { if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers()) || f.isSynthetic()) { continue; } Object self; Object other; try { f.setAccessible(true); self = f.get(target); other = f.get(o); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } if (self == null) { if (other != null) return false; } else if (self.getClass().isArray()) { if (self.getClass().equals(boolean[].class)) { if (!Arrays.equals((boolean[]) self, (boolean[]) other)) return false; } else if (self.getClass().equals(char[].class)) { if (!Arrays.equals((char[]) self, (char[]) other)) return false; } else if (self.getClass().equals(byte[].class)) { if (!Arrays.equals((byte[]) self, (byte[]) other)) return false; } else if (self.getClass().equals(short[].class)) { if (!Arrays.equals((short[]) self, (short[]) other)) return false; } else if (self.getClass().equals(int[].class)) { if (!Arrays.equals((int[]) self, (int[]) other)) return false; } else if (self.getClass().equals(long[].class)) { if (!Arrays.equals((long[]) self, (long[]) other)) return false; } else if (self.getClass().equals(float[].class)) { if (!Arrays.equals((float[]) self, (float[]) other)) return false; } else if (self.getClass().equals(double[].class)) { if (!Arrays.equals((double[]) self, (double[]) other)) return false; } else { if (!Arrays.equals((Object[]) self, (Object[]) other)) return false; } } else if (!self.equals(other)) { return false; } } current = current.getSuperclass(); } while (!Object.class.equals(current)); return true; }
From source file:com.yahoo.egads.data.JsonEncoder.java
public static void // modifies json_out toJson(Object object, JSONStringer json_out) throws Exception { json_out.object();/*from ww w .j a v a2 s.com*/ // for each inherited class... for (Class c = object.getClass(); c != Object.class; c = c.getSuperclass()) { // for each member variable... Field[] fields = c.getDeclaredFields(); for (Field f : fields) { // if variable is static/private... skip it if (Modifier.isStatic(f.getModifiers())) { continue; } if (Modifier.isPrivate(f.getModifiers())) { continue; } Object value = f.get(object); // if variable is a complex type... recurse on sub-objects if (value instanceof JsonAble) { json_out.key(f.getName()); ((JsonAble) value).toJson(json_out); // if variable is an array... recurse on sub-objects } else if (value instanceof ArrayList) { json_out.key(f.getName()); json_out.array(); for (Object e : (ArrayList) value) { toJson(e, json_out); } json_out.endArray(); // if variable is a simple type... convert to json } else { json_out.key(f.getName()).value(value); } } } json_out.endObject(); }
From source file:Main.java
public static String[] getClassStaticFieldNames(Class c, Type fieldType, String nameContains) { Field[] fields = c.getDeclaredFields(); List<String> list = new ArrayList<>(); for (Field field : fields) { try {//from ww w . j a v a 2 s . c o m boolean isString = field.getType().equals(fieldType); boolean containsExtra = field.getName().contains(nameContains); boolean isStatic = Modifier.isStatic(field.getModifiers()); if (field.getType().equals(String.class) && field.getName().contains("EXTRA_") && Modifier.isStatic(field.getModifiers())) list.add(String.valueOf(field.get(null))); } catch (IllegalAccessException iae) { Log.d(TAG, "!!!!!!!!!!!! class Static field, illegal access exception message: " + iae.getMessage()); } } return list.toArray(new String[list.size()]); }