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: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); System.out.println(f.getDouble(x)); }
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 ww.j a v a2 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:org.apache.lens.ml.AlgoArgParser.java
/** * Extracts feature names. If the algo has any parameters associated with @AlgoParam annotation, those are set * as well.//from w w w. j a va2s. c o m * * @param algo the algo * @param args the args * @return List of feature column names. */ public static List<String> parseArgs(MLAlgo algo, String[] args) { List<String> featureColumns = new ArrayList<String>(); Class<? extends MLAlgo> algoClass = algo.getClass(); // Get param fields Map<String, Field> fieldMap = new HashMap<String, Field>(); for (Field fld : algoClass.getDeclaredFields()) { fld.setAccessible(true); AlgoParam paramAnnotation = fld.getAnnotation(AlgoParam.class); if (paramAnnotation != null) { fieldMap.put(paramAnnotation.name(), fld); } } for (int i = 0; i < args.length; i += 2) { String key = args[i].trim(); String value = args[i + 1].trim(); try { if ("feature".equalsIgnoreCase(key)) { featureColumns.add(value); } else if (fieldMap.containsKey(key)) { Field f = fieldMap.get(key); if (String.class.equals(f.getType())) { f.set(algo, value); } else if (Integer.TYPE.equals(f.getType())) { f.setInt(algo, Integer.parseInt(value)); } else if (Double.TYPE.equals(f.getType())) { f.setDouble(algo, Double.parseDouble(value)); } else if (Long.TYPE.equals(f.getType())) { f.setLong(algo, Long.parseLong(value)); } else { // check if the algo provides a deserializer for this param String customParserClass = algo.getConf().getProperties().get("lens.ml.args." + key); if (customParserClass != null) { Class<? extends CustomArgParser<?>> clz = (Class<? extends CustomArgParser<?>>) Class .forName(customParserClass); CustomArgParser<?> parser = clz.newInstance(); f.set(algo, parser.parse(value)); } else { LOG.warn("Ignored param " + key + "=" + value + " as no parser found"); } } } } catch (Exception exc) { LOG.error("Error while setting param " + key + " to " + value + " for algo " + algo); } } return featureColumns; }
From source file:org.apache.lens.ml.TrainerArgParser.java
/** * Extracts feature names. If the trainer has any parameters associated with @TrainerParam annotation, those are set * as well./*from w w w .j a v a 2 s .com*/ * * @param trainer * the trainer * @param args * the args * @return List of feature column names. */ public static List<String> parseArgs(MLTrainer trainer, String[] args) { List<String> featureColumns = new ArrayList<String>(); Class<? extends MLTrainer> trainerClass = trainer.getClass(); // Get param fields Map<String, Field> fieldMap = new HashMap<String, Field>(); for (Field fld : trainerClass.getDeclaredFields()) { fld.setAccessible(true); TrainerParam paramAnnotation = fld.getAnnotation(TrainerParam.class); if (paramAnnotation != null) { fieldMap.put(paramAnnotation.name(), fld); } } for (int i = 0; i < args.length; i += 2) { String key = args[i].trim(); String value = args[i + 1].trim(); try { if ("feature".equalsIgnoreCase(key)) { featureColumns.add(value); } else if (fieldMap.containsKey(key)) { Field f = fieldMap.get(key); if (String.class.equals(f.getType())) { f.set(trainer, value); } else if (Integer.TYPE.equals(f.getType())) { f.setInt(trainer, Integer.parseInt(value)); } else if (Double.TYPE.equals(f.getType())) { f.setDouble(trainer, Double.parseDouble(value)); } else if (Long.TYPE.equals(f.getType())) { f.setLong(trainer, Long.parseLong(value)); } else { // check if the trainer provides a deserializer for this param String customParserClass = trainer.getConf().getProperties().get("lens.ml.args." + key); if (customParserClass != null) { Class<? extends CustomArgParser<?>> clz = (Class<? extends CustomArgParser<?>>) Class .forName(customParserClass); CustomArgParser<?> parser = clz.newInstance(); f.set(trainer, parser.parse(value)); } else { LOG.warn("Ignored param " + key + "=" + value + " as no parser found"); } } } } catch (Exception exc) { LOG.error("Error while setting param " + key + " to " + value + " for trainer " + trainer); } } return featureColumns; }
From source file:com.fjn.helper.common.io.file.common.FileUpAndDownloadUtil.java
/** * ???????/*from w w w. ja va 2s . com*/ * @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:org.sonar.api.batch.rule.Checks.java
private static void configureField(Object check, Field field, String value) { try {//from w ww. j av a2s .c o m field.setAccessible(true); if (field.getType().equals(String.class)) { field.set(check, value); } else if (int.class == field.getType()) { field.setInt(check, Integer.parseInt(value)); } else if (short.class == field.getType()) { field.setShort(check, Short.parseShort(value)); } else if (long.class == field.getType()) { field.setLong(check, Long.parseLong(value)); } else if (double.class == field.getType()) { field.setDouble(check, Double.parseDouble(value)); } else if (boolean.class == field.getType()) { field.setBoolean(check, Boolean.parseBoolean(value)); } else if (byte.class == field.getType()) { field.setByte(check, Byte.parseByte(value)); } else if (Integer.class == field.getType()) { field.set(check, Integer.parseInt(value)); } else if (Long.class == field.getType()) { field.set(check, Long.parseLong(value)); } else if (Double.class == field.getType()) { field.set(check, Double.parseDouble(value)); } else if (Boolean.class == field.getType()) { field.set(check, Boolean.parseBoolean(value)); } else { throw new SonarException( "The type of the field " + field + " is not supported: " + field.getType()); } } catch (IllegalAccessException e) { throw new SonarException( "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(), e); } }
From source file:org.sonar.plugins.openedge.foundation.OpenEdgeComponents.java
private static void configureField(Object check, Field field, String value) { try {/*from ww w. j a va 2 s . co m*/ field.setAccessible(true); if (field.getType().equals(String.class)) { field.set(check, value); } else if (int.class == field.getType()) { field.setInt(check, Integer.parseInt(value)); } else if (short.class == field.getType()) { field.setShort(check, Short.parseShort(value)); } else if (long.class == field.getType()) { field.setLong(check, Long.parseLong(value)); } else if (double.class == field.getType()) { field.setDouble(check, Double.parseDouble(value)); } else if (boolean.class == field.getType()) { field.setBoolean(check, Boolean.parseBoolean(value)); } else if (byte.class == field.getType()) { field.setByte(check, Byte.parseByte(value)); } else if (Integer.class == field.getType()) { field.set(check, new Integer(Integer.parseInt(value))); } else if (Long.class == field.getType()) { field.set(check, new Long(Long.parseLong(value))); } else if (Double.class == field.getType()) { field.set(check, new Double(Double.parseDouble(value))); } else if (Boolean.class == field.getType()) { field.set(check, Boolean.valueOf(Boolean.parseBoolean(value))); } else { throw MessageException .of("The type of the field " + field + " is not supported: " + field.getType()); } } catch (IllegalAccessException e) { throw MessageException.of( "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(), e); } }
From source file:org.apache.tika.server.resource.TikaResource.java
/** * Utility method to set a property on a class via reflection. * * @param httpHeaders the HTTP headers set. * @param object the <code>Object</code> to set the property on. * @param key the key of the HTTP Header. * @param prefix the name of the HTTP Header prefix used to find property. * @throws WebApplicationException thrown when field cannot be found. *//*w w w . jav a2 s . c o m*/ private static void processHeaderConfig(MultivaluedMap<String, String> httpHeaders, Object object, String key, String prefix) { try { String property = StringUtils.removeStart(key, prefix); Field field = object.getClass().getDeclaredField(StringUtils.uncapitalize(property)); field.setAccessible(true); if (field.getType() == String.class) { field.set(object, httpHeaders.getFirst(key)); } else if (field.getType() == int.class) { field.setInt(object, Integer.parseInt(httpHeaders.getFirst(key))); } else if (field.getType() == double.class) { field.setDouble(object, Double.parseDouble(httpHeaders.getFirst(key))); } else if (field.getType() == boolean.class) { field.setBoolean(object, Boolean.parseBoolean(httpHeaders.getFirst(key))); } else { //couldn't find a directly accessible field //try for setX(String s) String setter = StringUtils.uncapitalize(property); setter = "set" + setter.substring(0, 1).toUpperCase(Locale.US) + setter.substring(1); Method m = null; try { m = object.getClass().getMethod(setter, String.class); } catch (NoSuchMethodException e) { //swallow } if (m != null) { m.invoke(object, httpHeaders.getFirst(key)); } } } catch (Throwable ex) { throw new WebApplicationException( String.format(Locale.ROOT, "%s is an invalid %s header", key, X_TIKA_OCR_HEADER_PREFIX)); } }
From source file:com.browseengine.bobo.serialize.JSONSerializer.java
private static void loadObject(Object retObj, Field f, JSONObject jsonObj) throws JSONSerializationException { String key = f.getName();//from ww w . jav a 2s . co m Class type = f.getType(); try { if (type.isPrimitive()) { if (type == Integer.TYPE) { f.setInt(retObj, jsonObj.getInt(key)); } else if (type == Long.TYPE) { f.setLong(retObj, jsonObj.getInt(key)); } else if (type == Short.TYPE) { f.setShort(retObj, (short) jsonObj.getInt(key)); } else if (type == Boolean.TYPE) { f.setBoolean(retObj, jsonObj.getBoolean(key)); } else if (type == Double.TYPE) { f.setDouble(retObj, jsonObj.getDouble(key)); } else if (type == Float.TYPE) { f.setFloat(retObj, (float) jsonObj.getDouble(key)); } else if (type == Character.TYPE) { char ch = jsonObj.getString(key).charAt(0); f.setChar(retObj, ch); } else if (type == Byte.TYPE) { f.setByte(retObj, (byte) jsonObj.getInt(key)); } else { throw new JSONSerializationException("Unknown primitive: " + type); } } else if (type == String.class) { f.set(retObj, jsonObj.getString(key)); } else if (JSONSerializable.class.isAssignableFrom(type)) { JSONObject jObj = jsonObj.getJSONObject(key); JSONSerializable serObj = deSerialize(type, jObj); f.set(retObj, serObj); } } catch (Exception e) { throw new JSONSerializationException(e.getMessage(), e); } }
From source file:org.apache.openjpa.enhance.Reflection.java
/** * Set the value of the given field in the given object. *//*from w w w .j av a 2 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, _loc.get("set-field", new Object[] { target, field, value, "double" })); } }