NativeVerifier.java

  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. import org.apache.bcel.classfile.JavaClass;
  19. import org.apache.bcel.classfile.Utility;

  20. /**
  21.  * The NativeVerifier class implements a main(String[] args) method that's roughly compatible to the one in the Verifier
  22.  * class, but that uses the JVM's internal verifier for its class file verification. This can be used for comparison
  23.  * runs between the JVM-internal verifier and JustIce.
  24.  */
  25. public abstract class NativeVerifier {

  26.     /**
  27.      * Works only on the first argument.
  28.      */
  29.     public static void main(final String[] args) {
  30.         if (args.length != 1) {
  31.             System.out.println("Verifier front-end: need exactly one argument.");
  32.             System.exit(1);
  33.         }
  34.         final int dotclasspos = args[0].lastIndexOf(JavaClass.EXTENSION);
  35.         if (dotclasspos != -1) {
  36.             args[0] = args[0].substring(0, dotclasspos);
  37.         }
  38.         args[0] = Utility.pathToPackage(args[0]);
  39.         // System.out.println(args[0]);
  40.         try {
  41.             Class.forName(args[0]);
  42.         } catch (final ExceptionInInitializerError eiie) { // subclass of LinkageError!
  43.             System.out.println("NativeVerifier: ExceptionInInitializerError encountered on '" + args[0] + "'.");
  44.             System.out.println(eiie);
  45.             System.exit(1);
  46.         } catch (final LinkageError le) {
  47.             System.out.println("NativeVerifier: LinkageError encountered on '" + args[0] + "'.");
  48.             System.out.println(le);
  49.             System.exit(1);
  50.         } catch (final ClassNotFoundException cnfe) {
  51.             System.out.println("NativeVerifier: FILE NOT FOUND: '" + args[0] + "'.");
  52.             System.exit(1);
  53.         } catch (final Throwable t) { // OK to catch Throwable here as we call exit.
  54.             System.out.println("NativeVerifier: Unspecified verification error on '" + args[0] + "'.");
  55.             System.exit(1);
  56.         }
  57.         System.out.println("NativeVerifier: Class file '" + args[0] + "' seems to be okay.");
  58.         System.exit(0);
  59.     }

  60.     /**
  61.      * This class must not be instantiated.
  62.      */
  63.     private NativeVerifier() {
  64.     }
  65. }