1 /* 2 * Copyright (C) 2018 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 19 public class Main { main(String[] args)20 public static void main(String[] args) throws Throwable { 21 // Class BadField is defined in BadField.smali. 22 Class<?> c = Class.forName("BadField"); 23 24 // Storing null is OK. 25 c.getMethod("storeStaticNull").invoke(null); 26 c.getMethod("storeInstanceNull").invoke(null); 27 28 // Storing anything else should throw an exception. 29 testStoreObject(c, "storeStaticObject"); 30 testStoreObject(c, "storeInstanceObject"); 31 32 // Loading is OK. 33 c = Class.forName("BadFieldGet"); 34 testLoadObject(c, "loadStatic"); 35 } 36 testLoadObject(Class<?> c, String methodName)37 public static void testLoadObject(Class<?> c, String methodName) throws Throwable { 38 c.getMethod(methodName).invoke(null); 39 } 40 testStoreObject(Class<?> c, String methodName)41 public static void testStoreObject(Class<?> c, String methodName) throws Throwable { 42 try { 43 c.getMethod(methodName).invoke(null); 44 throw new Error("Expected NoClassDefFoundError"); 45 } catch (InvocationTargetException expected) { 46 Throwable e = expected.getCause(); 47 if (e instanceof NoClassDefFoundError) { 48 // The NoClassDefFoundError is for the field widget in class BadField. 49 if (!e.getMessage().equals("Failed resolution of: LWidget;")) { 50 throw new Error("Unexpected " + e); 51 } 52 } else { 53 throw new Error("Unexpected " + e); 54 } 55 } 56 } 57 privateMethod()58 private static void privateMethod() { 59 } 60 } 61