blob: 755d62ebb5f706984154607eae78a4b1ba7c3207 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
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
17import java.lang.reflect.Constructor;
18
jeffhao5d1ac922011-09-29 17:41:15 -070019/**
20 * Test instance creation.
21 */
22public class Main {
Elliott Hughes741b5b72012-01-31 19:18:51 -080023 private static boolean FULL_ACCESS_CHECKS = false; // b/5861201
24
jeffhao5d1ac922011-09-29 17:41:15 -070025 public static void main(String[] args) {
26 testClassNewInstance();
27 testConstructorNewInstance();
28 }
29
30 /**
31 * Tests Class.newInstance().
32 */
33 static void testClassNewInstance() {
34 // should succeed
35 try {
Andreas Gampe166aaee2016-07-18 08:27:23 -070036 Class<?> c = Class.forName("LocalClass");
jeffhao5d1ac922011-09-29 17:41:15 -070037 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 {
Andreas Gampe166aaee2016-07-18 08:27:23 -070046 Class<?> c = Class.forName("otherpackage.PackageAccess");
jeffhao5d1ac922011-09-29 17:41:15 -070047 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 */
71 static void testConstructorNewInstance() {
72 // should fail -- getConstructor only returns public constructors
73 try {
Andreas Gampe166aaee2016-07-18 08:27:23 -070074 Class<?> c = Class.forName("LocalClass");
75 Constructor<?> cons = c.getConstructor();
jeffhao5d1ac922011-09-29 17:41:15 -070076 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 {
Andreas Gampe166aaee2016-07-18 08:27:23 -070086 Class<?> c = Class.forName("LocalClass2");
87 Constructor<?> cons = c.getConstructor();
jeffhao5d1ac922011-09-29 17:41:15 -070088 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
Ian Rogers1441f6f2012-02-17 14:48:16 -080095 // should succeed
96 try {
Andreas Gampe166aaee2016-07-18 08:27:23 -070097 Class<?> c = Class.forName("Main$InnerClass");
98 Constructor<?> cons = c.getDeclaredConstructor(Main.class);
Ian Rogers1441f6f2012-02-17 14:48:16 -080099 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 {
Andreas Gampe166aaee2016-07-18 08:27:23 -0700108 Class<?> c = Class.forName("Main$StaticInnerClass");
109 Constructor<?> cons = c.getDeclaredConstructor();
Ian Rogers1441f6f2012-02-17 14:48:16 -0800110 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
jeffhao5d1ac922011-09-29 17:41:15 -0700117 // should fail
118 try {
Andreas Gampe166aaee2016-07-18 08:27:23 -0700119 Class<?> c = Class.forName("otherpackage.PackageAccess");
120 Constructor<?> cons = c.getConstructor();
jeffhao5d1ac922011-09-29 17:41:15 -0700121 System.err.println("ERROR: Cons PackageAccess succeeded unexpectedly");
122 } catch (NoSuchMethodException nsme) {
Elliott Hughes741b5b72012-01-31 19:18:51 -0800123 // constructor isn't public
jeffhao5d1ac922011-09-29 17:41:15 -0700124 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 {
Andreas Gampe166aaee2016-07-18 08:27:23 -0700132 Class<?> c = Class.forName("MaybeAbstract");
133 Constructor<?> cons = c.getConstructor();
jeffhao5d1ac922011-09-29 17:41:15 -0700134 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 }
Elliott Hughes741b5b72012-01-31 19:18:51 -0800143
144 // should fail
145 try {
Andreas Gampe166aaee2016-07-18 08:27:23 -0700146 Class<?> c = Class.forName("otherpackage.PackageAccess2");
147 Constructor<?> cons = c.getConstructor();
Elliott Hughes741b5b72012-01-31 19:18:51 -0800148 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
Przemyslaw Szczepaniakce025fa2015-12-09 09:57:36 +0000159 // should succeed
160 try {
161 otherpackage.ConstructorAccess.newConstructorInstance();
162 System.out.println("Cons ConstructorAccess succeeded");
163 } catch (Exception ex) {
164 System.err.println("Cons ConstructorAccess failed");
165 ex.printStackTrace();
166 }
jeffhao5d1ac922011-09-29 17:41:15 -0700167 }
Ian Rogers1441f6f2012-02-17 14:48:16 -0800168
169 class InnerClass {
170 }
171
172 static class StaticInnerClass {
173 }
jeffhao5d1ac922011-09-29 17:41:15 -0700174}
175
176class LocalClass {
177 // this class has a default constructor with package visibility
178}
179
180class LocalClass2 {
181 public LocalClass2() {}
182}
183
jeffhao5d1ac922011-09-29 17:41:15 -0700184class LocalClass3 {
185 public static void main() {
186 try {
187 CC.newInstance();
188 System.out.println("LocalClass3 succeeded");
189 } catch (Exception ex) {
190 System.err.println("Got unexpected LocalClass3 failure");
191 ex.printStackTrace();
192 }
193 }
194
195 static class CC {
196 private CC() {}
197
198 static Object newInstance() {
199 try {
Andreas Gampe166aaee2016-07-18 08:27:23 -0700200 Class<?> c = CC.class;
jeffhao5d1ac922011-09-29 17:41:15 -0700201 return c.newInstance();
202 } catch (Exception ex) {
203 ex.printStackTrace();
204 return null;
205 }
206 }
207 }
208}