1 /****************************************************************************
2 * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the LGPL License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 ****************************************************************************/
8 package it.imolinfo.jbi4corba.webservice.generator.bcm;
9
10 import it.imolinfo.jbi4corba.Logger;
11 import it.imolinfo.jbi4corba.LoggerFactory;
12 import java.util.ArrayList;
13 import java.util.List;
14
15
16 import org.objectweb.asm.ClassAdapter;
17 import org.objectweb.asm.ClassVisitor;
18 import org.objectweb.asm.FieldVisitor;
19 import org.objectweb.asm.Opcodes;
20
21 /**
22 * Used to find a class that provides a corba enum implementation.
23 */
24 public class CorbaEnumAdapter extends ClassAdapter {
25
26 /** Logger. */
27 private static final Logger LOG
28 = LoggerFactory.getLogger(CorbaEnumAdapter.class);
29
30 /** The internal class name. */
31 private String internalClassName = null;
32
33 /** true when the class inspected is a corba enum (not a enum class). */
34 private boolean corbaEnum = false;
35
36 /** The list of the labels of the enum. */
37 private List<String> enumLabelList = new ArrayList<String>();
38
39 /**
40 * The adapater used to manipulate the code.
41 *
42 * @param cv The ClassVisitor used in this object.
43 *
44 */
45 public CorbaEnumAdapter(ClassVisitor cv) {
46 super(cv);
47
48 LOG.debug("new CorbaEnumAdapter; ClassVisitor=" + cv);
49 }
50
51 /**
52 * Override.
53 * @param version The version
54 * @param access The access
55 * @param name The name
56 * @param signature The signature
57 * @param superName The superName
58 * @param interfaces The interfaces
59 */
60 @Override
61 public void visit(int version,
62 int access,
63 String name,
64 String signature,
65 String superName,
66 String [] interfaces) {
67 LOG.debug(">>>>> visit - begin");
68
69 LOG.debug("VISIT"
70 + ".\n version=" + version
71 + ";\n access=" + access
72 + ";\n name=" + name
73 + ";\n signature=" + signature
74 + ";\n superName=" + superName
75 + ";\n interfaces=" + interfaces);
76
77 setInternalClassName(name);
78
79 super.visit(version, access, name, signature, superName, interfaces);
80 }
81
82 /**
83 * @param access the field's access flags (see Opcodes).
84 * This parameter also indicates if the field is synthetic
85 * and/or deprecated.
86 * @param name the field's name.
87 * @param desc the field's descriptor (see Type).
88 * @param signature the field's signature. May be null if the field's type
89 * does not use generic types.
90 * @param value the field's initial value.
91 * This parameter, which may be null if the field does not
92 * have an initial value, must be an Integer, a Float,
93 * a Long, a Double or a String (for int, float, long or
94 * String fields respectively).
95 * This parameter is only used for static fields.
96 * Its value is IGNORED for non static fields,
97 * which must be initialized through bytecode
98 * instructions in constructors or methods.
99 *
100 * @return a visitor to visit field annotations and attributes,
101 * or null if this class visitor is not interested
102 * in visiting these annotations and attributes.
103 */
104 @Override
105 public FieldVisitor visitField(int access,
106 String name,
107 String desc,
108 String signature,
109 Object value) {
110
111 LOG.debug(">>>>> visitField - begin. access=" + access
112 + "; name=" + name
113 + "; desc=" + desc
114 + "; signature=" + signature
115 + "; value=" + value);
116
117 int __arrayFieldAccess = Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC;
118 String __arrayFieldDesc = "[L" + getInternalClassName() + ";";
119
120 if ("__array".equals(name)
121 && (__arrayFieldAccess == access) && (__arrayFieldDesc.equals(desc))) {
122
123 LOG.debug("The class " + getInternalClassName() + " is a CorbaEnum.");
124 setCorbaEnum(true);
125
126 } else {
127 checkLabel(access, name, desc);
128 }
129
130 return super.visitField(access, name, desc, signature, value);
131 }
132
133 /**
134 * This method is used to collect the corba enum labels.
135 *
136 * @param access The modifiers.
137 * @param name The field name.
138 * @param desc The data type of the field.
139 *
140 */
141 protected void checkLabel(int access, String name, String desc) {
142
143 int labelAccess = Opcodes.ACC_PUBLIC
144 + Opcodes.ACC_STATIC
145 + Opcodes.ACC_FINAL;
146
147 String labelDesc = "L" + getInternalClassName() + ";";
148
149 if (labelAccess == access && labelDesc.equals(desc)) {
150 LOG.debug("The field " + name + " is a label of the enumeration.");
151 getEnumLabelList().add(name);
152 }
153
154 }
155
156 /**
157 * A getter.
158 * @return The current value.
159 */
160 public List<String> getEnumLabelList() {
161 return enumLabelList;
162 }
163
164 /**
165 * A setter.
166 * @param val The new value
167 */
168 public void setEnumLabelList(List<String> val) {
169 this.enumLabelList = val;
170 }
171
172 /**
173 * A getter.
174 * @return The current value.
175 */
176 public boolean isCorbaEnum() {
177 return corbaEnum;
178 }
179
180 /**
181 * A setter.
182 * @param val The new value
183 */
184 public void setCorbaEnum(boolean val) {
185 this.corbaEnum = val;
186 }
187
188 /**
189 * A getter.
190 * @return The current value.
191 */
192 public String getInternalClassName() {
193 return internalClassName;
194 }
195
196 /**
197 * A setter.
198 * @param val The new value
199 */
200 public void setInternalClassName(String val) {
201 this.internalClassName = val;
202 }
203
204 }