List of usage examples for java.lang.reflect Field getFloat
@CallerSensitive @ForceInline public float getFloat(Object obj) throws IllegalArgumentException, IllegalAccessException
From source file:MyClass.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("MyClass"); MyClass x = (MyClass) clazz.newInstance(); Field f = clazz.getField("i"); System.out.println(f.getFloat(x)); f.setFloat(x, 9.99F);//from w ww.j a v a 2 s .co m System.out.println(f.getFloat(x)); }
From source file:Main.java
public static Object getStaticField(Class clz, String fieldName, int type) { if (null != clz) { try {// ww w . j a v a 2 s .c o m Field field = clz.getField(fieldName); switch (type) { case TYPE_OBJECT: return field.get(clz); case TYPE_INT: return field.getInt(clz); case TYPE_SHORT: return field.getShort(clz); case TYPE_BYTE: return field.getByte(clz); case TYPE_BOOLEAN: return field.getBoolean(clz); case TYPE_FLOAT: return field.getFloat(clz); case TYPE_LONG: return field.getLong(clz); case TYPE_DOUBLE: return field.getDouble(clz); default: return field.get(clz); } } catch (Exception e) { } return (clz == Object.class ? getDefault(type) : getStaticField(clz.getSuperclass(), fieldName, type)); } return getDefault(type); }
From source file:Main.java
public static ContentValues objectToContentValues(Object object) { if (object == null) { throw new IllegalArgumentException("please check your argument"); }//from w ww. ja v a 2 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:org.apache.openjpa.enhance.Reflection.java
/** * Return the value of the given field in the given object. *///from w ww . jav a 2 s . com public static float getFloat(Object target, Field field) { if (target == null || field == null) return 0F; makeAccessible(field, field.getModifiers()); try { return field.getFloat(target); } catch (Throwable t) { throw wrapReflectionException(t, _loc.get("get-field", target, field)); } }
From source file:org.acoveo.tools.Reflection.java
/** * Return the value of the given field in the given object. *///from ww w .ja v a 2 s.c o m public static float getFloat(Object target, Field field) { if (target == null || field == null) return 0F; makeAccessible(field, field.getModifiers()); try { return field.getFloat(target); } catch (Throwable t) { throw wrapReflectionException(t); } }
From source file:net.larry1123.elec.util.test.config.AbstractConfigTest.java
public void floatTest(String fieldName, Field testField) { try {// w w w . jav a 2s .c o m Assert.assertTrue(getPropertiesFile().getFloat(fieldName) == testField.getFloat(getConfigBase())); } catch (IllegalAccessException e) { assertFailFieldError(fieldName); } }
From source file:org.evosuite.regression.ObjectFields.java
private static Object getFieldValue(Field field, Object p) { try {//www. ja v a 2s . c o m /*Class objClass = p.getClass(); if(p instanceof java.lang.String){ ((String) p).hashCode(); }*/ Class<?> fieldType = field.getType(); field.setAccessible(true); if (fieldType.isPrimitive()) { if (fieldType.equals(Boolean.TYPE)) { return field.getBoolean(p); } if (fieldType.equals(Integer.TYPE)) { return field.getInt(p); } if (fieldType.equals(Byte.TYPE)) { return field.getByte(p); } if (fieldType.equals(Short.TYPE)) { return field.getShort(p); } if (fieldType.equals(Long.TYPE)) { return field.getLong(p); } if (fieldType.equals(Double.TYPE)) { return field.getDouble(p); } if (fieldType.equals(Float.TYPE)) { return field.getFloat(p); } if (fieldType.equals(Character.TYPE)) { return field.getChar(p); } throw new UnsupportedOperationException("Primitive type " + fieldType + " not implemented!"); } return field.get(p); } catch (IllegalAccessException exc) { throw new RuntimeException(exc); } catch (OutOfMemoryError e) { e.printStackTrace(); if (MAX_RECURSION != 0) MAX_RECURSION = 0; else throw new RuntimeErrorException(e); return getFieldValue(field, p); } }
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);//from w ww . j a va2s . c o m 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:nz.ac.otago.psyanlab.common.designer.program.stage.EditPropPropertiesFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Bundle args = getArguments();// w w w.ja v a2s. co m if (args.containsKey(ARG_PROP)) { mProp = args.getParcelable(ARG_PROP); } else { mProp = mCallbacks.getProp(args.getInt(ARG_PROP_ID)).getProp(); } mFieldMap = new HashMap<String, Field>(); mViewMap = new HashMap<String, View>(); // Run through the fields of the prop and build groups and mappings for // the fields and views to allow the user to change the property values. Field[] fields = mProp.getClass().getFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; PALEPropProperty annotation = field.getAnnotation(PALEPropProperty.class); if (annotation != null) { String fieldName = annotation.value(); View view; if (field.getType().isAssignableFrom(Integer.TYPE)) { try { view = newIntegerInputView(annotation.isSigned(), field.getInt(mProp)); } catch (IllegalArgumentException e) { throw new RuntimeException("Should never get here.", e); } catch (IllegalAccessException e) { throw new RuntimeException("Should never get here.", e); } } else if (field.getType().isAssignableFrom(Float.TYPE)) { try { view = newFloatInputView(annotation.isSigned(), field.getFloat(mProp)); } catch (IllegalArgumentException e) { throw new RuntimeException("Should never get here.", e); } catch (IllegalAccessException e) { throw new RuntimeException("Should never get here.", e); } } else if (field.getType().isAssignableFrom(String.class)) { try { view = newStringInputView((String) field.get(mProp)); } catch (IllegalArgumentException e) { throw new RuntimeException("Should never get here.", e); } catch (IllegalAccessException e) { throw new RuntimeException("Should never get here.", e); } } else { continue; } mFieldMap.put(fieldName, field); mViewMap.put(fieldName, view); if (mGroupings.containsKey(annotation.group())) { mGroupings.get(annotation.group()).add(fieldName); } else { ArrayList<String> list = new ArrayList<String>(); list.add(fieldName); mGroupings.put(annotation.group(), list); } } } // Initialise grid layout. GridLayout grid = new GridLayout(getActivity()); grid.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); grid.setAlignmentMode(GridLayout.ALIGN_BOUNDS); grid.setColumnCount(2); grid.setUseDefaultMargins(true); // Run through groupings of properties adding the created views to the // GridLayout in sections. for (String group : mGroupings.keySet()) { if (!TextUtils.isEmpty(group) && !TextUtils.equals(group, "")) { TextView sectionBreak = (TextView) inflater.inflate(R.layout.prop_property_section_break, grid, false); sectionBreak.setText(group); GridLayout.LayoutParams sectionBreakParams = new GridLayout.LayoutParams(); sectionBreakParams.columnSpec = GridLayout.spec(0, 2); sectionBreak.setLayoutParams(sectionBreakParams); grid.addView(sectionBreak); } for (String name : mGroupings.get(group)) { TextView propertyLabel = (TextView) inflater.inflate(R.layout.prop_property_label, grid, false); GridLayout.LayoutParams params = new GridLayout.LayoutParams(); propertyLabel.setLayoutParams(params); propertyLabel.setText(getResources().getString(R.string.format_property_label, name)); View propertyView = mViewMap.get(name); params = new GridLayout.LayoutParams(); params.setGravity(Gravity.FILL_HORIZONTAL); propertyView.setLayoutParams(params); grid.addView(propertyLabel); grid.addView(propertyView); } } return grid; }
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 w w w.j av a2 s . c o m*/ } } catch (Exception e) { log.error(e.toString()); throw new RuntimeException("Exception caught while calculating HardwareAddress hashcode.", e); } } } return result; }