List of usage examples for java.beans PropertyDescriptor getReadMethod
public synchronized Method getReadMethod()
From source file:net.mojodna.searchable.util.SearchableUtils.java
/** * Gets the boost factor for a specified property. * //from w w w. j a va 2s . c o m * @param descriptor Property descriptor. * @return Boost factor for a specified property. */ public static final float getBoost(final PropertyDescriptor descriptor) { final Annotation annotation = AnnotationUtils.getAnnotation(descriptor.getReadMethod(), Searchable.Indexed.class); if (annotation instanceof Searchable.Indexed) { final Searchable.Indexed i = (Searchable.Indexed) annotation; return i.boost(); } return Searchable.DEFAULT_BOOST_VALUE; }
From source file:com.mawujun.utils.bean.BeanUtils.java
/** * ????//ww w . jav a 2s . c o m * null * @param source * @param target * @throws BeansException * @throws IntrospectionException */ public static void copyExcludeNull(Object source, Object target) throws IntrospectionException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { try { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (value == null) {//?? continue; } Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } catch (Throwable ex) { throw new RuntimeException("Could not copy properties from source to target", ex); } } } } }
From source file:net.mojodna.searchable.util.SearchableUtils.java
/** * How should the specified property be indexed? * /* w w w . j av a 2 s . c om*/ * @param descriptor Property descriptor. * @return How the specified property should be indexed. */ public static final Field.Index getIndexStyle(final PropertyDescriptor descriptor) { final Annotation annotation = AnnotationUtils.getAnnotation(descriptor.getReadMethod(), Searchable.Indexed.class); if (null != annotation) { if (((Searchable.Indexed) annotation).tokenized()) { return Field.Index.TOKENIZED; } else { return Field.Index.UN_TOKENIZED; } } return Field.Index.NO; }
From source file:PropertyUtils.java
/** * Get specified property value//from w ww. java 2s . c o m */ public static Object getProperty(Object bean, String property) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { PropertyDescriptor descriptor = getPropertyDescriptor(bean.getClass(), property); if (descriptor == null) throw new NoSuchMethodException("Cannot find property " + bean.getClass().getName() + "." + property); Method method = descriptor.getReadMethod(); if (method == null) throw new NoSuchMethodException("Cannot find getter for " + bean.getClass().getName() + "." + property); return method.invoke(bean, null); }
From source file:org.zht.framework.util.ZBeanUtil.java
private static void copy(Object source, Object target, Boolean ignorNull, Class<?> editable, String... ignoreProperties) throws BeansException { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); if (editable != null) { if (!editable.isInstance(target)) { throw new IllegalArgumentException("Target class [" + target.getClass().getName() + "] not assignable to Editable class [" + editable.getName() + "]"); }/*from w w w . j a v a 2 s. c om*/ actualEditable = editable; } PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); for (PropertyDescriptor targetPd : targetPds) { Method writeMethod = targetPd.getWriteMethod(); if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null) { Method readMethod = sourcePd.getReadMethod(); if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); if (ignorNull != null && ignorNull) { if (value != null && (!"[]".equals(value.toString()))) {// ? if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } else { if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { writeMethod.setAccessible(true); } writeMethod.invoke(target, value); } } catch (Throwable ex) { throw new FatalBeanException( "Could not copy property '" + targetPd.getName() + "' from source to target", ex); } } } } } }
From source file:org.openlmis.fulfillment.testutils.DtoGenerator.java
private static <T> void generate(Class<T> clazz) { Object instance;// ww w . j ava 2 s. c o m try { instance = clazz.newInstance(); } catch (Exception exp) { throw new IllegalStateException("Missing no args constructor", exp); } for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(clazz)) { if ("class".equals(descriptor.getName())) { continue; } if (null == descriptor.getReadMethod() || null == descriptor.getWriteMethod()) { // we support only full property (it has to have a getter and setter) continue; } try { Object value = generateValue(clazz, descriptor.getPropertyType()); PropertyUtils.setProperty(instance, descriptor.getName(), value); } catch (Exception exp) { throw new IllegalStateException("Can't set value for property: " + descriptor.getName(), exp); } } REFERENCES.put(clazz, instance); }
From source file:cn.fql.utility.ClassUtility.java
/** * Export property value of specified object to a map * * @param obj object instance * @param isWithNullable denote whether it is required to skip null value * @return map instance includes property value *///w ww .j av a 2s .co m public static Map collectAsMap(Object obj, boolean isWithNullable) { Map mapOutput = new HashMap(); PropertyDescriptor[] pds; try { pds = exportPropertyDesc(obj.getClass()); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; Method getter = pd.getReadMethod(); if (getter != null && !pd.getName().equals("class")) { Object value = getter.invoke(obj); if (value == null) { if (!isWithNullable) { continue; } } if (pd.getPropertyType().equals(String.class) && value == null) { value = ""; } mapOutput.put(pd.getName(), value); } } } catch (Exception e) { System.out.println(e.getMessage()); } return mapOutput; }
From source file:com.zigbee.framework.common.util.BeanCopyUtil.java
/** * Override copyProperties Method// www.j ava 2s. c o m * @Date : 2011-8-5 * @param source source bean instance * @param target destination bean instance */ public static void copyProperties(Object source, Object target) { Assert.notNull(source, "Source must not be null"); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getWriteMethod() != null) { try { PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), targetPd.getName()); if (sourcePd != null && sourcePd.getReadMethod() != null) { Method readMethod = sourcePd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(source); //Check whether the value is empty, only copy the properties which are not empty if (value != null) { Method writeMethod = targetPd.getWriteMethod(); if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } writeMethod.invoke(target, value); } } } catch (BeansException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } catch (SecurityException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } catch (IllegalArgumentException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } catch (IllegalAccessException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } catch (InvocationTargetException e) { e.printStackTrace(); String errMsg = "BEAN COPY Exception!" + e.getMessage(); logger.error(errMsg); } } } }
From source file:org.eclipse.wb.internal.swing.databinding.model.generic.GenericUtils.java
public static IGenericType getObjectType(TypeVariable<?> superTypeParameter, Type superTypeParameterClass, PropertyDescriptor descriptor) { Method readMethod = descriptor.getReadMethod(); Class<?> rawType = descriptor.getPropertyType(); if (readMethod == null) { return new ClassGenericType(rawType, null, null); }/* w ww . j a v a 2 s .com*/ Type type = readMethod.getGenericReturnType(); if (type instanceof Class<?> || type instanceof TypeVariable<?>) { return new ClassGenericType(rawType, null, null); } if (type instanceof ParameterizedType) { GenericTypeContainer genericType = new GenericTypeContainer(rawType); ParameterizedType parameterizedType = (ParameterizedType) type; // if (superTypeParameter != null && parameterizedType.getActualTypeArguments().length == 1 && superTypeParameter == parameterizedType.getActualTypeArguments()[0]) { genericType.getSubTypes().add(resolveType(superTypeParameterClass)); return genericType; } for (Type subType : parameterizedType.getActualTypeArguments()) { genericType.getSubTypes().add(resolveType(subType)); } return genericType; } if (type instanceof GenericArrayType) { int dimension = 0; Type elementType = null; GenericArrayType arrayType = (GenericArrayType) type; while (true) { dimension++; elementType = arrayType.getGenericComponentType(); if (elementType instanceof GenericArrayType) { arrayType = (GenericArrayType) elementType; continue; } break; } GenericTypeContainer genericType = new GenericTypeContainer(rawType, dimension); genericType.getSubTypes().add(resolveType(elementType)); return genericType; } Assert.fail(MessageFormat.format("Undefine type: {0} {1}", readMethod, rawType)); return null; }
From source file:jp.terasoluna.fw.web.struts.actions.FileDownloadUtil.java
/** * uEU_E??[h?B/*from w w w. ja va 2 s.c o m*/ * * @param result _E??[hf?[^?CX^X?B * @param request NGXg?B * @param response X|X?B */ @SuppressWarnings("unchecked") public static void download(Object result, HttpServletRequest request, HttpServletResponse response) { List<AbstractDownloadObject> downloadList = new ArrayList<AbstractDownloadObject>(); if (result instanceof AbstractDownloadObject) { downloadList.add((AbstractDownloadObject) result); } else { BeanWrapper wrapper = new BeanWrapperImpl(result); PropertyDescriptor[] propertyDescriptors = wrapper.getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod == null) { continue; } Class type = readMethod.getReturnType(); if (AbstractDownloadObject.class.isAssignableFrom(type)) { downloadList .add((AbstractDownloadObject) wrapper.getPropertyValue(propertyDescriptor.getName())); } } } if (downloadList.isEmpty()) { return; } // _E??[hIuWFNg???O if (downloadList.size() != 1) { throw new SystemException(new IllegalStateException("Too many AbstractDownloadObject properties."), TOO_MANY_DOWNLOAD_ERROR); } try { download(downloadList.get(0), request, response, true); } catch (SocketException e) { if (log.isDebugEnabled()) { log.debug(e.getMessage(), e); } } catch (IOException e) { if (log.isErrorEnabled()) { log.error("IOException has occurred while downloading", e); } } }