TransitiveHull xref

View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    *  you may not use this file except in compliance with the License.
    6    *  You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   *  Unless required by applicable law or agreed to in writing, software
    11   *  distributed under the License is distributed on an "AS IS" BASIS,
    12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  See the License for the specific language governing permissions and
    14   *  limitations under the License.
    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      /* Implementing VerifierFactoryObserver. */
    43      public void update( String classname ) {
    44          System.gc(); // avoid swapping if possible.
    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]); // the observer is called back and does the actual trick.
    102         VerifierFactory.detach(th);
    103     }
    104 }