source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/sitraka/bytecode/Utils.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 11.9 KB
Line 
1/*
2 * Copyright 2001-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.taskdefs.optional.sitraka.bytecode;
19
20import java.util.Vector;
21import org.apache.tools.ant.taskdefs.optional.depend.constantpool.ConstantPool;
22import org.apache.tools.ant.taskdefs.optional.depend.constantpool.Utf8CPInfo;
23
24/**
25 * Utilities mostly to manipulate methods and access flags.
26 *
27 */
28public class Utils {
29 /** public access flag */
30 public static final short ACC_PUBLIC = 1;
31 /** private access flag */
32 public static final short ACC_PRIVATE = 2;
33 /** protected access flag */
34 public static final short ACC_PROTECTED = 4;
35 /** static access flag */
36 public static final short ACC_STATIC = 8;
37 /** final access flag */
38 public static final short ACC_FINAL = 16;
39 /** super access flag */
40 public static final short ACC_SUPER = 32;
41 /** synchronized access flag */
42 public static final short ACC_SYNCHRONIZED = 32;
43 /** volatile access flag */
44 public static final short ACC_VOLATILE = 64;
45 /** transient access flag */
46 public static final short ACC_TRANSIENT = 128;
47 /** native access flag */
48 public static final short ACC_NATIVE = 256;
49 /** interface access flag */
50 public static final short ACC_INTERFACE = 512;
51 /** abstract access flag */
52 public static final short ACC_ABSTRACT = 1024;
53 /** strict access flag */
54 public static final short ACC_STRICT = 2048;
55
56 /** private constructor */
57 private Utils() {
58 }
59
60 /**
61 * return an UTF8 value from the pool located a a specific index.
62 * @param pool the constant pool to look at
63 * @param index index of the UTF8 value in the constant pool
64 * @return the value of the string if it exists
65 * @throws ClassCastException if the index is not an UTF8 constant.
66 */
67 public static String getUTF8Value(ConstantPool pool, int index) {
68 return ((Utf8CPInfo) pool.getEntry(index)).getValue();
69 }
70
71 /**
72 * parse all parameters from a descritor into fields of java name.
73 * @param descriptor of a method.
74 * @return the parameter list of a given method descriptor. Each string
75 * represent a java object with its fully qualified classname or the
76 * primitive name such as int, long, ...
77 */
78 public static String[] getMethodParams(String descriptor) {
79 int i = 0;
80 if (descriptor.charAt(i) != '(') {
81 throw new IllegalArgumentException("Method descriptor should start with a '('");
82 }
83 Vector params = new Vector();
84 StringBuffer param = new StringBuffer();
85 i++;
86 while ((i = descriptor2java(descriptor, i, param)) < descriptor.length()) {
87 params.add(param.substring(0));
88 param = new StringBuffer();
89 if (descriptor.charAt(i) == ')') {
90 i++;
91 break;
92 }
93 }
94 String[] array = new String[params.size()];
95 params.copyInto(array);
96 return array;
97 }
98
99 /**
100 * return the object type of a return type.
101 * @param descriptor
102 * @return get the return type objet of a given descriptor
103 */
104 public static String getMethodReturnType(String descriptor) {
105 int pos = descriptor.indexOf(')');
106 StringBuffer rettype = new StringBuffer();
107 descriptor2java(descriptor, pos + 1, rettype);
108 return rettype.toString();
109 }
110
111 /**
112 * Parse a single descriptor symbol and returns it java equivalent.
113 * @param descriptor the descriptor symbol.
114 * @param i the index to look at the symbol in the descriptor string
115 * @param sb the stringbuffer to return the java equivalent of the symbol
116 * @return the index after the descriptor symbol
117 */
118 public static int descriptor2java(String descriptor, int i, StringBuffer sb) {
119 // get the dimension
120 StringBuffer dim = new StringBuffer();
121 for (; descriptor.charAt(i) == '['; i++) {
122 dim.append("[]");
123 }
124 // now get the type
125 switch (descriptor.charAt(i)) {
126 case 'B':
127 sb.append("byte");
128 break;
129 case 'C':
130 sb.append("char");
131 break;
132 case 'D':
133 sb.append("double");
134 break;
135 case 'F':
136 sb.append("float");
137 break;
138 case 'I':
139 sb.append("int");
140 break;
141 case 'J':
142 sb.append("long");
143 break;
144 case 'S':
145 sb.append("short");
146 break;
147 case 'Z':
148 sb.append("boolean");
149 break;
150 case 'V':
151 sb.append("void");
152 break;
153 case 'L':
154 // it is a class
155 int pos = descriptor.indexOf(';', i + 1);
156 String classname = descriptor.substring(i + 1, pos).replace('/', '.');
157 sb.append(classname);
158 i = pos;
159 break;
160 default:
161 //@todo, yeah this happens because I got things like:
162 // ()Ljava/lang/Object; and it will return and ) will be here
163 // think about it.
164
165 //ooooops should never happen
166 //throw new IllegalArgumentException("Invalid descriptor
167 // symbol: '" + i + "' in '" + descriptor + "'");
168 }
169 sb.append(dim.toString());
170 return ++i;
171 }
172
173 /**
174 * check for abstract access
175 * @param access_flags access flags
176 */
177 public static boolean isAbstract(int access_flags) {
178 return (access_flags & ACC_ABSTRACT) != 0;
179 }
180
181 /**
182 * check for public access
183 * @param access_flags access flags
184 */
185 public static boolean isPublic(int access_flags) {
186 return (access_flags & ACC_PUBLIC) != 0;
187 }
188
189 /**
190 * check for a static access
191 * @param access_flags access flags
192 */
193 public static boolean isStatic(int access_flags) {
194 return (access_flags & ACC_STATIC) != 0;
195 }
196
197 /**
198 * check for native access
199 * @param access_flags access flags
200 */
201 public static boolean isNative(int access_flags) {
202 return (access_flags & ACC_NATIVE) != 0;
203 }
204
205 /**
206 * check for class access
207 * @param access_flags access flags
208 */
209 public static boolean isClass(int access_flags) {
210 return !isInterface(access_flags);
211 }
212
213 /**
214 * check for strict access
215 * @param access_flags access flags
216 */
217 public static boolean isStrict(int access_flags) {
218 return (access_flags & ACC_STRICT) != 0;
219 }
220
221 /**
222 * check for interface access
223 * @param access_flags access flags
224 */
225 public static boolean isInterface(int access_flags) {
226 return (access_flags & ACC_INTERFACE) != 0;
227 }
228
229 /**
230 * check for private access
231 * @param access_flags access flags
232 */
233 public static boolean isPrivate(int access_flags) {
234 return (access_flags & ACC_PRIVATE) != 0;
235 }
236
237 /**
238 * check for transient flag
239 * @param access_flags access flags
240 */
241 public static boolean isTransient(int access_flags) {
242 return (access_flags & ACC_TRANSIENT) != 0;
243 }
244
245 /**
246 * check for volatile flag
247 * @param access_flags access flags
248 */
249 public static boolean isVolatile(int access_flags) {
250 return (access_flags & ACC_VOLATILE) != 0;
251 }
252
253 /**
254 * check for super flag
255 * @param access_flags access flag
256 */
257 public static boolean isSuper(int access_flags) {
258 return (access_flags & ACC_SUPER) != 0;
259 }
260
261 /**
262 * check for protected flag
263 * @param access_flags access flags
264 */
265 public static boolean isProtected(int access_flags) {
266 return (access_flags & ACC_PROTECTED) != 0;
267 }
268
269 /**
270 * chck for final flag
271 * @param access_flags access flags
272 */
273 public static boolean isFinal(int access_flags) {
274 return (access_flags & ACC_FINAL) != 0;
275 }
276
277 /**
278 * check for synchronized flag
279 * @param access_flags access flags
280 */
281 public static boolean isSynchronized(int access_flags) {
282 return (access_flags & ACC_SYNCHRONIZED) != 0;
283 }
284
285 /**
286 * return the method access flag as java modifiers
287 * @param access_flags access flags
288 * @return the access flags as modifier strings
289 */
290 public static String getMethodAccess(int access_flags) {
291 StringBuffer sb = new StringBuffer();
292 if (isPublic(access_flags)) {
293 sb.append("public ");
294 } else if (isPrivate(access_flags)) {
295 sb.append("private ");
296 } else if (isProtected(access_flags)) {
297 sb.append("protected ");
298 }
299 if (isFinal(access_flags)) {
300 sb.append("final ");
301 }
302 if (isStatic(access_flags)) {
303 sb.append("static ");
304 }
305 if (isSynchronized(access_flags)) {
306 sb.append("synchronized ");
307 }
308 if (isNative(access_flags)) {
309 sb.append("native ");
310 }
311 if (isAbstract(access_flags)) {
312 sb.append("abstract ");
313 }
314 return sb.toString().trim();
315 }
316
317 /**
318 * return the field access flag as java modifiers
319 * @param access_flags access flags
320 * @return the access flags as modifier strings
321 */
322 public static String getFieldAccess(int access_flags) {
323 StringBuffer sb = new StringBuffer();
324 if (isPublic(access_flags)) {
325 sb.append("public ");
326 } else if (isPrivate(access_flags)) {
327 sb.append("private ");
328 } else if (isProtected(access_flags)) {
329 sb.append("protected ");
330 }
331 if (isFinal(access_flags)) {
332 sb.append("final ");
333 }
334 if (isStatic(access_flags)) {
335 sb.append("static ");
336 }
337 if (isVolatile(access_flags)) {
338 sb.append("volatile ");
339 }
340 if (isTransient(access_flags)) {
341 sb.append("transient ");
342 }
343 return sb.toString().trim();
344 }
345
346 /**
347 * return the class access flag as java modifiers
348 * @param access_flags access flags
349 * @return the access flags as modifier strings
350 */
351 public static String getClassAccess(int access_flags) {
352 StringBuffer sb = new StringBuffer();
353 if (isPublic(access_flags)) {
354 sb.append("public ");
355 } else if (isProtected(access_flags)) {
356 sb.append("protected ");
357 } else if (isPrivate(access_flags)) {
358 sb.append("private ");
359 }
360 if (isFinal(access_flags)) {
361 sb.append("final ");
362 }
363 if (isSuper(access_flags)) {
364 sb.append("/*super*/ ");
365 }
366 if (isInterface(access_flags)) {
367 sb.append("interface ");
368 }
369 if (isAbstract(access_flags)) {
370 sb.append("abstract ");
371 }
372 if (isClass(access_flags)) {
373 sb.append("class ");
374 }
375 return sb.toString().trim();
376 }
377}
378
379
380
Note: See TracBrowser for help on using the repository browser.