Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-60094] Add demo for Specify Class Initialization Explicitly guide. #314

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/native-image-specify-class-init.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions native-image/specify-class-init/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.class
buildtime-parser
runtime-parser
3 changes: 3 additions & 0 deletions native-image/specify-class-init/README.md
Original file line number Diff line number Diff line change
@@ -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/).
69 changes: 69 additions & 0 deletions native-image/specify-class-init/TalkParser.java
Original file line number Diff line number Diff line change
@@ -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<Talk> 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) {}
10 changes: 10 additions & 0 deletions native-image/specify-class-init/run.sh
Original file line number Diff line number Diff line change
@@ -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
Loading