Path: blob/master/test/hotspot/jtreg/vmTestbase/metaspace/stressHierarchy/common/classloader/StressClassloader.java
41162 views
/*1* Copyright (c) 2013, 2018, 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*/22package metaspace.stressHierarchy.common.classloader;2324import java.net.URLClassLoader;25import java.net.URL;26import java.util.HashSet;27import java.util.Set;2829import metaspace.stressHierarchy.common.generateHierarchy.NodeDescriptor;3031/**32* This classloader tries to load each class itself before forwarding to parent.33* Also it stores loaded classes and class names.34*35*/36public class StressClassloader extends URLClassLoader {3738private final Set<Class<?>> loadedClasses = new HashSet<Class<?>>();3940private final Set<String> loadedClassesNames = new HashSet<String>();4142private String className;4344private byte[] bytecode;4546public StressClassloader(NodeDescriptor nodeDescriptor,47StressClassloader parentClassLoader) {48super(new URL[] {}, parentClassLoader);49this.className = nodeDescriptor.className;50this.bytecode = nodeDescriptor.bytecode;51}5253public Set<Class<?>> getLoadedClasses() {54return loadedClasses;55}5657public Set<String> getLoadedClassNames() {58return loadedClassesNames;59}6061@Override62public Class<?> loadClass(String name) throws ClassNotFoundException {63if (loadedClassesNames.contains(name)) {64throw new RuntimeException("Classloader " + toString() + " loads class " + name + " second time! ");65}66Class<?> alreadyLoaded = findLoadedClass(name);67if (alreadyLoaded != null) {68return alreadyLoaded;69}70if (className.equals(name)) {71Class<?> clazz = defineClass(name, bytecode, 0, bytecode.length);72loadedClasses.add(clazz);73loadedClassesNames.add(clazz.getName());74return clazz;75} else {76return super.loadClass(name);77}78}7980@Override81public String toString() {82return "StressClassloader@" + className;83}84858687}888990