View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.bcel.verifier;
18  
19  import org.apache.bcel.Repository;
20  import org.apache.bcel.classfile.JavaClass;
21  import org.apache.bcel.classfile.Utility;
22  
23  /**
24   * This class has a main method implementing a demonstration program of how to use the VerifierFactoryObserver. It
25   * transitively verifies all class files encountered; this may take up a lot of time and, more notably, memory.
26   */
27  public class TransitiveHull implements VerifierFactoryObserver {
28  
29      /**
30       * This method implements a demonstration program of how to use the VerifierFactoryObserver. It transitively verifies
31       * all class files encountered; this may take up a lot of time and, more notably, memory.
32       */
33      public static void main(final String[] args) {
34          if (args.length != 1) {
35              System.out.println("Need exactly one argument: The root class to verify.");
36              System.exit(1);
37          }
38          final int dotclasspos = args[0].lastIndexOf(JavaClass.EXTENSION);
39          if (dotclasspos != -1) {
40              args[0] = args[0].substring(0, dotclasspos);
41          }
42          args[0] = Utility.pathToPackage(args[0]);
43          final TransitiveHull th = new TransitiveHull();
44          VerifierFactory.attach(th);
45          VerifierFactory.getVerifier(args[0]); // the observer is called back and does the actual trick.
46          VerifierFactory.detach(th);
47      }
48  
49      /** Used for indentation. */
50      private int indent;
51  
52      /** Not publicly instantiable. */
53      private TransitiveHull() {
54      }
55  
56      /* Implementing VerifierFactoryObserver. */
57      @Override
58      public void update(final String className) {
59          System.gc(); // avoid swapping if possible.
60          for (int i = 0; i < indent; i++) {
61              System.out.print(" ");
62          }
63          System.out.println(className);
64          indent += 1;
65          final Verifier v = VerifierFactory.getVerifier(className);
66          VerificationResult vr;
67          vr = v.doPass1();
68          if (vr != VerificationResult.VR_OK) {
69              System.out.println("Pass 1:\n" + vr);
70          }
71          vr = v.doPass2();
72          if (vr != VerificationResult.VR_OK) {
73              System.out.println("Pass 2:\n" + vr);
74          }
75          if (vr == VerificationResult.VR_OK) {
76              try {
77                  final JavaClass jc = Repository.lookupClass(v.getClassName());
78                  for (int i = 0; i < jc.getMethods().length; i++) {
79                      vr = v.doPass3a(i);
80                      if (vr != VerificationResult.VR_OK) {
81                          System.out.println(v.getClassName() + ", Pass 3a, method " + i + " ['" + jc.getMethods()[i] + "']:\n" + vr);
82                      }
83                      vr = v.doPass3b(i);
84                      if (vr != VerificationResult.VR_OK) {
85                          System.out.println(v.getClassName() + ", Pass 3b, method " + i + " ['" + jc.getMethods()[i] + "']:\n" + vr);
86                      }
87                  }
88              } catch (final ClassNotFoundException e) {
89                  System.err.println("Could not find class " + v.getClassName() + " in Repository");
90              }
91          }
92          indent -= 1;
93      }
94  }