|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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.aot.graalvm; |
| 18 | + |
| 19 | +import java.lang.reflect.Field; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +import com.oracle.graal.pointsto.infrastructure.SubstitutionProcessor; |
| 23 | +import com.oracle.svm.core.meta.SubstrateObjectConstant; |
| 24 | +import com.oracle.svm.core.util.UserError; |
| 25 | +import jdk.vm.ci.meta.JavaConstant; |
| 26 | +import jdk.vm.ci.meta.JavaKind; |
| 27 | +import jdk.vm.ci.meta.ResolvedJavaField; |
| 28 | +import jdk.vm.ci.meta.ResolvedJavaType; |
| 29 | +import org.graalvm.compiler.debug.DebugContext; |
| 30 | + |
| 31 | +/** |
| 32 | + * {@link SubstitutionProcessor} to compute at build time the value of the |
| 33 | + * boolean static fields identified by {@link #patterns} in order to allow |
| 34 | + * efficient code shrinking without using class build time initialization. |
| 35 | + * |
| 36 | + * @author Phillip Webb |
| 37 | + * @author Sebastien Deleuze |
| 38 | + * @since 6.0 |
| 39 | + */ |
| 40 | +class ConstantFieldSubstitutionProcessor extends SubstitutionProcessor { |
| 41 | + |
| 42 | + // Later should be an explicit signal, like an annotation or even a Java keyword |
| 43 | + private static Pattern[] patterns = { |
| 44 | + Pattern.compile(Pattern.quote("org.springframework.core.NativeDetector#imageCode")), |
| 45 | + Pattern.compile(Pattern.quote("org.springframework.") + ".*#.*Present"), |
| 46 | + }; |
| 47 | + |
| 48 | + private final ThrowawayClassLoader throwawayClassLoader; |
| 49 | + |
| 50 | + |
| 51 | + ConstantFieldSubstitutionProcessor(DebugContext debug, ClassLoader applicationClassLoader) { |
| 52 | + this.throwawayClassLoader = new ThrowawayClassLoader(applicationClassLoader); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Override |
| 57 | + public ResolvedJavaField lookup(ResolvedJavaField field) { |
| 58 | + ResolvedJavaType declaringClass = field.getDeclaringClass(); |
| 59 | + if (field.getType().getJavaKind() == JavaKind.Boolean && field.isStatic()) { |
| 60 | + String fieldIdentifier = declaringClass.toJavaName() + "#" + field.getName(); |
| 61 | + for (Pattern pattern : patterns) { |
| 62 | + if (pattern.matcher(fieldIdentifier).matches()) { |
| 63 | + JavaConstant constant = lookupConstant(declaringClass.toJavaName(), field.getName()); |
| 64 | + if (constant != null) { |
| 65 | + // TODO Use proper logging only when --verbose is specified when https://github.com/oracle/graal/issues/4669 will be fixed |
| 66 | + System.out.println("Field " + fieldIdentifier + " set to " + constant.toValueString() + " at build time"); |
| 67 | + return new ConstantReadableJavaField(field, constant); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + return super.lookup(field); |
| 73 | + } |
| 74 | + |
| 75 | + private JavaConstant lookupConstant(String className, String fieldName) { |
| 76 | + try { |
| 77 | + Class<?> throwawayClass = this.throwawayClassLoader.loadClass(className); |
| 78 | + Field field = throwawayClass.getDeclaredField(fieldName); |
| 79 | + field.setAccessible(true); |
| 80 | + Object value = field.get(null); |
| 81 | + if (!(value instanceof Boolean)) { |
| 82 | + throw UserError.abort("Unable to get the value of " + className + "." + fieldName); |
| 83 | + } |
| 84 | + return SubstrateObjectConstant.forBoxedValue(JavaKind.Boolean, value); |
| 85 | + } |
| 86 | + catch (Exception ex) { |
| 87 | + throw new IllegalStateException("Unable to read value from " + className + "." + fieldName, ex); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments