001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.verifier;
020
021import org.apache.bcel.classfile.JavaClass;
022import org.apache.bcel.classfile.Utility;
023
024/**
025 * The NativeVerifier class implements a main(String[] args) method that's roughly compatible to the one in the Verifier
026 * class, but that uses the JVM's internal verifier for its class file verification. This can be used for comparison
027 * runs between the JVM-internal verifier and JustIce.
028 */
029public abstract class NativeVerifier {
030
031    /**
032     * Works only on the first argument.
033     *
034     * @param args command line arguments.
035     */
036    public static void main(final String[] args) {
037        if (args.length != 1) {
038            System.out.println("Verifier front-end: need exactly one argument.");
039            System.exit(1);
040        }
041        final int dotclasspos = args[0].lastIndexOf(JavaClass.EXTENSION);
042        if (dotclasspos != -1) {
043            args[0] = args[0].substring(0, dotclasspos);
044        }
045        args[0] = Utility.pathToPackage(args[0]);
046        // System.out.println(args[0]);
047        try {
048            Class.forName(args[0]);
049        } catch (final ExceptionInInitializerError eiie) { // subclass of LinkageError!
050            System.out.println("NativeVerifier: ExceptionInInitializerError encountered on '" + args[0] + "'.");
051            System.out.println(eiie);
052            System.exit(1);
053        } catch (final LinkageError le) {
054            System.out.println("NativeVerifier: LinkageError encountered on '" + args[0] + "'.");
055            System.out.println(le);
056            System.exit(1);
057        } catch (final ClassNotFoundException cnfe) {
058            System.out.println("NativeVerifier: FILE NOT FOUND: '" + args[0] + "'.");
059            System.exit(1);
060        } catch (final Throwable t) { // OK to catch Throwable here as we call exit.
061            System.out.println("NativeVerifier: Unspecified verification error on '" + args[0] + "'.");
062            System.exit(1);
063        }
064        System.out.println("NativeVerifier: Class file '" + args[0] + "' seems to be okay.");
065        System.exit(0);
066    }
067
068    /**
069     * This class must not be instantiated.
070     */
071    private NativeVerifier() {
072    }
073}