java.beans.BeanInfo#getPropertyDescriptors ( )源码实例Demo

下面列出了java.beans.BeanInfo#getPropertyDescriptors ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: tomcatsrc   文件: BeanELResolver.java
private void populateFromInterfaces(Class<?> aClass) throws IntrospectionException {
    Class<?> interfaces[] = aClass.getInterfaces();
    if (interfaces.length > 0) {
        for (Class<?> ifs : interfaces) {
            BeanInfo info = Introspector.getBeanInfo(ifs);
            PropertyDescriptor[] pds = info.getPropertyDescriptors();
            for (PropertyDescriptor pd : pds) {
                if (!this.properties.containsKey(pd.getName())) {
                    this.properties.put(pd.getName(), new BeanProperty(
                            this.type, pd));
                }
            }
        }
    }
    Class<?> superclass = aClass.getSuperclass();
    if (superclass != null) {
        populateFromInterfaces(superclass);
    }
}
 
源代码2 项目: metadata   文件: SuperTag.java
protected String getPrincipalProperty(Object principal, String property) throws TemplateModelException {
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(principal.getClass());

        // Loop through the properties to get the string value of the specified property
        for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
            if (propertyDescriptor.getName().equals(property)) {
                Object value = propertyDescriptor.getReadMethod().invoke(principal, (Object[]) null);

                return String.valueOf(value);
            }
        }

        // property not found, throw
        throw new TemplateModelException("Property ["+property+"] not found in principal of type ["+principal.getClass().getName()+"]");
    } catch (IllegalAccessException | IntrospectionException | InvocationTargetException e) {
        throw new TemplateModelException("Error reading property ["+property+"] from principal of type ["+principal.getClass().getName()+"]", e);
    }
}
 
源代码3 项目: thymeleaf-extras-shiro   文件: ShiroFacade.java
public static String getPrincipalProperty(final Object principal, final String property) {
    try {
        final BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
        for (final PropertyDescriptor pd : bi.getPropertyDescriptors()) {
            if (pd.getName().equals(property)) {
                final Object value = pd.getReadMethod().invoke(principal, (Object[]) null);
                return String.valueOf(value);
            }
        }
    } catch (final Exception e) {
        String message = "Error reading property [" + property + "] from principal of type [" + principal.getClass().getName() + "]";
        throw new IllegalArgumentException(message, e);
    }

    throw new IllegalArgumentException("Property [" + property + "] not found in principal of type [" + principal.getClass().getName() + "]");
}
 
源代码4 项目: jt808-server   文件: BeanUtils.java
public static Map<String, Object> toMapNotNull(Object obj) {
    if (obj == null)
        return null;

    Map<String, Object> result = new HashMap<>();

    BeanInfo beanInfo = getBeanInfo(obj.getClass());
    PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
    for (PropertyDescriptor pd : pds) {
        String name = pd.getName();
        if (ignores.contains(name))
            continue;

        Object value = getValue(obj, pd.getReadMethod());
        if (value != null)
            result.put(name, value);
    }
    return result;
}
 
源代码5 项目: tools   文件: BeanUtils.java
/**
 * map转bean
 *
 * @param map  map
 * @param bean bean对象
 * @param <T>  T
 * @return T
 */
public static <T> T toBean(Map<String, ?> map, Class<T> bean) {
    T object = null;
    try {
        object = bean.newInstance();
        BeanInfo beaninfo = Introspector.getBeanInfo(bean, Object.class);
        PropertyDescriptor[] pro = beaninfo.getPropertyDescriptors();
        for (PropertyDescriptor property : pro) {
            String name = property.getName();
            Object value = map.get(name);
            Method set = property.getWriteMethod();
            set.invoke(object, value);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return object;
}
 
源代码6 项目: openjdk-jdk8u   文件: Test6660539.java
private static PropertyDescriptor[] getPropertyDescriptors() {
    try {
        BeanInfo info = Introspector.getBeanInfo(Test6660539.class);
        return info.getPropertyDescriptors();
    }
    catch (IntrospectionException exception) {
        throw new Error("unexpected", exception);
    }
}
 
源代码7 项目: jivejdon   文件: BeanUtils.java
/**
 * Gets the properties from a Java Bean and returns them in a Map of String
 * name/value pairs. Because this method has to know how to convert a
 * bean property into a String value, only a few bean property
 * types are supported. They are: String, boolean, int, long, float, double,
 * Color, and Class.
 *
 * @param bean a Java Bean to get properties from.
 * @return a Map of all properties as String name/value pairs.
 */
public static Map getProperties(Object bean) {
    Map properties = new HashMap();
    try {
    	logger.debug("getProperties=" + bean.getClass().getName());
        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class);
        // Loop through all properties of the bean.
        PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
        String[] names = new String[descriptors.length];
        for (int i = 0; i < names.length; i++) {
            // Determine the property name.
            String name = descriptors[i].getName();
            Method methodc = descriptors[i].getReadMethod();
            logger.debug("name=" + name);
            logger.debug("Method=" + methodc.getName());
            // Decode the property value using the property type and
            // encoded String value.
            Object[] args = null; 
            Object value = methodc.invoke(bean, args);
            // Add to Map, encoding the value as a String.
            properties.put(name, encode(value));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return properties;
}
 
源代码8 项目: jdk8u-jdk   文件: Test6660539.java
private static PropertyDescriptor[] getPropertyDescriptors() {
    try {
        BeanInfo info = Introspector.getBeanInfo(Test6660539.class);
        return info.getPropertyDescriptors();
    }
    catch (IntrospectionException exception) {
        throw new Error("unexpected", exception);
    }
}
 
private boolean hasIndexedReadMethodForProperty(BeanInfo beanInfo, String propertyName) {
	for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
		if (pd.getName().equals(propertyName)) {
			if (!(pd instanceof IndexedPropertyDescriptor)) {
				return false;
			}
			return ((IndexedPropertyDescriptor)pd).getIndexedReadMethod() != null;
		}
	}
	return false;
}
 
源代码10 项目: dragonwell8_jdk   文件: Test4520754.java
/**
 * This is a regression test to ensure that 4168475 does not regress.
 */
private static void test4168475(Class type) {
    String[] newPath = {"infos"};
    String[] oldPath = Introspector.getBeanInfoSearchPath();

    Introspector.setBeanInfoSearchPath(newPath);
    BeanInfo info = getBeanInfo(Boolean.TRUE, type);
    Introspector.setBeanInfoSearchPath(oldPath);

    PropertyDescriptor[] pds = info.getPropertyDescriptors();
    if (pds.length != 1) {
        throw new Error("could not find custom BeanInfo for " + type);
    }
    Introspector.flushCaches();
}
 
protected void assertBeansEqual(String message,
                                Set<Object> comparedObjects,
                                Object expected,
                                Object actual) throws Exception {
   assertNotNull("Actual object should be equal to: " + expected + " but was null", actual);
   if (comparedObjects.contains(expected)) {
      return;
   }
   comparedObjects.add(expected);
   Class<? extends Object> type = expected.getClass();
   assertEquals("Should be of same type", type, actual.getClass());
   BeanInfo beanInfo = Introspector.getBeanInfo(type);
   PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
   for (int i = 0; i < descriptors.length; i++) {
      PropertyDescriptor descriptor = descriptors[i];
      Method method = descriptor.getReadMethod();
      if (method != null) {
         String name = descriptor.getName();
         Object expectedValue = null;
         Object actualValue = null;
         try {
            expectedValue = method.invoke(expected, EMPTY_ARGUMENTS);
            actualValue = method.invoke(actual, EMPTY_ARGUMENTS);
         } catch (Exception e) {
            LOG.info("Failed to access property: " + name);
         }
         assertPropertyValuesEqual(message + name, comparedObjects, expectedValue, actualValue);
      }
   }
}
 
源代码12 项目: j2objc   文件: IntrospectorTest.java
public void test_MixedBooleanSimpleClass2() throws Exception {
    BeanInfo info = Introspector
            .getBeanInfo(MixedBooleanSimpleClass2.class);
    Method getter = MixedBooleanSimpleClass2.class
            .getDeclaredMethod("getList");

    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertFalse(pd instanceof IndexedPropertyDescriptor);
            assertEquals(getter, pd.getReadMethod());
            assertNull(pd.getWriteMethod());
        }
    }
}
 
源代码13 项目: ion-java   文件: Injected.java
private static Dimension[] findDimensions(TestClass testClass)
throws Throwable
{
    List<FrameworkField> fields =
        testClass.getAnnotatedFields(Inject.class);
    if (fields.isEmpty())
    {
        throw new Exception("No fields of " + testClass.getName()
                            + " have the @Inject annotation");
    }

    BeanInfo beanInfo = Introspector.getBeanInfo(testClass.getJavaClass());
    PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();

    Dimension[] dimensions = new Dimension[fields.size()];

    int i = 0;
    for (FrameworkField field : fields)
    {
        int modifiers = field.getField().getModifiers();
        if (! Modifier.isPublic(modifiers) || ! Modifier.isStatic(modifiers))
        {
            throw new Exception("@Inject " + testClass.getName() + '.'
                                + field.getField().getName()
                                + " must be public static");
        }

        Dimension dim = new Dimension();
        dim.property = field.getField().getAnnotation(Inject.class).value();
        dim.descriptor = findDescriptor(testClass, descriptors, field, dim.property);
        dim.values = (Object[]) field.get(null);
        dimensions[i++] = dim;
    }

    return dimensions;
}
 
源代码14 项目: entando-core   文件: DataObjectModelAction.java
public List<String> getAllowedAttributeMethods(DataObject prototype, String attributeName) {
    List<String> methods = new ArrayList<String>();
    try {
        AttributeInterface attribute = (AttributeInterface) prototype.getAttribute(attributeName);
        if (null == attribute) {
            throw new ApsSystemException("Null Attribute '" + attributeName + "' for Data Type '"
                    + prototype.getTypeCode() + "' - '" + prototype.getTypeDescr());
        }
        String methodsString = this.getAllowedPublicAttributeMethods().getProperty(attribute.getType());
        if (null != methodsString) {
            String[] methodsArray = methodsString.split(";");
            methods = Arrays.asList(methodsArray);
        } else {
            BeanInfo beanInfo = Introspector.getBeanInfo(attribute.getClass(), AbstractAttribute.class);
            PropertyDescriptor[] prDescrs = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < prDescrs.length; i++) {
                PropertyDescriptor propertyDescriptor = prDescrs[i];
                if (null != propertyDescriptor.getReadMethod()) {
                    methods.add(propertyDescriptor.getDisplayName());
                }
            }
        }
    } catch (Throwable t) {
        _logger.error("error in getAllowedAttributeMethods", t);
    }
    return methods;
}
 
源代码15 项目: dragonwell8_jdk   文件: Test6660539.java
private static PropertyDescriptor[] getPropertyDescriptors() {
    try {
        BeanInfo info = Introspector.getBeanInfo(Test6660539.class);
        return info.getPropertyDescriptors();
    }
    catch (IntrospectionException exception) {
        throw new Error("unexpected", exception);
    }
}
 
源代码16 项目: j2objc   文件: IntrospectorTest.java
public void test_MixedBooleanSimpleClass36() throws Exception {
    BeanInfo info = Introspector
            .getBeanInfo(MixedBooleanSimpleClass36.class);
    Method normalGetter = MixedBooleanSimpleClass36.class
            .getDeclaredMethod("getList");

    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertFalse(pd instanceof IndexedPropertyDescriptor);
            assertEquals(normalGetter, pd.getReadMethod());
            assertNull(pd.getWriteMethod());
        }
    }
}
 
源代码17 项目: jdk8u_jdk   文件: Test4520754.java
/**
 * This is a regression test to ensure that 4168475 does not regress.
 */
private static void test4168475(Class type) {
    String[] newPath = {"infos"};
    String[] oldPath = Introspector.getBeanInfoSearchPath();

    Introspector.setBeanInfoSearchPath(newPath);
    BeanInfo info = getBeanInfo(Boolean.TRUE, type);
    Introspector.setBeanInfoSearchPath(oldPath);

    PropertyDescriptor[] pds = info.getPropertyDescriptors();
    if (pds.length != 1) {
        throw new Error("could not find custom BeanInfo for " + type);
    }
    Introspector.flushCaches();
}
 
源代码18 项目: jdk8u60   文件: Test4520754.java
/**
 * This is a regression test to ensure that 4168475 does not regress.
 */
private static void test4168475(Class type) {
    String[] newPath = {"infos"};
    String[] oldPath = Introspector.getBeanInfoSearchPath();

    Introspector.setBeanInfoSearchPath(newPath);
    BeanInfo info = getBeanInfo(Boolean.TRUE, type);
    Introspector.setBeanInfoSearchPath(oldPath);

    PropertyDescriptor[] pds = info.getPropertyDescriptors();
    if (pds.length != 1) {
        throw new Error("could not find custom BeanInfo for " + type);
    }
    Introspector.flushCaches();
}
 
源代码19 项目: logging-log4j2   文件: PropertySetter.java
/**
 * Uses JavaBeans {@link Introspector} to computer setters of object to be
 * configured.
 */
protected void introspect() {
    try {
        BeanInfo bi = Introspector.getBeanInfo(obj.getClass());
        props = bi.getPropertyDescriptors();
    } catch (IntrospectionException ex) {
        LOGGER.error("Failed to introspect {}: {}", obj, ex.getMessage());
        props = new PropertyDescriptor[0];
    }
}
 
public static void main( String[] args ) throws IOException, IntrospectionException {
  ClassicEngineBoot.getInstance().start();
  ExpressionQueryTool eqt = new ExpressionQueryTool();
  eqt.processDirectory( null );
  final Class[] classes = eqt.getExpressions();

  final DefaultTagDescription dtd = new DefaultTagDescription();
  dtd.setNamespaceHasCData( META_NAMESPACE, false );

  final XmlWriter writer = new XmlWriter( new PrintWriter( System.out ), dtd );

  final AttributeList attrList = new AttributeList();
  attrList.addNamespaceDeclaration( "", META_NAMESPACE );
  writer.writeTag( META_NAMESPACE, "meta-data", attrList, XmlWriter.OPEN );

  for ( int i = 0; i < classes.length; i++ ) {
    final Class aClass = classes[ i ];

    if ( OutputFunction.class.isAssignableFrom( aClass ) ) {
      // Output functions will not be recognized.
      continue;
    }
    if ( aClass.getName().indexOf( '$' ) >= 0 ) {
      // Inner-Classes will not be recognized.
      continue;
    }

    final AttributeList expressionAttrList = new AttributeList();
    expressionAttrList.setAttribute( META_NAMESPACE, "class", aClass.getName() );
    expressionAttrList
      .setAttribute( META_NAMESPACE, "bundle-name", "org.pentaho.reporting.engine.classic.core.metadata.messages" );
    expressionAttrList.setAttribute( META_NAMESPACE, "result", "java.lang.Object" );
    expressionAttrList.setAttribute( META_NAMESPACE, "expert", "false" );
    expressionAttrList.setAttribute( META_NAMESPACE, "hidden", "false" );
    expressionAttrList.setAttribute( META_NAMESPACE, "preferred", "false" );
    writer.writeTag( META_NAMESPACE, "expression", expressionAttrList, XmlWriter.OPEN );

    final BeanInfo beanInfo = Introspector.getBeanInfo( aClass );
    final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
    for ( int j = 0; j < descriptors.length; j++ ) {
      final PropertyDescriptor descriptor = descriptors[ j ];
      final String key = descriptor.getName();

      if ( "runtime".equals( key ) ) {
        continue;
      }
      if ( "active".equals( key ) ) {
        continue;
      }
      if ( "preserve".equals( key ) ) {
        continue;
      }

      if ( descriptor.getReadMethod() == null || descriptor.getWriteMethod() == null ) {
        continue;
      }

      final AttributeList propAttrList = new AttributeList();
      propAttrList.setAttribute( META_NAMESPACE, "name", descriptor.getName() );
      if ( "name".equals( key ) ) {
        propAttrList.setAttribute( META_NAMESPACE, "mandatory", "true" );
        propAttrList.setAttribute( META_NAMESPACE, "preferred", "true" );
        propAttrList.setAttribute( META_NAMESPACE, "value-role", "Name" );
        propAttrList.setAttribute( META_NAMESPACE, "expert", "false" );
      } else {
        propAttrList.setAttribute( META_NAMESPACE, "mandatory", "false" );
        propAttrList.setAttribute( META_NAMESPACE, "preferred", "false" );
        propAttrList.setAttribute( META_NAMESPACE, "value-role", "Value" );
        if ( "dependencyLevel".equals( key ) ) {
          propAttrList.setAttribute( META_NAMESPACE, "expert", "true" );
        } else {
          propAttrList.setAttribute( META_NAMESPACE, "expert", "false" );
        }
      }
      propAttrList.setAttribute( META_NAMESPACE, "hidden", "false" );
      writer.writeTag( META_NAMESPACE, "property", propAttrList, XmlWriter.CLOSE );

    }

    writer.writeCloseTag();
  }

  writer.writeCloseTag();
  writer.flush();
}