jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 19 | /** |
| 20 | * Test instance creation. |
| 21 | */ |
| 22 | public class Main { |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 23 | private static boolean FULL_ACCESS_CHECKS = false; // b/5861201 |
| 24 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 36 | Class<?> c = Class.forName("LocalClass"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 46 | Class<?> c = Class.forName("otherpackage.PackageAccess"); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 74 | Class<?> c = Class.forName("LocalClass"); |
| 75 | Constructor<?> cons = c.getConstructor(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 86 | Class<?> c = Class.forName("LocalClass2"); |
| 87 | Constructor<?> cons = c.getConstructor(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 | |
Ian Rogers | 1441f6f | 2012-02-17 14:48:16 -0800 | [diff] [blame] | 95 | // should succeed |
| 96 | try { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 97 | Class<?> c = Class.forName("Main$InnerClass"); |
| 98 | Constructor<?> cons = c.getDeclaredConstructor(Main.class); |
Ian Rogers | 1441f6f | 2012-02-17 14:48:16 -0800 | [diff] [blame] | 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 { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 108 | Class<?> c = Class.forName("Main$StaticInnerClass"); |
| 109 | Constructor<?> cons = c.getDeclaredConstructor(); |
Ian Rogers | 1441f6f | 2012-02-17 14:48:16 -0800 | [diff] [blame] | 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 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 117 | // should fail |
| 118 | try { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 119 | Class<?> c = Class.forName("otherpackage.PackageAccess"); |
| 120 | Constructor<?> cons = c.getConstructor(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 121 | System.err.println("ERROR: Cons PackageAccess succeeded unexpectedly"); |
| 122 | } catch (NoSuchMethodException nsme) { |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 123 | // constructor isn't public |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 132 | Class<?> c = Class.forName("MaybeAbstract"); |
| 133 | Constructor<?> cons = c.getConstructor(); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 | } |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 143 | |
| 144 | // should fail |
| 145 | try { |
Andreas Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 146 | Class<?> c = Class.forName("otherpackage.PackageAccess2"); |
| 147 | Constructor<?> cons = c.getConstructor(); |
Elliott Hughes | 741b5b7 | 2012-01-31 19:18:51 -0800 | [diff] [blame] | 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 | |
Przemyslaw Szczepaniak | ce025fa | 2015-12-09 09:57:36 +0000 | [diff] [blame] | 159 | // 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 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 167 | } |
Ian Rogers | 1441f6f | 2012-02-17 14:48:16 -0800 | [diff] [blame] | 168 | |
| 169 | class InnerClass { |
| 170 | } |
| 171 | |
| 172 | static class StaticInnerClass { |
| 173 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | class LocalClass { |
| 177 | // this class has a default constructor with package visibility |
| 178 | } |
| 179 | |
| 180 | class LocalClass2 { |
| 181 | public LocalClass2() {} |
| 182 | } |
| 183 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 184 | class 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 Gampe | 166aaee | 2016-07-18 08:27:23 -0700 | [diff] [blame] | 200 | Class<?> c = CC.class; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 201 | return c.newInstance(); |
| 202 | } catch (Exception ex) { |
| 203 | ex.printStackTrace(); |
| 204 | return null; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |