本文整理汇总了Java中java.beans.PropertyDescriptor.getWriteMethod方法的典型用法代码示例。如果您正苦于以下问题:Java PropertyDescriptor.getWriteMethod方法的具体用法?Java PropertyDescriptor.getWriteMethod怎么用?Java PropertyDescriptor.getWriteMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.beans.PropertyDescriptor
的用法示例。
在下文中一共展示了PropertyDescriptor.getWriteMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: _getMergedWriteMethod
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* Necessary because no support for PropertyDescriptor.setWriteMethod()
* until JDK 1.2.
*/
private static Method _getMergedWriteMethod(
PropertyDescriptor secondaryDescriptor,
PropertyDescriptor primaryDescriptor
)
{
//
// merge the write method
//
Method writeMethod = primaryDescriptor.getWriteMethod();
// Give priority to the primary write method.
if (writeMethod == null)
{
writeMethod = secondaryDescriptor.getWriteMethod();
}
return writeMethod;
}
示例2: run
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
public void run() {
for (String name : this.names) {
Object bean;
try {
bean = this.loader.loadClass(name).newInstance();
} catch (Exception exception) {
throw new Error("could not instantiate bean: " + name, exception);
}
if (this.loader != bean.getClass().getClassLoader()) {
throw new Error("bean class loader is not equal to default one");
}
PropertyDescriptor[] pds = getPropertyDescriptors(bean);
for (PropertyDescriptor pd : pds) {
Class type = pd.getPropertyType();
Method setter = pd.getWriteMethod();
Method getter = pd.getReadMethod();
if (type.equals(String.class)) {
executeMethod(setter, bean, "Foo");
} else if (type.equals(int.class)) {
executeMethod(setter, bean, Integer.valueOf(1));
}
executeMethod(getter, bean);
}
}
}
示例3: serializablePropertiesFor
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* @deprecated As of 1.3.1, use {@link #propertiesFor(Class)} instead
*/
@Deprecated
public Iterator<BeanProperty> serializablePropertiesFor(final Class<?> type) {
final Collection<BeanProperty> beanProperties = new ArrayList<BeanProperty>();
final Collection<PropertyDescriptor> descriptors = buildMap(type).values();
for (final PropertyDescriptor descriptor : descriptors) {
if (descriptor.getReadMethod() != null && descriptor.getWriteMethod() != null) {
beanProperties.add(new BeanProperty(type, descriptor.getName(), descriptor.getPropertyType()));
}
}
return beanProperties.iterator();
}
示例4: toBean
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* 将map 转为 bean
*/
public static <T> T toBean(Map<String, Object> beanMap, Class<T> valueType) {
T bean = BeanUtils.newInstance(valueType);
PropertyDescriptor[] beanPds = getPropertyDescriptors(valueType);
for (PropertyDescriptor propDescriptor : beanPds) {
String propName = propDescriptor.getName();
// 过滤class属性
if (propName.equals("class")) {
continue;
}
if (beanMap.containsKey(propName)) {
Method writeMethod = propDescriptor.getWriteMethod();
if (null == writeMethod) {
continue;
}
Object value = beanMap.get(propName);
if (!writeMethod.isAccessible()) {
writeMethod.setAccessible(true);
}
try {
writeMethod.invoke(bean, value);
} catch (Throwable e) {
throw new RuntimeException("Could not set property '" + propName + "' to bean", e);
}
}
}
return bean;
}
示例5: updateProperty
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* Set a value on a Bean, if its a persistent bean, try to use an update method first
* (for v1.0 domain objects), otherwise use the setter (for v1.1 and above).
*/
static void updateProperty(PropertyDescriptor pd, Object value, Object source)
throws IllegalAccessException, InvocationTargetException, TransformException {
if (source instanceof Persistent) {
if (pd != null && pd.getWriteMethod() != null) {
try {
Method m = source.getClass().getMethod("update" + StringHelper.getUpper1(pd.getName()), pd.getWriteMethod().getParameterTypes());
if (!m.isAccessible())
m.setAccessible(true);
Class tClass = m.getParameterTypes()[0];
if (value == null || tClass.isAssignableFrom(value.getClass())) {
m.invoke(source, new Object[]{value});
if (log.isDebugEnabled())
log.debug("Update property '" + pd.getName() + '=' + value + "' on object '" + source.getClass().getName() + '\'');
// See if there is a datatype mapper for these classes
} else if (DataTypeMapper.instance().isMappable(value.getClass(), tClass)) {
value = DataTypeMapper.instance().map(value, tClass);
m.invoke(source, new Object[]{value});
if (log.isDebugEnabled())
log.debug("Translate+Update property '" + pd.getName() + '=' + value + "' on object '" + source.getClass().getName() + '\'');
} else {
// Data type mismatch
throw new TransformException(TransformException.DATATYPE_MISMATCH, source.getClass().getName() + '.' + m.getName(), tClass.getName(), value.getClass().getName());
}
} catch (NoSuchMethodException e) {
if (log.isDebugEnabled())
log.debug("No Updator, try Setter for DO property '" + pd.getName() + "' on object '" + source.getClass().getName() + '\'');
// try to use the setter if there is no update method
setProperty(pd, value, source);
}
} else {
TransformException me = new TransformException(TransformException.NO_SETTER, null,
pd == null ? "???" : pd.getName(), source.getClass().getName());
log.error(me.getLocalizedMessage());
throw me;
}
} else
setProperty(pd, value, source);
}
示例6: findPropertyForMethod
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
public PropertyDescriptor findPropertyForMethod(Method method)
{
for( PropertyDescriptor pd : propertyMap.values() )
{
if( pd.getReadMethod() == method || pd.getWriteMethod() == method )
{
return pd;
}
}
return null;
}
示例7: EntityField
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* 构造方法
*
* @param field 字段
* @param propertyDescriptor 字段name对应的property
*/
public EntityField(Field field, PropertyDescriptor propertyDescriptor) {
if (field != null) {
this.field = field;
this.name = field.getName();
this.javaType = field.getType();
}
if (propertyDescriptor != null) {
this.name = propertyDescriptor.getName();
this.setter = propertyDescriptor.getWriteMethod();
this.getter = propertyDescriptor.getReadMethod();
this.javaType = propertyDescriptor.getPropertyType();
}
}
示例8: test
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
private static void test(Class type) {
PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, "prop");
if (pd instanceof IndexedPropertyDescriptor) {
error(pd, type.getSimpleName() + ".prop should not be an indexed property");
}
if (!pd.getPropertyType().equals(Color.class)) {
error(pd, type.getSimpleName() + ".prop type should be a Color");
}
if (null == pd.getReadMethod()) {
error(pd, type.getSimpleName() + ".prop should have classic read method");
}
if (null == pd.getWriteMethod()) {
error(pd, type.getSimpleName() + ".prop should have classic write method");
}
}
示例9: getWriter
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
protected Method getWriter(String name) throws IntrospectionException {
BeanInfo beanInfo = Introspector.getBeanInfo(into.getClass());
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (name.equals(pd.getName()))
return pd.getWriteMethod();
}
return null;
}
示例10: loadProperties
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* Force the definition of the bean to be loaded. Subclasses
* should call this method if they override <code>addProperty</code>
* @see #addProperty
*/
synchronized protected void loadProperties()
{
if (_defs == null)
{
Map<String, PropertyDef> defs;
try
{
Class<?> objClass = _getClass();
// Grab all the getters and setters using JavaBeans
BeanInfo info = JavaIntrospector.getBeanInfo(objClass);
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
int length = (descriptors != null) ? descriptors.length : 0;
defs = new HashMap<String, PropertyDef>(Math.max(length, 1));
_defs = defs;
for (int i = 0; i < length; i++)
{
PropertyDescriptor property = descriptors[i];
String name = property.getName();
if ((name != null) && (property.getWriteMethod() != null))
{
addProperty(property);
}
}
}
catch (Exception e)
{
// =-=AEW ERROR???
_defs = new HashMap<String, PropertyDef>(1);
}
}
}
示例11: extractCollectionComponentType
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
@SuppressWarnings( { "unchecked" })
private Class<?> extractCollectionComponentType(BeanInfo beanInfo, PropertyDescriptor propertyDescriptor) {
final java.lang.reflect.Type collectionAttributeType;
if ( propertyDescriptor.getReadMethod() != null ) {
collectionAttributeType = propertyDescriptor.getReadMethod().getGenericReturnType();
}
else if ( propertyDescriptor.getWriteMethod() != null ) {
collectionAttributeType = propertyDescriptor.getWriteMethod().getGenericParameterTypes()[0];
}
else {
// we need to look for the field and look at it...
try {
collectionAttributeType = ownerClass.getField( propertyDescriptor.getName() ).getGenericType();
}
catch ( Exception e ) {
return null;
}
}
if ( ParameterizedType.class.isInstance( collectionAttributeType ) ) {
final java.lang.reflect.Type[] types = ( (ParameterizedType) collectionAttributeType ).getActualTypeArguments();
if ( types == null ) {
return null;
}
else if ( types.length == 1 ) {
return (Class<?>) types[0];
}
else if ( types.length == 2 ) {
// Map<K,V>
return (Class<?>) types[1];
}
}
return null;
}
示例12: unsatisfiedNonSimpleProperties
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* Return an array of non-simple bean properties that are unsatisfied.
* These are probably unsatisfied references to other beans in the
* factory. Does not include simple properties like primitives or Strings.
* @param mbd the merged bean definition the bean was created with
* @param bw the BeanWrapper the bean was created with
* @return an array of bean property names
* @see org.springframework.beans.BeanUtils#isSimpleProperty
*/
protected String[] unsatisfiedNonSimpleProperties(AbstractBeanDefinition mbd, BeanWrapper bw) {
Set<String> result = new TreeSet<String>();
PropertyValues pvs = mbd.getPropertyValues();
PropertyDescriptor[] pds = bw.getPropertyDescriptors();
for (PropertyDescriptor pd : pds) {
if (pd.getWriteMethod() != null && !isExcludedFromDependencyCheck(pd) && !pvs.contains(pd.getName()) &&
!BeanUtils.isSimpleProperty(pd.getPropertyType())) {
result.add(pd.getName());
}
}
return StringUtils.toStringArray(result);
}
示例13: getDiff
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* @param oldBean
* @param newBean
* @return
*/
public static <T> T getDiff(T oldBean, T newBean) {
if (oldBean == null && newBean != null) {
return newBean;
} else if (newBean == null) {
return null;
} else {
Class<?> cls1 = oldBean.getClass();
try {
@SuppressWarnings("unchecked")
T object = (T)cls1.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(cls1);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
Object oldValue = getter.invoke(oldBean);
Object newValue = getter.invoke(newBean);
if (newValue != null) {
if (oldValue == null) {
setter.invoke(object, newValue);
} else if (oldValue != null && !newValue.equals(oldValue)) {
setter.invoke(object, newValue);
}
}
}
}
return object;
} catch (Exception e) {
throw new DataParseException(e);
}
}
}
示例14: testIntrospection
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/** Test resource introspection */
@SuppressWarnings("unused")
public void testIntrospection() throws Exception {
// get the gate.Document resource and its class
ResourceData docRd = reg.get("gate.corpora.DocumentImpl");
assertNotNull("couldn't find document res data (2)", docRd);
Class<?> resClass = docRd.getResourceClass();
// get the beaninfo and property descriptors for the resource
BeanInfo docBeanInfo = Introspector.getBeanInfo(resClass, Object.class);
PropertyDescriptor[] propDescrs = docBeanInfo.getPropertyDescriptors();
// print all the properties in the reource's bean info;
// remember the setFeatures method
Method setFeaturesMethod = null;
for(int i = 0; i<propDescrs.length; i++) {
Method getMethodDescr = null;
Method setMethodDescr = null;
Class<?> propClass = null;
PropertyDescriptor propDescr = propDescrs[i];
propClass = propDescr.getPropertyType();
getMethodDescr = propDescr.getReadMethod();
setMethodDescr = propDescr.getWriteMethod();
if(
setMethodDescr != null &&
setMethodDescr.getName().equals("setFeatures")
)
setFeaturesMethod = setMethodDescr;
if(DEBUG) printProperty(propDescrs[i]);
}
// try setting the features property
// invoke(Object obj, Object[] args)
LanguageResource res = (LanguageResource) resClass.newInstance();
FeatureMap feats = Factory.newFeatureMap();
feats.put("things are sunny in sunny countries", "aren't they?");
Object[] args = new Object[1];
args[0] = feats;
setFeaturesMethod.invoke(res, args);
assertTrue(
"features not added to resource properly",
res.getFeatures().get("things are sunny in sunny countries")
.equals("aren't they?")
);
}
示例15: hasServiceProperty
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
protected ServiceReference hasServiceProperty(PropertyDescriptor propertyDescriptor) {
Method setter = propertyDescriptor.getWriteMethod();
return setter != null ? AnnotationUtils.getAnnotation(setter, ServiceReference.class) : null;
}