List of usage examples for org.springframework.beans BeanUtils findPropertyForMethod
@Nullable public static PropertyDescriptor findPropertyForMethod(Method method) throws BeansException
From source file:com.eclecticlogic.pedal.dm.internal.MetamodelUtil.java
/** * @param attribute JPA metamodel attribute. * @param entity Entity to set the value on. * @param value Value to set.//from www. j a v a 2s . c o m */ public static <E extends Serializable, T extends Serializable> void set(Attribute<? super E, T> attribute, E entity, T value) { Member member = attribute.getJavaMember(); if (member instanceof Field) { Field field = (Field) member; field.setAccessible(true); try { field.set(entity, value); } catch (Exception e) { throw new RuntimeException(e); } } else if (member instanceof Method) { PropertyDescriptor pd = BeanUtils.findPropertyForMethod((Method) member); if (pd.getWriteMethod() != null) { pd.getWriteMethod().setAccessible(true); try { pd.getWriteMethod().invoke(entity, value); } catch (Exception e) { throw new RuntimeException(e); } } else { throw new RuntimeException( "No setter for " + attribute.getName() + " in " + entity.getClass().getName()); } } else { throw new RuntimeException("Failed to set " + attribute.getName() + " of type " + member.getClass().getName() + " in entity " + entity.getClass().getName()); } }
From source file:com.jinmibao.common.util.BeanUtil.java
/** * ? ????? ?/*from ww w . ja v a 2s .c o m*/ * * @param source * @param target */ public static void copyPropertiesIfNull(Object source, Object target) { if (source == null || target == null) { throw new RuntimeException("Source Or Target Can't Be Null"); } Method[] methods = target.getClass().getDeclaredMethods(); List<String> notNullPropertyList = new ArrayList<String>(); for (Method method : methods) { if (!method.getName().startsWith("get")) { continue; } try { Object methodResult = method.invoke(target); if (methodResult != null) { if (method.getReturnType().getName().equals("java.lang.String") && StringUtil.isBlank((String) methodResult)) { continue; } notNullPropertyList.add(BeanUtils.findPropertyForMethod(method).getName()); } } catch (Exception e) { logger.error("", e); } } BeanUtils.copyProperties(source, target, ListUtil.list2Array(notNullPropertyList)); }
From source file:com.github.jasonruckman.sidney.generator.BeanBuilder.java
public static <R> BeanBuilder<R> stub(final Class<R> type) { InstanceFactory<R> fact = new DefaultInstanceFactory<>(type); R stub = fact.newInstance();//from www . ja v a 2 s . co m ProxyFactory factory = new ProxyFactory(stub); final Stack<Method> m = new Stack<>(); factory.addAdvice(new MethodInterceptor() { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(methodInvocation.getMethod()); if (descriptor == null) { throw new IllegalStateException(String.format("Method %s is not a getter or setter", methodInvocation.getMethod().getName())); } m.push(methodInvocation.getMethod()); return defaultReturnObjects.get(methodInvocation.getMethod().getReturnType()); } }); return new BeanBuilder<>((R) factory.getProxy(), fact, m); }
From source file:com.ace.erp.common.inject.support.InjectBaseDependencyHelper.java
/** * ??/*from w ww.j av a 2 s.c o m*/ * * @param target * @param annotation */ private static Set<Object> findDependencies(final Object target, final Class<? extends Annotation> annotation) { final Set<Object> candidates = Sets.newHashSet(); ReflectionUtils.doWithFields(target.getClass(), new ReflectionUtils.FieldCallback() { @Override public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(field); Object obj = ReflectionUtils.getField(field, target); candidates.add(obj); } }, new ReflectionUtils.FieldFilter() { @Override public boolean matches(Field field) { return field.isAnnotationPresent(annotation); } }); ReflectionUtils.doWithMethods(target.getClass(), new ReflectionUtils.MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(method); PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method); candidates.add(ReflectionUtils.invokeMethod(descriptor.getReadMethod(), target)); } }, new ReflectionUtils.MethodFilter() { @Override public boolean matches(Method method) { boolean hasAnnotation = false; hasAnnotation = method.isAnnotationPresent(annotation); if (!hasAnnotation) { return false; } boolean hasReadMethod = false; PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method); hasReadMethod = descriptor != null && descriptor.getReadMethod() != null; if (!hasReadMethod) { return false; } return true; } }); return candidates; }
From source file:com.trigonic.utils.spring.cmdline.CommandLineMetaData.java
private PropertyDescriptor getPropertyForMethod(String annotationType, Annotation annotation, Method method) { PropertyDescriptor property = BeanUtils.findPropertyForMethod(method); if (property == null) { throw new BeanDefinitionStoreException( annotationType + " annotation cannot be applied to non-property methods"); }/*from www. j ava 2 s .c o m*/ return property; }
From source file:com.github.jasonruckman.sidney.generator.BeanBuilder.java
private Accessors.FieldAccessor accessor(Method method) { PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method); Class<?> declaringClass = method.getDeclaringClass(); Field field = Fields.getFieldByName(declaringClass, descriptor.getName()); return Accessors.newAccessor(field); }
From source file:org.querybyexample.jpa.JpaUtil.java
public static <T extends Identifiable<?>> String compositePkPropertyName(T entity) { for (Method m : entity.getClass().getMethods()) { if (m.getAnnotation(EmbeddedId.class) != null) { return BeanUtils.findPropertyForMethod(m).getName(); }//from ww w . ja v a2 s . c om } for (Field f : entity.getClass().getFields()) { if (f.getAnnotation(EmbeddedId.class) != null) { return f.getName(); } } return null; }
From source file:org.querybyexample.jpa.JpaUtil.java
public static String methodToProperty(Method m) { return BeanUtils.findPropertyForMethod(m).getName(); }
From source file:com.baidu.jprotobuf.pbrpc.spring.annotation.CommonAnnotationBeanPostProcessor.java
private InjectionMetadata findAnnotationMetadata(final Class clazz, final List<Class<? extends Annotation>> annotion) { // Quick check on the concurrent map first, with minimal locking. InjectionMetadata metadata = this.injectionMetadataCache.get(clazz); if (metadata == null) { synchronized (this.injectionMetadataCache) { metadata = this.injectionMetadataCache.get(clazz); if (metadata == null) { final InjectionMetadata newMetadata = new InjectionMetadata(clazz); ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() { public void doWith(Field field) { for (Class<? extends Annotation> anno : annotion) { Annotation annotation = field.getAnnotation(anno); if (annotation != null) { if (Modifier.isStatic(field.getModifiers())) { throw new IllegalStateException( "Autowired annotation is not supported on static fields"); }/*from w ww. j a v a2s.c o m*/ newMetadata.addInjectedField(new AutowiredFieldElement(field, annotation)); } } } }); ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() { public void doWith(Method method) { for (Class<? extends Annotation> anno : annotion) { Annotation annotation = method.getAnnotation(anno); if (annotation != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { if (Modifier.isStatic(method.getModifiers())) { throw new IllegalStateException( "Autowired annotation is not supported on static methods"); } if (method.getParameterTypes().length == 0) { throw new IllegalStateException( "Autowired annotation requires at least one argument: " + method); } PropertyDescriptor pd = BeanUtils.findPropertyForMethod(method); newMetadata .addInjectedMethod(new AutowiredMethodElement(method, annotation, pd)); } } } }); metadata = newMetadata; this.injectionMetadataCache.put(clazz, metadata); } } } return metadata; }
From source file:org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper.java
/** * Set the id of theObj to the uniqueId. Attempts to find a set method and * invoke it. If it fails it just swallows the exceptions and doesn't throw * them further.//from w w w. ja v a2s. c om * * @param theObj Object * @param uniqueId String */ public static void setUniqueId(Object theObj, String uniqueId) { Method annotatedMethod = ResourceInspector.findUniqueIdMethod(theObj.getClass()); if (annotatedMethod != null) { PropertyDescriptor pDesc = BeanUtils.findPropertyForMethod(annotatedMethod); if (pDesc != null) { Method writeMethod = pDesc.getWriteMethod(); if (writeMethod != null) { try { writeMethod.invoke(theObj, uniqueId); if (logger.isDebugEnabled()) { logger.debug("Unique id set for property: " + pDesc.getName()); } } catch (IllegalArgumentException error) { logger.warn("Invocation error", error); } catch (IllegalAccessException error) { logger.warn("IllegalAccessException", error); } catch (InvocationTargetException error) { logger.warn("InvocationTargetException", error); } } else { logger.warn("No setter method for property: " + pDesc.getName()); } } } }