KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > util > attrs > ASMAnnotationDefaultAttribute


1 /**
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000,2002,2003 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package oracle.toplink.libraries.asm.util.attrs;
32
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import oracle.toplink.libraries.asm.Attribute;
37 import oracle.toplink.libraries.asm.ClassReader;
38 import oracle.toplink.libraries.asm.Label;
39 import oracle.toplink.libraries.asm.Type;
40 import oracle.toplink.libraries.asm.attrs.Annotation;
41 import oracle.toplink.libraries.asm.attrs.AnnotationDefaultAttribute;
42 import oracle.toplink.libraries.asm.attrs.Annotation.EnumConstValue;
43
44 /**
45  * An {@link ASMifiable} {@link AnnotationDefaultAttribute} sub class.
46  *
47  * @author Eugene Kuleshov
48  */

49
50 public class ASMAnnotationDefaultAttribute extends AnnotationDefaultAttribute
51   implements ASMifiable
52 {
53
54   protected Attribute read (ClassReader cr, int off,
55     int len, char[] buf, int codeOff, Label[] labels)
56   {
57     AnnotationDefaultAttribute attr =
58       (AnnotationDefaultAttribute)super.read(
59         cr, off, len, buf, codeOff, labels);
60     
61     ASMAnnotationDefaultAttribute result = new ASMAnnotationDefaultAttribute();
62     result.defaultValue = attr.defaultValue;
63     return result;
64   }
65    
66   public void asmify (StringBuffer JavaDoc buf, String JavaDoc varName, Map JavaDoc labelNames) {
67     buf.append("AnnotationDefaultAttribute ").append(varName)
68       .append(" = new AnnotationDefaultAttribute();\n");
69     String JavaDoc val = asmify(defaultValue, buf, varName + "Val");
70     buf.append(varName).append(".defaultValue = ")
71       .append(val).append(";\n");
72   }
73   
74   static void asmifyAnnotations (StringBuffer JavaDoc buf, String JavaDoc varName, List JavaDoc annotations) {
75     if (annotations.size() > 0) {
76       buf.append("{\n");
77       for (int i = 0; i < annotations.size(); i++) {
78         String JavaDoc val = varName + "ann" + i;
79         asmify((Annotation)annotations.get(i), buf, val);
80         buf.append(varName).append(".annotations.add( ").append(val).append(");\n");
81       }
82       buf.append("}\n");
83     }
84   }
85
86   static void asmifyParameterAnnotations (StringBuffer JavaDoc buf, String JavaDoc varName, List JavaDoc parameters) {
87     if (parameters.size() > 0) {
88       buf.append("{\n");
89       for (int i = 0; i < parameters.size(); i++) {
90         String JavaDoc val = varName + "param" + i;
91         buf.append( "List ").append( val).append( " = new ArrayList();\n");
92         List JavaDoc annotations = (List JavaDoc)parameters.get(i);
93         if (annotations.size() > 0) {
94           buf.append("{\n");
95           for (int i1 = 0; i1 < annotations.size(); i1++) {
96             String JavaDoc val1 = val + "ann" + i1;
97             asmify((Annotation)annotations.get(i1), buf, val1);
98             buf.append(val).append(".add( ").append(val1).append(");\n");
99           }
100           buf.append("}\n");
101         }
102         buf.append(varName).append(".parameters.add( ").append(val).append(");\n\n");
103       }
104       buf.append("}\n");
105     }
106   }
107
108   static String JavaDoc asmify (Annotation a, StringBuffer JavaDoc buf, String JavaDoc varName) {
109     buf.append("Annotation ").append(varName)
110         .append(" = new Annotation(\"").append(a.type).append("\");\n");
111     List JavaDoc elementValues = a.elementValues;
112     if (elementValues.size() > 0) {
113       // buf.append("{\n");
114
for (int i = 0; i < elementValues.size(); i++) {
115         Object JavaDoc[] values = (Object JavaDoc[])elementValues.get(i);
116         String JavaDoc val = asmify(values[1], buf, varName + "val" + i);
117         buf.append(varName).append(".add( \"")
118           .append(values[0]).append("\", ").append(val).append(");\n");
119       }
120       // buf.append("}\n");
121
}
122     return varName;
123   }
124
125   static String JavaDoc asmify (Object JavaDoc value, StringBuffer JavaDoc buf, String JavaDoc valName) {
126     if (value instanceof String JavaDoc) {
127       return "\""+value+"\"";
128
129     } else if (value instanceof Integer JavaDoc) {
130       return "new Integer("+value+")";
131         
132     } else if (value instanceof Byte JavaDoc) {
133       return "new Byte((byte)"+value+")";
134
135     } else if (value instanceof Character JavaDoc) {
136       return "new Character((char)"+((int)((Character JavaDoc)value).charValue())+")";
137
138     } else if (value instanceof Double JavaDoc) {
139       return "new Double(\""+value+"\")";
140
141     } else if (value instanceof Float JavaDoc) {
142       return "new Float(\""+value+"\")";
143
144     } else if (value instanceof Long JavaDoc) {
145       return "new Long("+value+"L)";
146
147     } else if (value instanceof Short JavaDoc) {
148       return "new Short((short)"+value+")";
149
150     } else if (value instanceof Boolean JavaDoc) {
151       return "new Boolean("+value+")";
152
153     } else if (value instanceof EnumConstValue) {
154       EnumConstValue e = (EnumConstValue) value;
155       return "new Annotation.EnumConstValue(\""+e.typeName+"\", \""+e.constName+"\")";
156
157     } else if (value instanceof Type) {
158       Type t = (Type)value;
159       return "Type.getType(\""+t.getDescriptor()+"\")";
160
161     } else if (value instanceof Annotation) {
162       return asmify((Annotation)value, buf, valName);
163
164     } else if (value instanceof Object JavaDoc[]) {
165       Object JavaDoc[] v = (Object JavaDoc[])value;
166       buf.append("Object[] ").append(valName)
167         .append(" = new Object[").append(v.length).append("];\n");
168       for (int i = 0; i < v.length; i++) {
169         String JavaDoc val = asmify(v[i], buf, valName+"Arr"+i);
170         buf.append(valName+"["+i+"] = ").append(val).append(";\n");
171       }
172       return valName;
173     
174     } else if( value instanceof byte[]) {
175       byte[] v = (byte[])value;
176       StringBuffer JavaDoc sb = new StringBuffer JavaDoc( "new byte[] {");
177       String JavaDoc sep = "";
178       for (int i = 0; i < v.length; i++) {
179         sb.append(sep).append(v[i]);
180         sep = ", ";
181       }
182       sb.append("}");
183       return sb.toString();
184       
185     } else if( value instanceof char[]) {
186       char[] v = (char[])value;
187       StringBuffer JavaDoc sb = new StringBuffer JavaDoc( "new char[] {");
188       String JavaDoc sep = "";
189       for (int i = 0; i < v.length; i++) {
190         sb.append(sep).append("(char)").append((int)v[i]);
191         sep = ", ";
192       }
193       sb.append("}");
194       return sb.toString();
195       
196     } else if( value instanceof short[]) {
197       short[] v = (short[])value;
198       StringBuffer JavaDoc sb = new StringBuffer JavaDoc( "new short[] {");
199       String JavaDoc sep = "";
200       for (int i = 0; i < v.length; i++) {
201         sb.append(sep).append("(short)").append(v[i]);
202         sep = ", ";
203       }
204       sb.append("}");
205       return sb.toString();
206       
207     } else if( value instanceof long[]) {
208       long[] v = (long[])value;
209       StringBuffer JavaDoc sb = new StringBuffer JavaDoc( "new long[] {");
210       String JavaDoc sep = "";
211       for (int i = 0; i < v.length; i++) {
212         sb.append(sep).append(v[i]).append("L");
213         sep = ", ";
214       }
215       sb.append("}");
216       return sb.toString();
217       
218     } else if( value instanceof int[]) {
219       int[] v = (int[])value;
220       StringBuffer JavaDoc sb = new StringBuffer JavaDoc( "new int[] {");
221       String JavaDoc sep = "";
222       for (int i = 0; i < v.length; i++) {
223         sb.append(sep).append(v[i]);
224         sep = ", ";
225       }
226       sb.append("}");
227       return sb.toString();
228       
229     } else if( value instanceof boolean[]) {
230       boolean[] v = (boolean[])value;
231       StringBuffer JavaDoc sb = new StringBuffer JavaDoc( "new boolean[] {");
232       String JavaDoc sep = "";
233       for (int i = 0; i < v.length; i++) {
234         sb.append(sep).append(v[i]);
235         sep = ", ";
236       }
237       sb.append("}");
238       return sb.toString();
239       
240     } else if( value instanceof float[]) {
241       float[] v = (float[])value;
242       StringBuffer JavaDoc sb = new StringBuffer JavaDoc( "new float[] {");
243       String JavaDoc sep = "";
244       for (int i = 0; i < v.length; i++) {
245         sb.append(sep).append(v[i]).append("f");
246         sep = ", ";
247       }
248       sb.append("}");
249       return sb.toString();
250       
251     } else if( value instanceof double[]) {
252       double[] v = (double[])value;
253       StringBuffer JavaDoc sb = new StringBuffer JavaDoc( "new double[] {");
254       String JavaDoc sep = "";
255       for (int i = 0; i < v.length; i++) {
256         sb.append(sep).append(v[i]).append("d");
257         sep = ", ";
258       }
259       sb.append("}");
260       return sb.toString();
261       
262     } else {
263       throw new IllegalArgumentException JavaDoc( "Invalid value: "+value.getClass().getName()+" : "+value);
264       
265     }
266     // return null;
267
}
268 }
269
Popular Tags