01: /*
02: * BeanObjectDesc.java
03: *
04: * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.beans;
10:
11: import org.pnuts.lang.*;
12: import java.beans.*;
13: import java.util.*;
14: import java.lang.reflect.*;
15:
16: public class BeanObjectDesc implements ObjectDesc {
17: private Class cls;
18: private Class stopClass;
19: private int flag;
20: private PropertyDescriptor[] pd;
21:
22: public BeanObjectDesc(Class cls) throws IntrospectionException {
23: this (cls, null);
24: }
25:
26: public BeanObjectDesc(Class cls, Class stopClass)
27: throws IntrospectionException {
28: this (cls, stopClass, Introspector.IGNORE_ALL_BEANINFO);
29: }
30:
31: public BeanObjectDesc(Class cls, Class stopClass, int flag)
32: throws IntrospectionException {
33: this .cls = cls;
34: this .stopClass = stopClass;
35: this .flag = flag;
36: BeanInfo beanInfo = getBeanInfo(cls, stopClass);
37: this .pd = beanInfo.getPropertyDescriptors();
38: }
39:
40: BeanInfo getBeanInfo(Class targetClass, Class stopClass)
41: throws IntrospectionException {
42: if (stopClass == null) {
43: return Introspector.getBeanInfo(targetClass, flag);
44: } else {
45: return Introspector.getBeanInfo(targetClass, stopClass);
46: }
47: }
48:
49: public Method[] getMethods() {
50: try {
51: BeanInfo beanInfo;
52: if (stopClass != null) {
53: beanInfo = Introspector.getBeanInfo(cls, stopClass);
54: } else {
55: beanInfo = Introspector.getBeanInfo(cls, flag);
56: }
57: MethodDescriptor[] methodDesc = beanInfo
58: .getMethodDescriptors();
59: Method[] m = new Method[methodDesc.length];
60: for (int i = 0; i < methodDesc.length; i++) {
61: m[i] = methodDesc[i].getMethod();
62: }
63: return m;
64: } catch (IntrospectionException e) {
65: return new Method[] {};
66: }
67: }
68:
69: public void handleProperties(PropertyHandler handler) {
70: for (int i = 0; i < pd.length; i++) {
71: PropertyDescriptor p = pd[i];
72: handler.handle(p.getName(), p.getPropertyType(), p
73: .getReadMethod(), p.getWriteMethod());
74: }
75: }
76: }
|