1 /*
   2  * Copyright (c) 2011, 2014, 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 package org.graalvm.compiler.core.test;
  24 
  25 import java.lang.invoke.MethodHandle;
  26 import java.lang.invoke.MethodHandles;
  27 import java.lang.reflect.Field;
  28 
  29 import org.junit.Test;
  30 
  31 import org.graalvm.compiler.nodes.DeoptimizeNode;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  34 
  35 public final class MethodHandleEagerResolution extends GraalCompilerTest {
  36     private static final MethodHandle FIELD_HANDLE;
  37 
  38     static {
  39         Field field;
  40         try {
  41             field = String.class.getDeclaredField("value");
  42         } catch (NoSuchFieldException ex) {
  43             throw new RuntimeException(ex.getMessage(), ex);
  44         }
  45         field.setAccessible(true);
  46 
  47         try {
  48             FIELD_HANDLE = MethodHandles.lookup().unreflectGetter(field);
  49         } catch (IllegalAccessException e) {
  50             throw new RuntimeException("unable to initialize field handle", e);
  51         }
  52     }
  53 
  54     public static char[] getBackingCharArray(String str) {
  55         try {
  56             return (char[]) FIELD_HANDLE.invokeExact(str);
  57         } catch (Throwable e) {
  58             throw new IllegalStateException();
  59         }
  60     }
  61 
  62     @Test
  63     public void testFieldInvokeExact() {
  64         StructuredGraph graph = parseEager("getBackingCharArray", AllowAssumptions.NO);
  65         assertTrue(graph.getNodes().filter(DeoptimizeNode.class).isEmpty());
  66     }
  67 
  68 }