List of usage examples for java.lang.reflect Field getBoolean
@CallerSensitive @ForceInline public boolean getBoolean(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:info.papdt.blacklight.support.Utility.java
public static void setActionBarTranslation(Activity activity, float y) { ViewGroup vg = (ViewGroup) activity.findViewById(android.R.id.content).getParent(); int count = vg.getChildCount(); if (DEBUG) {/*from w w w .j a va2s . c o m*/ Log.d(TAG, "=========================="); } // Get the class of action bar Class<?> actionBarContainer = null; Field isSplit = null; try { actionBarContainer = Class.forName("com.android.internal.widget.ActionBarContainer"); isSplit = actionBarContainer.getDeclaredField("mIsSplit"); isSplit.setAccessible(true); } catch (Exception e) { if (DEBUG) { Log.e(TAG, Log.getStackTraceString(e)); } } for (int i = 0; i < count; i++) { View v = vg.getChildAt(i); if (v.getId() != android.R.id.content) { if (DEBUG) { Log.d(TAG, "Found View: " + v.getClass().getName()); } try { if (actionBarContainer.isInstance(v)) { if (DEBUG) { Log.d(TAG, "Found ActionBarContainer"); } if (isSplit.getBoolean(v)) { if (DEBUG) { Log.d(TAG, "Found Split Action Bar"); } continue; } } } catch (Exception e) { if (DEBUG) { Log.e(TAG, Log.getStackTraceString(e)); } } v.setTranslationY(y); } } if (DEBUG) { Log.d(TAG, "=========================="); } }
From source file:com.nonninz.robomodel.RoboModel.java
void saveField(Field field, TypedContentValues cv) { final Class<?> type = field.getType(); final boolean wasAccessible = field.isAccessible(); field.setAccessible(true);/* w w w . j av a 2s.com*/ try { if (type == String.class) { cv.put(field.getName(), (String) field.get(this)); } else if (type == Boolean.TYPE) { cv.put(field.getName(), field.getBoolean(this)); } else if (type == Byte.TYPE) { cv.put(field.getName(), field.getByte(this)); } else if (type == Double.TYPE) { cv.put(field.getName(), field.getDouble(this)); } else if (type == Float.TYPE) { cv.put(field.getName(), field.getFloat(this)); } else if (type == Integer.TYPE) { cv.put(field.getName(), field.getInt(this)); } else if (type == Long.TYPE) { cv.put(field.getName(), field.getLong(this)); } else if (type == Short.TYPE) { cv.put(field.getName(), field.getShort(this)); } else if (type.isEnum()) { final Object value = field.get(this); if (value != null) { final Method method = type.getMethod("name"); final String str = (String) method.invoke(value); cv.put(field.getName(), str); } } else { // Try to JSONify it (db column must be of type text) final String json = mMapper.writeValueAsString(field.get(this)); cv.put(field.getName(), json); } } catch (final IllegalAccessException e) { final String msg = String.format("Field %s is not accessible", type, field.getName()); throw new IllegalArgumentException(msg); } catch (final JsonProcessingException e) { Ln.w(e, "Error while dumping %s of type %s to Json", field.getName(), type); final String msg = String.format("Field %s is not accessible", type, field.getName()); throw new IllegalArgumentException(msg); } catch (final NoSuchMethodException e) { // Should not happen throw new RuntimeException(e); } catch (final InvocationTargetException e) { // Should not happen throw new RuntimeException(e); } finally { field.setAccessible(wasAccessible); } }
From source file:org.alex73.skarynka.scan.Book2.java
private void get(Object obj, String prefix, List<String> lines) throws Exception { for (Field f : obj.getClass().getFields()) { if (Modifier.isPublic(f.getModifiers()) && !Modifier.isStatic(f.getModifiers()) && !Modifier.isTransient(f.getModifiers())) { if (f.getType() == int.class) { int v = f.getInt(obj); if (v != -1) { lines.add(prefix + f.getName() + "=" + v); }//ww w .j a va 2 s . c o m } else if (f.getType() == boolean.class) { lines.add(prefix + f.getName() + "=" + f.getBoolean(obj)); } else if (f.getType() == String.class) { String s = (String) f.get(obj); if (s != null) { lines.add(prefix + f.getName() + "=" + s); } } else if (Set.class.isAssignableFrom(f.getType())) { Set<?> set = (Set<?>) f.get(obj); StringBuilder t = new StringBuilder(); for (Object o : set) { t.append(o.toString()).append(';'); } if (t.length() > 0) { t.setLength(t.length() - 1); } lines.add(prefix + f.getName() + "=" + t); } else { throw new RuntimeException("Unknown field class for get '" + f.getName() + "'"); } } } }
From source file:org.sakaiproject.tool.impl.SessionComponentRegressionTest.java
private void awaitExpirationOrFail(Session toBeExpired, int inactiveInterval) throws InterruptedException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { // unfortunate, but doesn't seem to be any other way around // timing problems Thread.sleep(inactiveInterval * 1500L); // ensure expiration window elapses, and then some Field isValid = toBeExpired.getClass().getDeclaredField("m_valid"); isValid.setAccessible(true);/*from w ww . j a va 2 s . c om*/ int cnt = 0; while (isValid.getBoolean(toBeExpired) && cnt++ < 10) { Thread.sleep(100); } if (isValid.getBoolean(toBeExpired)) { fail("Session should have expired"); } Thread.sleep(50); }
From source file:net.dmulloy2.ultimatearena.types.ArenaConfig.java
@Override public Map<String, Object> serialize() { Map<String, Object> data = new LinkedHashMap<>(); for (Field field : ArenaConfig.class.getDeclaredFields()) { try {//from w w w .j a v a 2 s .c o m if (Modifier.isTransient(field.getModifiers())) continue; boolean accessible = field.isAccessible(); field.setAccessible(true); if (field.getType().equals(Integer.TYPE)) { if (field.getInt(this) != 0) data.put(field.getName(), field.getInt(this)); } else if (field.getType().equals(Long.TYPE)) { if (field.getLong(this) != 0) data.put(field.getName(), field.getLong(this)); } else if (field.getType().equals(Boolean.TYPE)) { if (field.getBoolean(this)) data.put(field.getName(), field.getBoolean(this)); } else if (field.getType().isAssignableFrom(Collection.class)) { if (!((Collection<?>) field.get(this)).isEmpty()) data.put(field.getName(), field.get(this)); } else if (field.getType().isAssignableFrom(String.class)) { if ((String) field.get(this) != null) data.put(field.getName(), field.get(this)); } else if (field.getType().isAssignableFrom(Map.class)) { if (!((Map<?, ?>) field.get(this)).isEmpty()) data.put(field.getName(), field.get(this)); } else { if (field.get(this) != null) data.put(field.getName(), field.get(this)); } field.setAccessible(accessible); } catch (Throwable ex) { } } serializeCustomOptions(data); return data; }
From source file:org.rhq.enterprise.gui.legacy.taglib.ConstantsTag.java
public int doEndTag() throws JspException { try {/*www . ja v a2s . c o m*/ JspWriter out = pageContext.getOut(); if (className == null) { className = pageContext.getServletContext().getInitParameter(constantsClassNameParam); } if (validate(out)) { // we're misconfigured. getting this far // is a matter of what our failure mode is; // if we haven't thrown an Error, carry on log.debug("constants tag misconfigured"); return EVAL_PAGE; } Map<String, String> fieldMap; if (constants.containsKey(className)) { // we cache the result of the constant's class // reflection field walk as a map fieldMap = (Map<String, String>) constants.get(className); } else { fieldMap = new HashMap<String, String>(); Class typeClass = Class.forName(className); if (typeClass.isEnum()) { for (Object enumConstantObj : typeClass.getEnumConstants()) { Enum enumConstant = (Enum) enumConstantObj; // Set name *and* value to enum name (e.g. name of ResourceCategory.PLATFORM = "PLATFORM") // NOTE: We do not set the value to enumConstant.ordinal(), because there is no way to // convert the ordinal value back to an Enum (i.e. no Enum.valueOf(int ordinal) method). fieldMap.put(enumConstant.name(), enumConstant.name()); } } else { Object instance = typeClass.newInstance(); Field[] fields = typeClass.getFields(); for (Field field : fields) { // string comparisons of class names should be cheaper // than reflective Class comparisons, the asumption here // is that most constants are Strings, ints and booleans // but a minimal effort is made to accomadate all types // and represent them as String's for our tag's output String fieldType = field.getType().getName(); String strVal; if (fieldType.equals("java.lang.String")) { strVal = (String) field.get(instance); } else if (fieldType.equals("int")) { strVal = Integer.toString(field.getInt(instance)); } else if (fieldType.equals("boolean")) { strVal = Boolean.toString(field.getBoolean(instance)); } else if (fieldType.equals("char")) { strVal = Character.toString(field.getChar(instance)); } else if (fieldType.equals("double")) { strVal = Double.toString(field.getDouble(instance)); } else if (fieldType.equals("float")) { strVal = Float.toString(field.getFloat(instance)); } else if (fieldType.equals("long")) { strVal = Long.toString(field.getLong(instance)); } else if (fieldType.equals("short")) { strVal = Short.toString(field.getShort(instance)); } else if (fieldType.equals("byte")) { strVal = Byte.toString(field.getByte(instance)); } else { strVal = field.get(instance).toString(); } fieldMap.put(field.getName(), strVal); } } // cache the result constants.put(className, fieldMap); } if ((symbol != null) && !fieldMap.containsKey(symbol)) { // tell the developer that he's being a dummy and what // might be done to remedy the situation // TODO: what happens if the constants change? // do we need to throw a JspException, here? - mtk String err1 = symbol + " was not found in " + className + "\n"; String err2 = err1 + "use <constants:diag classname=\"" + className + "\"/>\n" + "to figure out what you're looking for"; log.error(err2); die(out, err1); } if (varSpecified) { doSet(fieldMap); } else { doOutput(fieldMap, out); } } catch (JspException e) { throw e; } catch (Exception e) { log.debug("doEndTag() failed: ", e); throw new JspException("Could not access constants tag", e); } return EVAL_PAGE; }
From source file:net.dmulloy2.ultimatearena.types.ArenaZone.java
/** * {@inheritDoc}/*from w w w. ja v a2 s . c om*/ */ @Override public Map<String, Object> serialize() { Map<String, Object> data = new LinkedHashMap<>(); for (java.lang.reflect.Field field : ArenaZone.class.getDeclaredFields()) { if (Modifier.isTransient(field.getModifiers())) continue; try { boolean accessible = field.isAccessible(); field.setAccessible(true); if (field.getType().equals(Integer.TYPE)) { if (field.getInt(this) != 0) data.put(field.getName(), field.getInt(this)); } else if (field.getType().equals(Long.TYPE)) { if (field.getLong(this) != 0) data.put(field.getName(), field.getLong(this)); } else if (field.getType().equals(Boolean.TYPE)) { if (field.getBoolean(this)) data.put(field.getName(), field.getBoolean(this)); } else if (field.getType().isAssignableFrom(Collection.class)) { if (!((Collection<?>) field.get(this)).isEmpty()) data.put(field.getName(), field.get(this)); } else if (field.getType().isAssignableFrom(String.class)) { if ((String) field.get(this) != null) data.put(field.getName(), field.get(this)); } else if (field.getType().isAssignableFrom(Map.class)) { if (!((Map<?, ?>) field.get(this)).isEmpty()) data.put(field.getName(), field.get(this)); } else { if (field.get(this) != null) data.put(field.getName(), field.get(this)); } field.setAccessible(accessible); } catch (Throwable ex) { } } data.put("version", CURRENT_VERSION); return data; }
From source file:cn.edu.zafu.corepage.base.BaseActivity.java
/** * ??//w ww. j av a 2s . c om * * @param outState Bundle */ @Override protected void onSaveInstanceState(Bundle outState) { Field[] fields = this.getClass().getDeclaredFields(); Field.setAccessible(fields, true); Annotation[] ans; for (Field f : fields) { ans = f.getDeclaredAnnotations(); for (Annotation an : ans) { if (an instanceof SaveWithActivity) { try { Object o = f.get(this); if (o == null) { continue; } String fieldName = f.getName(); if (o instanceof Integer) { outState.putInt(fieldName, f.getInt(this)); } else if (o instanceof String) { outState.putString(fieldName, (String) f.get(this)); } else if (o instanceof Long) { outState.putLong(fieldName, f.getLong(this)); } else if (o instanceof Short) { outState.putShort(fieldName, f.getShort(this)); } else if (o instanceof Boolean) { outState.putBoolean(fieldName, f.getBoolean(this)); } else if (o instanceof Byte) { outState.putByte(fieldName, f.getByte(this)); } else if (o instanceof Character) { outState.putChar(fieldName, f.getChar(this)); } else if (o instanceof CharSequence) { outState.putCharSequence(fieldName, (CharSequence) f.get(this)); } else if (o instanceof Float) { outState.putFloat(fieldName, f.getFloat(this)); } else if (o instanceof Double) { outState.putDouble(fieldName, f.getDouble(this)); } else if (o instanceof String[]) { outState.putStringArray(fieldName, (String[]) f.get(this)); } else if (o instanceof Parcelable) { outState.putParcelable(fieldName, (Parcelable) f.get(this)); } else if (o instanceof Serializable) { outState.putSerializable(fieldName, (Serializable) f.get(this)); } else if (o instanceof Bundle) { outState.putBundle(fieldName, (Bundle) f.get(this)); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } super.onSaveInstanceState(outState); }
From source file:io.realm.RealmTest.java
public void testFinalizerThread() throws NoSuchFieldException, IllegalAccessException { Field fieldReferences = FinalizerRunnable.class.getDeclaredField("references"); fieldReferences.setAccessible(true); Map<Reference<?>, Boolean> references = (Map<Reference<?>, Boolean>) fieldReferences.get(null); assertNotNull(references);// ww w . ja v a 2s . com Field fieldIsFinalizerStarted = Realm.class.getDeclaredField("isFinalizerStarted"); fieldIsFinalizerStarted.setAccessible(true); boolean isFinalizerStarted = fieldIsFinalizerStarted.getBoolean(null); assertTrue(isFinalizerStarted); //insert some rows, then give the FinalizerRunnable some time to cleanup // we have 8 reference so far let's add more final int numberOfPopulateTest = 10000; final int totalNumberOfReferences = 8 + 20 * 2 * numberOfPopulateTest; for (int i = 0; i < numberOfPopulateTest; i++) { populateTestRealm(testRealm, 20); } final int MAX_GC_RETRIES = 5; int numberOfRetries = 0; while (references.size() > 0 && numberOfRetries < MAX_GC_RETRIES) { SystemClock.sleep(TimeUnit.SECONDS.toMillis(1)); //1s numberOfRetries++; System.gc(); } // we can't guarantee that all references have been GC'd but we should detect a decrease boolean isDecreasing = references.size() < totalNumberOfReferences; if (!isDecreasing) { fail("FinalizerRunnable is not closing all native resources"); } else { android.util.Log.d(RealmTest.class.getName(), "FinalizerRunnable freed : " + (totalNumberOfReferences - references.size()) + " out of " + totalNumberOfReferences); } }
From source file:cern.c2mon.shared.common.datatag.address.impl.HardwareAddressImpl.java
@Override public final int hashCode() { int result = 0; Field[] fields = this.getClass().getDeclaredFields(); for (Field field : fields) { // compare non-final, non-static and non-transient fields only if (!Modifier.isFinal(field.getModifiers()) && !Modifier.isStatic(field.getModifiers()) && !Modifier.isTransient(field.getModifiers())) { try { // skip arrays if (!field.getType().isArray() && field.get(this) != null) { // for string take its length if (field.getType().equals(String.class)) { result ^= ((String) field.get(this)).length(); } else if (field.getType().equals(short.class) || field.getType().equals(Short.class)) { result ^= field.getShort(this); } else if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) { result ^= field.getInt(this); } else if (field.getType().equals(float.class) || field.getType().equals(Float.class)) { result ^= (int) field.getFloat(this); } else if (field.getType().equals(double.class) || field.getType().equals(Double.class)) { result ^= (int) field.getDouble(this); } else if (field.getType().equals(long.class) || field.getType().equals(Long.class)) { result ^= (int) field.getLong(this); } else if (field.getType().equals(byte.class) || field.getType().equals(Byte.class)) { result ^= field.getByte(this); } else if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) { result ^= field.getBoolean(this) == Boolean.TRUE ? 1 : 0; }/*from www. ja v a2 s .co m*/ } } catch (Exception e) { log.error(e.toString()); throw new RuntimeException("Exception caught while calculating HardwareAddress hashcode.", e); } } } return result; }