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

Using a Feature to specify MySampleFileSystemProvider is initialized at build time #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions child-project-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>svm</artifactId>
<version>22.0.0.2</version>
<scope>provided</scope>
</dependency>
</dependencies>

</project>
13 changes: 13 additions & 0 deletions child-project-2/src/main/java/com/example/FooClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example;

import java.util.Date;

public class FooClass {

static {
System.out.println("***** FooClass is being initialized *****");
d = new Date();
}

static final Date d;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.features;

import com.oracle.svm.core.annotate.AutomaticFeature;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;

@AutomaticFeature
final class MySampleFileSystemProviderFeature implements Feature {
private static final String clazz = "com.example.MySampleFileSystemProvider";

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
// For MySampleFileSystemProvider, this feature does not tell native-image to initialize the
// class at build time. Rather, this feature tells the analysis phase ("[2/7] Performing
// analysis... ") not to complain when the class is already initialized. It's because
// MySampleFileSystemProvider is already initialized before the initializing phase ("[1/7]
// Initializing... "), in which native-image reads user-provided feature(s).
RuntimeClassInitialization.initializeAtBuildTime(clazz);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// On the other hand, this tells the native-image to initialize com.example.FooClass at build
// time, because when native-image reads this feature, FooClass has not been initialized.
RuntimeClassInitialization.initializeAtBuildTime("com.example.FooClass");
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Args = --initialize-at-run-time=com.example \
--initialize-at-build-time=com.example.MySampleFileSystemProvider
Args = --enable-url-protocols=http -H:+PrintClassInitialization
28 changes: 28 additions & 0 deletions child-project-2/src/test/java/com/example/SimpleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.truth.Correspondence;
import java.nio.file.spi.FileSystemProvider;
import java.util.ServiceLoader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -32,4 +35,29 @@ public void testMessage(){
MySampleFileSystem.forBucket("bucket",config, gcsHelper.getOptions());
assertThat("hello1").isEqualTo("hello1");
}

@Test
public void testServiceLoader() {
ServiceLoader<FileSystemProvider> fileSystemProviders =
ServiceLoader.load(FileSystemProvider.class);
for (FileSystemProvider fileSystemProvider : fileSystemProviders) {
System.out.println("FileSystemProvider: " + fileSystemProvider.getClass().getName());
}
assertThat(fileSystemProviders)
.comparingElementsUsing(
Correspondence.transforming(
provider -> provider.getClass().getName(), "with its name == "))
.contains("com.example.MySampleFileSystemProvider");
assertThat(fileSystemProviders)
.comparingElementsUsing(
Correspondence.transforming(
(FileSystemProvider provider) -> provider.getScheme(), "with its scheme equals "))
.contains("gs");
}

@Test
public void testFoo() {
assertThat(FooClass.class.getName()).isEqualTo("com.example.FooClass");
System.out.println(FooClass.d);
}
}