1 /*
2  * Copyright (c) 2018, 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 
25 package org.graalvm.compiler.core.test;
26 
27 import org.objectweb.asm.Opcodes;
28 
29 public abstract class CustomizedBytecodePatternTest extends GraalCompilerTest implements Opcodes {
30 
getClass(String className)31     protected Class<?> getClass(String className) throws ClassNotFoundException {
32         return new CachedLoader(CustomizedBytecodePatternTest.class.getClassLoader(), className).findClass(className);
33     }
34 
35     private class CachedLoader extends ClassLoader {
36 
37         final String className;
38         Class<?> loaded;
39 
CachedLoader(ClassLoader parent, String className)40         CachedLoader(ClassLoader parent, String className) {
41             super(parent);
42             this.className = className;
43         }
44 
45         @Override
findClass(String name)46         protected Class<?> findClass(String name) throws ClassNotFoundException {
47             if (name.equals(className)) {
48                 if (loaded == null) {
49                     byte[] gen = generateClass(name.replace('.', '/'));
50                     loaded = defineClass(name, gen, 0, gen.length);
51                 }
52                 return loaded;
53             } else {
54                 return super.findClass(name);
55             }
56         }
57 
58     }
59 
generateClass(String internalClassName)60     protected abstract byte[] generateClass(String internalClassName);
61 
62 }
63