1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.beanfilter;
18
19 import java.beans.BeanInfo;
20 import java.beans.Introspector;
21 import java.beans.PropertyDescriptor;
22 import java.lang.reflect.Method;
23 import java.util.HashMap;
24 import java.util.Locale;
25 import java.util.Map;
26
27
28
29
30
31
32
33
34 public final class BeanDescriptor {
35
36
37
38 private static final Map<Class<?>, BeanDescriptor> DESCRIPTORS
39 = new HashMap<Class<?>, BeanDescriptor>();
40
41
42
43
44
45
46
47 public static synchronized BeanDescriptor getInstance(Class<?> c) {
48 BeanDescriptor d = (BeanDescriptor)DESCRIPTORS.get(c);
49 if (d == null) {
50 d = new BeanDescriptor(c);
51 DESCRIPTORS.put(c, d);
52 }
53 return new BeanDescriptor(c, d.accessors);
54 }
55
56
57
58
59 private Map<String, Method> accessors = new HashMap<String, Method>();
60
61
62
63
64
65
66
67 public void push(Class<?> parent, Token token) throws EvalException {
68
69 String field = token.image;
70
71 BeanDescriptor bd = BeanDescriptor.getInstance(parent);
72 Method method = bd.getMethod(field);
73 if (method == null) {
74
75 throw new EvalException("FTR005", token.beginLine, token.beginColumn, field);
76 }
77 }
78
79
80
81
82
83
84
85 private BeanDescriptor(Class<?> c, Map<String, Method> accessors) {
86 this(c);
87 this.accessors = accessors;
88 }
89
90
91
92
93
94
95 private BeanDescriptor(Class<?> c) {
96 inspect(c);
97 }
98
99
100
101
102
103
104
105 private void inspect(Class<?> c) {
106 try {
107 BeanInfo info = Introspector.getBeanInfo(c);
108
109 PropertyDescriptor[] props = info.getPropertyDescriptors();
110 int count = props == null ? 0 : props.length;
111 for (int i = 0; i < count; i++) {
112 if ("class".equals(props[i].getName())) {
113 continue;
114 }
115 Method m = props[i].getReadMethod();
116
117 String name = props[i].getName().toUpperCase(Locale.UK);
118 accessors.put(name, m);
119 }
120 } catch (Exception e) { }
121 }
122
123
124
125
126
127
128
129
130 public synchronized Method getMethod(String field) {
131 field = field.toUpperCase(Locale.UK);
132 Method m = (Method)accessors.get(field);
133 return m;
134 }
135
136 }