1
2
3
4
5
6
7
8 package it.imolinfo.jbi4corba.webservice.generator.bcm;
9
10 import java.util.HashSet;
11 import java.util.Set;
12
13 import it.imolinfo.jbi4corba.Logger;
14 import it.imolinfo.jbi4corba.LoggerFactory;
15 import it.imolinfo.jbi4corba.webservice.generator.ClassMetaInfo;
16
17 import org.objectweb.asm.ClassAdapter;
18 import org.objectweb.asm.ClassVisitor;
19 import org.objectweb.asm.FieldVisitor;
20 import org.objectweb.asm.Type;
21
22
23
24
25
26 public class SerializableInspectorAdapter extends ClassAdapter {
27
28
29
30
31
32 public static final String INTERNAL_NAME_OF_SERIALIZABLE
33 = "java/io/Serializable";
34
35
36
37
38 public static final String FIELDNAME_SERIAL_VERSION_UID = "serialVersionUID";
39
40
41
42
43 private static final Logger LOG
44 = LoggerFactory.getLogger(SerializableInspectorAdapter.class);
45
46
47
48
49
50 protected ClassMetaInfo classMetaInfo = new ClassMetaInfo();
51
52
53
54
55
56
57 public SerializableInspectorAdapter(ClassVisitor cv) {
58 super(cv);
59 }
60
61
62
63
64
65
66
67
68
69
70 @Override
71 public void visit(int version, int access, String name, String signature,
72 String superName, String [] interfaces) {
73
74 LOG.debug("visit. version=" + version + "; access=" + access
75 + "; name" + name + "; superName=" + superName);
76
77 if (implementsSerializable(interfaces)) {
78 LOG.debug("The class " + name + " implements Serializable.");
79 classMetaInfo.setSerializable(true);
80
81 } else {
82 LOG.debug("The class " + name + " does not implement Serializable.");
83 classMetaInfo.setSerializable(false);
84 }
85
86 classMetaInfo.setClassName(name.replace('/', '.'));
87 classMetaInfo.setSuperClassName(superName.replace('/', '.'));
88
89 Set<String> set = new HashSet<String>();
90 if (interfaces != null) {
91 for (String current : interfaces) {
92 set.add(current.replace('/', '.'));
93 }
94 }
95 classMetaInfo.setInterfaces(set);
96
97 super.visit(version, access, name, signature, superName, interfaces);
98 }
99
100
101
102
103
104
105
106
107
108
109 @Override
110 public FieldVisitor visitField(int access, String name, String desc,
111 String signature, Object value) {
112
113 LOG.debug("visitField. access=" + access + "; name=" + name
114 + "; desc=" + desc + "; signature=" + signature + "; value=" + value);
115
116 if (hasSerialVersionUIDField(name, desc)) {
117 LOG.debug("The class " + name + " has a serial version UID:" + value);
118
119 classMetaInfo.setClassSerialVersionUid((Long) value);
120 }
121
122 return super.visitField(access, name, desc, signature, value);
123 }
124
125
126
127
128
129
130
131
132 protected boolean hasSerialVersionUIDField(String name, String desc) {
133 if (FIELDNAME_SERIAL_VERSION_UID.equals(name)
134 && Type.LONG_TYPE.getDescriptor().equals(desc)) {
135
136 LOG.debug(FIELDNAME_SERIAL_VERSION_UID + " found.");
137 return true;
138 }
139
140 return false;
141 }
142
143
144
145
146
147 protected boolean implementsSerializable(String [] interfaces) {
148 if (interfaces == null || interfaces.length == 0) {
149 return false;
150 }
151
152 for (String current : interfaces) {
153 if (INTERNAL_NAME_OF_SERIALIZABLE.equals(current)) {
154 return true;
155 }
156 }
157
158 return false;
159 }
160
161
162
163
164 public ClassMetaInfo getClassMetaInfo() {
165 return classMetaInfo;
166 }
167
168
169
170
171 public void setClassMetaInfo(ClassMetaInfo classMetaInfo) {
172 this.classMetaInfo = classMetaInfo;
173 }
174
175 }