/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) throws Exception {
        checkLoad("NullArrayFailInt2Object", true);
        checkLoad("NullArrayFailObject2Int", true);
        checkLoad("NullArraySuccessInt", false);
        checkLoad("NullArraySuccessInt2Float", false);
        checkLoad("NullArraySuccessShort", false);
        checkLoad("NullArraySuccessRef", false);
    }

    private static void checkLoad(String className, boolean expectError) throws Exception {
        Class<?> c;
        try {
            c = Class.forName(className);
            if (expectError) {
                throw new RuntimeException("Expected error for " + className);
            }
            Method m = c.getMethod("method");
            try {
                m.invoke(null);
                throw new RuntimeException("Expected an InvocationTargetException");
            } catch (InvocationTargetException e) {
                if (!(e.getCause() instanceof NullPointerException)) {
                    throw new RuntimeException("Expected a NullPointerException");
                }
                System.out.println(className);
            }
        } catch (VerifyError e) {
            if (!expectError) {
                throw new RuntimeException(e);
            }
            System.out.println(className);
        }
    }
}
