1 /* 2 * Copyright (C) 2007 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.Constructor; 18 19 /** 20 * Test instance creation. 21 */ 22 public class Main { 23 private static boolean FULL_ACCESS_CHECKS = false; // b/5861201 24 main(String[] args)25 public static void main(String[] args) { 26 testClassNewInstance(); 27 testConstructorNewInstance(); 28 } 29 30 /** 31 * Tests Class.newInstance(). 32 */ testClassNewInstance()33 static void testClassNewInstance() { 34 // should succeed 35 try { 36 Class c = Class.forName("LocalClass"); 37 Object obj = c.newInstance(); 38 System.out.println("LocalClass succeeded"); 39 } catch (Exception ex) { 40 System.err.println("LocalClass failed"); 41 ex.printStackTrace(); 42 } 43 44 // should fail 45 try { 46 Class c = Class.forName("otherpackage.PackageAccess"); 47 Object obj = c.newInstance(); 48 System.err.println("ERROR: PackageAccess succeeded unexpectedly"); 49 } catch (IllegalAccessException iae) { 50 System.out.println("Got expected PackageAccess complaint"); 51 } catch (Exception ex) { 52 System.err.println("Got unexpected PackageAccess failure"); 53 ex.printStackTrace(); 54 } 55 56 LocalClass3.main(); 57 58 try { 59 MaybeAbstract ma = new MaybeAbstract(); 60 System.err.println("ERROR: MaybeAbstract succeeded unexpectedly"); 61 } catch (InstantiationError ie) { 62 System.out.println("Got expected InstantationError"); 63 } catch (Exception ex) { 64 System.err.println("Got unexpected MaybeAbstract failure"); 65 } 66 } 67 68 /** 69 * Tests Constructor.newInstance(). 70 */ testConstructorNewInstance()71 static void testConstructorNewInstance() { 72 // should fail -- getConstructor only returns public constructors 73 try { 74 Class c = Class.forName("LocalClass"); 75 Constructor cons = c.getConstructor(new Class[0] /*(Class[])null*/); 76 System.err.println("Cons LocalClass succeeded unexpectedly"); 77 } catch (NoSuchMethodException nsme) { 78 System.out.println("Cons LocalClass failed as expected"); 79 } catch (Exception ex) { 80 System.err.println("Cons LocalClass failed strangely"); 81 ex.printStackTrace(); 82 } 83 84 // should succeed 85 try { 86 Class c = Class.forName("LocalClass2"); 87 Constructor cons = c.getConstructor((Class[]) null); 88 Object obj = cons.newInstance(); 89 System.out.println("Cons LocalClass2 succeeded"); 90 } catch (Exception ex) { 91 System.err.println("Cons LocalClass2 failed"); 92 ex.printStackTrace(); 93 } 94 95 // should succeed 96 try { 97 Class c = Class.forName("Main$InnerClass"); 98 Constructor cons = c.getDeclaredConstructor(new Class<?>[]{Main.class}); 99 Object obj = cons.newInstance(new Main()); 100 System.out.println("Cons InnerClass succeeded"); 101 } catch (Exception ex) { 102 System.err.println("Cons InnerClass failed"); 103 ex.printStackTrace(); 104 } 105 106 // should succeed 107 try { 108 Class c = Class.forName("Main$StaticInnerClass"); 109 Constructor cons = c.getDeclaredConstructor((Class[]) null); 110 Object obj = cons.newInstance(); 111 System.out.println("Cons StaticInnerClass succeeded"); 112 } catch (Exception ex) { 113 System.err.println("Cons StaticInnerClass failed"); 114 ex.printStackTrace(); 115 } 116 117 // should fail 118 try { 119 Class c = Class.forName("otherpackage.PackageAccess"); 120 Constructor cons = c.getConstructor(new Class[0] /*(Class[])null*/); 121 System.err.println("ERROR: Cons PackageAccess succeeded unexpectedly"); 122 } catch (NoSuchMethodException nsme) { 123 // constructor isn't public 124 System.out.println("Cons got expected PackageAccess complaint"); 125 } catch (Exception ex) { 126 System.err.println("Cons got unexpected PackageAccess failure"); 127 ex.printStackTrace(); 128 } 129 130 // should fail 131 try { 132 Class c = Class.forName("MaybeAbstract"); 133 Constructor cons = c.getConstructor(new Class[0] /*(Class[])null*/); 134 Object obj = cons.newInstance(); 135 System.err.println("ERROR: Cons MaybeAbstract succeeded unexpectedly"); 136 } catch (InstantiationException ie) { 137 // note InstantiationException vs. InstantiationError 138 System.out.println("Cons got expected InstantationException"); 139 } catch (Exception ex) { 140 System.err.println("Cons got unexpected MaybeAbstract failure"); 141 ex.printStackTrace(); 142 } 143 144 // should fail 145 try { 146 Class c = Class.forName("otherpackage.PackageAccess2"); 147 Constructor cons = c.getConstructor((Class[]) null); 148 if (!FULL_ACCESS_CHECKS) { throw new IllegalAccessException(); } 149 Object obj = cons.newInstance(); 150 System.err.println("ERROR: Cons PackageAccess2 succeeded unexpectedly"); 151 } catch (IllegalAccessException iae) { 152 // constructor is public, but class has package scope 153 System.out.println("Cons got expected PackageAccess2 complaint"); 154 } catch (Exception ex) { 155 System.err.println("Cons got unexpected PackageAccess2 failure"); 156 ex.printStackTrace(); 157 } 158 159 } 160 161 class InnerClass { 162 } 163 164 static class StaticInnerClass { 165 } 166 } 167 168 class LocalClass { 169 // this class has a default constructor with package visibility 170 } 171 172 class LocalClass2 { LocalClass2()173 public LocalClass2() {} 174 } 175 176 177 class LocalClass3 { main()178 public static void main() { 179 try { 180 CC.newInstance(); 181 System.out.println("LocalClass3 succeeded"); 182 } catch (Exception ex) { 183 System.err.println("Got unexpected LocalClass3 failure"); 184 ex.printStackTrace(); 185 } 186 } 187 188 static class CC { CC()189 private CC() {} 190 newInstance()191 static Object newInstance() { 192 try { 193 Class c = CC.class; 194 return c.newInstance(); 195 } catch (Exception ex) { 196 ex.printStackTrace(); 197 return null; 198 } 199 } 200 } 201 } 202