Java Code Examples for java.beans.Introspector#getBeanInfo()

The following examples show how to use java.beans.Introspector#getBeanInfo() . 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: IntrospectorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_MixedBooleanSimpleClass14() throws Exception {
    BeanInfo info = Introspector
            .getBeanInfo(MixedBooleanSimpleClass14.class);
    Method getter = MixedBooleanSimpleClass14.class
            .getDeclaredMethod("getList");
    Method setter = MixedBooleanSimpleClass14.class.getDeclaredMethod(
            "setList", boolean.class);

    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertFalse(pd instanceof IndexedPropertyDescriptor);
            assertEquals(getter, pd.getReadMethod());
            assertEquals(setter, pd.getWriteMethod());
        }
    }
}
 
Example 2
Source File: ExtendedBeanInfoTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void propertyCountsWithNonStandardWriteMethod() throws IntrospectionException {
	class ExtendedTestBean extends TestBean {
		@SuppressWarnings("unused")
		public ExtendedTestBean setFoo(String s) { return this; }
	}
	BeanInfo bi = Introspector.getBeanInfo(ExtendedTestBean.class);
	BeanInfo ebi = new ExtendedBeanInfo(bi);

	boolean found = false;
	for (PropertyDescriptor pd : ebi.getPropertyDescriptors()) {
		if (pd.getName().equals("foo")) {
			found = true;
			break;
		}
	}
	assertThat(found, is(true));
	assertThat(ebi.getPropertyDescriptors().length, equalTo(bi.getPropertyDescriptors().length+1));
}
 
Example 3
Source File: TestUtils.java    From java-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Test access to the properties of an object through its accessors.
 *
 * @param obj the object to test
 * @throws Exception any exception
 */
public static void assertNoExceptionsOnGetters(final Object obj) throws Exception {

  final Class<?> clazz = obj.getClass();
  final BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
  final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

  for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) {
    final Method readMethod = propertyDescriptor.getReadMethod();

    if (readMethod != null) {
      try {
        readMethod.invoke(obj, new Object[] {});
      } catch (final InvocationTargetException e) {
        final StringBuffer msg = new StringBuffer();
        msg.append("Failure: " + propertyDescriptor.getName());
        msg.append(" Exception: " + e.getCause().getClass());
        msg.append(" Msg: " + e.getCause().getMessage());
        throw new AssertionFailedError(msg.toString());
      }
    }
  }
}
 
Example 4
Source File: IntrospectorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_MixedSimpleClass6() throws Exception {
    BeanInfo info = Introspector.getBeanInfo(MixedSimpleClass6.class);
    Method getter = MixedSimpleClass6.class.getDeclaredMethod("getList",
            int.class);

    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertTrue(pd instanceof IndexedPropertyDescriptor);
            assertNull(pd.getReadMethod());
            assertNull(pd.getWriteMethod());
            assertEquals(getter, ((IndexedPropertyDescriptor) pd)
                    .getIndexedReadMethod());
            assertNull(((IndexedPropertyDescriptor) pd)
                    .getIndexedWriteMethod());
        }
    }
}
 
Example 5
Source File: IntrospectorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_MixedBooleanExtendClass13() throws Exception {
    BeanInfo info = Introspector
            .getBeanInfo(MixedBooleanExtendClass13.class);
    Method getter = MixedBooleanSimpleClass42.class
            .getDeclaredMethod("isList");
    Method setter = MixedBooleanSimpleClass42.class.getDeclaredMethod(
            "setList", boolean.class);

    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertFalse(pd instanceof IndexedPropertyDescriptor);
            assertEquals(getter, pd.getReadMethod());
            assertEquals(setter, pd.getWriteMethod());
        }
    }
}
 
Example 6
Source File: CopyUtils.java    From SuperBoot with MIT License 6 votes vote down vote up
public static void Copy(Object source, Object dest) throws Exception {
    //获取属性
    BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), Object.class);
    PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();

    BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), Object.class);
    PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();

    try {
        for (int i = 0; i < sourceProperty.length; i++) {

            for (int j = 0; j < destProperty.length; j++) {

                if (sourceProperty[i].getName().equals(destProperty[j].getName())) {
                    //调用source的getter方法和dest的setter方法
                    destProperty[j].getWriteMethod().invoke(dest, sourceProperty[i].getReadMethod().invoke(source));
                    break;
                }
            }
        }
    } catch (Exception e) {
        throw new Exception("属性复制失败:" + e.getMessage());
    }
}
 
Example 7
Source File: IntrospectorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void test_MixedSimpleClass57() throws Exception {
    BeanInfo beanInfo = Introspector.getBeanInfo(MixedSimpleClass57.class);
    Method setter = MixedSimpleClass57.class.getMethod("setList",
            new Class<?>[] { int.class, boolean.class });
    assertEquals(2, beanInfo.getPropertyDescriptors().length);
    for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertNull(pd.getReadMethod());
            assertNull(pd.getWriteMethod());
            assertTrue(pd instanceof IndexedPropertyDescriptor);
            assertNull(((IndexedPropertyDescriptor) pd)
                    .getIndexedReadMethod());
            assertEquals(setter,
                    ((IndexedPropertyDescriptor) pd)
                            .getIndexedWriteMethod());
        }
    }
}
 
Example 8
Source File: ExtendedBeanInfoTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void standardReadAndNonStandardIndexedWriteMethod() throws IntrospectionException {
	@SuppressWarnings("unused") class C {
		public String[] getFoo() { return null; }
		public C setFoo(int i, String foo) { return this; }
	}

	BeanInfo bi = Introspector.getBeanInfo(C.class);

	assertThat(hasReadMethodForProperty(bi, "foo"), is(true));
	assertThat(hasWriteMethodForProperty(bi, "foo"), is(false));
	assertThat(hasIndexedWriteMethodForProperty(bi, "foo"), is(false));

	BeanInfo ebi = new ExtendedBeanInfo(bi);

	assertThat(hasReadMethodForProperty(ebi, "foo"), is(true));
	assertThat(hasWriteMethodForProperty(ebi, "foo"), is(false));
	assertThat(hasIndexedWriteMethodForProperty(ebi, "foo"), is(true));
}
 
Example 9
Source File: Intro.java    From onetwo with Apache License 2.0 6 votes vote down vote up
private Map<String, PropertyDescriptor> loadPropertyDescriptors0(){
	if(clazz==Object.class || clazz.isInterface() || clazz.isPrimitive())
		return Collections.emptyMap();
	BeanInfo beanInfo = null;
	try {
		beanInfo = Introspector.getBeanInfo(clazz, Object.class);
	} catch (Exception e) {
		throw new BaseException(e);
	}
	PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
	
	Map<String, PropertyDescriptor> maps = new LinkedHashMap<String, PropertyDescriptor>();
	for(PropertyDescriptor prop : props){
		maps.put(prop.getName(), prop);
	}
	return Collections.unmodifiableMap(maps);
}
 
Example 10
Source File: Test4520754.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static BeanInfo getBeanInfo(Boolean mark, Class type) {
    System.out.println("test=" + mark + " for " + type);
    BeanInfo info;
    try {
        info = Introspector.getBeanInfo(type);
    } catch (IntrospectionException exception) {
        throw new Error("unexpected exception", exception);
    }
    if (info == null) {
        throw new Error("could not find BeanInfo for " + type);
    }
    if (mark != info.getBeanDescriptor().getValue("test")) {
        throw new Error("could not find marked BeanInfo for " + type);
    }
    return info;
}
 
Example 11
Source File: PermissionUtils.java    From RuoYi with Apache License 2.0 6 votes vote down vote up
/**
 * 返回用户属性值
 *
 * @param property 属性名称
 * @return 用户属性值
 */
public static Object getPrincipalProperty(String property) {
    Subject subject = SecurityUtils.getSubject();
    if (ObjectUtil.isNotNull(subject)) {
        Object principal = subject.getPrincipal();
        try {
            BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
            for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
                if (pd.getName().equals(property)) {
                    return pd.getReadMethod().invoke(principal, (Object[]) null);
                }
            }
        } catch (Exception e) {
            log.error("Error reading property [{}] from principal of type [{}]", property,
                    principal.getClass().getName());
        }
    }
    return null;
}
 
Example 12
Source File: UnloadClassBeanInfo.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {
    Class cl = getStub();
    System.out.println("cl.getClassLoader() = " + cl.getClassLoader());
    final BeanInfo beanInfo = Introspector.getBeanInfo(cl, Object.class);
    MethodDescriptor[] mds = beanInfo.getMethodDescriptors();
    System.out.println("mds = " + Arrays.toString(mds));
    loader.close();
    loader=null;
    cl=null;
    Util.generateOOME();
    mds = beanInfo.getMethodDescriptors();
    System.out.println("mds = " + Arrays.toString(mds));
}
 
Example 13
Source File: Test6963811.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    try {
        Thread.sleep(this.time); // increase the chance of the deadlock
        Introspector.getBeanInfo(
                this.sync ? Super.class : Sub.class,
                this.sync ? null : Object.class);
    }
    catch (Exception exception) {
        exception.printStackTrace();
    }
}
 
Example 14
Source File: IntrospectorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_MixedSimpleClass2() throws Exception {
    BeanInfo info = Introspector.getBeanInfo(MixedSimpleClass2.class);
    Method getter = MixedSimpleClass2.class.getDeclaredMethod("getList");

    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertFalse(pd instanceof IndexedPropertyDescriptor);
            assertEquals(getter, pd.getReadMethod());
            assertNull(pd.getWriteMethod());
        }
    }
}
 
Example 15
Source File: Test4144543.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    Class type = Beans.instantiate(null, "Test4144543").getClass();

    // try all the various places that this would break before

    Introspector.getBeanInfo(type);
    new PropertyDescriptor("value", type);
    new PropertyDescriptor("value", type, "getValue", "setValue");
}
 
Example 16
Source File: TaskNotifier.java    From mdw with Apache License 2.0 5 votes vote down vote up
private Method getIncidentSetter(String name) throws IntrospectionException {
    if (incidentBeanInfo == null) {
        incidentBeanInfo = Introspector.getBeanInfo(Incident.class);
    }
    for (MethodDescriptor md : incidentBeanInfo.getMethodDescriptors()) {
        if (name.equals(md.getName()))
            return md.getMethod();
    }
    return null;
}
 
Example 17
Source File: IntrospectorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_MixedExtendClass1() throws Exception {
    BeanInfo info = Introspector.getBeanInfo(MixedExtendClass1.class);
    Method getter = MixedSimpleClass4.class.getDeclaredMethod("getList");
    Method setter = MixedExtendClass1.class.getDeclaredMethod("setList",
            Object.class);

    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        if (propertyName.equals(pd.getName())) {
            assertFalse(pd instanceof IndexedPropertyDescriptor);
            assertEquals(getter, pd.getReadMethod());
            assertEquals(setter, pd.getWriteMethod());
            break;
        }
    }
}
 
Example 18
Source File: Injected.java    From ion-java with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 19
Source File: ExtendedBeanInfoFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return an {@link ExtendedBeanInfo} for the given bean class, if applicable.
 */
@Override
@Nullable
public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
	return (supports(beanClass) ? new ExtendedBeanInfo(Introspector.getBeanInfo(beanClass)) : null);
}
 
Example 20
Source File: MissingExpressionMetaGenerator.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
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 ExpressionMetaData metaData;
    if ( ExpressionRegistry.getInstance().isExpressionRegistered( aClass.getName() ) ) {
      metaData = ExpressionRegistry.getInstance().getExpressionMetaData( aClass.getName() );
    } else {
      metaData = null;
    }

    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;
      }

      if ( metaData != null ) {
        final ExpressionPropertyMetaData data = metaData.getPropertyDescription( descriptor.getName() );
        if ( data != 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();
}