1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2007 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Constructor; 18*795d594fSAndroid Build Coastguard Worker 19*795d594fSAndroid Build Coastguard Worker /** 20*795d594fSAndroid Build Coastguard Worker * Test instance creation. 21*795d594fSAndroid Build Coastguard Worker */ 22*795d594fSAndroid Build Coastguard Worker public class Main { 23*795d594fSAndroid Build Coastguard Worker private static boolean FULL_ACCESS_CHECKS = false; // b/5861201 24*795d594fSAndroid Build Coastguard Worker main(String[] args)25*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) { 26*795d594fSAndroid Build Coastguard Worker testClassNewInstance(); 27*795d594fSAndroid Build Coastguard Worker testConstructorNewInstance(); 28*795d594fSAndroid Build Coastguard Worker } 29*795d594fSAndroid Build Coastguard Worker 30*795d594fSAndroid Build Coastguard Worker /** 31*795d594fSAndroid Build Coastguard Worker * Tests Class.newInstance(). 32*795d594fSAndroid Build Coastguard Worker */ testClassNewInstance()33*795d594fSAndroid Build Coastguard Worker static void testClassNewInstance() { 34*795d594fSAndroid Build Coastguard Worker // should succeed 35*795d594fSAndroid Build Coastguard Worker try { 36*795d594fSAndroid Build Coastguard Worker Class<?> c = Class.forName("LocalClass"); 37*795d594fSAndroid Build Coastguard Worker Object obj = c.newInstance(); 38*795d594fSAndroid Build Coastguard Worker System.out.println("LocalClass succeeded"); 39*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 40*795d594fSAndroid Build Coastguard Worker System.out.println("LocalClass failed"); 41*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 42*795d594fSAndroid Build Coastguard Worker } 43*795d594fSAndroid Build Coastguard Worker 44*795d594fSAndroid Build Coastguard Worker // should fail 45*795d594fSAndroid Build Coastguard Worker try { 46*795d594fSAndroid Build Coastguard Worker Class<?> c = Class.forName("otherpackage.PackageAccess"); 47*795d594fSAndroid Build Coastguard Worker Object obj = c.newInstance(); 48*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: PackageAccess succeeded unexpectedly"); 49*795d594fSAndroid Build Coastguard Worker } catch (IllegalAccessException iae) { 50*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected PackageAccess complaint"); 51*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 52*795d594fSAndroid Build Coastguard Worker System.out.println("Got unexpected PackageAccess failure"); 53*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 54*795d594fSAndroid Build Coastguard Worker } 55*795d594fSAndroid Build Coastguard Worker 56*795d594fSAndroid Build Coastguard Worker LocalClass3.main(); 57*795d594fSAndroid Build Coastguard Worker 58*795d594fSAndroid Build Coastguard Worker try { 59*795d594fSAndroid Build Coastguard Worker MaybeAbstract ma = new MaybeAbstract(); 60*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: MaybeAbstract succeeded unexpectedly"); 61*795d594fSAndroid Build Coastguard Worker } catch (InstantiationError ie) { 62*795d594fSAndroid Build Coastguard Worker System.out.println("Got expected InstantationError"); 63*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 64*795d594fSAndroid Build Coastguard Worker System.out.println("Got unexpected MaybeAbstract failure"); 65*795d594fSAndroid Build Coastguard Worker } 66*795d594fSAndroid Build Coastguard Worker } 67*795d594fSAndroid Build Coastguard Worker 68*795d594fSAndroid Build Coastguard Worker /** 69*795d594fSAndroid Build Coastguard Worker * Tests Constructor.newInstance(). 70*795d594fSAndroid Build Coastguard Worker */ testConstructorNewInstance()71*795d594fSAndroid Build Coastguard Worker static void testConstructorNewInstance() { 72*795d594fSAndroid Build Coastguard Worker // should fail -- getConstructor only returns public constructors 73*795d594fSAndroid Build Coastguard Worker try { 74*795d594fSAndroid Build Coastguard Worker Class<?> c = Class.forName("LocalClass"); 75*795d594fSAndroid Build Coastguard Worker Constructor<?> cons = c.getConstructor(); 76*795d594fSAndroid Build Coastguard Worker System.out.println("Cons LocalClass succeeded unexpectedly"); 77*795d594fSAndroid Build Coastguard Worker } catch (NoSuchMethodException nsme) { 78*795d594fSAndroid Build Coastguard Worker System.out.println("Cons LocalClass failed as expected"); 79*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 80*795d594fSAndroid Build Coastguard Worker System.out.println("Cons LocalClass failed strangely"); 81*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 82*795d594fSAndroid Build Coastguard Worker } 83*795d594fSAndroid Build Coastguard Worker 84*795d594fSAndroid Build Coastguard Worker // should succeed 85*795d594fSAndroid Build Coastguard Worker try { 86*795d594fSAndroid Build Coastguard Worker Class<?> c = Class.forName("LocalClass2"); 87*795d594fSAndroid Build Coastguard Worker Constructor<?> cons = c.getConstructor(); 88*795d594fSAndroid Build Coastguard Worker Object obj = cons.newInstance(); 89*795d594fSAndroid Build Coastguard Worker System.out.println("Cons LocalClass2 succeeded"); 90*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 91*795d594fSAndroid Build Coastguard Worker System.out.println("Cons LocalClass2 failed"); 92*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 93*795d594fSAndroid Build Coastguard Worker } 94*795d594fSAndroid Build Coastguard Worker 95*795d594fSAndroid Build Coastguard Worker // should succeed 96*795d594fSAndroid Build Coastguard Worker try { 97*795d594fSAndroid Build Coastguard Worker Class<?> c = Class.forName("Main$InnerClass"); 98*795d594fSAndroid Build Coastguard Worker Constructor<?> cons = c.getDeclaredConstructor(Main.class); 99*795d594fSAndroid Build Coastguard Worker Object obj = cons.newInstance(new Main()); 100*795d594fSAndroid Build Coastguard Worker System.out.println("Cons InnerClass succeeded"); 101*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 102*795d594fSAndroid Build Coastguard Worker System.out.println("Cons InnerClass failed"); 103*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 104*795d594fSAndroid Build Coastguard Worker } 105*795d594fSAndroid Build Coastguard Worker 106*795d594fSAndroid Build Coastguard Worker // should succeed 107*795d594fSAndroid Build Coastguard Worker try { 108*795d594fSAndroid Build Coastguard Worker Class<?> c = Class.forName("Main$StaticInnerClass"); 109*795d594fSAndroid Build Coastguard Worker Constructor<?> cons = c.getDeclaredConstructor(); 110*795d594fSAndroid Build Coastguard Worker Object obj = cons.newInstance(); 111*795d594fSAndroid Build Coastguard Worker System.out.println("Cons StaticInnerClass succeeded"); 112*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 113*795d594fSAndroid Build Coastguard Worker System.out.println("Cons StaticInnerClass failed"); 114*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 115*795d594fSAndroid Build Coastguard Worker } 116*795d594fSAndroid Build Coastguard Worker 117*795d594fSAndroid Build Coastguard Worker // should fail 118*795d594fSAndroid Build Coastguard Worker try { 119*795d594fSAndroid Build Coastguard Worker Class<?> c = Class.forName("otherpackage.PackageAccess"); 120*795d594fSAndroid Build Coastguard Worker Constructor<?> cons = c.getConstructor(); 121*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: Cons PackageAccess succeeded unexpectedly"); 122*795d594fSAndroid Build Coastguard Worker } catch (NoSuchMethodException nsme) { 123*795d594fSAndroid Build Coastguard Worker // constructor isn't public 124*795d594fSAndroid Build Coastguard Worker System.out.println("Cons got expected PackageAccess complaint"); 125*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 126*795d594fSAndroid Build Coastguard Worker System.out.println("Cons got unexpected PackageAccess failure"); 127*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 128*795d594fSAndroid Build Coastguard Worker } 129*795d594fSAndroid Build Coastguard Worker 130*795d594fSAndroid Build Coastguard Worker // should fail 131*795d594fSAndroid Build Coastguard Worker try { 132*795d594fSAndroid Build Coastguard Worker Class<?> c = Class.forName("MaybeAbstract"); 133*795d594fSAndroid Build Coastguard Worker Constructor<?> cons = c.getConstructor(); 134*795d594fSAndroid Build Coastguard Worker Object obj = cons.newInstance(); 135*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: Cons MaybeAbstract succeeded unexpectedly"); 136*795d594fSAndroid Build Coastguard Worker } catch (InstantiationException ie) { 137*795d594fSAndroid Build Coastguard Worker // note InstantiationException vs. InstantiationError 138*795d594fSAndroid Build Coastguard Worker System.out.println("Cons got expected InstantationException"); 139*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 140*795d594fSAndroid Build Coastguard Worker System.out.println("Cons got unexpected MaybeAbstract failure"); 141*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 142*795d594fSAndroid Build Coastguard Worker } 143*795d594fSAndroid Build Coastguard Worker 144*795d594fSAndroid Build Coastguard Worker // should fail 145*795d594fSAndroid Build Coastguard Worker try { 146*795d594fSAndroid Build Coastguard Worker Class<?> c = Class.forName("otherpackage.PackageAccess2"); 147*795d594fSAndroid Build Coastguard Worker Constructor<?> cons = c.getConstructor(); 148*795d594fSAndroid Build Coastguard Worker if (!FULL_ACCESS_CHECKS) { throw new IllegalAccessException(); } 149*795d594fSAndroid Build Coastguard Worker Object obj = cons.newInstance(); 150*795d594fSAndroid Build Coastguard Worker System.out.println("ERROR: Cons PackageAccess2 succeeded unexpectedly"); 151*795d594fSAndroid Build Coastguard Worker } catch (IllegalAccessException iae) { 152*795d594fSAndroid Build Coastguard Worker // constructor is public, but class has package scope 153*795d594fSAndroid Build Coastguard Worker System.out.println("Cons got expected PackageAccess2 complaint"); 154*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 155*795d594fSAndroid Build Coastguard Worker System.out.println("Cons got unexpected PackageAccess2 failure"); 156*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 157*795d594fSAndroid Build Coastguard Worker } 158*795d594fSAndroid Build Coastguard Worker 159*795d594fSAndroid Build Coastguard Worker // should succeed 160*795d594fSAndroid Build Coastguard Worker try { 161*795d594fSAndroid Build Coastguard Worker otherpackage.ConstructorAccess.newConstructorInstance(); 162*795d594fSAndroid Build Coastguard Worker System.out.println("Cons ConstructorAccess succeeded"); 163*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 164*795d594fSAndroid Build Coastguard Worker System.out.println("Cons ConstructorAccess failed"); 165*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 166*795d594fSAndroid Build Coastguard Worker } 167*795d594fSAndroid Build Coastguard Worker } 168*795d594fSAndroid Build Coastguard Worker 169*795d594fSAndroid Build Coastguard Worker class InnerClass { 170*795d594fSAndroid Build Coastguard Worker } 171*795d594fSAndroid Build Coastguard Worker 172*795d594fSAndroid Build Coastguard Worker static class StaticInnerClass { 173*795d594fSAndroid Build Coastguard Worker } 174*795d594fSAndroid Build Coastguard Worker } 175*795d594fSAndroid Build Coastguard Worker 176*795d594fSAndroid Build Coastguard Worker class LocalClass { 177*795d594fSAndroid Build Coastguard Worker // this class has a default constructor with package visibility 178*795d594fSAndroid Build Coastguard Worker } 179*795d594fSAndroid Build Coastguard Worker 180*795d594fSAndroid Build Coastguard Worker class LocalClass2 { LocalClass2()181*795d594fSAndroid Build Coastguard Worker public LocalClass2() {} 182*795d594fSAndroid Build Coastguard Worker } 183*795d594fSAndroid Build Coastguard Worker 184*795d594fSAndroid Build Coastguard Worker class LocalClass3 { main()185*795d594fSAndroid Build Coastguard Worker public static void main() { 186*795d594fSAndroid Build Coastguard Worker try { 187*795d594fSAndroid Build Coastguard Worker CC.newInstance(); 188*795d594fSAndroid Build Coastguard Worker System.out.println("LocalClass3 succeeded"); 189*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 190*795d594fSAndroid Build Coastguard Worker System.out.println("Got unexpected LocalClass3 failure"); 191*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 192*795d594fSAndroid Build Coastguard Worker } 193*795d594fSAndroid Build Coastguard Worker } 194*795d594fSAndroid Build Coastguard Worker 195*795d594fSAndroid Build Coastguard Worker static class CC { CC()196*795d594fSAndroid Build Coastguard Worker private CC() {} 197*795d594fSAndroid Build Coastguard Worker newInstance()198*795d594fSAndroid Build Coastguard Worker static Object newInstance() { 199*795d594fSAndroid Build Coastguard Worker try { 200*795d594fSAndroid Build Coastguard Worker Class<?> c = CC.class; 201*795d594fSAndroid Build Coastguard Worker return c.newInstance(); 202*795d594fSAndroid Build Coastguard Worker } catch (Exception ex) { 203*795d594fSAndroid Build Coastguard Worker ex.printStackTrace(System.out); 204*795d594fSAndroid Build Coastguard Worker return null; 205*795d594fSAndroid Build Coastguard Worker } 206*795d594fSAndroid Build Coastguard Worker } 207*795d594fSAndroid Build Coastguard Worker } 208*795d594fSAndroid Build Coastguard Worker } 209