Skip to content

Commit 3e73b1e

Browse files
committed
svm: Support for build-time initialization of HijrahChronology
1 parent a3b7acc commit 3e73b1e

File tree

2 files changed

+87
-38
lines changed

2 files changed

+87
-38
lines changed
Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,102 @@
11
/*
2-
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
5-
* The Universal Permissive License (UPL), Version 1.0
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.
610
*
7-
* Subject to the condition set forth below, permission is hereby granted to any
8-
* person obtaining a copy of this software, associated documentation and/or
9-
* data (collectively the "Software"), free of charge and under any and all
10-
* copyright rights in the Software, and any and all patent rights owned or
11-
* freely licensable by each licensor hereunder covering either (i) the
12-
* unmodified Software as contributed to or provided by such licensor, or (ii)
13-
* the Larger Works (as defined below), to deal in both
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).
1416
*
15-
* (a) the Software, and
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.
1620
*
17-
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18-
* one is included with the Software each a "Larger Work" to which the Software
19-
* is contributed by such licensors),
20-
*
21-
* without restriction, including without limitation the rights to copy, create
22-
* derivative works of, display, perform, and distribute the Software and make,
23-
* use, sell, offer for sale, import, export, have made, and have sold the
24-
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25-
* either these or other terms.
26-
*
27-
* This license is subject to the following condition:
28-
*
29-
* The above copyright notice and either this complete permission notice or at a
30-
* minimum a reference to the UPL must be included in all copies or substantial
31-
* portions of the Software.
32-
*
33-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39-
* SOFTWARE.
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.
4024
*/
4125
package com.oracle.svm.core.jdk;
4226

43-
import org.graalvm.nativeimage.hosted.RuntimeResourceAccess;
27+
import java.nio.file.Path;
28+
import java.time.chrono.HijrahChronology;
29+
import java.util.Properties;
4430

31+
import com.oracle.svm.core.annotate.Delete;
32+
import com.oracle.svm.core.annotate.Substitute;
33+
import com.oracle.svm.core.annotate.TargetClass;
4534
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
4635
import com.oracle.svm.core.feature.InternalFeature;
4736

37+
/**
38+
* Feature to configure {@link HijrahChronology} at build time. All {@link HijrahChronology} object
39+
* that end up in the image heap are fully initialized. This has the advantage that the resources
40+
* and files for configuring the {@link HijrahChronology} variants are not needed at run time. On
41+
* the other hand, the configuration can no longer be changed after building. See
42+
* {@link HijrahChronology} for configuration option.
43+
*/
4844
@AutomaticallyRegisteredFeature
4945
public class HijrahChronologyFeature implements InternalFeature {
5046
@Override
51-
public void beforeAnalysis(BeforeAnalysisAccess access) {
52-
access.registerReachabilityHandler((e) -> RuntimeResourceAccess.addResource(Class.class.getModule(),
53-
"java/time/chrono/hijrah-config-Hijrah-umalqura_islamic-umalqura.properties"), access.findClassByName("java.time.chrono.HijrahChronology"));
47+
public void duringSetup(DuringSetupAccess access) {
48+
access.registerObjectReplacer(o -> {
49+
if (o instanceof HijrahChronology hc) {
50+
// implicitly initialize via transitive call to checkCalendarInit()
51+
hc.isLeapYear(0);
52+
}
53+
return o;
54+
});
55+
}
56+
}
57+
58+
@TargetClass(HijrahChronology.class)
59+
final class Target_java_time_chrono_HijrahChronology {
60+
// Checkstyle: stop
61+
/**
62+
* Config path with includes JAVA_HOME. We force full initialization at build time
63+
* {@link HijrahChronologyFeature#duringSetup}, so no need to keep this.
64+
*/
65+
@Delete //
66+
private static Path CONF_PATH;
67+
// Checkstyle: resume
68+
69+
/**
70+
* @see #CONF_PATH
71+
*/
72+
@Delete
73+
private static native void registerCustomChrono();
74+
75+
/**
76+
* @see #CONF_PATH
77+
*/
78+
@Delete
79+
private native void loadCalendarData();
80+
81+
/**
82+
* @see #CONF_PATH
83+
*/
84+
@Delete
85+
private static native Properties readConfigProperties(String chronologyId, String calendarType);
86+
87+
/**
88+
* @see #CONF_PATH
89+
*/
90+
@Delete //
91+
private boolean initComplete;
92+
93+
/**
94+
* No more initialization needed.
95+
*
96+
* @see #CONF_PATH
97+
*/
98+
@Substitute
99+
private void checkCalendarInit() {
100+
// initialized at build time
54101
}
55-
}
102+
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jdk/JDKInitializationFeature.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public void afterRegistration(AfterRegistrationAccess access) {
7272
rci.initializeAtBuildTime("java.nio", JDK_CLASS_REASON);
7373
rci.initializeAtBuildTime("java.text", JDK_CLASS_REASON);
7474
rci.initializeAtBuildTime("java.time", JDK_CLASS_REASON);
75+
// see HijrahChronologyFeature for more details
76+
rci.initializeAtBuildTime("java.time.chrono.HijrahChronology", "Needs to be fully initialized at build time");
7577
rci.initializeAtBuildTime("java.util", JDK_CLASS_REASON);
7678
rci.initializeAtRunTime("java.util.concurrent.SubmissionPublisher", "Executor service must be recomputed");
7779

0 commit comments

Comments
 (0)