|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package org.graalvm.nativeimage; |
| 42 | + |
| 43 | +import java.io.Serial; |
| 44 | +import java.lang.reflect.Constructor; |
| 45 | +import java.lang.reflect.Field; |
| 46 | +import java.lang.reflect.Method; |
| 47 | + |
| 48 | +/** |
| 49 | + * This exception is thrown when a JNI query tries to access an element that was not |
| 50 | + * <a href= "https://www.graalvm.org/latest/reference-manual/native-image/metadata/#jni">registered |
| 51 | + * for JNI access</a> in the program. When an element is not registered, the exception will be |
| 52 | + * thrown both for elements that exist and elements that do not exist on the given classpath. |
| 53 | + * <p/> |
| 54 | + * The purpose of this exception is to easily discover unregistered elements and to assure that all |
| 55 | + * JNI operations for registered elements have the expected behaviour. |
| 56 | + * <p/> |
| 57 | + * Queries will succeed (or throw the expected error) if the element was registered for JNI access. |
| 58 | + * If that is not the case, a {@link MissingJNIRegistrationError} will be thrown. |
| 59 | + * <p/> |
| 60 | + * The exception thrown by the JNI query is a <em>pending</em> exception that needs to be explicitly |
| 61 | + * checked by the calling native code. |
| 62 | + * </ol> |
| 63 | + * Examples: |
| 64 | + * <p/> |
| 65 | + * Registration: {@code "fields": [{"name": "registeredField"}, {"name": |
| 66 | + * "registeredNonexistentField"}]}<br> |
| 67 | + * {@code GetFieldID(declaringClass, "registeredField")} will return the expected field.<br> |
| 68 | + * {@code GetFieldID(declaringClass, "registeredNonexistentField")} will throw a |
| 69 | + * {@link NoSuchFieldError}.<br> |
| 70 | + * {@code GetFieldID(declaringClass, "unregisteredField")} will throw a |
| 71 | + * {@link MissingJNIRegistrationError}.<br> |
| 72 | + * {@code GetFieldID(declaringClass, "unregisteredNonexistentField")} will throw a |
| 73 | + * {@link MissingJNIRegistrationError}.<br> |
| 74 | + * |
| 75 | + * @since 24.1 |
| 76 | + */ |
| 77 | +public final class MissingJNIRegistrationError extends Error { |
| 78 | + @Serial private static final long serialVersionUID = -8940056537864516986L; |
| 79 | + |
| 80 | + private final Class<?> elementType; |
| 81 | + |
| 82 | + private final Class<?> declaringClass; |
| 83 | + |
| 84 | + private final String elementName; |
| 85 | + |
| 86 | + private final String signature; |
| 87 | + |
| 88 | + /** |
| 89 | + * @since 24.1 |
| 90 | + */ |
| 91 | + public MissingJNIRegistrationError(String message, Class<?> elementType, Class<?> declaringClass, String elementName, String signature) { |
| 92 | + super(message); |
| 93 | + this.elementType = elementType; |
| 94 | + this.declaringClass = declaringClass; |
| 95 | + this.elementName = elementName; |
| 96 | + this.signature = signature; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return The type of the element trying to be queried ({@link Class}, {@link Method}, |
| 101 | + * {@link Field} or {@link Constructor}). |
| 102 | + * @since 23.0 |
| 103 | + */ |
| 104 | + public Class<?> getElementType() { |
| 105 | + return elementType; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return The class on which the missing query was tried, or null on static queries. |
| 110 | + * @since 23.0 |
| 111 | + */ |
| 112 | + public Class<?> getDeclaringClass() { |
| 113 | + return declaringClass; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @return The name of the queried element. |
| 118 | + * @since 23.0 |
| 119 | + */ |
| 120 | + public String getElementName() { |
| 121 | + return elementName; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @return The signature passed to the query, or null if the query doesn't take a signature as |
| 126 | + * argument. |
| 127 | + * @since 23.0 |
| 128 | + */ |
| 129 | + public String getSignature() { |
| 130 | + return signature; |
| 131 | + } |
| 132 | +} |
0 commit comments