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