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