List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
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} *//*from www. j a va2 s. com*/ 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:io.github.pellse.decorator.util.reflection.ReflectionUtils.java
public static void copyFields(Object src, Object target) { Class<?> targetClass = target.getClass(); do {//from w w w.j ava2 s .co m stream(targetClass.getDeclaredFields()) .forEach(field -> CheckedRunnable.of(() -> field.set(target, field.get(src))).run()); targetClass = targetClass.getSuperclass(); } while (targetClass != null && targetClass != Object.class); }
From source file:com.francetelecom.clara.cloud.presentation.utils.GetObjectsUtils.java
public static Field[] getAllFields(Class objectClass) { if (objectClass.getSuperclass() == null) { return null; }/*from w ww. j a va2s . co m*/ return (Field[]) ArrayUtils.addAll(objectClass.getDeclaredFields(), getAllFields(objectClass.getSuperclass())); }
From source file:com.edmunds.autotest.ClassUtil.java
public static Map<String, Field> getAllDeclaredFieldsMap(Class originalCls, boolean lowercase, AutoTestConfig config) {//w w w .java2 s. c o m Map<String, Field> fields = new HashMap<String, Field>(); Class cls = originalCls; do { for (Field field : cls.getDeclaredFields()) { String fieldName = field.getName(); if (lowercase) { fieldName = fieldName.toLowerCase(); } if (fields.containsKey(fieldName)) { if (isDeclaredUnderRootPackage(config, field) && !config.getFieldOverrideExceptions().contains(field.getName())) { String msg = "Instance variable (" + field.getName() + ") has been overridden: " + originalCls.getName(); log.warn(msg); if (config.isFailOnFieldOverride()) { fail(msg); } } } else { fields.put(fieldName, field); } } cls = cls.getSuperclass(); } while (cls != null); return fields; }
From source file:com.hortonworks.minicluster.util.ReflectionUtils.java
/** * * @param clazz// ww w . j av a 2 s . c om * @param fieldName * @return */ public static Field getFieldAndMakeAccessible(Class<?> clazz, String fieldName) { ObjectAssertUtils.assertNotNull(clazz); StringAssertUtils.assertNotEmptyAndNoSpaces(fieldName); Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if (fieldName == null || fieldName.equals(field.getName())) { field.setAccessible(true); return field; } } searchType = searchType.getSuperclass(); } return null; }
From source file:jfix.util.Reflections.java
/** * Initializes all declared static string fields in given class with name of * fields.//from w ww. jav a 2 s . c om */ public static void init(Class<?> clazz) { for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); try { field.set(null, field.getName()); } catch (Exception e) { } } }
From source file:org.wso2.carbon.ganalytics.publisher.GoogleAnalyticsDataPublisher.java
/** * Use this method to retireve a payload a list of NameValuePair. * @param o a GoogleAnalyticsData object * @return list of NamevaluePairs to be used in httpclient entity *//*from w w w . jav a 2 s .c o m*/ public static List<NameValuePair> buildPayload(Object o) { List<NameValuePair> payload = new ArrayList<NameValuePair>(); Class<?> clazz = o.getClass(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); String fieldValue = getFieldValue(fields[i], o); fields[i].setAccessible(false); if (fieldValue == null) { continue; } NameValuePair nvp = new BasicNameValuePair( GoogleAnalyticsConstants.METHOD_TO_PARAM_MAP.get(fields[i].getName()), fieldValue); payload.add(nvp); } return payload; }
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 {/*from w ww . j av a 2 s .co 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.wso2.carbon.ganalytics.publisher.GoogleAnalyticsDataPublisher.java
/** * Use this method to retrieve a payload as string. * @param o a GoogleAnalyticsData object * @return query param string//from ww w . ja v a 2s .c o m */ public static String buildPayloadString(Object o) { String queryString = ""; Class<?> clazz = o.getClass(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); String fieldValue = getFieldValue(fields[i], o); fields[i].setAccessible(false); if (fieldValue == null) { continue; } queryString = queryString + encodeString(GoogleAnalyticsConstants.METHOD_TO_PARAM_MAP.get(fields[i].getName())) + "=" + encodeString(fieldValue) + "&"; } queryString = queryString.substring(0, queryString.length() - 1); return queryString; }
From source file:com.yimidida.shards.utils.ParameterUtil.java
public static Serializable extractPrimaryKey(Object object) { if (object != null) { Class<?> clazz = object.getClass(); Field[] first = clazz.getDeclaredFields(); Field[] second = clazz.getSuperclass().getDeclaredFields(); Field[] fields = Arrays.copyOf(first, first.length + second.length); System.arraycopy(second, 0, fields, first.length, second.length); for (Field field : fields) { field.setAccessible(true);//from w w w .j av a 2 s . c om PrimaryKey primaryKey = field.getAnnotation(PrimaryKey.class); if (primaryKey != null) { try { //0 Object result = field.get(object); if (result != null && "0".equals(result.toString())) { return null; } return (Serializable) result; } catch (Exception e) { e.printStackTrace(); } } } } return null; }