List of usage examples for java.lang.reflect Field getDouble
@CallerSensitive @ForceInline public double getDouble(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.getDouble(x)); f.setDouble(x, 9.99);/*from www.ja v a 2 s .c om*/ System.out.println(f.getDouble(x)); }
From source file:Main.java
public static void main(String[] args) throws Exception { Object clazz = new TestClass(); String lookingForValue = "firstValue"; Field field = clazz.getClass().getField(lookingForValue); Class clazzType = field.getType(); if (clazzType.toString().equals("double")) System.out.println(field.getDouble(clazz)); else if (clazzType.toString().equals("int")) System.out.println(field.getInt(clazz)); //System.out.println(field.get(clazz)); }
From source file:X.java
public static void main(String[] args) throws Exception { Class<?> clazz = Class.forName("X"); X x = (X) clazz.newInstance();/*from w w w. j a va 2 s .c o m*/ Field f = clazz.getField("i"); System.out.println(f.getInt(x)); // Output: 10 f.setInt(x, 20); System.out.println(f.getInt(x)); // Output: 20 f = clazz.getField("PI"); System.out.println(f.getDouble(null)); // Output: 3.14 f.setDouble(x, 20); }
From source file:Main.java
public static Object getStaticField(Class clz, String fieldName, int type) { if (null != clz) { try {//from w w w . j av 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:com.oneis.javascript.Runtime.java
private static void checkJavaScriptTimeZoneIsGMT() { // Check that the timezone is GMT - datetime.js has created a Date object to run static initializers. // This is specific to the exact version of the Rhino implementation, but should fail gracefully if it's changed. boolean nativeDateTimeZoneOK = false; try {/*from w w w . j a v a 2 s.com*/ java.lang.Class nativeDate = java.lang.Class.forName("org.mozilla.javascript.NativeDate"); java.lang.reflect.Field nativeDateTimeZoneField = nativeDate.getDeclaredField("thisTimeZone"); nativeDateTimeZoneField.setAccessible(true); java.util.TimeZone tz = (java.util.TimeZone) nativeDateTimeZoneField.get(null); java.lang.reflect.Field nativeDateLocalTZAField = nativeDate.getDeclaredField("LocalTZA"); nativeDateLocalTZAField.setAccessible(true); if (tz != null && (tz.getID().equals("GMT0") || tz.getID().equals("GMT") || tz.getID().equals("UTC")) && nativeDateLocalTZAField.getDouble(null) == 0.0) { nativeDateTimeZoneOK = true; } } catch (Exception e) { // Ignore, nativeDateTimeZoneOK won't be set to true. } if (!nativeDateTimeZoneOK) { System.out.println("\n\nThe operating system's time zone must be set to GMT (GMT0 Java TimeZone).\n"); throw new RuntimeException("JavaScript interpreter's local time zone is not GMT (GMT0 Java TimeZone)."); } }
From source file:Main.java
public static ContentValues objectToContentValues(Object object) { if (object == null) { throw new IllegalArgumentException("please check your argument"); }//from w w w .ja va2s .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 w w . j av a 2 s . co m*/ public static double getDouble(Object target, Field field) { if (target == null || field == null) return 0D; makeAccessible(field, field.getModifiers()); try { return field.getDouble(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 w w w .j a v a 2 s .c o m*/ public static double getDouble(Object target, Field field) { if (target == null || field == null) return 0D; makeAccessible(field, field.getModifiers()); try { return field.getDouble(target); } catch (Throwable t) { throw wrapReflectionException(t); } }
From source file:com.zlfun.framework.excel.ExcelUtils.java
private static String get(Object obj, Field field) { try {/* w w w .j a v a 2 s . c om*/ if (!field.isAccessible()) { field.setAccessible(true); } if (field.getType() == int.class) { return String.valueOf(field.getInt(obj)); } else if (field.getType() == long.class) { return String.valueOf(field.getLong(obj)); } else if (field.getType() == double.class) { return String.valueOf(field.getDouble(obj)); } else if (field.getType() == byte.class) { return String.valueOf(field.getByte(obj)); } else if (field.getType() == boolean.class) { return String.valueOf(field.getBoolean(obj)); } else if (field.getType() == String.class) { if (field.get(obj) == null) { return ""; } return String.valueOf(field.get(obj)); } else if (field.getType() == Date.class) { if (field.get(obj) == null) { return ""; } return MiscDateUtils.getDateTime((Date) field.get(obj)); } } catch (Exception e) { } return ""; }
From source file:de.thkwalter.et.ortskurve.OrtskurveTest.java
/** * Dieser Test berprft den Konstruktor {@link Ortskurve#Ortskurve(Vector2D, double)}. * @throws NoSuchFieldException /*w ww . ja v a 2 s . c om*/ * @throws SecurityException * @throws IllegalAccessException * @throws IllegalArgumentException */ @Test public void testOrtskurve() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { // Es wird berprft, ob das Objekt der zu testenden Klasse erzeugt worden ist. assertNotNull(this.ortskurve); // Der Mittelpunkt der Ortskurve wird gelesen. Field mittelpunktOrtskurveFeld = Ortskurve.class.getDeclaredField("mittelpunktOrtskurve"); mittelpunktOrtskurveFeld.setAccessible(true); Vector2D mittelpunktOrtskurve = (Vector2D) mittelpunktOrtskurveFeld.get(this.ortskurve); // Es wird berprft, ob der Mittelpunkt der Ortskurve korrekt initialisiert worden ist. assertEquals(this.testMittelpunktOrtskurve, mittelpunktOrtskurve); // Der Radius der Ortskurve wird gelesen. Field radiusOrtskurveFeld = Ortskurve.class.getDeclaredField("radiusOrtskurve"); radiusOrtskurveFeld.setAccessible(true); double radiusOrtskurve = radiusOrtskurveFeld.getDouble(this.ortskurve); // Es wird berprft, ob der Mittelpunkt der Ortskurve korrekt initialisiert worden ist. assertEquals(this.testRadiusOrtskurve, radiusOrtskurve, 0.0); }