List of usage examples for java.lang Class getDeclaredFields
@CallerSensitive public Field[] getDeclaredFields() throws SecurityException
From source file:net.minecraftforge.fml.relauncher.ReflectionHelper.java
public static <T, E> void setPrivateValue(Class<? super T> classToAccess, T instance, E value, int fieldIndex) { try {/* w ww. jav a 2 s .c om*/ Field f = classToAccess.getDeclaredFields()[fieldIndex]; f.setAccessible(true); f.set(instance, value); } catch (Exception e) { throw new UnableToAccessFieldException(new String[0], e); } }
From source file:net.minecraftforge.fml.relauncher.ReflectionHelper.java
@SuppressWarnings("unchecked") public static <T, E> T getPrivateValue(Class<? super E> classToAccess, @Nullable E instance, int fieldIndex) { try {// w ww.j a va 2s . co m Field f = classToAccess.getDeclaredFields()[fieldIndex]; f.setAccessible(true); return (T) f.get(instance); } catch (Exception e) { throw new UnableToAccessFieldException(new String[0], e); } }
From source file:com.frand.easyandroid.db.sql.FFSqlBuilder.java
public static FFArrayList getFieldsAndValue(Object entity) throws FFDBException, IllegalArgumentException, IllegalAccessException { FFArrayList arrayList = new FFArrayList(); if (entity == null) { throw new FFDBException("?"); }/*from w w w . j a va2 s.c o m*/ Class<?> clazz = entity.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (!FFDBUtils.isTransient(field) && FFFieldUtil.isBaseDateType(field)) { FFPrimaryKey annotation = field.getAnnotation(FFPrimaryKey.class); if (annotation != null && annotation.autoIncrement()) { } else { String columnName = FFDBUtils.getColumnByField(field); field.setAccessible(true); if (columnName == null || columnName.equals("")) { columnName = field.getName(); } String value = ""; if (field.getType().equals(Date.class)) { value = field.get(entity) != null ? FFDBUtils.dateToString((Date) field.get(entity)) : ""; } else { value = field.get(entity) != null ? field.get(entity).toString() : ""; } arrayList.add(columnName, value); } } } return arrayList; }
From source file:br.com.bea.androidtools.api.model.EntityUtils.java
public static final <E extends Entity<?>> List<Field> columnFields(final Class<E> targetClass) { if (null == EntityUtils.columnsCache.get(targetClass.getName().hashCode())) { final List<Field> fields = new LinkedList<Field>(); for (final Field field : targetClass.getDeclaredFields()) if (field.isAnnotationPresent(Column.class)) fields.add(field);/*from w ww.ja va2 s . c om*/ EntityUtils.columnsCache.put(targetClass.getName().hashCode(), fields); } return EntityUtils.columnsCache.get(targetClass.getName().hashCode()); }
From source file:org.fastmongo.odm.util.ReflectionUtils.java
/** * Attempt to find a {@link Field field} on the supplied {@link Class} with the * supplied {@code name} and/or {@link Class type}. Searches all superclasses * up to {@link Object}./*from ww w. j a v a2 s .co m*/ * * @param clazz the class to introspect * @param name the name of the field (may be {@code null} if type is specified) * @param type the type of the field (may be {@code null} if name is specified) * @return the corresponding Field object, or {@code null} if not found */ public static Field findField(Class<?> clazz, String name, Class<?> type) { Class<?> searchType = clazz; while (!Object.class.equals(searchType) && searchType != null) { Field[] fields = searchType.getDeclaredFields(); for (Field field : fields) { if ((name == null || name.equals(field.getName())) && (type == null || type.equals(field.getType()))) { return field; } } searchType = searchType.getSuperclass(); } return null; }
From source file:org.LexGrid.LexBIG.caCore.utils.LexEVSCaCoreUtils.java
public static Object setFieldValue(Object input, Object value, String fieldName) throws Exception { Class searchClass = input.getClass(); while (searchClass != null) { Field[] fields = searchClass.getDeclaredFields(); for (Field field : fields) { if (field.getName().equals(fieldName)) { field.setAccessible(true); //Check to see if we're trying to set a int, long, etc with a String if (field.getType().getName().equals("java.lang.Long")) { if (value instanceof String) { field.set(input, Long.getLong((String) value)); } else { field.set(input, value); }//www. j av a2 s . c om } else if (field.getType().getName().equals("java.lang.Integer")) { if (value instanceof String) { field.set(input, Integer.getInteger((String) value)); } else { field.set(input, value); } } else { field.set(input, value); } } } searchClass = searchClass.getSuperclass(); } return input; }
From source file:br.com.bea.androidtools.api.model.EntityUtils.java
public static final <E extends ValueObject> List<Field> metadataFields(final Class<E> targetClass) { if (null == EntityUtils.metadatasCache.get(targetClass.getName().hashCode())) { final List<Field> fields = new LinkedList<Field>(); for (final Field field : targetClass.getDeclaredFields()) if (field.isAnnotationPresent(Metadata.class)) fields.add(field);//w w w.j a v a 2 s .com EntityUtils.metadatasCache.put(targetClass.getName().hashCode(), fields); } return EntityUtils.metadatasCache.get(targetClass.getName().hashCode()); }
From source file:Main.java
public static <S, T> T castObject(S source, Class<T> targetClass) throws IllegalAccessException, InstantiationException { T newInstance = targetClass.newInstance(); for (Field field : source.getClass().getDeclaredFields()) { for (Field fieldTarget : targetClass.getDeclaredFields()) { if (isFieldsEqual(field, fieldTarget)) { setField(getField(field, source), fieldTarget, newInstance); }/* www .jav a 2 s. c o m*/ } } return newInstance; }
From source file:com.tridion.storage.si4t.JPASearchDAOFactory.java
private static Field getPrivateFieldRec(final Class<?> clazz, final String fieldName, Logger log) { for (Field field : clazz.getDeclaredFields()) { if (fieldName.equals(field.getName())) { return field; }// w w w .j a v a 2s . c o m } final Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null) { return getPrivateFieldRec(superClazz, fieldName, log); } return null; }
From source file:edu.mayo.cts2.framework.webapp.rest.view.jsp.Beans.java
/** * Inspect./* w ww .j a v a 2 s . c o m*/ * * @param bean the bean * @return the list */ public static List<Map.Entry<String, Object>> inspect(Object bean) { Map<String, Object> props = new LinkedHashMap<String, Object>(); Class<?> clazz = bean.getClass(); while (clazz != null) { for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); String name = field.getName(); Object value; try { value = field.get(bean); } catch (Exception e) { throw new RuntimeException(e); } if (value != null) { props.put(name, value); } } clazz = clazz.getSuperclass(); } List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>(props.entrySet()); Collections.sort(list, BEAN_COMPARATOR); return list; }