List of usage examples for java.lang.reflect Field get
@CallerSensitive @ForceInline public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:com.antonjohansson.elasticsearchshell.utils.ReflectionUtils.java
/** * Gets the value of a field of an object. * * @param clazz The class of the object to get field value from. * @param object The object to get field value from. * @param fieldName The name of the field to get value from. * @return Returns the field value./* w ww . j av a 2s . c o m*/ */ @SuppressWarnings("unchecked") public static <V, T> V getFieldValue(Class<T> clazz, T object, String fieldName) { Field field = FieldUtils.getField(clazz, fieldName, true); try { return (V) field.get(object); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
private static String getValue(String packageName, String fieldName) { String buildConfigClassName = packageName + ".BuildConfig"; try {//from www. j ava 2 s . c o m Class<?> buildConfigClass = Class.forName(buildConfigClassName); Field field = buildConfigClass.getField(fieldName); return (String) field.get(null); } catch (Exception e) { throw new RuntimeException( String.format("%s.%s not set or not accessible", buildConfigClassName, fieldName), e); } }
From source file:Main.java
@SuppressWarnings("unchecked") public static <E> E getProperty(Object object, String fieldName) { Class<?> clazz = object.getClass(); while (clazz != null) { try {//from w w w . ja v a 2 s . c o m Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return (E) field.get(object); } catch (NoSuchFieldException e) { clazz = clazz.getSuperclass(); } catch (Exception e) { throw new IllegalStateException(e); } } return null; }
From source file:Main.java
/********************************************************************************* * Returns the resource-IDs for all attributes specified in the given * <declare-styleable>-resource tag as an int array. * * @param context/* w w w .j a v a2s.c om*/ * The current application context. * @param name * The name of the <declare-styleable>-resource-tag to pick. * @return All resource-IDs of the child-attributes for the given * <declare-styleable>-resource or <code>null</code> if this tag * could not be found or an error occured. *********************************************************************************/ public static final int[] getResourceDeclareStyleableIntArray(Context context, String name) { try { Field[] fields2 = Class.forName(context.getPackageName() + ".R$styleable").getFields(); for (Field f : fields2) { if (f.getName().equals(name)) { int[] ret = (int[]) f.get(null); return ret; } } } catch (Throwable t) { } return null; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T getFieldValue(Object object, Field field) { setAccessibleIfNeeded(field);/*from ww w .j a va2s . com*/ try { return (T) field.get(object); } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String getChannelNum(Context context) { Resources resources = context.getResources(); try {// w w w . j a v a2 s .c om Class<?> className = Class.forName( (new StringBuilder(String.valueOf(context.getPackageName()))).append(".R$raw").toString()); Field field = className.getField("parent"); Integer result = (Integer) field.get(className); InputStream num = resources.openRawResource(result); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i = 1; try { while ((i = num.read()) != -1) { baos.write(i); } } catch (IOException e) { e.printStackTrace(); } finally { resources = null; if (num != null) { try { num.close(); } catch (IOException e) { e.printStackTrace(); } } } return new String(baos.toByteArray()); } catch (Exception e) { } return "0"; }
From source file:Main.java
@SuppressWarnings("unchecked") public static Object searchProperty(Object leftParameter, String name) throws Exception { Class<?> leftClass = leftParameter.getClass(); Object result;/*from w w w .ja va2 s . c o m*/ if (leftParameter.getClass().isArray() && "length".equals(name)) { result = Array.getLength(leftParameter); } else if (leftParameter instanceof Map) { result = ((Map<Object, Object>) leftParameter).get(name); } else { try { String getter = "get" + name.substring(0, 1).toUpperCase() + name.substring(1); Method method = leftClass.getMethod(getter, new Class<?>[0]); if (!method.isAccessible()) { method.setAccessible(true); } result = method.invoke(leftParameter, new Object[0]); } catch (NoSuchMethodException e2) { try { String getter = "is" + name.substring(0, 1).toUpperCase() + name.substring(1); Method method = leftClass.getMethod(getter, new Class<?>[0]); if (!method.isAccessible()) { method.setAccessible(true); } result = method.invoke(leftParameter, new Object[0]); } catch (NoSuchMethodException e3) { Field field = leftClass.getField(name); result = field.get(leftParameter); } } } return result; }
From source file:ReflectionHelper.java
public static Object getValue(Member member, Object object) { Object value = null;/*from www .j a va 2 s.co m*/ if (member instanceof Method) { Method method = (Method) member; try { value = method.invoke(object); } catch (IllegalAccessException e) { throw new RuntimeException("Unable to access " + method.getName(), e); } catch (InvocationTargetException e) { throw new RuntimeException("Unable to access " + method.getName(), e); } } else if (member instanceof Field) { Field field = (Field) member; try { value = field.get(object); } catch (IllegalAccessException e) { throw new RuntimeException("Unable to access " + field.getName(), e); } } return value; }
From source file:Main.java
public static Object getProperty(Object classObject, String paramString) { try {//www . jav a2 s .c o m Field localField = classObject.getClass().getDeclaredField(paramString); localField.setAccessible(true); return localField.get(classObject); } catch (Exception var3) { return null; } }
From source file:springobjectmapper.ReflectionHelper.java
public static Object getFieldValue(Field field, Object o) { try {/* w ww .ja v a 2 s .com*/ return field.get(o); } catch (Exception e) { ReflectionUtils.handleReflectionException(e); return null; } }