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 */
017package org.apache.bcel.verifier;
018
019import org.apache.bcel.classfile.Utility;
020
021/**
022 * The NativeVerifier class implements a main(String[] args) method that's roughly compatible to the one in the Verifier
023 * class, but that uses the JVM's internal verifier for its class file verification. This can be used for comparison
024 * runs between the JVM-internal verifier and JustIce.
025 */
026public abstract class NativeVerifier {
027
028    /**
029     * Works only on the first argument.
030     */
031    public static void main(final String[] args) {
032        if (args.length != 1) {
033            System.out.println("Verifier front-end: need exactly one argument.");
034            System.exit(1);
035        }
036        final int dotclasspos = args[0].lastIndexOf(".class");
037        if (dotclasspos != -1) {
038            args[0] = args[0].substring(0, dotclasspos);
039        }
040        args[0] = Utility.pathToPackage(args[0]);
041        // System.out.println(args[0]);
042        try {
043            Class.forName(args[0]);
044        } catch (final ExceptionInInitializerError eiie) { // subclass of LinkageError!
045            System.out.println("NativeVerifier: ExceptionInInitializerError encountered on '" + args[0] + "'.");
046            System.out.println(eiie);
047            System.exit(1);
048        } catch (final LinkageError le) {
049            System.out.println("NativeVerifier: LinkageError encountered on '" + args[0] + "'.");
050            System.out.println(le);
051            System.exit(1);
052        } catch (final ClassNotFoundException cnfe) {
053            System.out.println("NativeVerifier: FILE NOT FOUND: '" + args[0] + "'.");
054            System.exit(1);
055        } catch (final Throwable t) { // OK to catch Throwable here as we call exit.
056            System.out.println("NativeVerifier: Unspecified verification error on '" + args[0] + "'.");
057            System.exit(1);
058        }
059        System.out.println("NativeVerifier: Class file '" + args[0] + "' seems to be okay.");
060        System.exit(0);
061    }
062
063    /**
064     * This class must not be instantiated.
065     */
066    private NativeVerifier() {
067    }
068}