< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/CustomizedBytecodePatternTest.java

Print this page
rev 56282 : [mq]: graal
   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  */


  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);
   1 /*
   2  * Copyright (c) 2018, 2019, 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  */


  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(className, e);
  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);
< prev index next >