|
| 1 | +/* Copyright (c) 2015 Adam Marcionek, All Rights Reserved |
| 2 | + * |
| 3 | + * The contents of this file is dual-licensed under 2 |
| 4 | + * alternative Open Source/Free licenses: LGPL 2.1 or later and |
| 5 | + * Apache License 2.0. (starting with JNA version 4.0.0). |
| 6 | + * |
| 7 | + * You can freely decide which license you want to apply to |
| 8 | + * the project. |
| 9 | + * |
| 10 | + * You may obtain a copy of the LGPL License at: |
| 11 | + * |
| 12 | + * http://www.gnu.org/licenses/licenses.html |
| 13 | + * |
| 14 | + * A copy is also included in the downloadable source code package |
| 15 | + * containing JNA, in file "LGPL2.1". |
| 16 | + * |
| 17 | + * You may obtain a copy of the Apache License at: |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/ |
| 20 | + * |
| 21 | + * A copy is also included in the downloadable source code package |
| 22 | + * containing JNA, in file "AL2.0". |
| 23 | + */ |
| 24 | +package com.sun.jna; |
| 25 | + |
| 26 | +import org.graalvm.nativeimage.hosted.Feature; |
| 27 | +import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; |
| 28 | +import org.graalvm.nativeimage.hosted.RuntimeJNIAccess; |
| 29 | +import org.graalvm.nativeimage.hosted.RuntimeProxyCreation; |
| 30 | +import org.graalvm.nativeimage.hosted.RuntimeReflection; |
| 31 | +import org.graalvm.nativeimage.hosted.RuntimeResourceAccess; |
| 32 | + |
| 33 | +import java.lang.reflect.Field; |
| 34 | +import java.lang.reflect.Method; |
| 35 | +import java.util.Arrays; |
| 36 | + |
| 37 | +// Provides common logic for JNA-related GraalVM feature classes. These classes should only be included |
| 38 | +// at build time for a `native-image` target. |
| 39 | +abstract class AbstractJNAFeature implements Feature { |
| 40 | + /** |
| 41 | + * Obtain a reference to a method on a class, in order to register it for reflective access |
| 42 | + * |
| 43 | + * @param clazz Class to obtain method reference from |
| 44 | + * @param methodName Name of the method to obtain a reference to |
| 45 | + * @param args Method arguments |
| 46 | + * @return Method reference |
| 47 | + */ |
| 48 | + protected static Method method(Class<?> clazz, String methodName, Class<?>... args) { |
| 49 | + try { |
| 50 | + return clazz.getDeclaredMethod(methodName, args); |
| 51 | + } catch (NoSuchMethodException e) { |
| 52 | + throw new IllegalStateException(e); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Obtain a reference to one or more fields on a class, in order to register them for reflective access |
| 58 | + * |
| 59 | + * @param clazz Class to obtain field references from |
| 60 | + * @param fieldNames Names of the fields to obtain references to |
| 61 | + * @return Field references |
| 62 | + */ |
| 63 | + protected static Field[] fields(Class<?> clazz, String... fieldNames) { |
| 64 | + try { |
| 65 | + Field[] fields = new Field[fieldNames.length]; |
| 66 | + for (int i = 0; i < fieldNames.length; i++) { |
| 67 | + fields[i] = clazz.getDeclaredField(fieldNames[i]); |
| 68 | + } |
| 69 | + return fields; |
| 70 | + } catch (NoSuchFieldException e) { |
| 71 | + throw new IllegalStateException(e); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Register a class for reflective access at runtime |
| 77 | + * |
| 78 | + * @param clazz Class to register |
| 79 | + */ |
| 80 | + protected static void reflectiveClass(Class<?>... clazz) { |
| 81 | + RuntimeReflection.register(clazz); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Register a resource for use in the final image |
| 86 | + * |
| 87 | + * @param module Module which owns the resource |
| 88 | + * @param resource Path to the resource to register |
| 89 | + */ |
| 90 | + protected static void registerResource(Module module, String resource) { |
| 91 | + RuntimeResourceAccess.addResource(module, resource); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Register a resource for use in the final image |
| 96 | + * |
| 97 | + * @param resource Path to the resource to register |
| 98 | + */ |
| 99 | + protected static void registerResource(String resource) { |
| 100 | + registerResource(AbstractJNAFeature.class.getModule(), resource); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Register a class for JNI access at runtime |
| 105 | + * |
| 106 | + * @param clazz Class to register |
| 107 | + */ |
| 108 | + protected static void registerJniClass(Class<?> clazz) { |
| 109 | + RuntimeJNIAccess.register(clazz); |
| 110 | + Arrays.stream(clazz.getConstructors()).forEach(RuntimeJNIAccess::register); |
| 111 | + Arrays.stream(clazz.getMethods()).forEach(RuntimeJNIAccess::register); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Register a class for JNI access at runtime, potentially with reflective access as well |
| 116 | + * |
| 117 | + * @param clazz Class to register |
| 118 | + * @param reflective Whether to register the class and constructors for reflective access |
| 119 | + */ |
| 120 | + protected static void registerJniClass(Class<?> clazz, Boolean reflective) { |
| 121 | + registerJniClass(clazz); |
| 122 | + if (reflective) { |
| 123 | + RuntimeReflection.register(clazz); |
| 124 | + RuntimeReflection.registerAllConstructors(clazz); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Register a suite of JNA methods for use at runtime |
| 130 | + * |
| 131 | + * @param reflective Whether to register the methods for reflective access |
| 132 | + * @param methods Methods to register |
| 133 | + */ |
| 134 | + protected static void registerJniMethods(Boolean reflective, Method... methods) { |
| 135 | + RuntimeJNIAccess.register(methods); |
| 136 | + if (reflective) { |
| 137 | + RuntimeReflection.register(methods); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Register a suite of JNA methods for use at runtime |
| 143 | + * |
| 144 | + * @param methods Methods to register |
| 145 | + */ |
| 146 | + protected static void registerJniMethods(Method... methods) { |
| 147 | + registerJniMethods(false, methods); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Register a suite of JNA fields for use at runtime |
| 152 | + * |
| 153 | + * @param reflective Whether to register the fields for reflective access |
| 154 | + * @param fields Fields to register |
| 155 | + */ |
| 156 | + protected static void registerJniFields(Boolean reflective, Field[] fields) { |
| 157 | + RuntimeJNIAccess.register(fields); |
| 158 | + if (reflective) { |
| 159 | + RuntimeReflection.register(fields); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Register a suite of JNA fields for use at runtime |
| 165 | + * |
| 166 | + * @param fields Fields to register |
| 167 | + */ |
| 168 | + protected static void registerJniFields(Field[] fields) { |
| 169 | + registerJniFields(false, fields); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Register a combination of interfaces used at runtime as a dynamic proxy object |
| 174 | + * |
| 175 | + * @param classes Combination of interface classes; order matters |
| 176 | + */ |
| 177 | + protected static void registerProxyInterfaces(Class<?>... classes) { |
| 178 | + RuntimeProxyCreation.register(classes); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Assign the specified class or classes to initialize at image build time |
| 183 | + * |
| 184 | + * @param clazz Classes to register for build-time initialization |
| 185 | + */ |
| 186 | + protected static void initializeAtBuildTime(Class<?>... clazz) { |
| 187 | + for (Class<?> c : clazz) { |
| 188 | + RuntimeClassInitialization.initializeAtBuildTime(c); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Assign the specified class or classes to initialize at image run-time |
| 194 | + * |
| 195 | + * @param clazz Classes to register for run-time initialization |
| 196 | + */ |
| 197 | + protected static void initializeAtRunTime(Class<?>... clazz) { |
| 198 | + for (Class<?> c : clazz) { |
| 199 | + RuntimeClassInitialization.initializeAtRunTime(c); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments