01: /*
02: * @(#)getBeanProperties.java 1.2 04/12/06
03: *
04: * Copyright (c) 2002-2004 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 pnuts.lang.*;
12: import pnuts.lang.Runtime;
13: import java.beans.*;
14: import java.util.*;
15:
16: /*
17: * getBeanProperties(bean)
18: */
19: public class getBeanProperties extends PnutsFunction {
20:
21: public getBeanProperties() {
22: super ("getBeanProperties");
23: }
24:
25: public boolean defined(int narg) {
26: return (narg == 1);
27: }
28:
29: protected Object exec(Object[] args, Context context) {
30: if (args.length != 1) {
31: undefined(args, context);
32: return null;
33: }
34: Object bean = args[0];
35: try {
36: BeanInfo info = Introspector.getBeanInfo(bean.getClass());
37: PropertyDescriptor[] props = info.getPropertyDescriptors();
38: HashMap map = new HashMap();
39: for (int i = 0; i < props.length; i++) {
40: PropertyDescriptor p = props[i];
41: if (p.getReadMethod() != null) {
42: String key = p.getName();
43: map.put(key, Runtime.getBeanProperty(context, bean,
44: key));
45: }
46: }
47: return map;
48: } catch (IntrospectionException e) {
49: throw new PnutsException(e, context);
50: }
51: }
52:
53: public String toString() {
54: return "function getBeanProperties(bean)";
55: }
56: }
|