List of usage examples for java.lang Class getField
@CallerSensitive public Field getField(String name) throws NoSuchFieldException, SecurityException
From source file:Main.java
/** * Check application's R.id class and retrieve the value of the static * member with the given name.//w w w. ja v a 2 s. c o m */ public static int getIdentifierFromR(Context context, String type, String name) { Class<?> rClass = null; try { rClass = Class.forName(context.getPackageName() + ".R$" + type); } catch (ClassNotFoundException e) { // No R.id class? This should never happen. throw new RuntimeException(e); } try { Field rField = rClass.getField(name); Object intValue; try { intValue = rField.get(null); } catch (IllegalAccessException e) { throw new RuntimeException(e); } if (!(intValue instanceof Integer)) { throw new RuntimeException("Not an int: " + rClass.getCanonicalName() + "." + rField.getName()); } return ((Integer) intValue).intValue(); } catch (NoSuchFieldException e) { throw new RuntimeException("There is no such id in the R class: " + context.getPackageName() + ".R." + type + "." + name + ")"); } catch (SecurityException e) { throw new RuntimeException(e); } }
From source file:com.l2jfree.network.mmocore.packethandlers.PacketDefinition.java
public static Integer findOpcode(Class<?> clazz, String fieldName, boolean required) throws Exception { // easier than checking all the modifiers, visibility, type, etc :) final Field field; try {/* ww w .j a va2s. c o m*/ field = clazz.getField(fieldName); } catch (NoSuchFieldException e) { if (required) { System.err.println("Missing 'public static final int " + fieldName + "'!"); throw e; } else return null; // ignore as it was expected } return field.getInt(null); }
From source file:jp.co.acroquest.endosnipe.perfdoctor.rule.RuleInstanceUtil.java
/** * ??//from w w w . ja va 2s.c o m * @param obj ? * @param fieldName ?? * @param value * @throws RuleCreateException ??????? */ protected static void setValue(final Object obj, final String fieldName, final String value) throws RuleCreateException { Class<? extends Object> clazz = obj.getClass(); Object[] args = new Object[] { clazz.getCanonicalName(), fieldName, value }; try { Field field = clazz.getField(fieldName); Object convertedValue = ConvertUtils.convert(value, field.getType()); field.set(obj, convertedValue); } catch (NoSuchFieldException ex) { throw new RuleCreateException(PerfConstants.PROPERTY_NOT_FOUND, args); } catch (SecurityException ex) { throw new RuleCreateException(PerfConstants.PROPERTY_ERROR, args); } catch (IllegalArgumentException ex) { throw new RuleCreateException(PerfConstants.PROPERTY_TYPE_ERROR, args); } catch (IllegalAccessException ex) { throw new RuleCreateException(PerfConstants.PROPERTY_ACCESS_ERROR, args); } }
From source file:com.cleanwiz.applock.ui.BaseActivity.java
private static int getStatusBarHeight(Context context) { Class<?> c = null; Object obj = null;/* w ww .ja v a 2 s .c o m*/ Field field = null; int x = 0, statusBarHeight = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); statusBarHeight = context.getResources().getDimensionPixelSize(x); } catch (Exception e1) { e1.printStackTrace(); } return statusBarHeight; }
From source file:net.sf.jvifm.ResourceManager.java
@SuppressWarnings("unchecked") public static Font getFont(String name, int size, int style, boolean strikeout, boolean underline) { String fontName = name + "|" + size + "|" + style + "|" + strikeout + "|" + underline; if (resources.containsKey(fontName)) return (Font) resources.get(fontName); FontData fd = new FontData(name, size, style); if (strikeout || underline) { try {//from w w w . j a v a 2s.co m Class lfCls = Class.forName("org.eclipse.swt.internal.win32.LOGFONT"); Object lf = FontData.class.getField("data").get(fd); if (lf != null && lfCls != null) { if (strikeout) lfCls.getField("lfStrikeOut").set(lf, new Byte((byte) 1)); if (underline) lfCls.getField("lfUnderline").set(lf, new Byte((byte) 1)); } } catch (Throwable e) { System.err.println( "Unable to set underline or strikeout" + " (probably on a non-Windows platform). " + e); } } Font font = new Font(Display.getDefault(), fd); resources.put(fontName, font); return font; }
From source file:com.feilong.commons.core.lang.reflect.FieldUtil.java
/** * ./* w ww . jav a2 s . c o m*/ * * @param owner * the owner * @param fieldName * * @param value * * @throws ReflectException * the reflect exception * @see java.lang.Object#getClass() * @see java.lang.Class#getField(String) * @see java.lang.reflect.Field#set(Object, Object) */ public static void setProperty(Object owner, String fieldName, Object value) throws ReflectException { try { Class<?> ownerClass = owner.getClass(); Field field = ownerClass.getField(fieldName); field.set(ownerClass, value); } catch (Exception e) { log.error(e.getClass().getName(), e); throw new ReflectException(e); } }
From source file:com.feilong.commons.core.lang.reflect.FieldUtil.java
/** * ?./*from w w w . j a v a 2s.co m*/ * * @param <T> * the generic type * @param owner * the owner * @param fieldName * the field name * @return * @throws ReflectException * the reflect exception * * @see java.lang.Object#getClass() * @see java.lang.Class#getField(String) * @see java.lang.reflect.Field#get(Object) */ @SuppressWarnings("unchecked") public static <T> T getProperty(Object owner, String fieldName) throws ReflectException { try { Class<?> ownerClass = owner.getClass(); Field field = ownerClass.getField(fieldName); Object property = field.get(owner); return (T) property; } catch (Exception e) { log.error(e.getClass().getName(), e); throw new ReflectException(e); } }
From source file:org.talend.core.utils.ReflectionUtils.java
/** * DOC ycbai Comment method "getPublicField". * //from w w w .j av a 2s . com * Returns the value of a public field. * * @param owner * @param fieldName * @return * @throws NoSuchFieldException * @throws SecurityException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws Exception */ public static Object getPublicField(Object owner, String fieldName) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Object fieldValue = null; Class ownerClass = owner.getClass(); Field field = ownerClass.getField(fieldName); fieldValue = field.get(owner); return fieldValue; }
From source file:Main.java
public static void mapToAndroidLayoutMapper(Class classObj, Map map, String prefixStr, Activity view) { if (map == null) { return;// w ww . j ava 2 s. com } for (Object keyObj : map.keySet().toArray()) { String keyStr = keyObj.toString(); Field fieldObj = null; try { fieldObj = classObj.getField(prefixStr + "_" + keyStr); } catch (NoSuchFieldException e) { continue; } Object layoutObj = null; try { layoutObj = fieldObj.get(fieldObj); } catch (IllegalAccessException e) { continue; } Integer layoutIntvalue = (Integer) layoutObj; View selectedView = view.findViewById(layoutIntvalue); if (selectedView instanceof TextView) { TextView textView = (TextView) selectedView; textView.setText((String) map.get(keyStr)); } else if (selectedView instanceof ImageView) { ImageView imageView = (ImageView) selectedView; } } }
From source file:com.crazy.pss.common.utils.Reflections.java
public static boolean hasAttribute(final String attrName, final Class clazz) { Field f = null;//from w w w . j a v a2 s . c om try { f = clazz.getField(attrName); } catch (SecurityException e) { if (logger.isErrorEnabled()) { logger.error("?Class=" + clazz + "" + attrName + "", e.getCause()); } return false; } catch (NoSuchFieldException e) { if (logger.isErrorEnabled()) { logger.error("?Class=" + clazz + "" + attrName + "", e.getCause()); } return false; } return f == null ? false : true; }