Path: blob/master/test/jdk/java/beans/Statement/ClassForName/ClassForName.java
41153 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.beans.Expression;24import java.beans.Statement;2526/**27* @test28* @bug 814631329* @run main/othervm ClassForName30* @run main/othervm/policy=java.policy -Djava.security.manager ClassForName31*/32public final class ClassForName {3334static boolean initialized;3536static final String[] classes = {37"A.A", "java.lang.String", "ClassForName$Bean", "sun.awt.SunToolkit"38};3940static final ClassLoader appl = new Object() {}.getClass().getClassLoader();4142static final ClassLoader[] loaders = {43String.class.getClassLoader(), null, appl44};4546static boolean[] inits = {false, true};4748public static void main(final String[] args) throws Exception {49// Check that the Class.forName(name, boolean, classloader) is executed50// when requested via JavaBeans51simpleTest();5253// Check that the Class.forName and Expression returns the same classes54for (final String cls : classes) {55complexTest1Args(cls);56for (final ClassLoader loader : loaders) {57for (final boolean init : inits) {58complexTest3Args(cls, loader, init);59}60}61}62}6364private static void simpleTest() throws Exception {65// load the class without initialization66new Statement(Class.class, "forName", new Object[]{67"ClassForName$Bean", false, Bean.class.getClassLoader()68}).execute();69if (initialized) {70throw new RuntimeException("Should not be initialized");71}7273// load the class and initialize it74new Statement(Class.class, "forName", new Object[]{75"ClassForName$Bean", true, Bean.class.getClassLoader()76}).execute();77if (!initialized) {78throw new RuntimeException("Should be initialized");79}80}8182private static void complexTest1Args(final String cls) {83// load via standard Class.forName();84Class<?> classForName = null;85try {86classForName = Class.forName(cls);87} catch (final Exception ignored) {88}8990// load via Expression.execute()91Class<?> classStatement = null;92try {93final Expression exp = new Expression(Class.class, "forName",94new Object[]{95cls96});97exp.execute();98classStatement = (Class<?>) exp.getValue();99} catch (final Exception ignored) {100}101if (classForName != classStatement) {102System.err.println(classForName);103System.err.println(classStatement);104throw new RuntimeException();105}106}107108private static void complexTest3Args(final String cls,109final ClassLoader loader,110final boolean init) {111// load via standard Class.forName();112Class<?> classForName = null;113Class<?> excForName = null;114try {115classForName = Class.forName(cls, init, loader);116} catch (final Exception e) {117excForName = e.getClass();118}119120// load via Expression.execute()121Class<?> classStatement = null;122Class<?> excStatement = null;123try {124final Expression exp = new Expression(Class.class, "forName",125new Object[]{126cls, init, loader127});128exp.execute();129classStatement = (Class<?>) exp.getValue();130} catch (final Exception e) {131excStatement = e.getClass();132}133if (classForName != classStatement) {134System.err.println(classForName);135System.err.println(classStatement);136throw new RuntimeException();137}138if (excForName != excStatement) {139System.err.println(excForName);140System.err.println(excStatement);141throw new RuntimeException();142}143}144145public static final class Bean {146147static {148initialized = true;149}150}151}152153154