Skip to content

Commit ea078f8

Browse files
authored
chore: set Java header at native image runtime (#2069)
* chore: set Java header during native image runtime
1 parent 3502186 commit ea078f8

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.google.api.client.http.UriTemplate;
3838
import com.google.api.client.util.GenericData;
3939
import com.google.api.client.util.Preconditions;
40+
import com.google.common.base.Joiner;
4041
import java.io.IOException;
4142
import java.io.InputStream;
4243
import java.io.OutputStream;
@@ -164,6 +165,16 @@ static class ApiClientVersion {
164165
}
165166

166167
public String toString() {
168+
// When running the application as a native image, append `-graalvm` to the
169+
// version.
170+
String imageCode = System.getProperty("org.graalvm.nativeimage.imagecode");
171+
if (imageCode != null && imageCode.equals("runtime")) {
172+
String[] tokens = versionString.split(" ");
173+
if (tokens.length > 0 && tokens[0].startsWith("gl-java")) {
174+
tokens[0] += "-graalvm";
175+
return Joiner.on(" ").join(tokens);
176+
}
177+
}
167178
return versionString;
168179
}
169180

google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientRequestTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ public void testSetsApiClientHeaderWithOsVersion() {
261261
assertTrue("Api version should contain the os version", version.matches(".* my-os/1.2.3"));
262262
}
263263

264+
public void testSetsApiClientHeader_NativeImage() throws IOException {
265+
System.setProperty("org.graalvm.nativeimage.imagecode", "runtime");
266+
System.setProperty("java.version", "11.0.0");
267+
String version = new ApiClientVersion().toString();
268+
assertTrue(
269+
"Api version should contain -graalvm suffix", version.matches("gl-java/11.0.0-graalvm.*"));
270+
}
271+
264272
public void testSetsApiClientHeaderWithoutOsVersion() {
265273
System.setProperty("os.name", "My OS");
266274
System.clearProperty("os.version");

0 commit comments

Comments
 (0)