|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. Oracle designates this |
| 9 | + * particular file as subject to the "Classpath" exception as provided |
| 10 | + * by Oracle in the LICENSE file that accompanied this code. |
| 11 | + * |
| 12 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 16 | + * accompanied this code). |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License version |
| 19 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 20 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | + * |
| 22 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 23 | + * or visit www.oracle.com if you need additional information or have any |
| 24 | + * questions. |
| 25 | + */ |
| 26 | +package com.oracle.svm.hosted; |
| 27 | + |
| 28 | +import com.oracle.svm.core.annotate.AutomaticFeature; |
| 29 | +import com.oracle.svm.core.graal.GraalFeature; |
| 30 | +import com.oracle.svm.core.jdk.SystemPropertiesSupport; |
| 31 | +import jdk.vm.ci.meta.ResolvedJavaMethod; |
| 32 | +import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; |
| 33 | +import org.graalvm.compiler.nodes.ValueNode; |
| 34 | +import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; |
| 35 | +import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin; |
| 36 | +import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; |
| 37 | +import org.graalvm.compiler.phases.util.Providers; |
| 38 | + |
| 39 | +import java.util.Set; |
| 40 | +import java.util.concurrent.ConcurrentHashMap; |
| 41 | + |
| 42 | +@AutomaticFeature |
| 43 | +public class SystemPropertyFeature implements GraalFeature { |
| 44 | + |
| 45 | + private final Set<String> systemPropertyCalls = ConcurrentHashMap.newKeySet(); |
| 46 | + |
| 47 | + @Override |
| 48 | + public void afterImageWrite(AfterImageWriteAccess access) { |
| 49 | + if (!systemPropertyCalls.isEmpty()) { |
| 50 | + StringBuilder sb = new StringBuilder("\n\nNative image incompatible system properties are used in the program:\n"); |
| 51 | + for (String s : systemPropertyCalls) { |
| 52 | + sb.append(" ").append(s).append("\n"); |
| 53 | + } |
| 54 | + sb.append("The returned value of calling System.getProperty with these keys will be null in native image.\n"); |
| 55 | + sb.append("Please make sure the expected property values are specified with -D when running the native image program or just avoid using them.\n\n"); |
| 56 | + System.out.println(sb.toString()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void registerInvocationPlugins(Providers providers, SnippetReflectionProvider snippetReflection, InvocationPlugins invocationPlugins, boolean analysis, boolean hosted) { |
| 62 | + InvocationPlugins.Registration r = new InvocationPlugins.Registration(invocationPlugins, System.class); |
| 63 | + r.register1("getProperty", String.class, new InvocationPlugin() { |
| 64 | + @Override |
| 65 | + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode name) { |
| 66 | + if (name.isConstant()) { |
| 67 | + String propertyKey = snippetReflection.asObject(String.class, name.asJavaConstant()); |
| 68 | + if (SystemPropertiesSupport.isIncompatible(propertyKey)) { |
| 69 | + systemPropertyCalls.add("Incompatible property:" + propertyKey + " is used at " + b.getCode().asStackTraceElement(b.bci())); |
| 70 | + } |
| 71 | + } |
| 72 | + return false; |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
0 commit comments