List of usage examples for java.lang.reflect Field getType
public Class<?> getType()
From source file:ambroafb.general.AnnotiationUtils.java
private static Object[] getNodesTypeAndContent(Field field, Object ownerClassObject) { Object[] results = new Object[2]; try {/*from w ww . ja v a 2s .c o m*/ boolean accessible = field.isAccessible(); field.setAccessible(true); if (field.getType().equals(TextField.class) || field.getType().getSuperclass().equals(NumberField.class)) { TextField textField = (TextField) field.get(ownerClassObject); results[0] = textField; results[1] = textField.getText(); } else if (field.getType().equals(ADatePicker.class)) { ADatePicker datePicker = (ADatePicker) field.get(ownerClassObject); results[0] = datePicker; results[1] = datePicker.getEditor().getText(); } else if (field.getType().equals(MapEditor.class)) { MapEditor mapEditor = (MapEditor) field.get(ownerClassObject); results[0] = mapEditor; results[1] = mapEditor.getEditor().getText(); } else if (field.getType().equals(ImageGalleryController.class)) { ImageGalleryController gallery = (ImageGalleryController) field.get(ownerClassObject); results[0] = gallery.getRoot(); results[1] = (gallery.isEmpty()) ? null : "not empty"; } else if (field.getType().equals(CountComboBox.class)) { CountComboBox countComboBox = (CountComboBox) field.get(ownerClassObject); results[0] = countComboBox; results[1] = (countComboBox.getBasket().isEmpty()) ? null : countComboBox.getValue(); } // Note: ClientComboBox is not ComboBox extened, so this case specific case: else if (field.getType().equals(ClientComboBox.class)) { ClientComboBox clientComboBox = (ClientComboBox) field.get(ownerClassObject); results[0] = clientComboBox; results[1] = (clientComboBox.getValue() == null) ? null : clientComboBox.getValue(); } else if (field.getType().toString().contains("ComboBox")) { ComboBox comboBox = (ComboBox) field.get(ownerClassObject); results[0] = comboBox; // Note: comboBox.getValue() may be null but some class may provides to make some action that avoid nullable and return empty string for example. So we check selection index. // int selectedIndex = comboBox.getSelectionModel().getSelectedIndex(); // results[1] = (comboBox.getValue() == null || selectedIndex < 0) ? null : comboBox.getValue(); results[1] = (comboBox.getValue() == null) ? null : comboBox.getValue(); } field.setAccessible(accessible); } catch (IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex); } return results; }
From source file:com.qumoon.commons.BeanMapper.java
public static <T> T convertProto2Bean(com.google.protobuf.GeneratedMessage message, T descObject, Class srcClass) {/*from ww w . jav a 2s .co m*/ for (Field srcField : srcClass.getDeclaredFields()) { Descriptors.FieldDescriptor fd = message.getDescriptorForType().findFieldByName(srcField.getName()); if (null == fd) { continue; } try { String fieldStrValue = String.valueOf(message.getField(fd)); if (fieldStrValue.isEmpty()) { continue; } if (srcField.getType() == BigDecimal.class) { PropertyUtils.setProperty(descObject, srcField.getName(), new BigDecimal(fieldStrValue)); } else { if (srcField.getType() == Date.class) { PropertyUtils.setProperty(descObject, srcField.getName(), new Date((Long) (message.getField(fd)))); } else { if (srcField.getType() == Byte.class) { PropertyUtils.setProperty(descObject, srcField.getName(), Byte.valueOf(fieldStrValue)); } else { PropertyUtils.setProperty(descObject, srcField.getName(), message.getField(fd)); } } } } catch (Exception e) { logger.error(e.getMessage()); } finally { continue; } } return descObject; }
From source file:com.fjn.helper.common.io.file.common.FileUpAndDownloadUtil.java
/** * ???????//from w w w . ja v a 2 s. co m * @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.eviware.x.form.support.ADialogBuilder.java
public static XFormDialog buildTabbedDialog(Class<? extends Object> tabbedFormClass, ActionList actions) { AForm formAnnotation = tabbedFormClass.getAnnotation(AForm.class); if (formAnnotation == null) { throw new RuntimeException("formClass is not annotated correctly.."); }//from ww w. j av a 2 s .c o m MessageSupport messages = MessageSupport.getMessages(tabbedFormClass); XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name()); for (Field field : tabbedFormClass.getFields()) { APage pageAnnotation = field.getAnnotation(APage.class); if (pageAnnotation != null) { buildForm(builder, pageAnnotation.name(), field.getType(), messages); } AField fieldAnnotation = field.getAnnotation(AField.class); if (fieldAnnotation != null) { try { Class<?> formClass = Class.forName(fieldAnnotation.description()); buildForm(builder, fieldAnnotation.name(), formClass, messages); } catch (Exception e) { SoapUI.logError(e); } } } ActionList defaultActions = StringUtils.isBlank(formAnnotation.helpUrl()) ? builder.buildOkCancelActions() : builder.buildOkCancelHelpActions(formAnnotation.helpUrl()); if (actions == null) { actions = defaultActions; } else { actions.addActions(defaultActions); } XFormDialog dialog = builder.buildDialog(actions, formAnnotation.description(), UISupport.createImageIcon(formAnnotation.icon())); return dialog; }
From source file:net.sf.jasperreports.engine.query.JRHibernateQueryExecuter.java
private static final Type loadTypeConstant(Class<?> typeConstantsClass, String name) { try {//w w w . j av a 2s.c o m Field constant = typeConstantsClass.getField(name); if (!Modifier.isStatic(constant.getModifiers()) || !Type.class.isAssignableFrom(constant.getType())) { throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_UNRESOLVED_TYPE_CONSTANT, new Object[] { name, typeConstantsClass.getName() }); } Type type = (Type) constant.get(null); return type; } catch (NoSuchFieldException e) { throw new JRRuntimeException(e); } catch (SecurityException e) { throw new JRRuntimeException(e); } catch (IllegalArgumentException e) { throw new JRRuntimeException(e); } catch (IllegalAccessException e) { throw new JRRuntimeException(e); } }
From source file:com.github.gekoh.yagen.util.FieldInfo.java
private static List<FieldInfo> convertFields(List<FieldInfo> fields, Class baseEntity) { for (Field field : baseEntity.getDeclaredFields()) { FieldInfo fi;/*from w w w . j a va 2 s. com*/ Class type = field.getType(); String name = field.getName(); Column column = field.getAnnotation(Column.class); if (field.isAnnotationPresent(Embedded.class)) { if (field.isAnnotationPresent(AttributeOverride.class)) { fi = new FieldInfo(type, name, field.getAnnotation(AttributeOverride.class)); } else { fi = new FieldInfo(type, name, field.getAnnotation(AttributeOverrides.class)); } } else if (field.isAnnotationPresent(Enumerated.class)) { fi = new FieldInfo(type, name, true, column); } else if (column != null && !field.isAnnotationPresent(CollectionTable.class)) { if (type.isPrimitive()) { if (type.equals(Boolean.TYPE)) { type = Boolean.class; } else if (type.equals(Long.TYPE)) { type = Long.class; } else if (type.equals(Integer.TYPE)) { type = Integer.class; } else if (type.equals(Short.TYPE)) { type = Short.class; } else if (type.equals(Byte.TYPE)) { type = Byte.class; } else if (type.equals(Double.TYPE)) { type = Double.class; } else if (type.equals(Float.TYPE)) { type = Float.class; } else if (type.equals(Character.TYPE)) { type = Character.class; } } fi = new FieldInfo(type, name, false, column); } else if ((field.isAnnotationPresent(ManyToOne.class) && !field.isAnnotationPresent(JoinTable.class)) || (field.isAnnotationPresent(OneToOne.class) && StringUtils.isEmpty(field.getAnnotation(OneToOne.class).mappedBy()))) { String columnName = field.isAnnotationPresent(JoinColumn.class) ? field.getAnnotation(JoinColumn.class).name() : field.getName(); fi = getIdFieldInfo(type, name, columnName); } else if (!field.isAnnotationPresent(Transient.class) && (Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type)) && (field.isAnnotationPresent(JoinColumn.class) || field.isAnnotationPresent(JoinTable.class) || field.isAnnotationPresent(CollectionTable.class))) { fi = new FieldInfo(type, name); } else { continue; } if (field.isAnnotationPresent(Type.class)) { fi.addAnnotation(field.getAnnotation(Type.class)); } fi.setField(field); fields.add(fi); } return fields; }
From source file:com.eviware.x.form.support.ADialogBuilder.java
public static XFormDialog buildTabbedDialogWithCustomActions(Class<? extends Object> tabbedFormClass, ActionList actions) {//from www .j a v a2s. c o m AForm formAnnotation = tabbedFormClass.getAnnotation(AForm.class); if (formAnnotation == null) { throw new RuntimeException("formClass is not annotated correctly.."); } MessageSupport messages = MessageSupport.getMessages(tabbedFormClass); XFormDialogBuilder builder = XFormFactory.createDialogBuilder(formAnnotation.name()); for (Field field : tabbedFormClass.getFields()) { APage pageAnnotation = field.getAnnotation(APage.class); if (pageAnnotation != null) { buildForm(builder, pageAnnotation.name(), field.getType(), messages); } AField fieldAnnotation = field.getAnnotation(AField.class); if (fieldAnnotation != null) { try { Class<?> formClass = Class.forName(fieldAnnotation.description()); buildForm(builder, fieldAnnotation.name(), formClass, messages); } catch (Exception e) { SoapUI.logError(e); } } } ActionList defaultActions = StringUtils.isBlank(formAnnotation.helpUrl()) ? null : builder.buildHelpActions(formAnnotation.helpUrl()); if (actions == null) { actions = defaultActions; } else { defaultActions.addActions(actions); actions = defaultActions; } XFormDialog dialog = builder.buildDialog(actions, formAnnotation.description(), UISupport.createImageIcon(formAnnotation.icon())); return dialog; }
From source file:com.google.gdt.eclipse.designer.uibinder.parser.UiBinderParser.java
/** * @return the argument value for parameter of given type. *///from w ww . j a v a 2 s.com private static Object getDefaultValue(Class<?> parameterType) throws Exception { // try INSTANCE field { Field instanceField = ReflectionUtils.getFieldByName(parameterType, "INSTANCE"); if (instanceField != null && ReflectionUtils.isStatic(instanceField) && parameterType.isAssignableFrom(instanceField.getType())) { return instanceField.get(null); } } // use default value return ReflectionUtils.getDefaultValue(parameterType); }
From source file:bdi4jade.util.ReflectionUtils.java
/** * Sets plan body fields annotated with {@link bdi4jade.annotation.Belief}. * /*w w w . j a va2 s . com*/ * @param planBody * the plan body to be setup with beliefs. */ public static void setupBeliefs(PlanBody planBody) { Capability capability = planBody.getPlan().getPlanLibrary().getCapability(); Class<?> currentClass = planBody.getClass(); while (PlanBody.class.isAssignableFrom(currentClass)) { for (Field field : currentClass.getDeclaredFields()) { boolean b = field.isAccessible(); field.setAccessible(true); try { if (field.isAnnotationPresent(bdi4jade.annotation.Belief.class)) { if (Belief.class.isAssignableFrom(field.getType())) { bdi4jade.annotation.Belief beliefAnnotation = field .getAnnotation(bdi4jade.annotation.Belief.class); String beliefName = beliefAnnotation.name(); if (beliefName == null || "".equals(beliefName)) { beliefName = field.getName(); } Belief<?, ?> belief = capability.getBeliefBase().getBelief(beliefName); field.set(planBody, belief); } else { throw new ClassCastException("Field " + field.getName() + " should be a Belief"); } } } catch (Exception exc) { log.warn(exc); } field.setAccessible(b); } currentClass = currentClass.getSuperclass(); } }
From source file:com.plusub.lib.annotate.JsonParserUtils.java
/** * ????JSON?/* w w w .j a va 2 s . c om*/ * <p>Title: getPageInfo * <p>Description: * @param jo * @param field * @return */ private static Object getPageInfo(JSONObject jo, Field field) { Object pageObj = null; try { pageObj = parserField(getInstance(field.getType().getName()), jo); } catch (Exception e) { // TODO Auto-generated catch block if (showLog) { e.printStackTrace(); } return pageObj; } return pageObj; }