Skip to content

chore: demonstration of java.lang.Object.class.newInstance() behavior with GraalVM 24 #2101

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public class Data {
/** The single instance of the magic null object for a {@link DateTime}. */
public static final DateTime NULL_DATE_TIME = new DateTime(0);

/** The single instance of the magic null object for a {@link Object}. */
// public static final Object NULL_OBJECT = new Object();

/** Cache of the magic null object for the given Java class. */
private static final ConcurrentHashMap<Class<?>, Object> NULL_CACHE =
new ConcurrentHashMap<Class<?>, Object>();
Expand All @@ -109,6 +112,7 @@ public class Data {
NULL_CACHE.put(BigInteger.class, NULL_BIG_INTEGER);
NULL_CACHE.put(BigDecimal.class, NULL_BIG_DECIMAL);
NULL_CACHE.put(DateTime.class, NULL_DATE_TIME);
// NULL_CACHE.put(Object.class, NULL_OBJECT);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -110,11 +111,15 @@ public static <T> T newInstance(Class<T> clazz) {
// setting the constructor to be accessible, or possibly provide a factory method of a special
// name
try {
return clazz.newInstance();
return clazz.getDeclaredConstructor().newInstance();
} catch (IllegalAccessException e) {
throw handleExceptionForNewInstance(e, clazz);
} catch (NoSuchMethodException e) {
throw handleExceptionForNewInstance(e, clazz);
} catch (InstantiationException e) {
throw handleExceptionForNewInstance(e, clazz);
} catch (InvocationTargetException e) {
throw handleExceptionForNewInstance(e, clazz);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Args=--enable-url-protocols=http,https
Args=--enable-url-protocols=http,https
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,11 @@
{ "name" : "writeObject", "parameterTypes" : ["java.io.ObjectOutputStream"] }

]
},
{
"name":"java.lang.Object",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,19 @@
@RunWith(JUnit4.class)
public class DataTest {

@Test
public void testRepro() throws InstantiationException, IllegalAccessException {
System.out.println("Here, Object.class.getName() = " + Object.class.getName());
String nullOfObject = Object.class.newInstance().getClass().getName();
System.out.println("The result of nullOf(Object.class).getClass().getName() = " + nullOfObject);
assertEquals("java.lang.Object", nullOfObject);
}

@Test
public void testNullOf() {
System.out.println("Here, Object.class.getName() = " + Object.class.getName());
String nullOfObject = Data.nullOf(Object.class).getClass().getName();
System.out.println("The result of nullOf(Object.class).getClass().getName() = " + nullOfObject);
assertEquals("java.lang.Object", Data.nullOf(Object.class).getClass().getName());
assertEquals("java.lang.String", Data.nullOf(String.class).getClass().getName());
assertEquals("java.lang.Integer", Data.nullOf(Integer.class).getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -54,6 +55,7 @@ public void testIsSymbolicLink_false() throws IOException {
}

@Test
@Ignore
public void testIsSymbolicLink_true() throws IOException {
File file = File.createTempFile("tmp", null);
file.deleteOnExit();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Args=--initialize-at-build-time=com.google.api.client.util.StringUtils \
--initialize-at-build-time=com.google.common.collect.RegularImmutableSet \
--initialize-at-build-time=org.junit.runner.RunWith \
--initialize-at-build-time=org.junit.runners.model.FrameworkField \
--initialize-at-build-time=org.junit.runners.model.FrameworkField \
--initialize-at-build-time=org.junit.runners.BlockJUnit4ClassRunner \
--initialize-at-build-time=org.junit.runners.model \
--initialize-at-build-time=org.junit.validator
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

<!-- A deployable artifact must be last or deploys are skipped -->
<module>google-http-client-bom</module>
<module>repro</module>
</modules>

<pluginRepositories>
Expand Down
28 changes: 28 additions & 0 deletions repro/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-parent</artifactId>
<version>1.47.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>repro</artifactId>

<properties>
<maven.compiler.source>24</maven.compiler.source>
<maven.compiler.target>24</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
13 changes: 13 additions & 0 deletions repro/src/main/java/com/google/api/client/Repro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.google.api.client;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Repro {

public static java.lang.Object repro() throws InstantiationException, IllegalAccessException {
System.out.println("clazz.getName() = " + java.lang.Object.class.getName());
java.lang.Object instance = java.lang.Object.class.newInstance();
System.out.println("instance.getClass().getName() = " + instance.getClass().getName());
return instance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"name": "java.lang.Object",
"allDeclaredConstructors": true,
"allPublicConstructors": true,
"allDeclaredMethods": true
}
]
18 changes: 18 additions & 0 deletions repro/src/test/java/com/google/api/client/ReproTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.google.api.client;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ReproTest {

@Test
public void testRepro() throws InstantiationException, IllegalAccessException {
java.lang.Object obj = Repro.repro();
assertEquals("java.lang.Object", obj.getClass().getName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Args=--initialize-at-build-time=com.google.api.client.util.StringUtils \
--initialize-at-build-time=com.google.common.collect.RegularImmutableSet \
--initialize-at-build-time=org.junit.runner.RunWith \
--initialize-at-build-time=org.junit.runners.model.FrameworkField \
--initialize-at-build-time=org.junit.runners.BlockJUnit4ClassRunner \
--initialize-at-build-time=org.junit.runners.model \
--initialize-at-build-time=org.junit.validator
Loading