Example usage for java.beans PropertyDescriptor PropertyDescriptor

List of usage examples for java.beans PropertyDescriptor PropertyDescriptor

Introduction

In this page you can find the example usage for java.beans PropertyDescriptor PropertyDescriptor.

Prototype

PropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) 

Source Link

Document

Package-private constructor.

Usage

From source file:ColorsBeanInfo.java

  public PropertyDescriptor[] getPropertyDescriptors() {
  try {//  w w w  . ja v a2  s  . co m
    PropertyDescriptor rectangular = new PropertyDescriptor("rectangular", Colors.class);
    PropertyDescriptor pd[] = { rectangular };
    return pd;
  } catch (Exception e) {
  }
  return null;
}

From source file:org.neovera.jdiablo.internal.BeanUtils.java

public static Method getWriteMethod(String fieldName, Class<?> clz) {
    try {/*w  w  w  .  j av  a2s  . c o m*/
        return PropertyUtils.getWriteMethod(new PropertyDescriptor(fieldName, clz));
    } catch (IntrospectionException e) {
        _logger.error(e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:org.mypsycho.beans.DescriptorExtension.java

/**
 * Constructor./*from www  . j a  va 2  s.  c o  m*/
 *
 * @param applicable type applicable
 * @param propName property name
 * @throws IntrospectionException
 */
public DescriptorExtension(Class<?> type, String propName, boolean override) throws IntrospectionException {
    this(type, propName, override ? new PropertyDescriptor(propName, type) : null);
}

From source file:com.googlecode.jsonschema2pojo.integration.PropertiesIT.java

@Test
@SuppressWarnings("rawtypes")
public void propertiesWithNullValuesAreOmittedWhenSerialized() throws ClassNotFoundException,
        IntrospectionException, InstantiationException, IllegalAccessException, InvocationTargetException {

    ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/nullProperties.json",
            "com.example");

    Class generatedType = resultsClassLoader.loadClass("com.example.NullProperties");
    Object instance = generatedType.newInstance();

    Method setter = new PropertyDescriptor("property", generatedType).getWriteMethod();
    setter.invoke(instance, "value");

    assertThat(mapper.valueToTree(instance).toString(), containsString("property"));

    setter.invoke(instance, (Object) null);

    assertThat(mapper.valueToTree(instance).toString(), not(containsString("property")));

}

From source file:org.jsonschema2pojo.integration.PropertiesIT.java

@Test
@SuppressWarnings("rawtypes")
public void propertiesWithNullValuesAreOmittedWhenSerialized() throws ClassNotFoundException,
        IntrospectionException, InstantiationException, IllegalAccessException, InvocationTargetException {

    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/nullProperties.json",
            "com.example");

    Class generatedType = resultsClassLoader.loadClass("com.example.NullProperties");
    Object instance = generatedType.newInstance();

    Method setter = new PropertyDescriptor("property", generatedType).getWriteMethod();
    setter.invoke(instance, "value");

    assertThat(mapper.valueToTree(instance).toString(), containsString("property"));

    setter.invoke(instance, (Object) null);

    assertThat(mapper.valueToTree(instance).toString(), not(containsString("property")));

}

From source file:BeanUtil.java

/**
 * Retreives a property descriptor object for a given property.
 * <p>//w  w  w. j a  v a2 s.co m
 * Uses the classes in <code>java.beans</code> to get back
 * a descriptor for a property.  Read-only and write-only
 * properties will have a slower return time.
 * </p>
 *
 * @param propertyName The programmatic name of the property
 * @param beanClass The Class object for the target bean.
 *                  For example sun.beans.OurButton.class.
 * @return a PropertyDescriptor for a property that follows the
 *         standard Java naming conventions.
 * @throws PropertyNotFoundException indicates that the property
 *         could not be found on the bean class.
 */
private static final PropertyDescriptor getPropertyDescriptor(String propertyName, Class beanClass) {

    PropertyDescriptor resultPropertyDescriptor = null;

    char[] pNameArray = propertyName.toCharArray();
    pNameArray[0] = Character.toLowerCase(pNameArray[0]);
    propertyName = new String(pNameArray);

    try {
        resultPropertyDescriptor = new PropertyDescriptor(propertyName, beanClass);
    } catch (IntrospectionException e1) {
        // Read-only and write-only properties will throw this
        // exception.  The properties must be looked up using
        // brute force.

        // This will get the list of all properties and iterate
        // through looking for one that matches the property
        // name passed into the method.
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);

            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (int i = 0; i < propertyDescriptors.length; i++) {
                if (propertyDescriptors[i].getName().equals(propertyName)) {

                    // If the names match, this this describes the
                    // property being searched for.  Break out of
                    // the iteration.
                    resultPropertyDescriptor = propertyDescriptors[i];
                    break;
                }
            }
        } catch (IntrospectionException e2) {
            e2.printStackTrace();
        }
    }

    // If no property descriptor was found, then this bean does not
    // have a property matching the name passed in.
    if (resultPropertyDescriptor == null) {
        System.out.println("resultPropertyDescriptor == null");
    }

    return resultPropertyDescriptor;
}

From source file:jp.gr.java_conf.ka_ka_xyz.processor.JPA20AnnotationProcessor.java

private void registerMethodAnnotation(Field field, Class<?> clazz) {
    try {/*  ww w . ja  va 2 s . c o  m*/
        String fieldName = field.getName();
        PropertyDescriptor pd = new PropertyDescriptor(fieldName, clazz);
        Column column = pd.getReadMethod().getAnnotation(Column.class);
        String colName;
        if (column != null) {
            colName = column.name();
            fieldColMap.put(fieldName, colName);
        }

    } catch (IntrospectionException e) {
        e.printStackTrace();
        return;
    }
}

From source file:ch.silviowangler.dox.AutomaticTranslatorAdvice.java

public void addTranslationIfNeeded(Object retVal) throws Throwable {

    if (retVal == null)
        return;//from  w  w  w  . j a  va 2s .c  om

    translateWhenPossible(retVal);

    TranslateProperties translateProperties = retVal.getClass().getAnnotation(TranslateProperties.class);
    if (translateProperties != null) {
        final String className = retVal.getClass().getName();
        logger.debug("detected class that declares translatable properties {}", className);

        for (String propertyName : translateProperties.value()) {
            logger.trace("About to inspect property {} of class {}", propertyName, className);
            Object propertyValue = new PropertyDescriptor(propertyName, retVal.getClass()).getReadMethod()
                    .invoke(retVal);

            if (propertyValue != null) {
                logger.trace("Property {} is class {}", propertyName, propertyValue.getClass().getName());
                translateWhenPossible(propertyValue);
            } else {
                logger.trace("Property {} has null value", propertyName);
            }
        }
    }
    logger.debug("Return value is {}", retVal);
}

From source file:com.googlecode.jsonschema2pojo.integration.PropertiesIT.java

@Test
@SuppressWarnings("rawtypes")
public void propertiesAreSerializedInCorrectOrder() throws ClassNotFoundException, IntrospectionException,
        InstantiationException, IllegalAccessException, InvocationTargetException {

    ClassLoader resultsClassLoader = generateAndCompile("/schema/properties/orderedProperties.json",
            "com.example");

    Class generatedType = resultsClassLoader.loadClass("com.example.OrderedProperties");
    Object instance = generatedType.newInstance();

    new PropertyDescriptor("type", generatedType).getWriteMethod().invoke(instance, "1");
    new PropertyDescriptor("id", generatedType).getWriteMethod().invoke(instance, "2");
    new PropertyDescriptor("name", generatedType).getWriteMethod().invoke(instance, "3");
    new PropertyDescriptor("hastickets", generatedType).getWriteMethod().invoke(instance, true);
    new PropertyDescriptor("starttime", generatedType).getWriteMethod().invoke(instance, "4");

    String serialized = mapper.valueToTree(instance).toString();

    assertThat("Properties are not in expected order", serialized.indexOf("type"),
            is(lessThan(serialized.indexOf("id"))));
    assertThat("Properties are not in expected order", serialized.indexOf("id"),
            is(lessThan(serialized.indexOf("name"))));
    assertThat("Properties are not in expected order", serialized.indexOf("name"),
            is(lessThan(serialized.indexOf("hastickets"))));
    assertThat("Properties are not in expected order", serialized.indexOf("hastickets"),
            is(lessThan(serialized.indexOf("starttime"))));

}

From source file:org.suren.autotest.web.framework.data.PageRefDynamicData.java

@Override
public String getValue(String orginData) {
    if (StringUtils.isBlank(orginData)) {
        return null;
    }//from  w  w  w.  ja v  a2  s .c  om

    String[] orginArrayData = orginData.split("\\.", 2);

    String clsName = orginArrayData[0];
    String fieldName = orginArrayData[1];

    clsName = StringUtils.uncapitalize(clsName); //springbean????

    Page page = null;

    try {
        page = context.getBean(clsName, Page.class);
    } catch (NoSuchBeanDefinitionException e) {
        LOGGER.error("Can not found page class by name [{}].", clsName);
    } catch (BeanNotOfRequiredTypeException e) {
        LOGGER.error("The class [{}] is not a Page type.", clsName);
    }

    if (page == null) {
        throw new RuntimeException("Can not found page class!");
    }

    try {
        Method readMethod = new PropertyDescriptor(fieldName, page.getClass()).getReadMethod();

        Object result = readMethod.invoke(page);
        if (result != null && AbstractElement.class.isAssignableFrom(result.getClass())) {
            if (result instanceof Text) {
                return ((Text) result).getValue();
            } else {
                throw new RuntimeException("Not support field type [" + result.getClass() + "].");
            }
        }
    } catch (IntrospectionException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

    return null;
}