Example usage for java.beans PropertyDescriptor getPropertyType

List of usage examples for java.beans PropertyDescriptor getPropertyType

Introduction

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

Prototype

public synchronized Class<?> getPropertyType() 

Source Link

Document

Returns the Java type info for the property.

Usage

From source file:org.openempi.webapp.server.util.BaseModelDataDtoGenerator.java

/**
 * @param args/*from  w  w  w  .j  a va2  s .c  o m*/
 */
public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.err.println("Usage: BaseModelDataDtoGenerator <FQ-Class-Name> <Destination-Package-Name>");
        System.exit(-1);
    }

    String fqClassName = args[0];
    String className = extractClassName(fqClassName);
    String packageName = args[1];
    log.debug("Generating DTO for class: " + fqClassName + " into class " + className);
    StringBuilder sourceCode = generateClassHeader(packageName, className);
    Class<?> beanClass = Class.forName(fqClassName);
    PropertyDescriptor[] descs = PropertyUtils.getPropertyDescriptors(beanClass);
    for (PropertyDescriptor desc : descs) {
        log.debug(desc.getName() + " of type " + desc.getPropertyType());

        if (!desc.getName().equalsIgnoreCase("class")
                && !desc.getPropertyType().getCanonicalName().startsWith("org")) {
            generateGetterSetter(sourceCode, desc);
        }
    }
    sourceCode.append("}");
    System.out.println(sourceCode.toString());
}

From source file:name.ikysil.beanpathdsl.sandbox.describe.Main.java

/**
 * @param args the command line arguments
 */// w  ww . j a va2s .com
public static void main(String[] args) {
    PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
    Reflections reflections = new Reflections(TestBean.class, new SubTypesScanner(false));
    Set<Class<?>> classes = reflections.getSubTypesOf(Object.class);
    for (Class<?> clazz : classes) {
        System.out.println(String.format("Class name: %s", clazz.getName()));
        System.out.println(String.format("Class simple name: %s", clazz.getSimpleName()));
        System.out.println(String.format("Class canonical name: %s", clazz.getCanonicalName()));
        PropertyDescriptor[] pds = propertyUtilsBean.getPropertyDescriptors(clazz);
        for (PropertyDescriptor pd : pds) {
            System.out.println(String.format("    Property name: %s", pd.getName()));
            Class<?> pc = pd.getPropertyType();
            System.out.println(String.format("    Class name: %s", pc.getName()));
            System.out.println(String.format("    Class simple name: %s", pc.getSimpleName()));
            System.out.println(String.format("    Class canonical name: %s", pc.getCanonicalName()));
        }
    }
}

From source file:org.kuali.rice.devtools.generators.dd.BeanDDCreator.java

/**
 * This method.../*from w  w w.j a v a2s.  co  m*/
 * @param args
 */
public static void main(String[] args) throws Exception {
    String className = args[0];
    Class<? extends BusinessObject> boClass = (Class<? extends BusinessObject>) Class.forName(className);
    PropertyDescriptor[] props = PropertyUtils.getPropertyDescriptors(boClass);

    StringBuffer sb = new StringBuffer(4000);
    sb.append("<beans xmlns=\"http://www.springframework.org/schema/beans\"\r\n"
            + "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n"
            + "    xmlns:p=\"http://www.springframework.org/schema/p\"\r\n"
            + "    xsi:schemaLocation=\"http://www.springframework.org/schema/beans\r\n"
            + "        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd\">\r\n" + "\r\n"
            + "  <bean id=\"");
    sb.append(boClass.getSimpleName());
    sb.append("\" parent=\"");
    sb.append(boClass.getSimpleName());
    sb.append("-parentBean\" />\r\n" + "\r\n" + "  <bean id=\"");
    sb.append(boClass.getSimpleName());
    sb.append("-parentBean\" abstract=\"true\" parent=\"BusinessObjectEntry\"\r\n"
            + "        p:businessObjectClass=\"");
    sb.append(boClass.getName());
    sb.append("\"\r\n");
    sb.append("        p:titleAttribute=\"");
    sb.append("FILL ME IN");
    sb.append("\"\r\n");
    sb.append("        p:objectLabel=\"");
    sb.append(camelCaseToString(boClass.getSimpleName()));
    sb.append("\"\r\n");
    sb.append("        p:inquiryDefinition-ref=\"");
    sb.append(boClass.getSimpleName());
    sb.append("-inquiryDefinition\"\r\n");
    sb.append("        p:lookupDefinition-ref=\"");
    sb.append(boClass.getSimpleName());
    sb.append("-lookupDefinition\" >\r\n");
    sb.append("    <property name=\"attributes\" >\r\n" + "      <list>\r\n");
    for (PropertyDescriptor p : props) {
        if (isNormalProperty(p)) {
            sb.append("        <ref bean=\"").append(boClass.getSimpleName()).append('-');
            sb.append(p.getName());
            sb.append("\" />\r\n");
        }
    }

    sb.append("      </list>\r\n" + "    </property>\r\n" + "  </bean>\r\n" + "\r\n");
    for (PropertyDescriptor p : props) {
        if (isNormalProperty(p)) {

            if (p.getName().equals("versionNumber")) {
                sb.append(getSimpleParentBeanReference(boClass, p.getName()));
                sb.append(getSimpleAbstractInheritanceBean(boClass, p.getName(),
                        "GenericAttributes-versionNumber"));

            } else if (p.getName().endsWith("chartOfAccountsCode")) {
                sb.append(getSimpleParentBeanReference(boClass, p.getName()));
                sb.append(getSimpleAbstractInheritanceBean(boClass, p.getName(), "Chart-chartOfAccountsCode"));

            } else if (p.getName().endsWith("organizationCode")) {
                sb.append(getSimpleParentBeanReference(boClass, p.getName()));
                sb.append(getSimpleAbstractInheritanceBean(boClass, p.getName(), "Org-organizationCode"));

            } else if (p.getName().endsWith("accountNumber")) {
                sb.append(getSimpleParentBeanReference(boClass, p.getName()));
                sb.append(getSimpleAbstractInheritanceBean(boClass, p.getName(), "Account-accountNumber"));

            } else if (p.getName().equals("active")) {
                sb.append(getSimpleParentBeanReference(boClass, p.getName()));
                sb.append(getSimpleAbstractInheritanceBean(boClass, p.getName(),
                        "GenericAttributes-activeIndicator"));

            } else if (p.getName().equals("codeAndDescription")) {
                sb.append(getSimpleParentBeanReference(boClass, p.getName()));
                sb.append(getSimpleAbstractInheritanceBeanWithLabel(boClass, p.getName(),
                        "CommonField-CodeAndDescription", camelCaseToString(boClass.getSimpleName())));

            } else if (p.getPropertyType() == Boolean.TYPE) {
                sb.append(getSimpleParentBeanReference(boClass, p.getName()));
                sb.append(getSimpleAbstractInheritanceBean(boClass, p.getName(),
                        "GenericAttributes-genericBoolean"));

            } else {
                // attribute bean
                sb.append(getSimpleParentBeanReference(boClass, p.getName()));
                // attribute parent bean
                sb.append("  <bean id=\"").append(boClass.getSimpleName()).append('-');
                sb.append(p.getName())
                        .append("-parentBean\" parent=\"AttributeDefinition\" abstract=\"true\"\r\n");
                sb.append("        p:name=\"").append(p.getName()).append("\"\r\n");
                sb.append("        p:forceUppercase=\"false\"\r\n");
                sb.append("        p:label=\"").append(camelCaseToString(p.getName())).append("\"\r\n");
                sb.append("        p:shortLabel=\"").append(camelCaseToString(p.getName())).append("\"\r\n");
                sb.append("        p:maxLength=\"10\"\r\n");
                sb.append("        p:required=\"false\" >\r\n");
                sb.append("    <property name=\"validationPattern\" >\r\n"
                        + "      <bean parent=\"AnyCharacterValidationPattern\"\r\n"
                        + "            p:allowWhitespace=\"true\" />\r\n" + "    </property>\r\n"
                        + "    <property name=\"control\" >\r\n"
                        + "      <bean parent=\"TextControlDefinition\"\r\n"
                        + "            p:size=\"10\" />\r\n" + "    </property>\r\n" + "  </bean>\r\n");

            }
            sb.append("\r\n");
        }
    }
    // inquiry definition

    sb.append(getSimpleParentBeanReference(boClass, "inquiryDefinition"));
    sb.append("\r\n");
    sb.append("  <bean id=\"");
    sb.append(boClass.getSimpleName());
    sb.append("-inquiryDefinition-parentBean\" abstract=\"true\" parent=\"InquiryDefinition\"\r\n"
            + "        p:title=\"");
    sb.append(camelCaseToString(boClass.getSimpleName()));
    sb.append(" Inquiry\" >\r\n" + "    <property name=\"inquirySections\" >\r\n" + "      <list>\r\n"
            + "        <bean parent=\"InquirySectionDefinition\"\r\n" + "              p:title=\"");
    sb.append(camelCaseToString(boClass.getSimpleName()));
    sb.append(" Attributes\"\r\n" + "              p:numberOfColumns=\"1\" >\r\n"
            + "          <property name=\"inquiryFields\" >\r\n" + "            <list>\r\n");
    for (PropertyDescriptor p : props) {
        if (isNormalProperty(p)) {
            sb.append("              <bean parent=\"FieldDefinition\" p:attributeName=\"");
            sb.append(p.getName()).append("\" />\r\n");
        }
    }
    sb.append("            </list>\r\n" + "          </property>\r\n" + "        </bean>\r\n"
            + "      </list>\r\n" + "    </property>\r\n" + "  </bean>\r\n" + "\r\n");

    sb.append(getSimpleParentBeanReference(boClass, "lookupDefinition"));
    sb.append("\r\n");
    sb.append("  <bean id=\"");
    sb.append(boClass.getSimpleName());
    sb.append("-lookupDefinition-parentBean\" abstract=\"true\" parent=\"LookupDefinition\"\r\n"
            + "        p:title=\"");
    sb.append(camelCaseToString(boClass.getSimpleName()));
    sb.append(" Lookup\" \r\n");
    //sb.append( "        p:instructions=\"Lookup a " );
    //sb.append( camelCaseToString(boClass.getSimpleName() ) );
    sb.append("\" >\r\n");
    sb.append("    <property name=\"defaultSort\" >\r\n" + "      <bean parent=\"SortDefinition\">\r\n"
            + "        <property name=\"attributeNames\" >\r\n" + "          <list>\r\n"
            + "            <value>FILL ME IN</value>\r\n" + "          </list>\r\n" + "        </property>\r\n"
            + "        <property name=\"sortAscending\" value=\"true\" />\r\n" + "      </bean>\r\n"
            + "    </property>\r\n" + "    <property name=\"lookupFields\" >\r\n" + "      <list>\r\n");
    for (PropertyDescriptor p : props) {
        if (isNormalProperty(p)) {
            sb.append("        <bean parent=\"FieldDefinition\" p:attributeName=\"");
            sb.append(p.getName()).append("\" />\r\n");
        }
    }
    sb.append("      </list>\r\n" + "    </property>\r\n" + "    <property name=\"resultFields\" >\r\n"
            + "      <list>\r\n");
    for (PropertyDescriptor p : props) {
        if (isNormalProperty(p)) {
            sb.append("        <bean parent=\"FieldDefinition\" p:attributeName=\"");
            sb.append(p.getName()).append("\" />\r\n");
        }
    }
    sb.append("      </list>\r\n" + "    </property>\r\n" + "  </bean>\r\n");
    sb.append("\r\n</beans>");
    System.out.println(sb.toString());
}

From source file:org.openempi.webapp.server.util.BaseModelDataDtoGenerator.java

private static void generateGetterSetter(StringBuilder sourceCode, PropertyDescriptor desc) {
    // getter method declaration
    sourceCode.append("\tpublic ").append(desc.getPropertyType().getCanonicalName()).append(" ")
            .append(desc.getReadMethod().getName()).append("() {\n");

    // getter method body
    sourceCode.append("\t\treturn get(\"").append(desc.getName()).append("\");\n\t}\n\n");

    // setter method declaration
    sourceCode.append("\tpublic void ").append(desc.getWriteMethod().getName()).append("(")
            .append(desc.getPropertyType().getCanonicalName()).append(" ").append(desc.getName())
            .append(") {\n");

    // getter method body
    sourceCode.append("\t\tset(\"").append(desc.getName()).append("\", ").append(desc.getName())
            .append(");\n\t}\n\n");
}

From source file:org.kuali.rice.devtools.generators.dd.MaintDocDDCreator.java

public static boolean isReferenceBoProperty(PropertyDescriptor p) {
    return p.getPropertyType() != null && BusinessObject.class.isAssignableFrom(p.getPropertyType())
            && !p.getName().startsWith("boNote") && !p.getName().startsWith("extension")
            && !p.getName().equals("newCollectionRecord");
}

From source file:org.force66.beantester.utils.InjectionUtils.java

public static void injectValues(Object bean, ValueGeneratorFactory valueGeneratorFactory,
        boolean reportExceptions) {

    ValueGenerator<?> generator;/*w w  w  . jav  a 2  s  .com*/
    Object[] generatedValues = null;
    for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(bean)) {
        try {
            generator = valueGeneratorFactory.forClass(descriptor.getPropertyType());
            if (generator != null && descriptor.getWriteMethod() != null) {
                generatedValues = generator.makeValues();

                descriptor.getWriteMethod().invoke(bean, generatedValues[0]);

            }
        } catch (Exception e) {
            if (reportExceptions) {
                throw new BeanTesterException("Error setting property value", e)
                        .addContextValue("class", bean.getClass().getName())
                        .addContextValue("field", descriptor.getName()).addContextValue("value",
                                generatedValues != null && generatedValues.length > 0 ? generatedValues[0]
                                        : null);
            }
        }
    }
}

From source file:net.mojodna.sprout.support.SproutUtils.java

/**
 * Bean initialization method.  Uses reflection to determine properties
 * for which auto-wiring may be appropriate.  Subsequently attempts to
 * retrieve appropriate beans from the WebApplicationContext and set them
 * locally./*w w  w  .j av a2s. co  m*/
 * 
 * @param bean Bean to initialize.
 * @param context WebApplicationContext containing Spring beans.
 * @param clazz Type of Sprout.  This is used to determine which declared
 * methods are candidates for auto-wiring.
 */
public static void initialize(final Object bean, final WebApplicationContext context, final Class clazz) {
    final Collection<Method> methods = SproutUtils.getDeclaredMethods(bean.getClass(), clazz);

    final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean.getClass());
    for (final PropertyDescriptor descriptor : descriptors) {
        final Class type = descriptor.getPropertyType();

        // beans should never be of type String
        // there must be a write method present
        // the write method must exist within the relevant subset of declared methods
        if (!type.equals(String.class) && null != descriptor.getWriteMethod()
                && methods.contains(descriptor.getWriteMethod())) {
            final Object serviceBean = context.getBean(descriptor.getName());
            if (null != serviceBean) {
                try {
                    log.debug("Wiring property '" + descriptor.getName() + "' with bean of type "
                            + serviceBean.getClass().getName());
                    PropertyUtils.setProperty(bean, descriptor.getName(), serviceBean);
                } catch (final IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (final InvocationTargetException e) {
                    throw new RuntimeException(e);
                } catch (final NoSuchMethodException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * TODO additional lifecycle interface callbacks as defined in BeanFactory
     * should be implemented here
     * @see org.springframework.beans.factory.BeanFactory
     */

    // InitializingBean callback
    if (bean instanceof InitializingBean) {
        try {
            ((InitializingBean) bean).afterPropertiesSet();
        } catch (final Exception e) {
            log.warn("Exception while running afterPropertiesSet() on an InitializingBean: " + e.getMessage(),
                    e);
        }
    }
}

From source file:org.kuali.rice.devtools.generators.dd.BeanDDCreator.java

public static boolean isNormalProperty(PropertyDescriptor p) {
    return p.getPropertyType() != null && !BusinessObject.class.isAssignableFrom(p.getPropertyType())
            && !p.getName().equals("objectId") && !p.getName().equals("class")
            && !p.getName().startsWith("boNote") && !p.getName().startsWith("autoIncrementSet")
            && !p.getName().equals("newCollectionRecord");
}

From source file:ar.com.fdvs.dj.domain.builders.ReflectiveReportBuilder.java

/**
 * Checks if a property's type is valid to be included in the report.
 * @param _property the property./*from w w w .j a  v  a 2 s .  c om*/
 * @return true if the property is is of a valid type.
 */
private static boolean isValidPropertyClass(final PropertyDescriptor _property) {
    final Class type = _property.getPropertyType();
    return Number.class.isAssignableFrom(type) || type == String.class || Date.class.isAssignableFrom(type)
            || type == Boolean.class;
}

From source file:io.github.moosbusch.lumpi.util.FormUtil.java

public static Map<String, Class<?>> getPropertyTypesMap(final Class<?> type,
        Map<String, Set<Class<?>>> excludedProperties) {
    final Map<String, Class<?>> result = new HashMap<>();
    PropertyDescriptor[] propDescs = PropertyUtils.getPropertyDescriptors(type);
    Map<String, Set<Class<?>>> excludedProps = excludedProperties;

    if (excludedProps != null) {
        for (PropertyDescriptor propDesc : propDescs) {
            Class<?> propertyType = propDesc.getPropertyType();

            if (propertyType != null) {
                String propertyName = propDesc.getName();

                if (excludedProps.containsKey(propertyName)) {
                    Set<Class<?>> ignoredPropertyTypes = excludedProps.get(propertyName);

                    if (!ignoredPropertyTypes.contains(type)) {
                        Set<Class<?>> superTypes = LumpiUtil.getSuperTypes(type, false, true, true);

                        for (Class<?> superType : superTypes) {
                            if (ignoredPropertyTypes.contains(superType)) {
                                result.put(propertyName, propertyType);
                                break;
                            }//from  www  . ja  v a 2  s. c o m
                        }
                    }

                }
            }
        }
    }

    return result;
}