    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    public static void main(final String[] args) {
    035        if (args.length != 1) {
    036            System.out.println("Verifier front-end: need exactly one argument.");
    037            System.exit(1);
    038        }
    039        final int dotclasspos = args[0].lastIndexOf(JavaClass.EXTENSION);
    040        if (dotclasspos != -1) {
    041            args[0] = args[0].substring(0, dotclasspos);
    042        }
    043        args[0] = Utility.pathToPackage(args[0]);
    044        // System.out.println(args[0]);
    045        try {
    046            Class.forName(args[0]);
    047        } catch (final ExceptionInInitializerError eiie) { // subclass of LinkageError!
    048            System.out.println("NativeVerifier: ExceptionInInitializerError encountered on '" + args[0] + "'.");
    049            System.out.println(eiie);
    050            System.exit(1);
    051        } catch (final LinkageError le) {
    052            System.out.println("NativeVerifier: LinkageError encountered on '" + args[0] + "'.");
    053            System.out.println(le);
    054            System.exit(1);
    055        } catch (final ClassNotFoundException cnfe) {
    056            System.out.println("NativeVerifier: FILE NOT FOUND: '" + args[0] + "'.");
    057            System.exit(1);
    058        } catch (final Throwable t) { // OK to catch Throwable here as we call exit.
    059            System.out.println("NativeVerifier: Unspecified verification error on '" + args[0] + "'.");
    060            System.exit(1);
    061        }
    062        System.out.println("NativeVerifier: Class file '" + args[0] + "' seems to be okay.");
    063        System.exit(0);
    064    }
    065
    066    /**
    067     * This class must not be instantiated.
    068     */
    069    private NativeVerifier() {
    070    }
    071}