From a2f27834c49cd706a4d8cad80596b66355884402 Mon Sep 17 00:00:00 2001 From: Olya Gupalo Date: Tue, 10 Dec 2024 10:25:04 +0200 Subject: [PATCH] Add demo for Specify Class Initialization Explicitly guide --- .../native-image-specify-class-init.yml | 35 ++++++++++ native-image/specify-class-init/.gitignore | 3 + native-image/specify-class-init/README.md | 3 + .../specify-class-init/TalkParser.java | 69 +++++++++++++++++++ native-image/specify-class-init/run.sh | 10 +++ 5 files changed, 120 insertions(+) create mode 100644 .github/workflows/native-image-specify-class-init.yml create mode 100644 native-image/specify-class-init/.gitignore create mode 100644 native-image/specify-class-init/README.md create mode 100644 native-image/specify-class-init/TalkParser.java create mode 100755 native-image/specify-class-init/run.sh diff --git a/.github/workflows/native-image-specify-class-init.yml b/.github/workflows/native-image-specify-class-init.yml new file mode 100644 index 000000000..5401b6122 --- /dev/null +++ b/.github/workflows/native-image-specify-class-init.yml @@ -0,0 +1,35 @@ +name: native-image/specify-class-init +on: + push: + paths: + - 'native-image/specify-class-init/**' + - '.github/workflows/native-image-specify-class-init.yml' + pull_request: + paths: + - 'native-image/specify-class-init/**' + - '.github/workflows/native-image-specify-class-init.yml' + schedule: + - cron: "0 0 1 * *" # run every month + workflow_dispatch: +permissions: + contents: read +jobs: + run: + name: Run 'native-image/specify-class-init' + runs-on: ubuntu-latest + timeout-minutes: 15 + strategy: + matrix: + java-version: ['21', '24-ea'] + steps: + - uses: actions/checkout@v4 + - uses: graalvm/setup-graalvm@v1 + with: + java-version: ${{ matrix.java-version }} + distribution: 'graalvm' + github-token: ${{ secrets.GITHUB_TOKEN }} + native-image-job-reports: 'true' + - name: Run 'native-image/specify-class-init' + run: | + cd native-image/specify-class-init + ./run.sh \ No newline at end of file diff --git a/native-image/specify-class-init/.gitignore b/native-image/specify-class-init/.gitignore new file mode 100644 index 000000000..4550c598d --- /dev/null +++ b/native-image/specify-class-init/.gitignore @@ -0,0 +1,3 @@ +.class +buildtime-parser +runtime-parser \ No newline at end of file diff --git a/native-image/specify-class-init/README.md b/native-image/specify-class-init/README.md new file mode 100644 index 000000000..2780601c3 --- /dev/null +++ b/native-image/specify-class-init/README.md @@ -0,0 +1,3 @@ +# Specify Class Initialization Explicitly + +You can find the steps to run this demo on [the website](https://www.graalvm.org/jdk23/reference-manual/native-image/guides/specify-class-initialization/). \ No newline at end of file diff --git a/native-image/specify-class-init/TalkParser.java b/native-image/specify-class-init/TalkParser.java new file mode 100644 index 000000000..c9a7e726a --- /dev/null +++ b/native-image/specify-class-init/TalkParser.java @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * The Universal Permissive License (UPL), Version 1.0 + * + * Subject to the condition set forth below, permission is hereby granted to any + * person obtaining a copy of this software, associated documentation and/or + * data (collectively the "Software"), free of charge and under any and all + * copyright rights in the Software, and any and all patent rights owned or + * freely licensable by each licensor hereunder covering either (i) the + * unmodified Software as contributed to or provided by such licensor, or (ii) + * the Larger Works (as defined below), to deal in both + * + * (a) the Software, and + * + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if + * one is included with the Software each a "Larger Work" to which the Software + * is contributed by such licensors), + * + * without restriction, including without limitation the rights to copy, create + * derivative works of, display, perform, and distribute the Software and make, + * use, sell, offer for sale, import, export, have made, and have sold the + * Software and the Larger Work(s), and to sublicense the foregoing rights on + * either these or other terms. + * + * This license is subject to the following condition: + * + * The above copyright notice and either this complete permission notice or at a + * minimum a reference to the UPL must be included in all copies or substantial + * portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class TalkParser { +private static final List TALKS = new ArrayList<>(); +static { + Scanner s = new Scanner(""" + Asynchronous Programming in Java: Options to Choose from by Venkat Subramaniam + Anatomy of a Spring Boot App with Clean Architecture by Steve Pember + Java in the Cloud with GraalVM by Alina Yurenko + Bootiful Spring Boot 3 by Josh Long + """); + while (s.hasNextLine()) { + TALKS.add(new Talk(s.nextLine())); + } + s.close(); +} + +public static void main(String[] args) { + System.out.println("Talks loaded using scanner:"); + for (Talk talk : TALKS) { + System.out.println("- " + talk.name()); + } + } +} + +record Talk (String name) {} \ No newline at end of file diff --git a/native-image/specify-class-init/run.sh b/native-image/specify-class-init/run.sh new file mode 100755 index 000000000..8ceae7280 --- /dev/null +++ b/native-image/specify-class-init/run.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -ex + +javac TalkParser.java +native-image -Ob -o runtime-parser TalkParser +time ./runtime-parser +du -sh runtime-parser +native-image -Ob --initialize-at-build-time=TalkParser,Talk -o buildtime-parser TalkParser +time ./buildtime-parser +du -sh buildtime-parser \ No newline at end of file