1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.beans.Expression; 25 import java.beans.Statement; 26 27 /** 28 * @test 29 * @bug 8146313 30 * @run main/othervm ClassForName 31 * @run main/othervm/policy=java.policy -Djava.security.manager ClassForName 32 */ 33 public final class ClassForName { 34 35 static boolean initialized; 36 37 static final String[] classes = { 38 "java.lang.String", "ClassForName$Bean", "sun.awt.SunToolkit" 39 }; 40 41 static final ClassLoader appl = new Object() {}.getClass().getClassLoader(); 42 43 static final ClassLoader[] loaders = { 44 String.class.getClassLoader(), null, appl 45 }; 46 47 static boolean[] inits = {false, true}; 48 49 public static void main(final String[] args) throws Exception { 50 // Check that the Class.forName(name, boolean, classloader) is executed 51 // when requested via JavaBeans 52 simpleTest(); 53 54 // Check that the Class.forName and Expression returns the same classes 55 for (final String cls : classes) { 56 complexTest1Args(cls); 57 for (final ClassLoader loader : loaders) { 58 for (final boolean init : inits) { 59 complexTest3Args(cls, loader, init); 60 } 61 } 62 } 63 } 64 65 private static void simpleTest() throws Exception { 66 // load the class without initialization 67 new Statement(Class.class, "forName", new Object[]{ 68 "ClassForName$Bean", false, Bean.class.getClassLoader() 69 }).execute(); 70 if (initialized) { 71 throw new RuntimeException("Should not be initialized"); 72 } 73 74 // load the class and initialize it 75 new Statement(Class.class, "forName", new Object[]{ 76 "ClassForName$Bean", true, Bean.class.getClassLoader() 77 }).execute(); 78 if (!initialized) { 79 throw new RuntimeException("Should be initialized"); 80 } 81 } 82 83 private static void complexTest1Args(final String cls) { 84 // load via standard Class.forName(); 85 Class<?> classForName = null; 86 try { 87 classForName = Class.forName(cls); 88 } catch (final Exception ignored) { 89 } 90 91 // load via Expression.execute() 92 Class<?> classStatement = null; 93 try { 94 final Expression exp = new Expression(Class.class, "forName", 95 new Object[]{ 96 cls 97 }); 98 exp.execute(); 99 classStatement = (Class<?>) exp.getValue(); 100 } catch (final Exception ignored) { 101 } 102 if (classForName != classStatement) { 103 System.err.println(classForName); 104 System.err.println(classStatement); 105 throw new RuntimeException(); 106 } 107 } 108 109 private static void complexTest3Args(final String cls, 110 final ClassLoader loader, 111 final boolean init) { 112 // load via standard Class.forName(); 113 Class<?> classForName = null; 114 try { 115 classForName = Class.forName(cls, init, loader); 116 } catch (final Exception ignored) { 117 } 118 119 // load via Expression.execute() 120 Class<?> classStatement = null; 121 try { 122 final Expression exp = new Expression(Class.class, "forName", 123 new Object[]{ 124 cls, init, loader 125 }); 126 exp.execute(); 127 classStatement = (Class<?>) exp.getValue(); 128 } catch (final Exception ignored) { 129 } 130 if (classForName != classStatement) { 131 System.err.println(classForName); 132 System.err.println(classStatement); 133 throw new RuntimeException(); 134 } 135 } 136 137 public static final class Bean { 138 139 static { 140 initialized = true; 141 } 142 } 143 }