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.classfile.JavaClass;
20  import org.apache.bcel.classfile.Utility;
21  
22  /**
23   * The NativeVerifier class implements a main(String[] args) method that's roughly compatible to the one in the Verifier
24   * class, but that uses the JVM's internal verifier for its class file verification. This can be used for comparison
25   * runs between the JVM-internal verifier and JustIce.
26   */
27  public abstract class NativeVerifier {
28  
29      /**
30       * Works only on the first argument.
31       */
32      public static void main(final String[] args) {
33          if (args.length != 1) {
34              System.out.println("Verifier front-end: need exactly one argument.");
35              System.exit(1);
36          }
37          final int dotclasspos = args[0].lastIndexOf(JavaClass.EXTENSION);
38          if (dotclasspos != -1) {
39              args[0] = args[0].substring(0, dotclasspos);
40          }
41          args[0] = Utility.pathToPackage(args[0]);
42          // System.out.println(args[0]);
43          try {
44              Class.forName(args[0]);
45          } catch (final ExceptionInInitializerError eiie) { // subclass of LinkageError!
46              System.out.println("NativeVerifier: ExceptionInInitializerError encountered on '" + args[0] + "'.");
47              System.out.println(eiie);
48              System.exit(1);
49          } catch (final LinkageError le) {
50              System.out.println("NativeVerifier: LinkageError encountered on '" + args[0] + "'.");
51              System.out.println(le);
52              System.exit(1);
53          } catch (final ClassNotFoundException cnfe) {
54              System.out.println("NativeVerifier: FILE NOT FOUND: '" + args[0] + "'.");
55              System.exit(1);
56          } catch (final Throwable t) { // OK to catch Throwable here as we call exit.
57              System.out.println("NativeVerifier: Unspecified verification error on '" + args[0] + "'.");
58              System.exit(1);
59          }
60          System.out.println("NativeVerifier: Class file '" + args[0] + "' seems to be okay.");
61          System.exit(0);
62      }
63  
64      /**
65       * This class must not be instantiated.
66       */
67      private NativeVerifier() {
68      }
69  }