Java Code Examples for java.beans.PropertyDescriptor#setWriteMethod()

The following examples show how to use java.beans.PropertyDescriptor#setWriteMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FluentPropertyBeanIntrospectorWithIgnores.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public void introspect(IntrospectionContext icontext) throws IntrospectionException {
   for (Method m : icontext.getTargetClass().getMethods()) {
      if (m.getName().startsWith(getWriteMethodPrefix())) {
         String propertyName = propertyName(m);
         PropertyDescriptor pd = icontext.getPropertyDescriptor(propertyName);

         if (isIgnored(icontext.getTargetClass().getName(), m.getName())) {
            logger.trace(m.getName() + " Ignored for " + icontext.getTargetClass().getName());
            continue;
         }
         try {
            if (pd == null) {
               icontext.addPropertyDescriptor(createFluentPropertyDescritor(m, propertyName));
            } else if (pd.getWriteMethod() == null) {
               pd.setWriteMethod(m);
            }
         } catch (IntrospectionException e) {
            logger.debug(e.getMessage(), e);
         }
      }
   }
}
 
Example 2
Source File: PropertyDescriptorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * write method without argument
 */
public void testSetWriteMethod_Invalid_NoArgs() throws SecurityException,
        NoSuchMethodException, IntrospectionException {
    Class<MockJavaBean> beanClass = MockJavaBean.class;
    String propertyName = "PropertyOne";
    Method writeMethod = beanClass.getMethod("get" + "PropertyTwo",
            (Class[]) null);
    PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null);

    assertNull(pd.getWriteMethod());
    try {
        pd.setWriteMethod(writeMethod);
        fail("Should throw IntrospectionException.");
    } catch (IntrospectionException e) {
    }
}
 
Example 3
Source File: PropertyDescriptorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * write method is incompatible with read method
 */
public void testSetWriteMethod_WriteReadIncompatible()
        throws SecurityException, NoSuchMethodException,
        IntrospectionException {
    Class<MockJavaBean> beanClass = MockJavaBean.class;
    String propertyName = "PropertyOne";
    Method readMethod = beanClass.getMethod("get" + "PropertyTwo",
            (Class[]) null);
    Method writeMethod = beanClass.getMethod("set" + propertyName,
            new Class[] { String.class });
    PropertyDescriptor pd = new PropertyDescriptor(propertyName,
            readMethod, null);

    assertNull(pd.getWriteMethod());
    try {
        pd.setWriteMethod(writeMethod);
        fail("Should throw IntrospectionException.");
    } catch (IntrospectionException e) {
    }
}
 
Example 4
Source File: FluentPropertyBeanIntrospector.java    From commons-beanutils with Apache License 2.0 6 votes vote down vote up
/**
 * Performs introspection. This method scans the current class's methods for
 * property write methods which have not been discovered by default
 * introspection.
 *
 * @param icontext the introspection context
 * @throws IntrospectionException if an error occurs
 */
@Override
public void introspect(final IntrospectionContext icontext)
        throws IntrospectionException {
    for (final Method m : icontext.getTargetClass().getMethods()) {
        if (m.getName().startsWith(getWriteMethodPrefix())) {
            final String propertyName = propertyName(m);
            final PropertyDescriptor pd = icontext
                    .getPropertyDescriptor(propertyName);
            try {
                if (pd == null) {
                    icontext.addPropertyDescriptor(createFluentPropertyDescritor(
                            m, propertyName));
                } else if (pd.getWriteMethod() == null) {
                    pd.setWriteMethod(m);
                }
            } catch (final IntrospectionException e) {
                log.debug("Error when creating PropertyDescriptor for " + m
                        + "! Ignoring this property.");
                log.debug("Exception is:", e);
            }
        }
    }
}
 
Example 5
Source File: BeanIntrospectionData.java    From commons-beanutils with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the write method for the property determined by the given
 * {@code PropertyDescriptor}. This information is normally available in the
 * descriptor object itself. However, at least by the ORACLE implementation, the
 * method is stored as a {@code SoftReference}. If this reference has been freed by
 * the GC, it may be the case that the method cannot be obtained again. Then,
 * additional information stored in this object is necessary to obtain the method
 * again.
 *
 * @param beanCls the class of the affected bean
 * @param desc the {@code PropertyDescriptor} of the desired property
 * @return the write method for this property or <b>null</b> if there is none
 */
public Method getWriteMethod(final Class<?> beanCls, final PropertyDescriptor desc) {
    Method method = desc.getWriteMethod();
    if (method == null) {
        final String methodName = writeMethodNames.get(desc.getName());
        if (methodName != null) {
            method = MethodUtils.getAccessibleMethod(beanCls, methodName,
                    desc.getPropertyType());
            if (method != null) {
                try {
                    desc.setWriteMethod(method);
                } catch (final IntrospectionException e) {
                    // ignore, in this case the method is not cached
                }
            }
        }
    }

    return method;
}
 
Example 6
Source File: ExtendedBeanInfo.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void handleCandidateWriteMethod(Method method) throws IntrospectionException {
	int nParams = method.getParameterCount();
	String propertyName = propertyNameFor(method);
	Class<?> propertyType = method.getParameterTypes()[nParams - 1];
	PropertyDescriptor existingPd = findExistingPropertyDescriptor(propertyName, propertyType);
	if (nParams == 1) {
		if (existingPd == null) {
			this.propertyDescriptors.add(new SimplePropertyDescriptor(propertyName, null, method));
		}
		else {
			existingPd.setWriteMethod(method);
		}
	}
	else if (nParams == 2) {
		if (existingPd == null) {
			this.propertyDescriptors.add(
					new SimpleIndexedPropertyDescriptor(propertyName, null, null, null, method));
		}
		else if (existingPd instanceof IndexedPropertyDescriptor) {
			((IndexedPropertyDescriptor) existingPd).setIndexedWriteMethod(method);
		}
		else {
			this.propertyDescriptors.remove(existingPd);
			this.propertyDescriptors.add(new SimpleIndexedPropertyDescriptor(
					propertyName, existingPd.getReadMethod(), existingPd.getWriteMethod(), null, method));
		}
	}
	else {
		throw new IllegalArgumentException("Write method must have exactly 1 or 2 parameters: " + method);
	}
}
 
Example 7
Source File: ExtendedBeanInfo.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void handleCandidateWriteMethod(Method method) throws IntrospectionException {
	int nParams = method.getParameterCount();
	String propertyName = propertyNameFor(method);
	Class<?> propertyType = method.getParameterTypes()[nParams - 1];
	PropertyDescriptor existingPd = findExistingPropertyDescriptor(propertyName, propertyType);
	if (nParams == 1) {
		if (existingPd == null) {
			this.propertyDescriptors.add(new SimplePropertyDescriptor(propertyName, null, method));
		}
		else {
			existingPd.setWriteMethod(method);
		}
	}
	else if (nParams == 2) {
		if (existingPd == null) {
			this.propertyDescriptors.add(
					new SimpleIndexedPropertyDescriptor(propertyName, null, null, null, method));
		}
		else if (existingPd instanceof IndexedPropertyDescriptor) {
			((IndexedPropertyDescriptor) existingPd).setIndexedWriteMethod(method);
		}
		else {
			this.propertyDescriptors.remove(existingPd);
			this.propertyDescriptors.add(new SimpleIndexedPropertyDescriptor(
					propertyName, existingPd.getReadMethod(), existingPd.getWriteMethod(), null, method));
		}
	}
	else {
		throw new IllegalArgumentException("Write method must have exactly 1 or 2 parameters: " + method);
	}
}
 
Example 8
Source File: ExtendedBeanInfo.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void handleCandidateWriteMethod(Method method) throws IntrospectionException {
	int nParams = method.getParameterTypes().length;
	String propertyName = propertyNameFor(method);
	Class<?> propertyType = method.getParameterTypes()[nParams - 1];
	PropertyDescriptor existingPd = findExistingPropertyDescriptor(propertyName, propertyType);
	if (nParams == 1) {
		if (existingPd == null) {
			this.propertyDescriptors.add(new SimplePropertyDescriptor(propertyName, null, method));
		}
		else {
			existingPd.setWriteMethod(method);
		}
	}
	else if (nParams == 2) {
		if (existingPd == null) {
			this.propertyDescriptors.add(
					new SimpleIndexedPropertyDescriptor(propertyName, null, null, null, method));
		}
		else if (existingPd instanceof IndexedPropertyDescriptor) {
			((IndexedPropertyDescriptor) existingPd).setIndexedWriteMethod(method);
		}
		else {
			this.propertyDescriptors.remove(existingPd);
			this.propertyDescriptors.add(new SimpleIndexedPropertyDescriptor(
					propertyName, existingPd.getReadMethod(), existingPd.getWriteMethod(), null, method));
		}
	}
	else {
		throw new IllegalArgumentException("Write method must have exactly 1 or 2 parameters: " + method);
	}
}
 
Example 9
Source File: ExtendedBeanInfo.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
private void handleCandidateWriteMethod(Method method) throws IntrospectionException {
	int nParams = method.getParameterTypes().length;
	String propertyName = propertyNameFor(method);
	Class<?> propertyType = method.getParameterTypes()[nParams-1];
	PropertyDescriptor existingPd = findExistingPropertyDescriptor(propertyName, propertyType);
	if (nParams == 1) {
		if (existingPd == null) {
			this.propertyDescriptors.add(new SimplePropertyDescriptor(propertyName, null, method));
		}
		else {
			existingPd.setWriteMethod(method);
		}
	}
	else if (nParams == 2) {
		if (existingPd == null) {
			this.propertyDescriptors.add(
					new SimpleIndexedPropertyDescriptor(propertyName, null, null, null, method));
		}
		else if (existingPd instanceof IndexedPropertyDescriptor) {
			((IndexedPropertyDescriptor) existingPd).setIndexedWriteMethod(method);
		}
		else {
			this.propertyDescriptors.remove(existingPd);
			this.propertyDescriptors.add(new SimpleIndexedPropertyDescriptor(
					propertyName, existingPd.getReadMethod(), existingPd.getWriteMethod(), null, method));
		}
	}
	else {
		throw new IllegalArgumentException("Write method must have exactly 1 or 2 parameters: " + method);
	}
}
 
Example 10
Source File: ExtendedBeanInfo.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void handleCandidateWriteMethod(Method method) throws IntrospectionException {
	int nParams = method.getParameterTypes().length;
	String propertyName = propertyNameFor(method);
	Class<?> propertyType = method.getParameterTypes()[nParams - 1];
	PropertyDescriptor existingPd = findExistingPropertyDescriptor(propertyName, propertyType);
	if (nParams == 1) {
		if (existingPd == null) {
			this.propertyDescriptors.add(new SimplePropertyDescriptor(propertyName, null, method));
		}
		else {
			existingPd.setWriteMethod(method);
		}
	}
	else if (nParams == 2) {
		if (existingPd == null) {
			this.propertyDescriptors.add(
					new SimpleIndexedPropertyDescriptor(propertyName, null, null, null, method));
		}
		else if (existingPd instanceof IndexedPropertyDescriptor) {
			((IndexedPropertyDescriptor) existingPd).setIndexedWriteMethod(method);
		}
		else {
			this.propertyDescriptors.remove(existingPd);
			this.propertyDescriptors.add(new SimpleIndexedPropertyDescriptor(
					propertyName, existingPd.getReadMethod(), existingPd.getWriteMethod(), null, method));
		}
	}
	else {
		throw new IllegalArgumentException("Write method must have exactly 1 or 2 parameters: " + method);
	}
}
 
Example 11
Source File: PropertyDescriptorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * normal input
 */
public void testSetWriteMethod() throws SecurityException,
        NoSuchMethodException, IntrospectionException {
    Class<MockJavaBean> beanClass = MockJavaBean.class;
    String propertyName = "PropertyOne";
    Method writeMethod = beanClass.getMethod("set" + propertyName,
            new Class[] { String.class });
    PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null);

    assertNull(pd.getWriteMethod());
    pd.setWriteMethod(writeMethod);
    assertSame(writeMethod, pd.getWriteMethod());
}
 
Example 12
Source File: PropertyDescriptorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * setWriteMethod(null)
 */
public void testSetWriteMethod_Null() throws SecurityException,
        NoSuchMethodException, IntrospectionException {
    Class<MockJavaBean> beanClass = MockJavaBean.class;
    String propertyName = "PropertyOne";
    Method writeMethod = beanClass.getMethod("set" + propertyName,
            new Class[] { String.class });
    PropertyDescriptor pd = new PropertyDescriptor(propertyName, null,
            writeMethod);

    assertSame(writeMethod, pd.getWriteMethod());
    pd.setWriteMethod(null);
    assertNull(pd.getWriteMethod());
}
 
Example 13
Source File: PropertyDescriptorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * write method is incompatible with property name (read method is null)
 */
public void testSetWriteMethod_Invalid() throws SecurityException,
        NoSuchMethodException, IntrospectionException {
    Class<MockJavaBean> beanClass = MockJavaBean.class;
    String propertyName = "PropertyOne";
    Method writeMethod = beanClass.getMethod("set" + "PropertyTwo",
            new Class[] { Integer.class });
    PropertyDescriptor pd = new PropertyDescriptor(propertyName, null, null);

    assertNull(pd.getWriteMethod());
    pd.setWriteMethod(writeMethod);
    assertSame(writeMethod, pd.getWriteMethod());
}
 
Example 14
Source File: BeanIntrospectionDataTestCase.java    From commons-beanutils with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a write method can be queried that is currently not available in the
 * property descriptor.
 */
public void testGetWriteMethodUndefined() throws Exception {
    final BeanIntrospectionData data = setUpData();
    final PropertyDescriptor pd = fetchTestDescriptor(data);
    final Method writeMethod = pd.getWriteMethod();
    pd.setWriteMethod(null);
    assertEquals("Wrong write method", writeMethod,
            data.getWriteMethod(BEAN_CLASS, pd));
    assertEquals("Method not set in descriptor", writeMethod, pd.getWriteMethod());
}
 
Example 15
Source File: BeanIntrospectionDataTestCase.java    From commons-beanutils with Apache License 2.0 5 votes vote down vote up
/**
 * Tests getWriteMethod() if the method cannot be resolved. (This is a corner case
 * which should normally not happen in practice.)
 */
public void testGetWriteMethodNonExisting() throws Exception {
    final PropertyDescriptor pd = new PropertyDescriptor(TEST_PROP,
            BEAN_CLASS.getMethod("getFluentGetProperty"), BEAN_CLASS.getMethod(
                    "setFluentGetProperty", String.class));
    final Map<String, String> methods = new HashMap<>();
    methods.put(TEST_PROP, "hashCode");
    final BeanIntrospectionData data = new BeanIntrospectionData(
            new PropertyDescriptor[] { pd }, methods);
    pd.setWriteMethod(null);
    assertNull("Got a write method", data.getWriteMethod(BEAN_CLASS, pd));
}
 
Example 16
Source File: Jira456TestCase.java    From commons-beanutils with Apache License 2.0 5 votes vote down vote up
/**
 * Clears the reference to the write method in the property descriptor of the test
 * property. This simulates that the write method reference is freed by the GC.
 *
 * @return the bean instance used for testing
 * @throws Exception if an error occurs
 */
private FluentIntrospectionTestBean clearWriteMethodRef() throws Exception {
    final FluentIntrospectionTestBean bean = new FluentIntrospectionTestBean();
    final PropertyDescriptor pd = pub.getPropertyDescriptor(bean, TEST_PROP);

    // simulate that the write method reference is freed
    pd.setWriteMethod(null);
    return bean;
}