Path: blob/master/test/jdk/java/lang/ClassLoader/defineClass/DefineClassByteBuffer.java
41153 views
/*1* Copyright (c) 2003, 2010, 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*/2223/*24* @test25* @bug 489489926* @summary Test various cases of passing java.nio.ByteBuffers27* to defineClass().28*29* @build DefineClassByteBuffer TestClass30* @run main DefineClassByteBuffer31*/3233import java.nio.*;34import java.nio.channels.*;35import java.io.*;3637public class DefineClassByteBuffer {3839static void test(ClassLoader cl) throws Exception {40Class<?> c = Class.forName("TestClass", true, cl);41if (!"TestClass".equals(c.getName())) {42throw new RuntimeException("Got wrong class: " + c);43}44if (c.getClassLoader() != cl) {45throw new RuntimeException("TestClass defined by wrong classloader: " + c.getClassLoader());46}47}4849public static void main(String arg[]) throws Exception {50DummyClassLoader[] cls = new DummyClassLoader[DummyClassLoader.MAX_TYPE];51for (int i = 0; i < cls.length; i++) {52cls[i] = new DummyClassLoader(i);53}5455/* Create several instances of the class using different classloaders,56which are using different types of ByteBuffer. */57for (int i = 0; i < cls.length; i++) {58test(cls[i]);59}60}6162/** Always loads the same class, using various types of ByteBuffers */63public static class DummyClassLoader extends ClassLoader {6465public static final String CLASS_NAME = "TestClass";6667public static final int MAPPED_BUFFER = 0;68public static final int DIRECT_BUFFER = 1;69public static final int ARRAY_BUFFER = 2;70public static final int WRAPPED_BUFFER = 3;71public static final int READ_ONLY_ARRAY_BUFFER = 4;72public static final int READ_ONLY_DIRECT_BUFFER = 5;73public static final int DUP_ARRAY_BUFFER = 6;74public static final int DUP_DIRECT_BUFFER = 7;75public static final int MAX_TYPE = 7;7677int loaderType;7879DummyClassLoader(int loaderType) {80this.loaderType = loaderType;81}8283static ByteBuffer[] buffers = new ByteBuffer[MAX_TYPE + 1];8485static ByteBuffer readClassFile(String name) {86try {87File f = new File(System.getProperty("test.classes", "."),88name);89FileInputStream fin = new FileInputStream(f);90FileChannel fc = fin.getChannel();91return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());92} catch (FileNotFoundException e) {93throw new RuntimeException("Can't open file: " + name, e);94} catch (IOException e) {95throw new RuntimeException("Can't open file: " + name, e);96}97}9899static {100/* create a bunch of different ByteBuffers, starting with a mapped101buffer from a class file, and create various duplicate and wrapped102buffers. */103buffers[MAPPED_BUFFER] = readClassFile(CLASS_NAME + ".class");104byte[] array = new byte[buffers[MAPPED_BUFFER].limit()];105buffers[MAPPED_BUFFER].get(array).flip();106107buffers[DIRECT_BUFFER] = ByteBuffer.allocateDirect(array.length);108buffers[DIRECT_BUFFER].put(array).flip();109110buffers[ARRAY_BUFFER] = ByteBuffer.allocate(array.length);111buffers[ARRAY_BUFFER].put(array).flip();112113buffers[WRAPPED_BUFFER] = ByteBuffer.wrap(array);114115buffers[READ_ONLY_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].asReadOnlyBuffer();116117buffers[READ_ONLY_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].asReadOnlyBuffer();118119buffers[DUP_ARRAY_BUFFER] = buffers[ARRAY_BUFFER].duplicate();120121buffers[DUP_DIRECT_BUFFER] = buffers[DIRECT_BUFFER].duplicate();122}123124protected Class<?> loadClass(String name, boolean resolve)125throws ClassNotFoundException126{127Class<?> c;128if (!"TestClass".equals(name)) {129c = super.loadClass(name, resolve);130} else {131// should not delegate to the system class loader132c = findClass(name);133if (resolve) {134resolveClass(c);135}136}137return c;138}139140protected Class<?> findClass(String name)141throws ClassNotFoundException142{143if (!"TestClass".equals(name)) {144throw new ClassNotFoundException("Unexpected class: " + name);145}146return defineClass(name, buffers[loaderType], null);147}148} /* DummyClassLoader */149150} /* DefineClassByteBuffer */151152153