List of usage examples for java.lang.reflect Field setDouble
@CallerSensitive @ForceInline public void setDouble(Object obj, double d) throws IllegalArgumentException, IllegalAccessException
From source file:org.acoveo.tools.Reflection.java
/** * Set the value of the given field in the given object. *//* ww w . jav a2 s .co m*/ public static void set(Object target, Field field, double value) { if (target == null || field == null) return; makeAccessible(field, field.getModifiers()); try { field.setDouble(target, value); } catch (Throwable t) { throw wrapReflectionException(t); } }
From source file:com.vk.sdk.api.model.ParseUtils.java
/** * Parses object with follow rules:/*from w w w. j a v a 2s .co m*/ * * 1. All fields should had a public access. * 2. The name of the filed should be fully equal to name of JSONObject key. * 3. Supports parse of all Java primitives, all {@link String}, * arrays of primitive types, {@link String}s and {@link com.vk.sdk.api.model.VKApiModel}s, * list implementation line {@link com.vk.sdk.api.model.VKList}, {@link com.vk.sdk.api.model.VKAttachments.VKAttachment} or {@link com.vk.sdk.api.model.VKPhotoSizes}, * {@link com.vk.sdk.api.model.VKApiModel}s. * * 4. Boolean fields defines by vk_int == 1 expression. * @param object object to initialize * @param source data to read values * @return initialized according with given data object * @throws JSONException if source object structure is invalid */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException { if (source.has("response")) { source = source.optJSONObject("response"); } if (source == null) { return object; } for (Field field : object.getClass().getFields()) { field.setAccessible(true); String fieldName = field.getName(); Class<?> fieldType = field.getType(); Object value = source.opt(fieldName); if (value == null) { continue; } try { if (fieldType.isPrimitive() && value instanceof Number) { Number number = (Number) value; if (fieldType.equals(int.class)) { field.setInt(object, number.intValue()); } else if (fieldType.equals(long.class)) { field.setLong(object, number.longValue()); } else if (fieldType.equals(float.class)) { field.setFloat(object, number.floatValue()); } else if (fieldType.equals(double.class)) { field.setDouble(object, number.doubleValue()); } else if (fieldType.equals(boolean.class)) { field.setBoolean(object, number.intValue() == 1); } else if (fieldType.equals(short.class)) { field.setShort(object, number.shortValue()); } else if (fieldType.equals(byte.class)) { field.setByte(object, number.byteValue()); } } else { Object result = field.get(object); if (value.getClass().equals(fieldType)) { result = value; } else if (fieldType.isArray() && value instanceof JSONArray) { result = parseArrayViaReflection((JSONArray) value, fieldType); } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKList.class.equals(fieldType)) { ParameterizedType genericTypes = (ParameterizedType) field.getGenericType(); Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0]; if (VKApiModel.class.isAssignableFrom(genericType) && Parcelable.class.isAssignableFrom(genericType) && Identifiable.class.isAssignableFrom(genericType)) { if (value instanceof JSONArray) { result = new VKList((JSONArray) value, genericType); } else if (value instanceof JSONObject) { result = new VKList((JSONObject) value, genericType); } } } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) { result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value); } field.set(object, result); } } catch (InstantiationException e) { throw new JSONException(e.getMessage()); } catch (IllegalAccessException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodException e) { throw new JSONException(e.getMessage()); } catch (InvocationTargetException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodError e) { // ?????????? ???????: // ?? ?? ????????, ?? ? ????????? ???????? getFields() ???????? ??? ???. // ?????? ? ??????? ???????????, ????????? ?? ? ????????, ?????? Android ? ???????? ????????? ??????????. throw new JSONException(e.getMessage()); } } return object; }
From source file:org.flycraft.vkontakteapi.model.ParseUtils.java
/** * Parses object with follow rules:/*from www . jav a 2 s .co m*/ * <p/> * 1. All fields should had a public access. * 2. The name of the filed should be fully equal to name of JSONObject key. * 3. Supports parse of all Java primitives, all {@link java.lang.String}, * arrays of primitive types, {@link java.lang.String}s and {@link org.flycraft.vkontakteapi.model.VKApiModel}s, * list implementation line {@link org.flycraft.vkontakteapi.model.VKList}, {@link org.flycraft.vkontakteapi.model.VKAttachments.VKApiAttachment} or {@link org.flycraft.vkontakteapi.model.VKPhotoSizes}, * {@link org.flycraft.vkontakteapi.model.VKApiModel}s. * <p/> * 4. Boolean fields defines by vk_int == 1 expression. * * @param object object to initialize * @param source data to read values * @param <T> type of result * @return initialized according with given data object * @throws JSONException if source object structure is invalid */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException { if (source.has("response")) { source = source.optJSONObject("response"); } if (source == null) { return object; } for (Field field : object.getClass().getFields()) { field.setAccessible(true); String fieldName = field.getName(); Class<?> fieldType = field.getType(); Object value = source.opt(fieldName); if (value == null) { continue; } try { if (fieldType.isPrimitive() && value instanceof Number) { Number number = (Number) value; if (fieldType.equals(int.class)) { field.setInt(object, number.intValue()); } else if (fieldType.equals(long.class)) { field.setLong(object, number.longValue()); } else if (fieldType.equals(float.class)) { field.setFloat(object, number.floatValue()); } else if (fieldType.equals(double.class)) { field.setDouble(object, number.doubleValue()); } else if (fieldType.equals(boolean.class)) { field.setBoolean(object, number.intValue() == 1); } else if (fieldType.equals(short.class)) { field.setShort(object, number.shortValue()); } else if (fieldType.equals(byte.class)) { field.setByte(object, number.byteValue()); } } else { Object result = field.get(object); if (value.getClass().equals(fieldType)) { result = value; } else if (fieldType.isArray() && value instanceof JSONArray) { result = parseArrayViaReflection((JSONArray) value, fieldType); } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKList.class.equals(fieldType)) { ParameterizedType genericTypes = (ParameterizedType) field.getGenericType(); Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0]; if (VKApiModel.class.isAssignableFrom(genericType) && Identifiable.class.isAssignableFrom(genericType)) { if (value instanceof JSONArray) { result = new VKList((JSONArray) value, genericType); } else if (value instanceof JSONObject) { result = new VKList((JSONObject) value, genericType); } } } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) { result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value); } field.set(object, result); } } catch (InstantiationException e) { throw new JSONException(e.getMessage()); } catch (IllegalAccessException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodException e) { throw new JSONException(e.getMessage()); } catch (InvocationTargetException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodError e) { // ?: // , getFields() . // ? ? ?, ? ?, Android ? . throw new JSONException(e.getMessage()); } } return object; }
From source file:com.vk.sdkweb.api.model.ParseUtils.java
/** * Parses object with follow rules:/*from w w w .ja v a 2 s . c o m*/ * * 1. All fields should had a public access. * 2. The name of the filed should be fully equal to name of JSONObject key. * 3. Supports parse of all Java primitives, all {@link java.lang.String}, * arrays of primitive types, {@link java.lang.String}s and {@link com.vk.sdkweb.api.model.VKApiModel}s, * list implementation line {@link com.vk.sdkweb.api.model.VKList}, {@link com.vk.sdkweb.api.model.VKAttachments.VKAttachment} or {@link com.vk.sdkweb.api.model.VKPhotoSizes}, * {@link com.vk.sdkweb.api.model.VKApiModel}s. * * 4. Boolean fields defines by vk_int == 1 expression. * * @param object object to initialize * @param source data to read values * @param <T> type of result * @return initialized according with given data object * @throws JSONException if source object structure is invalid */ @SuppressWarnings("rawtypes") public static <T> T parseViaReflection(T object, JSONObject source) throws JSONException { if (source.has("response")) { source = source.optJSONObject("response"); } if (source == null) { return object; } for (Field field : object.getClass().getFields()) { field.setAccessible(true); String fieldName = field.getName(); Class<?> fieldType = field.getType(); Object value = source.opt(fieldName); if (value == null) { continue; } try { if (fieldType.isPrimitive() && value instanceof Number) { Number number = (Number) value; if (fieldType.equals(int.class)) { field.setInt(object, number.intValue()); } else if (fieldType.equals(long.class)) { field.setLong(object, number.longValue()); } else if (fieldType.equals(float.class)) { field.setFloat(object, number.floatValue()); } else if (fieldType.equals(double.class)) { field.setDouble(object, number.doubleValue()); } else if (fieldType.equals(boolean.class)) { field.setBoolean(object, number.intValue() == 1); } else if (fieldType.equals(short.class)) { field.setShort(object, number.shortValue()); } else if (fieldType.equals(byte.class)) { field.setByte(object, number.byteValue()); } } else { Object result = field.get(object); if (value.getClass().equals(fieldType)) { result = value; } else if (fieldType.isArray() && value instanceof JSONArray) { result = parseArrayViaReflection((JSONArray) value, fieldType); } else if (VKPhotoSizes.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKAttachments.class.isAssignableFrom(fieldType) && value instanceof JSONArray) { Constructor<?> constructor = fieldType.getConstructor(JSONArray.class); result = constructor.newInstance((JSONArray) value); } else if (VKList.class.equals(fieldType)) { ParameterizedType genericTypes = (ParameterizedType) field.getGenericType(); Class<?> genericType = (Class<?>) genericTypes.getActualTypeArguments()[0]; if (VKApiModel.class.isAssignableFrom(genericType) && Parcelable.class.isAssignableFrom(genericType) && Identifiable.class.isAssignableFrom(genericType)) { if (value instanceof JSONArray) { result = new VKList((JSONArray) value, genericType); } else if (value instanceof JSONObject) { result = new VKList((JSONObject) value, genericType); } } } else if (VKApiModel.class.isAssignableFrom(fieldType) && value instanceof JSONObject) { result = ((VKApiModel) fieldType.newInstance()).parse((JSONObject) value); } field.set(object, result); } } catch (InstantiationException e) { throw new JSONException(e.getMessage()); } catch (IllegalAccessException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodException e) { throw new JSONException(e.getMessage()); } catch (InvocationTargetException e) { throw new JSONException(e.getMessage()); } catch (NoSuchMethodError e) { // ?: // , getFields() . // ? ? ?, ? ?, Android ? . throw new JSONException(e.getMessage()); } } return object; }
From source file:de.thkwalter.et.betriebspunkt.BetriebspunktTest.java
/** * Test der Methode {@link Betriebspunkt#getP_s()}. * /*from w w w .j a v a2 s . c o m*/ * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException */ @Test public void testGetP_s() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // Die in diesem Test verwendete Scheinleistung (in kVA) wird im Prfling gespeichert. Field p_sFeld = Betriebspunkt.class.getDeclaredField("p_s"); p_sFeld.setAccessible(true); p_sFeld.setDouble(this.betriebspunkt, 1.9820); // Es wird berprft, ob die Scheinleistung (in kVA) korrekt zurckgegeben wird. assertEquals(1.9820, this.betriebspunkt.getP_s(), 0.0); }
From source file:android.reflect.ClazzLoader.java
/** * ????/*from w w w . jav a 2s . c o m*/ */ public static <O, V> void setFieldValue(O o, Field field, V v) { if (field != null) { try { field.setAccessible(true); if (v == null) { field.set(o, null); } else { Class<?> vType = v.getClass(); if (vType == Integer.TYPE) { field.setInt(o, (Integer) v); } else if (vType == Long.TYPE) { field.setLong(o, (Long) v); } else if (vType == Boolean.TYPE) { field.setBoolean(o, (Boolean) v); } else if (vType == Float.TYPE) { field.setFloat(o, (Float) v); } else if (vType == Short.TYPE) { field.setShort(o, (Short) v); } else if (vType == Byte.TYPE) { field.setByte(o, (Byte) v); } else if (vType == Double.TYPE) { field.setDouble(o, (Double) v); } else if (vType == Character.TYPE) { field.setChar(o, (Character) v); } else { field.set(o, v); } } } catch (Throwable t) { Log.e(TAG, t); } } }
From source file:de.thkwalter.et.betriebspunkt.BetriebspunktTest.java
/** * Test der Methode {@link Betriebspunkt#getI_L()}. * // ww w . j a v a 2 s. c o m * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException */ @Test public void testGetI_L() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // Der in diesem Test verwendete effektive Leiterstrom (in A) wird im Prfling gespeichert. Field i_L_Feld = Betriebspunkt.class.getDeclaredField("i_L"); i_L_Feld.setAccessible(true); i_L_Feld.setDouble(this.betriebspunkt, 2.8769); // Es wird berprft, ob der effektive Leiterstrom (in A) korrekt zurckgegeben wird. assertEquals(2.8769, this.betriebspunkt.getI_L(), 0.0); }
From source file:de.thkwalter.et.betriebspunkt.BetriebspunktTest.java
/** * Test der Methode {@link Betriebspunkt#getU_LL()}. * /*from ww w. j ava2s. c om*/ * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException */ @Test public void testGetU_LL() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // Die in diesem Test verwendete effektive Leiter-Leiterspannung (in V) wird im Prfling gespeichert. Field u_LLFeld = Betriebspunkt.class.getDeclaredField("u_LL"); u_LLFeld.setAccessible(true); u_LLFeld.setDouble(this.betriebspunkt, 397.72); // Es wird berprft, ob die effektive Leiter-Leiterspannung (in A) korrekt zurckgegeben wird. assertEquals(397.72, this.betriebspunkt.getU_LL(), 0.0); }
From source file:de.thkwalter.et.betriebspunkt.BetriebspunktTest.java
/** * Test der Methode {@link Betriebspunkt#getP_el()}. * /* www . j a va 2s.c om*/ * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException */ @Test public void testGetP_el() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // Die in diesem Test verwendete elektrische Leistung (in kW) wird im Prfling gespeichert. Field p_elFeld = Betriebspunkt.class.getDeclaredField("p_el"); p_elFeld.setAccessible(true); p_elFeld.setDouble(this.betriebspunkt, 1.6462); // Es wird berprft, ob die elektrische Leistung (in kW) korrekt zurckgegeben wird. assertEquals(1.6462, this.betriebspunkt.getP_el(), 0.0); }
From source file:de.thkwalter.et.betriebspunkt.BetriebspunktTest.java
/** * Test der Methode {@link Betriebspunkt#getCosPhi()}. * /* www. j ava 2 s. c o m*/ * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalAccessException * @throws IllegalArgumentException */ @Test public void testGetCosPhi() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // Die in diesem Test verwendete Phasenverschiebung zwischen Strangstrom und Strangspannung wird im Prfling // gespeichert. Field cosPhiFeld = Betriebspunkt.class.getDeclaredField("cosPhi"); cosPhiFeld.setAccessible(true); cosPhiFeld.setDouble(this.betriebspunkt, 0.83062); // Es wird berprft, ob die Phasenverschiebung zwischen Strangstrom und Strangspannung korrekt zurckgegeben wird. assertEquals(0.83062, this.betriebspunkt.getCosPhi(), 0.0); }