KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > enhancer > Util


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.generation.enhancer;
19
20 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
21 import org.objectweb.speedo.metadata.SpeedoPackage;
22 import org.objectweb.speedo.metadata.SpeedoClass;
23 import org.objectweb.speedo.generation.lib.NamingRules;
24 import org.objectweb.asm.Type;
25 import org.objectweb.asm.Constants;
26 import org.objectweb.asm.CodeVisitor;
27
28 import java.util.Set JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * Utility class.
35  *
36  * Adapted from isPersistentType and isPersitentCapable in EnhancerTool.
37  */

38 public class Util {
39
40     private static Set JavaDoc PERSISTENT_CLASSES;
41     private static Set JavaDoc AUTO_PERSISTENT_CLASSES;
42
43     static {
44         PERSISTENT_CLASSES = new HashSet JavaDoc();
45         AUTO_PERSISTENT_CLASSES = new HashSet JavaDoc();
46
47         AUTO_PERSISTENT_CLASSES.add("java.lang.String");
48         AUTO_PERSISTENT_CLASSES.add("java.lang.Character");
49         AUTO_PERSISTENT_CLASSES.add("java.lang.Boolean");
50         AUTO_PERSISTENT_CLASSES.add("java.lang.Number");
51         AUTO_PERSISTENT_CLASSES.add("java.lang.Byte");
52         AUTO_PERSISTENT_CLASSES.add("java.lang.Integer");
53         AUTO_PERSISTENT_CLASSES.add("java.lang.Long");
54         AUTO_PERSISTENT_CLASSES.add("java.lang.Double");
55         AUTO_PERSISTENT_CLASSES.add("java.lang.Float");
56         AUTO_PERSISTENT_CLASSES.add("java.lang.Short");
57
58         AUTO_PERSISTENT_CLASSES.add("java.math.BigInteger");
59         AUTO_PERSISTENT_CLASSES.add("java.math.BigDecimal");
60
61         PERSISTENT_CLASSES.add("java.util.Collection");
62         PERSISTENT_CLASSES.add("java.util.Map");
63         PERSISTENT_CLASSES.add("java.util.Set");
64         PERSISTENT_CLASSES.add("java.util.List");
65         PERSISTENT_CLASSES.add("java.util.LinkedList");
66         PERSISTENT_CLASSES.add("java.util.ArrayList");
67         PERSISTENT_CLASSES.add("java.util.HashMap");
68         PERSISTENT_CLASSES.add("java.util.TreeMap");
69         PERSISTENT_CLASSES.add("java.util.WeakHashMap");
70         PERSISTENT_CLASSES.add("java.util.Properties");
71
72         PERSISTENT_CLASSES.add("java.util.Vector");
73         PERSISTENT_CLASSES.add("java.util.HashTable");
74         PERSISTENT_CLASSES.add("java.util.Stack");
75
76         PERSISTENT_CLASSES.add("java.util.Date");
77         PERSISTENT_CLASSES.add("java.util.Locale");
78
79         AUTO_PERSISTENT_CLASSES.add("java.sql.Date");
80         AUTO_PERSISTENT_CLASSES.add("java.sql.Time");
81         AUTO_PERSISTENT_CLASSES.add("java.sql.Timestamp");
82
83         PERSISTENT_CLASSES.addAll(AUTO_PERSISTENT_CLASSES);
84     }
85
86     /**
87      * Tests whether a specific Type can be defined persistent.
88      * The test of may be persistent type is based on <b>JDO Specification</b>.
89      *
90      * @see #isPersistentCapable
91      * @param desc the descriptor of the type that is tested
92      * @param xml a collection of SpeedoXMLDescriptor
93      * @return true is the type can be defaulted as persistent, false either
94      */

95     public static boolean isPersistentType(final String JavaDoc desc,
96                                            final Collection JavaDoc xml) {
97         Type type = Type.getType(desc);
98         if (type.getSort() == Type.ARRAY) {
99             // an array can be defaulted as persistent if its elements can
100
return isPersistentType(type.getElementType().getDescriptor(), xml);
101         } else if (type.getSort() == Type.OBJECT) {
102             // for the class types it depends on the package and the class
103
String JavaDoc className = type.getClassName();
104             return PERSISTENT_CLASSES.contains(className)
105                 || isPersistentCapable(className, xml) != null;
106         } else {
107             // a basic type can always be defaulted as persistent
108
return true;
109         }
110     }
111     public static boolean isAutomaticPersistentType(final String JavaDoc desc,
112             final Collection JavaDoc xml) {
113         Type type = Type.getType(desc);
114         if (type.getSort() == Type.ARRAY) {
115             return false;
116         } else if (type.getSort() == Type.OBJECT) {
117             // for the class types it depends on the package and the class
118
String JavaDoc className = type.getClassName();
119             return AUTO_PERSISTENT_CLASSES.contains(className)
120                 || isPersistentCapable(className, xml) != null;
121         } else {
122             // a basic type can always be defaulted as persistent
123
return true;
124         }
125     }
126
127     /**
128      * Tests whether a JDO Instance exists in the object model.
129      *
130      * @param className the complete name of the class
131      * @param xml a collection of SpeedoXMLDescriptor
132      * @return null if the class is not known as a persistent capable class
133      */

134     public static SpeedoClass isPersistentCapable(final String JavaDoc className,
135                                                   final Collection JavaDoc xml) {
136         Iterator JavaDoc i = xml.iterator();
137         while (i.hasNext()) {
138             SpeedoXMLDescriptor xmlDesc = (SpeedoXMLDescriptor) i.next();
139             String JavaDoc fieldPackage = NamingRules.packageName(className);
140             String JavaDoc fieldClass = NamingRules.className(className);
141             SpeedoPackage pkg = (SpeedoPackage) xmlDesc.jdoPackage.get(fieldPackage);
142             if (pkg != null) {
143                 SpeedoClass c = (SpeedoClass) pkg.jdoClass.get(fieldClass);
144                 if (c != null) {
145                     return c;
146                 }
147             }
148         }
149         return null;
150     }
151
152     // ASM TOOLS //
153
//-----------//
154

155     public static void visitIntConstant(CodeVisitor cv, int value) {
156         if (value == 0) {
157             cv.visitInsn(Constants.ICONST_0);
158         } else if (value == 1) {
159             cv.visitInsn(Constants.ICONST_1);
160         } else if (value == 2) {
161             cv.visitInsn(Constants.ICONST_2);
162         } else if (value == 3) {
163             cv.visitInsn(Constants.ICONST_3);
164         } else if (value == 4) {
165             cv.visitInsn(Constants.ICONST_4);
166         } else if (value == 5) {
167             cv.visitInsn(Constants.ICONST_5);
168         } else if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE) {
169             cv.visitIntInsn(Constants.BIPUSH, value);
170         } else if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
171             cv.visitIntInsn(Constants.SIPUSH, value);
172         } else {
173             cv.visitLdcInsn(new Integer JavaDoc(value));
174         }
175     }
176
177     public static void visitLongConstant(CodeVisitor cv, long value) {
178         if (value == 0) {
179             cv.visitInsn(Constants.LCONST_0);
180         } else if (value == 1) {
181             cv.visitInsn(Constants.LCONST_1);
182         } else {
183             cv.visitLdcInsn(new Long JavaDoc(value));
184         }
185     }
186
187     // CONVERSIONS FROM ASM TO JAVA SOURCE CODE //
188
//------------------------------------------//
189

190     public static String JavaDoc modifier(int access) {
191         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
192         if ((access & Constants.ACC_PUBLIC) != 0) {
193             buf.append("public ");
194         }
195         if ((access & Constants.ACC_PRIVATE) != 0) {
196             buf.append("private ");
197         }
198         if ((access & Constants.ACC_PROTECTED) != 0) {
199             buf.append("protected ");
200         }
201         if ((access & Constants.ACC_FINAL) != 0) {
202             buf.append("final ");
203         }
204         if ((access & Constants.ACC_STATIC) != 0) {
205             buf.append("static ");
206         }
207         if ((access & Constants.ACC_SYNCHRONIZED) != 0) {
208             buf.append("synchronized ");
209         }
210         if ((access & Constants.ACC_VOLATILE) != 0) {
211             buf.append("volatile ");
212         }
213         if ((access & Constants.ACC_TRANSIENT) != 0) {
214             buf.append("transient ");
215         }
216         if ((access & Constants.ACC_NATIVE) != 0) {
217             buf.append("native ");
218         }
219         if ((access & Constants.ACC_ABSTRACT) != 0) {
220             buf.append("abstract ");
221         }
222         if ((access & Constants.ACC_STRICT) != 0) {
223             buf.append("strictfp ");
224         }
225         return buf.toString();
226     }
227
228     public static String JavaDoc type(Type type) {
229         switch (type.getSort()) {
230         case Type.BOOLEAN:
231             return "boolean";
232         case Type.CHAR:
233             return "char";
234         case Type.BYTE:
235             return "byte";
236         case Type.SHORT:
237             return "short";
238         case Type.INT:
239             return "int";
240         case Type.FLOAT:
241             return "float";
242         case Type.LONG:
243             return "long";
244         case Type.DOUBLE:
245             return "double";
246         case Type.ARRAY:
247             String JavaDoc result = type(type.getElementType());
248             for (int i = 0; i < type.getDimensions(); ++i) {
249                 result += "[]";
250             }
251             return result;
252         default:
253             result = type.getClassName();
254             if (result.startsWith("java.lang.")) {
255                 return result.substring("java.lang.".length());
256             }
257             return result;
258         }
259     }
260
261     public static Class JavaDoc getClass(Type type, ClassLoader JavaDoc cl) throws Exception JavaDoc {
262         switch (type.getSort()) {
263         case Type.BOOLEAN:
264             return Boolean.TYPE;
265         case Type.CHAR:
266             return Character.TYPE;
267         case Type.BYTE:
268             return Byte.TYPE;
269         case Type.SHORT:
270             return Short.TYPE;
271         case Type.INT:
272             return Integer.TYPE;
273         case Type.FLOAT:
274             return Float.TYPE;
275         case Type.LONG:
276             return Long.TYPE;
277         case Type.DOUBLE:
278             return Double.TYPE;
279         default:
280             return Class.forName(type.getClassName(), false, cl);
281         }
282     }
283     public static String JavaDoc getClassName(Type type) {
284         switch (type.getSort()) {
285         case Type.BOOLEAN:
286             return "Boolean.TYPE";
287         case Type.CHAR:
288             return "Character.TYPE";
289         case Type.BYTE:
290             return "Byte.TYPE";
291         case Type.SHORT:
292             return "Short.TYPE";
293         case Type.INT:
294             return "Integer.TYPE";
295         case Type.FLOAT:
296             return "Float.TYPE";
297         case Type.LONG:
298             return "Long.TYPE";
299         case Type.DOUBLE:
300             return "Double.TYPE";
301         case Type.ARRAY:
302             StringBuffer JavaDoc result = new StringBuffer JavaDoc(type(type.getElementType()));
303             for (int i = 0; i < type.getDimensions(); ++i) {
304                 result.append("[]");
305             }
306             return result.append(".class").toString();
307         default:
308             return type.getClassName() + ".class";
309         }
310     }
311 }
312
Popular Tags