|
| 1 | +/* |
| 2 | + * Copyright 2025. 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 | + * http://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.data.aot; |
| 18 | + |
| 19 | +import java.io.Serializable; |
| 20 | +import java.util.List; |
| 21 | +import java.util.function.Predicate; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import org.springframework.aop.SpringProxy; |
| 25 | +import org.springframework.aop.framework.Advised; |
| 26 | +import org.springframework.aot.generate.GenerationContext; |
| 27 | +import org.springframework.aot.hint.MemberCategory; |
| 28 | +import org.springframework.aot.hint.TypeReference; |
| 29 | +import org.springframework.core.DecoratingProxy; |
| 30 | +import org.springframework.core.env.Environment; |
| 31 | +import org.springframework.data.projection.TargetAware; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Christoph Strobl |
| 35 | + */ |
| 36 | +public interface AotTypeConfiguration { |
| 37 | + |
| 38 | + AotTypeConfiguration forDataBinding(); |
| 39 | + |
| 40 | + AotTypeConfiguration forReflectiveAccess(MemberCategory... categories); |
| 41 | + |
| 42 | + AotTypeConfiguration generateEntityInstantiator(); |
| 43 | + |
| 44 | + // TODO: ? should this be a global condition for the entire configuration or do we need it for certain aspects ? |
| 45 | + AotTypeConfiguration conditional(Predicate<TypeReference> filter); |
| 46 | + |
| 47 | + default AotTypeConfiguration usedAsProjectionInterface() { |
| 48 | + return proxyInterface(TargetAware.class, SpringProxy.class, DecoratingProxy.class); |
| 49 | + } |
| 50 | + |
| 51 | + default AotTypeConfiguration springProxy() { |
| 52 | + return proxyInterface(SpringProxy.class, Advised.class, DecoratingProxy.class); |
| 53 | + } |
| 54 | + |
| 55 | + default AotTypeConfiguration repositoryProxy() { |
| 56 | + |
| 57 | + springProxy(); |
| 58 | + |
| 59 | + List<TypeReference> transactionalProxy = List.of(TypeReference.of("org.springframework.data.repository.Repository"), |
| 60 | + TypeReference.of("org.springframework.transaction.interceptor.TransactionalProxy"), |
| 61 | + TypeReference.of("org.springframework.aop.framework.Advised"), TypeReference.of(DecoratingProxy.class)); |
| 62 | + proxyInterface(transactionalProxy); |
| 63 | + |
| 64 | + proxyInterface( |
| 65 | + Stream.concat(transactionalProxy.stream(), Stream.of(TypeReference.of(Serializable.class))).toList()); |
| 66 | + |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + AotTypeConfiguration proxyInterface(List<TypeReference> proxyInterfaces); |
| 71 | + |
| 72 | + default AotTypeConfiguration proxyInterface(TypeReference... proxyInterfaces) { |
| 73 | + return proxyInterface(List.of(proxyInterfaces)); |
| 74 | + } |
| 75 | + |
| 76 | + default AotTypeConfiguration proxyInterface(Class<?>... proxyInterfaces) { |
| 77 | + return proxyInterface(Stream.of(proxyInterfaces).map(TypeReference::of).toList()); |
| 78 | + } |
| 79 | + |
| 80 | + AotTypeConfiguration forQuerydsl(); |
| 81 | + |
| 82 | + void contribute(GenerationContext generationContext); |
| 83 | + |
| 84 | + static Predicate<TypeReference> userConfiguredCondition(Environment environment) { |
| 85 | + |
| 86 | + return new Predicate<TypeReference>() { |
| 87 | + |
| 88 | + private final List<String> allowedAccessorTypes = environment.getProperty("spring.data.aot.generate.accessor", |
| 89 | + List.class, List.of()); |
| 90 | + |
| 91 | + @Override |
| 92 | + @SuppressWarnings("unchecked") |
| 93 | + public boolean test(TypeReference typeReference) { |
| 94 | + |
| 95 | + if (!allowedAccessorTypes.isEmpty()) { |
| 96 | + if (allowedAccessorTypes.contains("none") || allowedAccessorTypes.contains("false") |
| 97 | + || allowedAccessorTypes.contains("off")) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + if (!allowedAccessorTypes.contains(typeReference.getName())) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return true; |
| 106 | + } |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments