1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.verifier;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.bcel.classfile.JavaClass;
27 import org.apache.bcel.classfile.Utility;
28 import org.apache.bcel.verifier.statics.Pass1Verifier;
29 import org.apache.bcel.verifier.statics.Pass2Verifier;
30 import org.apache.bcel.verifier.statics.Pass3aVerifier;
31 import org.apache.bcel.verifier.structurals.Pass3bVerifier;
32 import org.apache.commons.lang3.ArrayUtils;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class Verifier {
47
48 static final String NAME = "Apache Commons BCEL";
49 static final String BANNER = NAME + "\nhttps://commons.apache.org/bcel\n";
50
51 static final Verifier[] EMPTY_ARRAY = {};
52
53
54
55
56
57
58
59 public static void main(final String[] args) {
60 System.out.println(BANNER);
61 for (int index = 0; index < args.length; index++) {
62 try {
63 if (args[index].endsWith(JavaClass.EXTENSION)) {
64 final int dotclasspos = args[index].lastIndexOf(JavaClass.EXTENSION);
65 if (dotclasspos != -1) {
66 args[index] = args[index].substring(0, dotclasspos);
67 }
68 }
69 args[index] = Utility.pathToPackage(args[index]);
70 System.out.println("Now verifying: " + args[index] + "\n");
71 verifyType(args[index]);
72 org.apache.bcel.Repository.clearCache();
73 System.gc();
74 } catch (final ClassNotFoundException e) {
75 e.printStackTrace();
76 }
77 }
78 }
79
80 static void verifyType(final String fullyQualifiedClassName) throws ClassNotFoundException {
81 final Verifier verifier = VerifierFactory.getVerifier(fullyQualifiedClassName);
82 VerificationResult verificationResult;
83 verificationResult = verifier.doPass1();
84 System.out.println("Pass 1:\n" + verificationResult);
85 verificationResult = verifier.doPass2();
86 System.out.println("Pass 2:\n" + verificationResult);
87 if (verificationResult == VerificationResult.VR_OK) {
88 final JavaClass jc = org.apache.bcel.Repository.lookupClass(fullyQualifiedClassName);
89 for (int i = 0; i < jc.getMethods().length; i++) {
90 verificationResult = verifier.doPass3a(i);
91 System.out.println("Pass 3a, method number " + i + " ['" + jc.getMethods()[i] + "']:\n" + verificationResult);
92 verificationResult = verifier.doPass3b(i);
93 System.out.println("Pass 3b, method number " + i + " ['" + jc.getMethods()[i] + "']:\n" + verificationResult);
94 }
95 }
96 System.out.println("Warnings:");
97 final String[] warnings = verifier.getMessages();
98 if (warnings.length == 0) {
99 System.out.println("<none>");
100 }
101 for (final String warning : warnings) {
102 System.out.println(warning);
103 }
104 System.out.println("\n");
105
106 verifier.flush();
107 }
108
109
110
111
112 private final String className;
113
114
115 private Pass1Verifier p1v;
116
117
118 private Pass2Verifier p2v;
119
120
121 private final Map<String, Pass3aVerifier> p3avs = new HashMap<>();
122
123
124 private final Map<String, Pass3bVerifier> p3bvs = new HashMap<>();
125
126
127
128
129
130
131 Verifier(final String fullyQualifiedClassName) {
132 className = fullyQualifiedClassName;
133 }
134
135
136 public VerificationResult doPass1() {
137 if (p1v == null) {
138 p1v = new Pass1Verifier(this);
139 }
140 return p1v.verify();
141 }
142
143
144 public VerificationResult doPass2() {
145 if (p2v == null) {
146 p2v = new Pass2Verifier(this);
147 }
148 return p2v.verify();
149 }
150
151
152
153
154
155
156
157 public VerificationResult doPass3a(final int methodNo) {
158 return p3avs.computeIfAbsent(Integer.toString(methodNo), k -> new Pass3aVerifier(this, methodNo)).verify();
159 }
160
161
162
163
164
165
166
167 public VerificationResult doPass3b(final int methodNo) {
168 return p3bvs.computeIfAbsent(Integer.toString(methodNo), k -> new Pass3bVerifier(this, methodNo)).verify();
169 }
170
171
172
173
174
175 public void flush() {
176 p1v = null;
177 p2v = null;
178 p3avs.clear();
179 p3bvs.clear();
180 }
181
182
183
184
185
186
187
188
189 public final String getClassName() {
190 return className;
191 }
192
193
194
195
196
197
198
199 public String[] getMessages() throws ClassNotFoundException {
200 final List<String> messages = new ArrayList<>();
201 if (p1v != null) {
202 p1v.getMessagesList().forEach(element -> messages.add("Pass 1: " + element));
203 }
204 if (p2v != null) {
205 p2v.getMessagesList().forEach(element -> messages.add("Pass 2: " + element));
206 }
207 for (final Pass3aVerifier pv : p3avs.values()) {
208 final int meth = pv.getMethodNo();
209 for (final String element : pv.getMessages()) {
210 messages.add("Pass 3a, method " + meth + " ('" + org.apache.bcel.Repository.lookupClass(className).getMethods()[meth] + "'): " + element);
211 }
212 }
213 for (final Pass3bVerifier pv : p3bvs.values()) {
214 final int meth = pv.getMethodNo();
215 for (final String element : pv.getMessages()) {
216 messages.add("Pass 3b, method " + meth + " ('" + org.apache.bcel.Repository.lookupClass(className).getMethods()[meth] + "'): " + element);
217 }
218 }
219
220 return messages.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
221 }
222 }