|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 2023, 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package com.oracle.svm.hosted.annotation; |
| 26 | + |
| 27 | +import java.lang.annotation.Annotation; |
| 28 | +import java.lang.reflect.Array; |
| 29 | +import java.lang.reflect.Proxy; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.concurrent.ConcurrentHashMap; |
| 32 | + |
| 33 | +import org.graalvm.nativeimage.ImageSingletons; |
| 34 | +import org.graalvm.nativeimage.impl.ConfigurationCondition; |
| 35 | +import org.graalvm.nativeimage.impl.RuntimeReflectionSupport; |
| 36 | + |
| 37 | +import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature; |
| 38 | +import com.oracle.svm.core.feature.InternalFeature; |
| 39 | +import com.oracle.svm.hosted.reflect.ReflectionDataBuilder; |
| 40 | + |
| 41 | +@AutomaticallyRegisteredFeature |
| 42 | +public class AnnotationFeature implements InternalFeature { |
| 43 | + |
| 44 | + private RuntimeReflectionSupport runtimeReflectionSupport; |
| 45 | + private final Set<Class<? extends Annotation>> processedTypes = ConcurrentHashMap.newKeySet(); |
| 46 | + |
| 47 | + @Override |
| 48 | + public void duringSetup(DuringSetupAccess access) { |
| 49 | + runtimeReflectionSupport = ImageSingletons.lookup(RuntimeReflectionSupport.class); |
| 50 | + access.registerObjectReplacer(this::registerDeclaredMethods); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * For annotations that are materialized at image run time, all necessary methods are registered |
| 55 | + * for reflection in {@link ReflectionDataBuilder#registerTypesForAnnotation}. But if an |
| 56 | + * annotation type is only used by an annotation that is already in the image heap, then we need |
| 57 | + * to also register its methods for reflection. This is done here by inspecting every image heap |
| 58 | + * object and checking if it is an annotation that was materialized by the JDK, i.e., implements |
| 59 | + * the {@link Annotation} interface and is a {@link Proxy}. |
| 60 | + */ |
| 61 | + private Object registerDeclaredMethods(Object obj) { |
| 62 | + if (obj instanceof Annotation annotation && Proxy.isProxyClass(annotation.getClass())) { |
| 63 | + Class<? extends Annotation> annotationType = annotation.annotationType(); |
| 64 | + if (processedTypes.add(annotationType)) { |
| 65 | + runtimeReflectionSupport.registerAllDeclaredMethodsQuery(ConfigurationCondition.alwaysTrue(), false, annotationType); |
| 66 | + } |
| 67 | + } |
| 68 | + return obj; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void beforeAnalysis(BeforeAnalysisAccess access) { |
| 73 | + access.registerSubtypeReachabilityHandler(this::registerArrayClass, Annotation.class); |
| 74 | + } |
| 75 | + |
| 76 | + /* |
| 77 | + * The JDK implementation of repeatable annotations always instantiates an array of a requested |
| 78 | + * annotation. We need to mark arrays of all reachable annotations as in heap. |
| 79 | + */ |
| 80 | + private void registerArrayClass(DuringAnalysisAccess access, Class<?> subclass) { |
| 81 | + if (subclass.isAnnotation()) { |
| 82 | + Class<?> arrayClass = Array.newInstance(subclass, 0).getClass(); |
| 83 | + access.registerAsInHeap(arrayClass); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments