blob: 8faef1349f3654d44a370c137aab5504f8acbba7 [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
19import java.lang.reflect.Constructor;
20
21/**
22 * Test instance creation.
23 */
24public class Main {
25 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) {
101 System.out.println("Cons got expected PackageAccess complaint");
102 } catch (Exception ex) {
103 System.err.println("Cons got unexpected PackageAccess failure");
104 ex.printStackTrace();
105 }
106
107 // should fail
108 try {
109 Class c = Class.forName("MaybeAbstract");
110 Constructor cons = c.getConstructor(new Class[0] /*(Class[])null*/);
111 Object obj = cons.newInstance();
112 System.err.println("ERROR: Cons MaybeAbstract succeeded unexpectedly");
113 } catch (InstantiationException ie) {
114 // note InstantiationException vs. InstantiationError
115 System.out.println("Cons got expected InstantationException");
116 } catch (Exception ex) {
117 System.err.println("Cons got unexpected MaybeAbstract failure");
118 ex.printStackTrace();
119 }
120 }
121}
122
123class LocalClass {
124 // this class has a default constructor with package visibility
125}
126
127class LocalClass2 {
128 public LocalClass2() {}
129}
130
131
132class LocalClass3 {
133 public static void main() {
134 try {
135 CC.newInstance();
136 System.out.println("LocalClass3 succeeded");
137 } catch (Exception ex) {
138 System.err.println("Got unexpected LocalClass3 failure");
139 ex.printStackTrace();
140 }
141 }
142
143 static class CC {
144 private CC() {}
145
146 static Object newInstance() {
147 try {
148 Class c = CC.class;
149 return c.newInstance();
150 } catch (Exception ex) {
151 ex.printStackTrace();
152 return null;
153 }
154 }
155 }
156}