List of usage examples for java.lang.reflect Field get
@CallerSensitive @ForceInline public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:de.cbb.mplayer.util.ReflectionHelper.java
/** * Utility method to read a field value. If the field is not accessible, it will be set to be accessible. * @param object Instance in which the value should be read * @param name Name of the field who's value should be read * @return The value of the field//from w w w.j a v a 2 s. c o m */ public static Object getFieldValue(Object object, String name) { try { Field field = getField(object.getClass(), name); if (!field.isAccessible()) { field.setAccessible(true); } return field.get(object); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { throw new RuntimeException( "Could not read field value: " + object.getClass().getSimpleName() + "." + name, e); //$NON-NLS-1$ //$NON-NLS-2$ } }
From source file:io.github.pellse.decorator.util.reflection.ReflectionUtils.java
public static Optional<Object> getField(Object obj, Field field) { return Optional.ofNullable(CheckedSupplier.of(() -> { field.setAccessible(true);/*from w w w.ja va2 s .co m*/ return field.get(obj); }).get()); }
From source file:Main.java
public static int getStatusBarHeight(Context context) { Class<?> c;//from w w w . ja va 2 s. co m Object obj; Field field; int x, 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; /** * Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; */ }
From source file:Main.java
/** * Swing menus are looking pretty bad on Linux when the GTK LaF is used (See * bug #6925412). It will most likely never be fixed anytime soon so this * method provides a workaround for it. It uses reflection to change the GTK * style objects of Swing so popup menu borders have a minimum thickness of * 1 and menu separators have a minimum vertical thickness of 1. *///from www .j a va 2s. c o m public static void installGtkPopupBugWorkaround() { // Get current look-and-feel implementation class LookAndFeel laf = UIManager.getLookAndFeel(); Class<?> lafClass = laf.getClass(); // Do nothing when not using the problematic LaF if (!lafClass.getName().equals("com.sun.java.swing.plaf.gtk.GTKLookAndFeel")) return; // We do reflection from here on. Failure is silently ignored. The // workaround is simply not installed when something goes wrong here try { // Access the GTK style factory Field field = lafClass.getDeclaredField("styleFactory"); boolean accessible = field.isAccessible(); field.setAccessible(true); Object styleFactory = field.get(laf); field.setAccessible(accessible); // Fix the horizontal and vertical thickness of popup menu style Object style = getGtkStyle(styleFactory, new JPopupMenu(), "POPUP_MENU"); fixGtkThickness(style, "yThickness"); fixGtkThickness(style, "xThickness"); // Fix the vertical thickness of the popup menu separator style style = getGtkStyle(styleFactory, new JSeparator(), "POPUP_MENU_SEPARATOR"); fixGtkThickness(style, "yThickness"); } catch (Exception e) { // Silently ignored. Workaround can't be applied. } }
From source file:Main.java
public static int getStatusBarHeight(Context context) { Class<?> c = null;//from w ww . java 2 s . co m Object obj = null; Field field = null; int x = 0; int sbar = 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()); sbar = context.getResources().getDimensionPixelSize(x); } catch (Exception e) { e.printStackTrace(); } return sbar; }
From source file:Main.java
public static Dialog waitForCurrentDialogShowing(final Activity activity, final long timeout) throws InterruptedException { long start = System.currentTimeMillis(); try {// w w w .j a va 2s . c o m Field currentDialogField = activity.getClass().getDeclaredField("currentDialog"); currentDialogField.setAccessible(true); Dialog dialog = null; while (dialog == null || !dialog.isShowing()) { dialog = (Dialog) currentDialogField.get(activity); Thread.sleep(50); long now = System.currentTimeMillis(); if (now - start > timeout) { throw new InterruptedException( String.format("Timeout exceeded while waiting for dialog showing")); } } return dialog; } catch (Exception e) { throw new InterruptedException(String.format("Exception while waiting for dialog showing")); } }
From source file:Main.java
private static List<Thread> getThreadsFor(Runnable myRunnable) throws Exception { Method getThreads = Thread.class.getDeclaredMethod("getThreads"); Field target = Thread.class.getDeclaredField("target"); target.setAccessible(true);//from www. ja va 2s . c om getThreads.setAccessible(true); Thread[] threads = (Thread[]) getThreads.invoke(null); List<Thread> result = new ArrayList<Thread>(); for (Thread thread : threads) { Object runnable = target.get(thread); if (runnable == myRunnable) result.add(thread); } return result; }
From source file:org.web4thejob.util.L10nUtil.java
public static List<L10nString> getLocalizableResources(final Class<?> localizable) { final List<L10nString> strings = new ArrayList<L10nString>(); final Set<Class> classes = new HashSet<Class>(); classes.add(localizable);/*from w w w . j av a2 s . com*/ classes.addAll(ClassUtils.getAllInterfacesForClassAsSet(localizable)); for (Class<?> clazz : classes) { ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { strings.add((L10nString) field.get(null)); } }, new ReflectionUtils.FieldFilter() { @Override public boolean matches(Field field) { return ReflectionUtils.isPublicStaticFinal(field) && L10nString.class.equals(field.getType()); } }); } //get localizable resources from extension modules for (Module module : ContextUtil.getModules()) { if (module instanceof LocalizableModule) { strings.addAll(((LocalizableModule) module).getLocalizableStrings(classes)); } } // add commands,settings and global strings here... if (DesktopLayoutPanel.class.isAssignableFrom(localizable)) { for (CommandEnum commandEnum : CommandEnum.values()) { L10nString l10nString = new L10nString(CommandEnum.class, commandEnum.name(), L10nUtil.getMessage(commandEnum.getClass(), commandEnum.name(), commandEnum.name())); strings.add(l10nString); } for (SettingEnum settingEnum : SettingEnum.values()) { L10nString l10nString = new L10nString(SettingEnum.class, settingEnum.name(), L10nUtil.getMessage(settingEnum.getClass(), settingEnum.name(), settingEnum.name())); strings.add(l10nString); } for (Condition condition : Condition.getConditions()) { L10nString l10nString = new L10nString(Condition.class, condition.getKey(), condition.getKey()); strings.add(l10nString); } } return strings; }
From source file:Main.java
public static ContentValues objectToContentValues(Object object) { if (object == null) { throw new IllegalArgumentException("please check your argument"); }//from www .j a va2 s . c o m ContentValues contentValues = new ContentValues(); try { Field[] fields = object.getClass().getDeclaredFields(); for (Field field : fields) { String fieldName = field.getName(); Type type = field.getType(); field.setAccessible(true); if (type.equals(String.class)) { contentValues.put(fieldName, field.get(object).toString()); } else if (type.equals(Integer.class) || type.equals(Integer.TYPE)) { contentValues.put(fieldName, field.getInt(object)); } else if (type.equals(Float.class) || type.equals(Float.TYPE)) { contentValues.put(fieldName, field.getFloat(object)); } else if (type.equals(Long.class) || type.equals(Long.TYPE)) { contentValues.put(fieldName, field.getLong(object)); } else if (type.equals(Double.class) || type.equals(Double.TYPE)) { contentValues.put(fieldName, field.getDouble(object)); } else if (type.equals(Boolean.class) || type.equals(Boolean.TYPE)) { contentValues.put(fieldName, field.getBoolean(object)); } } } catch (IllegalAccessException | IllegalArgumentException e) { e.printStackTrace(); } return contentValues; }
From source file:ColorUtils.java
private static Map<String, Color> buildNameToColorMap() { Map<String, Color> colorMap = new HashMap<String, Color>(); for (Field field : Color.class.getDeclaredFields()) { if (isConstantColorField(field)) { try { Color color = (Color) field.get(null); colorMap.put(field.getName().toUpperCase(), color); } catch (IllegalAccessException e) { // Should not occur because the field is public and static }/*from w w w . j av a2 s . c o m*/ } } colorMap.put("DARKGREY", getColor("555555")); colorMap.put("DARKBLUE", getColor("000055")); colorMap.put("DARKRED", getColor("550000")); colorMap.put("DARKGREEN", getColor("005500")); colorMap.put("DARK_GREY", getColor("555555")); colorMap.put("DARK_BLUE", getColor("000055")); colorMap.put("DARK_RED", getColor("550000")); colorMap.put("DARK_GREEN", getColor("005500")); return colorMap; }