|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.springframework.nativex.hint.ProxyBits; |
| 23 | +import org.springframework.nativex.type.ComponentProcessor; |
| 24 | +import org.springframework.nativex.type.Method; |
| 25 | +import org.springframework.nativex.type.NativeContext; |
| 26 | +import org.springframework.nativex.type.Type; |
| 27 | +import org.springframework.nativex.type.TypeSystem; |
| 28 | + |
| 29 | +/** |
| 30 | + * Recognize spring.components that need an AotProxy generating in order to work. This is |
| 31 | + * a simple version that triggers proxy creation based on one of a number of annotations |
| 32 | + * appearing on a method in a component. For simple cases this might be sufficient, if |
| 33 | + * the annotations presence needs something deeper to happen, maybe it would spin off |
| 34 | + * into its own component processor. |
| 35 | + * |
| 36 | + * @author Andy Clement |
| 37 | + */ |
| 38 | +public class AotProxyComponentProcessor implements ComponentProcessor { |
| 39 | + |
| 40 | + // A list of annotations that, if found on a method in a component, should trigger |
| 41 | + // creation of an AotProxy for the class |
| 42 | + public static String[] MethodLevelAnnotations = new String[]{ |
| 43 | + "Lorg/springframework/scheduling/annotation/Async;" |
| 44 | + }; |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean handle(NativeContext imageContext, String componentType, List<String> classifiers) { |
| 48 | + TypeSystem ts = imageContext.getTypeSystem(); |
| 49 | + Type component = ts.resolveDotted(componentType); |
| 50 | + for (String annotationName: MethodLevelAnnotations) { |
| 51 | + if (ts.Lresolve(annotationName,true)!=null) { |
| 52 | + List<Method> interestingMethods = component.getMethods(m -> m.hasAnnotation(annotationName,false)); |
| 53 | + if (!interestingMethods.isEmpty()) { |
| 54 | + imageContext.log("AotProxyComponentProcessor: found annotation "+annotationName+" on component "+componentType+": creating class proxy"); |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void process(NativeContext imageContext, String componentType, List<String> classifiers) { |
| 64 | + // TODO is IS_STATIC always right here? |
| 65 | + imageContext.addAotProxy(componentType, Collections.emptyList(), ProxyBits.IS_STATIC); |
| 66 | + imageContext.log("AotProxyComponentProcessor: creating proxy for this class: "+componentType); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments