blob: e43c5a5680648fc267a01e4649e69922c482c054 [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 {
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 */
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 fail
96 try {
97 Class c = Class.forName("otherpackage.PackageAccess");
98 Constructor cons = c.getConstructor(new Class[0] /*(Class[])null*/);
99 System.err.println("ERROR: Cons PackageAccess succeeded unexpectedly");
100 } catch (NoSuchMethodException nsme) {
Elliott Hughes741b5b72012-01-31 19:18:51 -0800101 // constructor isn't public
jeffhao5d1ac922011-09-29 17:41:15 -0700102 System.out.println("Cons got expected PackageAccess complaint");
103 } catch (Exception ex) {
104 System.err.println("Cons got unexpected PackageAccess failure");
105 ex.printStackTrace();
106 }
107
108 // should fail
109 try {
110 Class c = Class.forName("MaybeAbstract");
111 Constructor cons = c.getConstructor(new Class[0] /*(Class[])null*/);
112 Object obj = cons.newInstance();
113 System.err.println("ERROR: Cons MaybeAbstract succeeded unexpectedly");
114 } catch (InstantiationException ie) {
115 // note InstantiationException vs. InstantiationError
116 System.out.println("Cons got expected InstantationException");
117 } catch (Exception ex) {
118 System.err.println("Cons got unexpected MaybeAbstract failure");
119 ex.printStackTrace();
120 }
Elliott Hughes741b5b72012-01-31 19:18:51 -0800121
122 // should fail
123 try {
124 Class c = Class.forName("otherpackage.PackageAccess2");
125 Constructor cons = c.getConstructor((Class[]) null);
126 if (!FULL_ACCESS_CHECKS) { throw new IllegalAccessException(); }
127 Object obj = cons.newInstance();
128 System.err.println("ERROR: Cons PackageAccess2 succeeded unexpectedly");
129 } catch (IllegalAccessException iae) {
130 // constructor is public, but class has package scope
131 System.out.println("Cons got expected PackageAccess2 complaint");
132 } catch (Exception ex) {
133 System.err.println("Cons got unexpected PackageAccess2 failure");
134 ex.printStackTrace();
135 }
136
jeffhao5d1ac922011-09-29 17:41:15 -0700137 }
138}
139
140class LocalClass {
141 // this class has a default constructor with package visibility
142}
143
144class LocalClass2 {
145 public LocalClass2() {}
146}
147
148
149class LocalClass3 {
150 public static void main() {
151 try {
152 CC.newInstance();
153 System.out.println("LocalClass3 succeeded");
154 } catch (Exception ex) {
155 System.err.println("Got unexpected LocalClass3 failure");
156 ex.printStackTrace();
157 }
158 }
159
160 static class CC {
161 private CC() {}
162
163 static Object newInstance() {
164 try {
165 Class c = CC.class;
166 return c.newInstance();
167 } catch (Exception ex) {
168 ex.printStackTrace();
169 return null;
170 }
171 }
172 }
173}