1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.verifier;
18
19 import org.apache.bcel.Repository;
20 import org.apache.bcel.classfile.JavaClass;
21
22 /***
23 * This class has a main method implementing a demonstration program
24 * of how to use the VerifierFactoryObserver. It transitively verifies
25 * all class files encountered; this may take up a lot of time and,
26 * more notably, memory.
27 *
28 * @version $Id: TransitiveHull.java 386056 2006-03-15 11:31:56Z tcurdt $
29 * @author Enver Haase
30 */
31 public class TransitiveHull implements VerifierFactoryObserver {
32
33 /*** Used for indentation. */
34 private int indent = 0;
35
36
37 /*** Not publicly instantiable. */
38 private TransitiveHull() {
39 }
40
41
42
43 public void update( String classname ) {
44 System.gc();
45 for (int i = 0; i < indent; i++) {
46 System.out.print(" ");
47 }
48 System.out.println(classname);
49 indent += 1;
50 Verifier v = VerifierFactory.getVerifier(classname);
51 VerificationResult vr;
52 vr = v.doPass1();
53 if (vr != VerificationResult.VR_OK) {
54 System.out.println("Pass 1:\n" + vr);
55 }
56 vr = v.doPass2();
57 if (vr != VerificationResult.VR_OK) {
58 System.out.println("Pass 2:\n" + vr);
59 }
60 if (vr == VerificationResult.VR_OK) {
61 try {
62 JavaClass jc = Repository.lookupClass(v.getClassName());
63 for (int i = 0; i < jc.getMethods().length; i++) {
64 vr = v.doPass3a(i);
65 if (vr != VerificationResult.VR_OK) {
66 System.out.println(v.getClassName() + ", Pass 3a, method " + i + " ['"
67 + jc.getMethods()[i] + "']:\n" + vr);
68 }
69 vr = v.doPass3b(i);
70 if (vr != VerificationResult.VR_OK) {
71 System.out.println(v.getClassName() + ", Pass 3b, method " + i + " ['"
72 + jc.getMethods()[i] + "']:\n" + vr);
73 }
74 }
75 } catch (ClassNotFoundException e) {
76 System.err.println("Could not find class " + v.getClassName() + " in Repository");
77 }
78 }
79 indent -= 1;
80 }
81
82
83 /***
84 * This method implements a demonstration program
85 * of how to use the VerifierFactoryObserver. It transitively verifies
86 * all class files encountered; this may take up a lot of time and,
87 * more notably, memory.
88 */
89 public static void main( String[] args ) {
90 if (args.length != 1) {
91 System.out.println("Need exactly one argument: The root class to verify.");
92 System.exit(1);
93 }
94 int dotclasspos = args[0].lastIndexOf(".class");
95 if (dotclasspos != -1) {
96 args[0] = args[0].substring(0, dotclasspos);
97 }
98 args[0] = args[0].replace('/', '.');
99 TransitiveHull th = new TransitiveHull();
100 VerifierFactory.attach(th);
101 VerifierFactory.getVerifier(args[0]);
102 VerifierFactory.detach(th);
103 }
104 }