1
2
3
4
5
6
7
8 package it.imolinfo.jbi4corba.webservice.generator.bcm;
9
10 import it.imolinfo.jbi4corba.Logger;
11 import it.imolinfo.jbi4corba.LoggerFactory;
12
13 import org.objectweb.asm.ClassAdapter;
14 import org.objectweb.asm.ClassVisitor;
15 import org.objectweb.asm.ClassWriter;
16 import org.objectweb.asm.FieldVisitor;
17 import org.objectweb.asm.MethodVisitor;
18 import org.objectweb.asm.Opcodes;
19
20
21
22
23
24
25
26
27 public class ValueTypeAdapter extends ClassAdapter {
28
29
30
31
32 private static final Logger LOG
33 = LoggerFactory.getLogger(ValueTypeAdapter.class);
34
35
36
37
38 protected ClassWriter classWriter = null;
39
40
41
42
43 protected String className = null;
44
45
46
47
48
49
50
51
52 public ValueTypeAdapter(ClassVisitor cv, ClassWriter cw, String cn) {
53 super(cv);
54 classWriter = cw;
55 className = cn;
56 }
57
58
59
60
61
62
63
64
65
66
67 @Override
68 public void visit(int version,
69 int access,
70 String name,
71 String signature,
72 String superName,
73 String [] interfaces) {
74
75 LOG.debug(">>>>> visit - begin");
76
77 LOG.debug("VISIT"
78 + ".\n version=" + version
79 + ";\n access=" + access
80 + ";\n name=" + name
81 + ";\n signature=" + signature
82 + ";\n superName=" + superName
83 + ";\n interfaces=" + interfaces);
84
85
86
87
88 if (interfaces == null || interfaces.length == 0) {
89
90
91 interfaces = new String [] {"java/io/Serializable"};
92
93 } else {
94
95
96
97 String [] temp = new String [interfaces.length + 1];
98
99 System.arraycopy(interfaces, 0, temp, 0, interfaces.length);
100 temp[interfaces.length] = "java/io/Serializable";
101
102 interfaces = temp;
103 }
104
105 LOG.debug("<<<<< visit - end");
106 super.visit(version, access, name, signature, superName, interfaces);
107 }
108
109
110
111
112
113
114
115
116
117
118 @Override
119 public FieldVisitor visitField(int access,
120 String name,
121 String desc,
122 String signature,
123 Object value) {
124 LOG.debug(">>>>> visitField - begin");
125
126 if ((Opcodes.ACC_STATIC & access) != Opcodes.ACC_STATIC) {
127 access = Opcodes.ACC_PUBLIC;
128 }
129 LOG.debug("The field " + name + " will be 'public'.");
130
131 LOG.debug(">>>>> visitField - end");
132 return super.visitField(access, name, desc, signature, value);
133 }
134
135
136
137
138
139
140
141
142
143
144 @Override
145 public MethodVisitor visitMethod(int access,
146 String name,
147 String desc,
148 String signature,
149 String[] exceptions) {
150
151 LOG.debug(">>>>> visitMethod - begin");
152
153 LOG.debug("visitMethod. access=" + access
154 + "; name=" + name
155 + "; desc=" + desc
156 + "; signature=" + signature
157 + "; exceptions=" + exceptions);
158
159
160 if (!name.equals("getFaultInfo")) {
161
162 if (isGetter(name, desc) || isSetter(name, desc)) {
163
164 LOG.debug("<<<<< visitMethod - remove method:" + name);
165 return null;
166 }
167 }
168
169
170 LOG.debug("<<<<< visitMethod - untouched:" + name);
171 return super.visitMethod(access, name, desc, signature, exceptions);
172 }
173
174
175
176
177
178
179
180 private boolean isGetter(String methodName, String params) {
181 if (methodName == null) {
182 return false;
183 }
184
185 if (methodName.startsWith("get") && params.startsWith("()")
186 && methodName.length() > 3) {
187 return true;
188 }
189
190 if (methodName.startsWith("is") && params.startsWith("()")
191 && methodName.length() > 2) {
192 return true;
193 }
194
195 return false;
196 }
197
198
199
200
201
202
203 private boolean isSetter(String methodName, String params) {
204 if (methodName == null) {
205 return false;
206 }
207
208 if (methodName.length() <= 3) {
209 return false;
210 }
211
212 if (methodName.startsWith("set") && (! params.startsWith("()"))) {
213 return true;
214 }
215
216 return false;
217 }
218
219 }