1 /*
   2  * Copyright (c) 2014, 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.api.replacements;
  26 
  27 import java.util.Objects;
  28 
  29 import jdk.vm.ci.meta.JavaConstant;
  30 import jdk.vm.ci.meta.JavaKind;
  31 import jdk.vm.ci.meta.ResolvedJavaType;
  32 
  33 /**
  34  * Reflection operations on values represented as {@linkplain JavaConstant constants} for the
  35  * processing of snippets. Snippets need a direct access to the value of object constants, which is
  36  * not allowed in other parts of Graal to enforce compiler-VM separation.
  37  * <p>
  38  * This interface must not be used in Graal code that is not related to snippet processing.
  39  */
  40 public interface SnippetReflectionProvider {
  41 
  42     /**
  43      * Creates a boxed {@link JavaKind#Object object} constant.
  44      *
  45      * @param object the object value to box
  46      * @return a constant containing {@code object}
  47      */
  48     JavaConstant forObject(Object object);
  49 
  50     /**
  51      * Gets the object reference a given constant represents if it is of a given type. The constant
  52      * must have kind {@link JavaKind#Object}.
  53      *
  54      * @param type the expected type of the object represented by {@code constant}. If the object is
  55      *            required to be of this type, then wrap the call to this method in
  56      *            {@link Objects#requireNonNull(Object)}.
  57      * @param constant an object constant
  58      * @return the object value represented by {@code constant} cast to {@code type} if it is an
  59      *         {@link Class#isInstance(Object) instance of} {@code type} otherwise {@code null}
  60      */
  61     <T> T asObject(Class<T> type, JavaConstant constant);
  62 
  63     /**
  64      * Gets the object reference a given constant represents if it is of a given type. The constant
  65      * must have kind {@link JavaKind#Object}.
  66      *
  67      * @param type the expected type of the object represented by {@code constant}. If the object is
  68      *            required to be of this type, then wrap the call to this method in
  69      *            {@link Objects#requireNonNull(Object)}.
  70      * @param constant an object constant
  71      * @return the object value represented by {@code constant} if it is an
  72      *         {@link ResolvedJavaType#isInstance(JavaConstant) instance of} {@code type} otherwise
  73      *         {@code null}
  74      */
  75     Object asObject(ResolvedJavaType type, JavaConstant constant);
  76 
  77     /**
  78      * Creates a boxed constant for the given kind from an Object. The object needs to be of the
  79      * Java boxed type corresponding to the kind.
  80      *
  81      * @param kind the kind of the constant to create
  82      * @param value the Java boxed value: a {@link Byte} instance for {@link JavaKind#Byte}, etc.
  83      * @return the boxed copy of {@code value}
  84      */
  85     JavaConstant forBoxed(JavaKind kind, Object value);
  86 
  87     /**
  88      * Gets the value to bind to an injected parameter in a node intrinsic.
  89      *
  90      * @param type the type of a parameter in a node intrinsic constructor
  91      * @return the value that should be bound to the parameter when invoking the constructor or null
  92      *         if this provider cannot provide a value of the requested type
  93      */
  94     <T> T getInjectedNodeIntrinsicParameter(Class<T> type);
  95 
  96     /**
  97      * Get the original Java class corresponding to a {@link ResolvedJavaType}.
  98      *
  99      * @param type the type for which the original Java class is requested
 100      * @return the original Java class corresponding to the {@code type} parameter
 101      */
 102     Class<?> originalClass(ResolvedJavaType type);
 103 }