1 /* 2 * Copyright (C) 2017 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) { 22 System.loadLibrary(args[0]); 23 24 testGetFieldId(TestClass.class, "intField", ""); 25 testGetFieldId(TestClass.class, "intField", "I"); 26 testGetFieldId(TestClass.class, "intField", "int"); 27 testGetFieldId(TestClass.class, "intField", "Lint;"); 28 testGetFieldId(TestClass.class, "stringField", "I"); 29 testGetFieldId(TestClass.class, "stringField", "Ljava/lang/String;"); 30 testGetFieldId(TestClass.class, "stringField", "java/lang/String"); 31 testGetFieldId(TestClass.class, "stringField", "Ljava.lang.String;"); 32 testGetFieldId(TestClass.class, "stringField", "java.lang.String"); 33 34 try { 35 Method get = Main.class.getDeclaredMethod("getFieldId", 36 Class.class, 37 String.class, 38 String.class); 39 MyClassLoader loader = new MyClassLoader(Main.class.getClassLoader()); 40 Class<?> otherMain = Class.forName("Main", true, loader); 41 Method m = otherMain.getDeclaredMethod("testClassLoading", Method.class); 42 m.invoke(null, get); 43 } catch (Throwable t) { 44 t.printStackTrace(System.out); 45 } 46 } 47 testClassLoading(Method get)48 public static void testClassLoading(Method get) throws Exception { 49 System.out.println("Test that MyClassLoader.loadClass(\"Bad.Class\") shall not be called."); 50 String[] bad_class_names = { "Bad/Class", "Bad.Class", "LBad.Class;" }; 51 for (String signature : bad_class_names) { 52 try { 53 get.invoke(null, TestClass.class, "bogus", signature); 54 System.out.println("FAIL!"); 55 } catch (InvocationTargetException ite) { 56 if (!(ite.getCause() instanceof NoSuchFieldError) || 57 !(ite.getCause().getCause() instanceof NoClassDefFoundError)) { 58 throw ite; 59 } 60 NoClassDefFoundError ncdfe = (NoClassDefFoundError) ite.getCause().getCause(); 61 System.out.println(" Error message for " + signature + ": " + ncdfe.getMessage()); 62 } 63 } 64 } 65 testGetFieldId(Class<?> cls, String name, String signature)66 public static void testGetFieldId(Class<?> cls, String name, String signature) { 67 System.out.println("getFieldId(" + cls + ", \"" + name + "\", \"" + signature + "\")"); 68 try { 69 boolean result = getFieldId(cls, name, signature); 70 System.out.println("Result: " + result); 71 } catch (Throwable t) { 72 System.out.println("Caught " + DescribeThrowable(t)); 73 for (Throwable cause = t.getCause(); cause != null; cause = cause.getCause()) { 74 System.out.println(" caused by " + DescribeThrowable(cause)); 75 } 76 } 77 } 78 DescribeThrowable(Throwable t)79 public static String DescribeThrowable(Throwable t) { 80 return PRINT_MESSAGE ? t.getClass().getName() + ": " + t.getMessage() 81 : t.getClass().getName(); 82 } 83 getFieldId(Class<?> cls, String name, String signature)84 public static native boolean getFieldId(Class<?> cls, String name, String signature); 85 86 // Set to true to see actual messages. 87 public static final boolean PRINT_MESSAGE = false; 88 } 89 90 class TestClass { 91 public int intField; 92 public String stringField; 93 } 94 95 class MyClassLoader extends DefiningLoader { MyClassLoader(ClassLoader parent)96 public MyClassLoader(ClassLoader parent) { 97 super(parent); 98 } 99 loadClass(String name)100 public Class<?> loadClass(String name) throws ClassNotFoundException 101 { 102 if (name.equals("Bad.Class")) { 103 throw new Error("findClass(\"Bad.Class\")"); 104 } 105 return super.loadClass(name); 106 } 107 } 108