List of usage examples for java.lang.reflect Field setLong
@CallerSensitive @ForceInline public void setLong(Object obj, long l) throws IllegalArgumentException, IllegalAccessException
From source file:com.vk.sdkweb.api.model.ParseUtils.java
/** * Parses object with follow rules:// w w w . j av a 2 s. c om * * 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:android.reflect.ClazzLoader.java
/** * ????// w w w . ja v a2 s . 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:org.apache.openjpa.enhance.Reflection.java
/** * Set the value of the given field in the given object. *//*from ww w . j a va2s.c o m*/ public static void set(Object target, Field field, long value) { if (target == null || field == null) return; makeAccessible(field, field.getModifiers()); try { field.setLong(target, value); } catch (Throwable t) { throw wrapReflectionException(t, _loc.get("set-field", new Object[] { target, field, value, "long" })); } }
From source file:org.acoveo.tools.Reflection.java
/** * Set the value of the given field in the given object. *//*from ww w . j av a 2s . c o m*/ public static void set(Object target, Field field, long value) { if (target == null || field == null) return; makeAccessible(field, field.getModifiers()); try { field.setLong(target, value); } catch (Throwable t) { throw wrapReflectionException(t); } }
From source file:com.fjn.helper.common.io.file.common.FileUpAndDownloadUtil.java
/** * ???????//from w ww . j a v a 2s .c om * @param klass ???klass?Class * @param filepath ? * @param sizeThreshold ?? * @param isFileNameBaseTime ???? * @return bean */ public static <T> Object upload(HttpServletRequest request, Class<T> klass, String filepath, int sizeThreshold, boolean isFileNameBaseTime) throws Exception { FileItemFactory fileItemFactory = null; if (sizeThreshold > 0) { File repository = new File(filepath); fileItemFactory = new DiskFileItemFactory(sizeThreshold, repository); } else { fileItemFactory = new DiskFileItemFactory(); } ServletFileUpload upload = new ServletFileUpload(fileItemFactory); ServletRequestContext requestContext = new ServletRequestContext(request); T bean = null; if (klass != null) { bean = klass.newInstance(); } // if (ServletFileUpload.isMultipartContent(requestContext)) { File parentDir = new File(filepath); List<FileItem> fileItemList = upload.parseRequest(requestContext); for (int i = 0; i < fileItemList.size(); i++) { FileItem item = fileItemList.get(i); // ?? if (item.isFormField()) { String paramName = item.getFieldName(); String paramValue = item.getString("UTF-8"); log.info("?" + paramName + "=" + paramValue); request.setAttribute(paramName, paramValue); // ?bean if (klass != null) { Field field = null; try { field = klass.getDeclaredField(paramName); if (field != null) { field.setAccessible(true); Class type = field.getType(); if (type == Integer.TYPE) { field.setInt(bean, Integer.valueOf(paramValue)); } else if (type == Double.TYPE) { field.setDouble(bean, Double.valueOf(paramValue)); } else if (type == Float.TYPE) { field.setFloat(bean, Float.valueOf(paramValue)); } else if (type == Boolean.TYPE) { field.setBoolean(bean, Boolean.valueOf(paramValue)); } else if (type == Character.TYPE) { field.setChar(bean, paramValue.charAt(0)); } else if (type == Long.TYPE) { field.setLong(bean, Long.valueOf(paramValue)); } else if (type == Short.TYPE) { field.setShort(bean, Short.valueOf(paramValue)); } else { field.set(bean, paramValue); } } } catch (NoSuchFieldException e) { log.info("" + klass.getName() + "?" + paramName); } } } // else { // <input type='file' name='xxx'> xxx String paramName = item.getFieldName(); log.info("?" + item.getSize()); if (sizeThreshold > 0) { if (item.getSize() > sizeThreshold) continue; } String clientFileName = item.getName(); int index = -1; // ?IE? if ((index = clientFileName.lastIndexOf("\\")) != -1) { clientFileName = clientFileName.substring(index + 1); } if (clientFileName == null || "".equals(clientFileName)) continue; String filename = null; log.info("" + paramName + "\t??" + clientFileName); if (isFileNameBaseTime) { filename = buildFileName(clientFileName); } else filename = clientFileName; request.setAttribute(paramName, filename); // ?bean if (klass != null) { Field field = null; try { field = klass.getDeclaredField(paramName); if (field != null) { field.setAccessible(true); field.set(bean, filename); } } catch (NoSuchFieldException e) { log.info("" + klass.getName() + "? " + paramName); continue; } } if (!parentDir.exists()) { parentDir.mkdirs(); } File newfile = new File(parentDir, filename); item.write(newfile); String serverPath = newfile.getPath(); log.info("?" + serverPath); } } } return bean; }
From source file:com.baidu.api.client.core.VersionService.java
private void setField(Field field, String value) throws Exception { field.setAccessible(true);/*from ww w. ja v a2 s . c om*/ Class<?> cls = field.getType(); if (cls.equals(int.class)) { field.setInt(this, Integer.parseInt(value)); } else if (cls.equals(long.class)) { field.setLong(this, Long.parseLong(value)); } else if (cls.equals(boolean.class)) { field.setBoolean(this, Boolean.parseBoolean(value)); } else if (cls.equals(Integer.class)) { field.set(this, Integer.parseInt(value)); } else if (cls.equals(Long.class)) { field.set(this, Long.parseLong(value)); } else if (cls.equals(Boolean.class)) { field.set(this, Boolean.parseBoolean(value)); } else { field.set(this, value); } }
From source file:org.kv.KVConfig.java
/** * Code to read a json file into this structure. * Sorta possible with the json library, but this works * * @param configFile File to read./*from w w w . j a va2 s .co m*/ * @return A KVConfig instance with values set from the file. */ public static KVConfig load(File configFile) { try { BufferedReader in = new BufferedReader(new FileReader(configFile)); KVConfig config = new KVConfig(); // read the whole file into a single string String jsonStr = ""; String line; while ((line = in.readLine()) != null) jsonStr += line + "\n"; in.close(); // build a json object from the string JSONObject json = new JSONObject(jsonStr); // while there are keys, set the members of the KVConfig instance Iterator<?> keyiter = json.keys(); while (keyiter.hasNext()) { String key = (String) keyiter.next(); Field field = KVConfig.class.getField(key); // handle arrays first, then scalars if (field.getType().isArray()) { JSONArray array = json.getJSONArray(key); Class<?> component = field.getType().getComponentType(); Object value = Array.newInstance(component, array.length()); for (int i = 0; i < array.length(); i++) { if (component == int.class) { Array.setInt(value, i, array.getInt(i)); } if (component == long.class) { Array.setLong(value, i, array.getLong(i)); } else if (component == String.class) { Array.set(value, i, array.getString(i)); } else if (component == double.class) { Array.setDouble(value, i, array.getDouble(i)); } } field.set(config, value); } else if (field.getType() == int.class) { field.setInt(config, json.getInt(key)); } else if (field.getType() == long.class) { field.setLong(config, json.getLong(key)); } else if (field.getType() == String.class) { field.set(config, json.getString(key)); } else if (field.getType() == double.class) { field.set(config, json.getDouble(key)); } } return config; } catch (Exception e) { e.printStackTrace(); System.exit(-1); } return null; }
From source file:com.blackducksoftware.integration.hub.jenkins.site.BlackDuckHubUpdateSite.java
private void setLastAttempt(final long lastAttempt) { try {// w w w . j a va 2 s . c o m // try reflection to be safe for the parent class changing the location final Field field = UpdateSite.class.getDeclaredField("lastAttempt"); final boolean accessible = field.isAccessible(); try { field.setLong(this, lastAttempt); } finally { if (!accessible) { field.setAccessible(false); } } } catch (final Throwable e) { // ignore } this.lastAttempt = lastAttempt; }
From source file:com.blackducksoftware.integration.hub.jenkins.site.BlackDuckHubUpdateSite.java
/** * Sets the data timestamp (and tries to propagate the change to {@link UpdateSite#dataTimestamp} * *//*from w ww . j a va 2s .c o m*/ private void setDataTimestamp(final long dataTimestamp) { try { // try reflection to be safe for the parent class changing the location final Field field = UpdateSite.class.getDeclaredField("dataTimestamp"); final boolean accessible = field.isAccessible(); try { field.setLong(this, dataTimestamp); } finally { if (!accessible) { field.setAccessible(false); } } } catch (final Throwable e) { // ignore } this.dataTimestamp = dataTimestamp; }
From source file:com.cloudbees.jenkins.plugins.enterpriseplugins.CloudBeesUpdateSite.java
private void setLastAttempt(long lastAttempt) { try {/*ww w . j a v a 2 s . c o m*/ // try reflection to be safe for the parent class changing the location Field field = UpdateSite.class.getDeclaredField("lastAttempt"); boolean accessible = field.isAccessible(); try { field.setLong(this, lastAttempt); } finally { if (!accessible) { field.setAccessible(false); } } } catch (Throwable e) { // ignore } this.lastAttempt = lastAttempt; }