1 package attrib4j.cfparse;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.ObjectInputStream;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.List;
10
11 import attrib4j.Attribute;
12 import attrib4j.AttributeExtractor;
13
14 import com.ibm.toad.cfparse.ClassFile;
15 import com.ibm.toad.cfparse.FieldInfoList;
16 import com.ibm.toad.cfparse.MethodInfo;
17 import com.ibm.toad.cfparse.MethodInfoList;
18 import com.ibm.toad.cfparse.attributes.AttrInfoList;
19
20 /***
21 * AttributeExtractor implementation using the CFParse bytecode library. Make
22 * sure you use the AttributeExtractor that pairs up with the corresponding
23 * ClassAnnotator implementation.
24 *
25 * @author <a href="mailto:mpollack@speakeasy.net">Mark Pollack</a>
26 * @author Ted Neward
27 * @version $Revision: 1.9 $ $Date: 2003/04/18 23:52:41 $
28 */
29 public class CFParseAttributeExtractor implements AttributeExtractor {
30
31 ClassFile cf;
32 MethodInfoList mil;
33 FieldInfoList fil;
34
35 /***
36 * Do nothing ctor.
37 *
38 */
39 public CFParseAttributeExtractor() {
40
41 }
42
43 /***
44 * Using the provided class loader, load the class so that it can
45 * be passed to a bytecode manipulation library.
46 *
47 * @param qualifiedClassname The fully qualified classname - contains
48 * package information, ie java.lang.String
49 * @param cl Classloader to use to load the class.
50 */
51 public void open(String qualifiedClassname, ClassLoader cl) {
52 String classFileName = qualifiedClassname.replace('.', '/') + ".class";
53
54 try {
55 InputStream clsInput = cl.getResourceAsStream(classFileName);
56
57 cf = new ClassFile(clsInput);
58 mil = cf.getMethods();
59 fil = cf.getFields();
60 } catch (IOException ioEx) {
61 // Not sure what to do here--repackage and throw?
62 ioEx.printStackTrace();
63 }
64 }
65
66 /***
67 * Close any resouces that were opened.
68 *
69 */
70 public void close() {
71 }
72
73 public Attribute[] getClassAttributes() {
74 return null;
75 }
76
77 public Attribute[] getClassAttributes(Class filter) {
78 return null;
79 }
80
81 /***
82 * Retreives custom attributes applied to the class.
83 *
84 * @return An array of custom attributes. Null if there are no
85 * attributes.
86 */
87 public Object[] getCustomClassAttributes() {
88 ArrayList al = new ArrayList();
89
90 AttrInfoList ail = cf.getAttrs();
91 for (int i = 0; i < ail.length(); i++) {
92 if (ail.getName(i).equals("attrib4j.cfparse.CFCustom")) {
93 CFCustomAttrInfo custAttrInfo = (CFCustomAttrInfo) ail.get(i);
94 byte[] serializedCustomAttribute =
95 custAttrInfo.getSerializedData();
96 try {
97 Object customAttr =
98 new ObjectInputStream(
99 new ByteArrayInputStream(serializedCustomAttribute))
100 .readObject();
101 al.add(customAttr);
102 } catch (IOException ioEx) {
103 //todo: Repackage and throw
104 ioEx.printStackTrace();
105 } catch (Exception ex) {
106 //todo: Repackage and throw
107 ex.printStackTrace();
108 }
109 }
110 }
111 return al.toArray();
112 }
113
114 /***
115 * Return the list (possible empty) of attribute associated with
116 * the class that instances of the 'filter' class.
117 *
118 * @param filter The instance that will filter out only matching
119 * class types.
120 * @return An array of attributes that satisfy the instanceof
121 * comparison with the filter class.
122 */
123 public Object[] getCustomClassAttributes(Object filter) {
124 ArrayList al = new ArrayList();
125
126 AttrInfoList ail = cf.getAttrs();
127 for (int i = 0; i < ail.length(); i++) {
128 if (ail.getName(i).equals("attrib4j.cfparse.CFCustom")) {
129 CFCustomAttrInfo custAttrInfo = (CFCustomAttrInfo) ail.get(i);
130 byte[] serializedCustomAttribute =
131 custAttrInfo.getSerializedData();
132 try {
133 Object customAttr =
134 new ObjectInputStream(
135 new ByteArrayInputStream(serializedCustomAttribute))
136 .readObject();
137 //anticipate getClassAttributes() call this method
138 if (filter != null) {
139 if (customAttr.getClass().isInstance(filter)) {
140 al.add(customAttr);
141 }
142 } else {
143 al.add(customAttr);
144 }
145 } catch (IOException ioEx) {
146 //todo: Repackage and throw
147 ioEx.printStackTrace();
148 } catch (Exception ex) {
149 //todo: Repackage and throw
150 ex.printStackTrace();
151 }
152 }
153 }
154 return al.toArray();
155 }
156
157 /***
158 * A no-op implementation since cfparse is very low on the list of priorities to support
159 * now that there is a BCEL implementation.
160 * @see attrib4j.AttributeExtractor#getMethodAttributes(String, String[])
161 * @return null
162 */
163 public Attribute[] getMethodAttributes(
164 String methodName,
165 String[] methodParamTypes) {
166 return null;
167 }
168
169 /***
170 * A no-op implementation since cfparse is very low on the list of priorities to support
171 * now that there is a BCEL implementation.
172 * @see attrib4j.AttributeExtractor#getMethodAttributes(java.lang.String, java.lang.String[], java.lang.Class)
173 * @return null
174 */
175 public Attribute[] getMethodAttributes(
176 String methodName,
177 String[] methodParamTypes,
178 Class filter) {
179 return null;
180 }
181
182 /***
183 * Retreives custom attributes applied to a specific method of the class.
184 *
185 * @param methodName The name of the method.
186 * @param methodParamTypes The signature of the method.
187 * @return An array of custom attributes. Null if there are no
188 * attributes.
189 */
190 public Object[] getCustomMethodAttributes(
191 String methodName,
192 String[] methodParamTypes) {
193 return getCustomMethodAttributes(methodName, methodParamTypes, null);
194 }
195 /***
196 * Retreives custom attributes applied to a specific method of the class.
197 *
198 * @param methodName The name of the method.
199 * @param methodParamTypes The signature of the method.
200 * @return An array of custom attributes. Null if there are no
201 * attributes.
202 */
203 public Object[] getCustomMethodAttributes(
204 String methodName,
205 String[] methodParamTypes,
206 Class filter) {
207 ArrayList al = new ArrayList();
208
209 MethodInfo mi = null;
210 for (int i = 0; i < mil.length(); i++) {
211 mi = mil.get(i);
212 if (mil.getMethodName(i).equals(methodName)
213 && Arrays.equals(methodParamTypes, mi.getParams())) {
214 harvestAttributes(mi.getAttrs(), al);
215 break;
216 }
217 }
218
219 return al.toArray();
220 }
221
222 public Attribute[] getFieldAttributes(String fieldName) {
223 return null;
224 }
225
226 /***
227 * Retreives custom attributes applied to a specific field of the class.
228 *
229 * @param fieldName the name of a class field.
230 * @return An array of custom attributes. Null if there are no
231 * attributes.
232 */
233 public Object[] getCustomFieldAttributes(String fieldName) {
234 ArrayList al = new ArrayList();
235
236 for (int i = 0; i < fil.length(); i++) {
237 if (fil.getFieldName(i).equals(fieldName)) {
238 harvestAttributes(fil.get(i).getAttrs(), al);
239 break;
240 }
241 }
242
243 return al.toArray();
244 }
245
246 /***
247 * Private implementation method to "harvest" custom attributes out of the
248 * AttrInfoList provided and put the deserialized attribute instances into
249 * the List provided.
250 */
251 private void harvestAttributes(AttrInfoList ail, List list) {
252 for (int i = 0; i < ail.length(); i++) {
253 //TODO: dependencyon package names.
254 if (ail.getName(i).equals("attrib4j.cfparse.CFCustom")) {
255 CFCustomAttrInfo custAttrInfo = (CFCustomAttrInfo) ail.get(i);
256 byte[] serializedCustomAttribute =
257 custAttrInfo.getSerializedData();
258 try {
259 Object customAttr =
260 new ObjectInputStream(
261 new ByteArrayInputStream(serializedCustomAttribute))
262 .readObject();
263 list.add(customAttr);
264 } catch (IOException ioEx) {
265 //todo Repackage and throw
266 ioEx.printStackTrace();
267 } catch (Exception ex) {
268 //todo Repackage and throw
269 ex.printStackTrace();
270 }
271 }
272 }
273 }
274 }
This page was automatically generated by Maven