List of usage examples for java.lang.reflect Field get
@CallerSensitive @ForceInline public Object get(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:net.drgnome.virtualpack.components.VAnvil.java
public static IInventory getInv(VAnvil anvil, String name) { try {//from w w w .j a va 2 s . co m Field f = ContainerAnvil.class.getDeclaredField(name); f.setAccessible(true); return (IInventory) f.get(anvil); } catch (Exception e) { e.printStackTrace(); return null; } }
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 ww w .j av a 2 s. c o 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:com.music.Generator.java
@SuppressWarnings("unused") private static void initJMusicSynthesizer() { try {//from www .j av a2 s .c om Field fld = ReflectionUtils.findField(Play.class, "ms"); ReflectionUtils.makeAccessible(fld); MidiSynth synth = (MidiSynth) fld.get(null); // playing for the first time initializes the synthesizer try { synth.play(null); } catch (Exception ex) { } ; Field synthField = ReflectionUtils.findField(MidiSynth.class, "m_synth"); ReflectionUtils.makeAccessible(synthField); Synthesizer synthsizer = (Synthesizer) synthField.get(synth); loadSoundbankInstruments(synthsizer); } catch (Exception ex) { throw new IllegalStateException(ex); } }
From source file:com.framework.infrastructure.utils.ReflectionUtils.java
/** * , private/protected, getter.//from w w w. ja v a 2 s.c o m */ public static Object getFieldValue(final Object obj, final String fieldName) { Field field = getAccessibleField(obj, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } Object result = null; try { result = field.get(obj); } catch (IllegalAccessException e) { logger.error("{}", e.getMessage()); } return result; }
From source file:Main.java
private static int getStatusBarHeight(Activity activity) { Class<?> c;//from w ww .j ava 2 s .c o m Object obj; Field field; int x; int 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 = activity.getResources().getDimensionPixelSize(x); } catch (Exception e1) { e1.printStackTrace(); } return statusBarHeight; }
From source file:misc.TestUtils.java
public static List<Pair<String, Object>> getEntityFields(Object o) { List<Pair<String, Object>> ret = new ArrayList<Pair<String, Object>>(); for (Class<?> clazz = o.getClass(); clazz != Object.class; clazz = clazz.getSuperclass()) { for (Field field : clazz.getDeclaredFields()) { //ignore some weird fields with unique values if (field.getName().contains("$")) continue; boolean accessible = field.isAccessible(); try { field.setAccessible(true); Object val = field.get(o); ret.add(new Pair<String, Object>(field.getName(), val)); // System.out.println(field.getName()+"="+val); } catch (IllegalArgumentException e) { e.printStackTrace();//from www. ja va 2s .c om } catch (IllegalAccessException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } finally { field.setAccessible(accessible); } } } return ret; }
From source file:io.github.benas.randombeans.util.ReflectionUtils.java
/** * Get the value (accessible or not accessible) of a field of a target object. * * @param object instance to get the field of * @param field field to get the value of * @throws IllegalAccessException if field can not be accessed *///ww w. j a va 2s . co m public static Object getFieldValue(final Object object, final Field field) throws IllegalAccessException { boolean access = field.isAccessible(); field.setAccessible(true); Object value = field.get(object); field.setAccessible(access); return value; }
From source file:com.aw.swing.mvp.view.IPView.java
public static List<JComponent> getCmps(Object target) { // logger.info("searching attributes " + target.getClass().getName()); List components = new ArrayList(); List<Field> forms = new ArrayList(); Class cls = target.getClass(); Field[] fields = cls.getFields(); for (int i = 0; i < fields.length; i++) { if ((fields[i].getName().startsWith("txt") || fields[i].getName().startsWith("chk")) && !fields[i].getName().startsWith("chkSel")) { JComponent jComponemt; try { jComponemt = (JComponent) fields[i].get(target); if (jComponemt != null) { jComponemt.putClientProperty("Field", fields[i]); components.add(jComponemt); } else { System.out.println("Null:<" + target.getClass() + ">- <" + fields[i].getName() + ">"); }/* ww w . j ava 2 s . c o m*/ } catch (IllegalAccessException e) { e.printStackTrace(); throw new AWSystemException("Error getting teh value of:<" + fields[i].getName() + ">", e); } } if ((fields[i].getType().getSimpleName().startsWith("Frm"))) { forms.add(fields[i]); } } if (forms.size() > 0) { for (Field field : forms) { try { Object formToBeChecked = field.get(target); if (formToBeChecked != null) { List formAttributes = getCmps(formToBeChecked); if (formAttributes.size() > 0) { components.addAll(formAttributes); } } else { throw new IllegalStateException("FRM NULL:" + field.getName()); } } catch (IllegalAccessException e) { e.printStackTrace(); throw new AWSystemException("Problems getting value for:<" + field.getName() + ">", e); } } } return components; }
From source file:net.dv8tion.jda.utils.DebugUtil.java
public static JSONObject fromJDA(JDA iJDA, boolean includeUsers, boolean includeGuilds, boolean includeGuildUsers, boolean includeChannels, boolean includePrivateChannels) { JDAImpl jda = (JDAImpl) iJDA;/*from ww w.ja v a2 s . co m*/ JSONObject obj = new JSONObject(); obj.put("self_info", fromUser(jda.getSelfInfo())) .put("proxy", jda.getGlobalProxy() == null ? JSONObject.NULL : new JSONObject().put("host", jda.getGlobalProxy().getHostName()).put("port", jda.getGlobalProxy().getPort())) .put("response_total", jda.getResponseTotal()).put("audio_enabled", jda.isAudioEnabled()) .put("auto_reconnect", jda.isAutoReconnect()); JSONArray array = new JSONArray(); for (Object listener : jda.getRegisteredListeners()) array.put(listener.getClass().getCanonicalName()); obj.put("event_listeners", array); try { Field f = EntityBuilder.class.getDeclaredField("cachedJdaGuildJsons"); f.setAccessible(true); HashMap<String, JSONObject> cachedJsons = ((HashMap<JDA, HashMap<String, JSONObject>>) f.get(null)) .get(jda); array = new JSONArray(); for (String guildId : cachedJsons.keySet()) array.put(guildId); obj.put("second_pass_json_guild_ids", array); f = EntityBuilder.class.getDeclaredField("cachedJdaGuildCallbacks"); f.setAccessible(true); HashMap<String, Consumer<Guild>> cachedCallbacks = ((HashMap<JDA, HashMap<String, Consumer<Guild>>>) f .get(null)).get(jda); array = new JSONArray(); for (String guildId : cachedCallbacks.keySet()) array.put(guildId); obj.put("second_pass_callback_guild_ids", array); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } if (includeGuilds) { array = new JSONArray(); for (Guild guild : jda.getGuilds()) array.put(fromGuild(guild, includeGuildUsers, includeChannels, true, true)); obj.put("guilds", array); } if (includePrivateChannels) { array = new JSONArray(); for (PrivateChannel chan : jda.getPrivateChannels()) array.put(fromPrivateChannel(chan, false)); obj.put("private_channels", array); array = new JSONArray(); for (Map.Entry<String, String> entry : jda.getOffline_pms().entrySet()) { array.put(new JSONObject().put("id", entry.getValue()).put("user_id", entry.getKey())); } obj.put("offline_private_channels", array); } if (includeUsers) { array = new JSONArray(); for (User user : jda.getUsers()) array.put(fromUser(user)); obj.put("users", array); } return obj; }
From source file:com.cn.util.Reflections.java
/** * ?, private/protected, ??getter./*from w w w .j a v a 2 s .co m*/ */ public static Object getFieldValue(final Object obj, final String fieldName) { Field field = getAccessibleField(obj, fieldName); if (field == null) { throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]"); } Object result = null; try { result = field.get(obj); } catch (IllegalAccessException e) { } return result; }