xref: /aosp_15_r20/art/test/518-null-array-get/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.lang.reflect.InvocationTargetException;
18 import java.lang.reflect.Method;
19 
20 public class Main {
main(String[] args)21     public static void main(String[] args) throws Exception {
22         checkLoad("NullArrayFailInt2Object", true);
23         checkLoad("NullArrayFailObject2Int", true);
24         checkLoad("NullArraySuccessInt", false);
25         checkLoad("NullArraySuccessInt2Float", false);
26         checkLoad("NullArraySuccessShort", false);
27         checkLoad("NullArraySuccessRef", false);
28     }
29 
checkLoad(String className, boolean expectError)30     private static void checkLoad(String className, boolean expectError) throws Exception {
31         Class<?> c;
32         try {
33             c = Class.forName(className);
34             if (expectError) {
35                 throw new RuntimeException("Expected error for " + className);
36             }
37             Method m = c.getMethod("method");
38             try {
39                 m.invoke(null);
40                 throw new RuntimeException("Expected an InvocationTargetException");
41             } catch (InvocationTargetException e) {
42                 if (!(e.getCause() instanceof NullPointerException)) {
43                     throw new RuntimeException("Expected a NullPointerException");
44                 }
45                 System.out.println(className);
46             }
47         } catch (VerifyError e) {
48             if (!expectError) {
49                 throw new RuntimeException(e);
50             }
51             System.out.println(className);
52         }
53     }
54 }
55