Home | History | Annotate | Download | only in src
      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 
     19 /**
     20  * Test instance creation.
     21  */
     22 public class Main {
     23     private static boolean FULL_ACCESS_CHECKS = false;  // b/5861201
     24 
     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();
     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();
     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 succeed
     96         try {
     97             Class<?> c = Class.forName("Main$InnerClass");
     98             Constructor<?> cons = c.getDeclaredConstructor(Main.class);
     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 {
    108             Class<?> c = Class.forName("Main$StaticInnerClass");
    109             Constructor<?> cons = c.getDeclaredConstructor();
    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 
    117         // should fail
    118         try {
    119             Class<?> c = Class.forName("otherpackage.PackageAccess");
    120             Constructor<?> cons = c.getConstructor();
    121             System.err.println("ERROR: Cons PackageAccess succeeded unexpectedly");
    122         } catch (NoSuchMethodException nsme) {
    123             // constructor isn't public
    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 {
    132             Class<?> c = Class.forName("MaybeAbstract");
    133             Constructor<?> cons = c.getConstructor();
    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         }
    143 
    144         // should fail
    145         try {
    146             Class<?> c = Class.forName("otherpackage.PackageAccess2");
    147             Constructor<?> cons = c.getConstructor();
    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 
    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         }
    167     }
    168 
    169     class InnerClass {
    170     }
    171 
    172     static class StaticInnerClass {
    173     }
    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 
    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 {
    200                 Class<?> c = CC.class;
    201                 return c.newInstance();
    202             } catch (Exception ex) {
    203                 ex.printStackTrace();
    204                 return null;
    205             }
    206         }
    207     }
    208 }
    209