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 1 commit
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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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) {
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.

}
}
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,15 @@

import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.testing.RemoteStorageHelper;
import com.google.common.truth.Correspondence;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.spi.FileSystemProvider;
import java.util.List;
import java.util.ServiceLoader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -32,4 +41,23 @@ 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");
}
}