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 java.lang.invoke.MethodHandles;
  28 import java.lang.reflect.Method;
  29 import java.security.ProtectionDomain;
  30 
  31 import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
  32 import org.objectweb.asm.Opcodes;
  33 
  34 import sun.misc.Unsafe;
  35 
  36 public abstract class CustomizedBytecodePatternTest extends GraalCompilerTest implements Opcodes {
  37 
  38     protected Class<?> getClass(String className) throws ClassNotFoundException {
  39         return new CachedLoader(CustomizedBytecodePatternTest.class.getClassLoader(), className).findClass(className);
  40     }
  41 
  42     /**
  43      * @param className
  44      * @param lookUp lookup object with boot class load capability (required for jdk 9 and above)
  45      * @return loaded class
  46      * @throws ClassNotFoundException
  47      */
  48     protected Class<?> getClassBL(String className, MethodHandles.Lookup lookUp) throws ClassNotFoundException {
  49         byte[] gen = generateClass(className.replace('.', '/'));
  50         Method defineClass = null;
  51         Class<?> loadedClass = null;
  52         try {
  53             if (JavaVersionUtil.JAVA_SPEC <= 8) {
  54                 defineClass = Unsafe.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class, ClassLoader.class, ProtectionDomain.class);
  55                 loadedClass = (Class<?>) defineClass.invoke(UNSAFE, className, gen, 0, gen.length, null, null);
  56             } else {
  57                 defineClass = MethodHandles.lookup().getClass().getDeclaredMethod("defineClass", byte[].class);
  58                 loadedClass = (Class<?>) defineClass.invoke(lookUp, gen);
  59             }
  60         } catch (Exception e) {
  61             throw new ClassNotFoundException();
  62         }
  63         return loadedClass;
  64     }
  65 
  66     private class CachedLoader extends ClassLoader {
  67 
  68         final String className;
  69         Class<?> loaded;
  70 
  71         CachedLoader(ClassLoader parent, String className) {
  72             super(parent);
  73             this.className = className;
  74         }
  75 
  76         @Override
  77         protected Class<?> findClass(String name) throws ClassNotFoundException {
  78             if (name.equals(className)) {
  79                 if (loaded == null) {
  80                     byte[] gen = generateClass(name.replace('.', '/'));
  81                     loaded = defineClass(name, gen, 0, gen.length);
  82                 }
  83                 return loaded;
  84             } else {
  85                 return super.findClass(name);
  86             }
  87         }
  88 
  89     }
  90 
  91     protected abstract byte[] generateClass(String internalClassName);
  92 
  93 }