Skip to content

Commit 4b464e2

Browse files
committed
[GR-52365] Add ConstantReflectionProvider.identityHashCode.
PullRequest: labsjdk-ce/249
2 parents f73bcb6 + ca17b0d commit 4b464e2

File tree

5 files changed

+66
-21
lines changed

5 files changed

+66
-21
lines changed

.ci/common.libsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
# The JVMCI releases that can be built from this repo.
33
jvmci_releases:: [
4-
self.JVMCIRelease(name='25.1', build='b10', jdk_version='25.0.1+8')
4+
self.JVMCIRelease(name='25.1', build='b11', jdk_version='25.0.1+8')
55
],
66

77
# Specifies a JVMCI release.

src/jdk.internal.vm.ci/share/classes/jdk/vm/ci/hotspot/HotSpotConstantReflectionProvider.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
*/
2323
package jdk.vm.ci.hotspot;
2424

25-
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
26-
27-
import java.util.Objects;
28-
2925
import jdk.vm.ci.common.JVMCIError;
3026
import jdk.vm.ci.meta.Constant;
3127
import jdk.vm.ci.meta.ConstantReflectionProvider;
@@ -36,6 +32,10 @@
3632
import jdk.vm.ci.meta.ResolvedJavaField;
3733
import jdk.vm.ci.meta.ResolvedJavaType;
3834

35+
import java.util.Objects;
36+
37+
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
38+
3939
/**
4040
* HotSpot implementation of {@link ConstantReflectionProvider}.
4141
*/
@@ -190,4 +190,16 @@ public Constant asObjectHub(ResolvedJavaType type) {
190190
throw JVMCIError.unimplemented();
191191
}
192192
}
193+
194+
@Override
195+
public Integer identityHashCode(JavaConstant constant) {
196+
JavaKind kind = Objects.requireNonNull(constant).getJavaKind();
197+
if (kind != JavaKind.Object) {
198+
throw new IllegalArgumentException("Constant has unexpected kind " + kind + ": " + constant);
199+
} else if (constant.isNull()) {
200+
/* System.identityHashCode is specified to return 0 when passed null. */
201+
return 0;
202+
}
203+
return ((HotSpotObjectConstant) constant).getIdentityHashCode();
204+
}
193205
}

src/jdk.internal.vm.ci/share/classes/jdk/vm/ci/hotspot/HotSpotObjectConstantImpl.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
*/
2323
package jdk.vm.ci.hotspot;
2424

25-
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
26-
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
27-
2825
import jdk.vm.ci.meta.Assumptions;
2926
import jdk.vm.ci.meta.JavaConstant;
3027
import jdk.vm.ci.meta.JavaKind;
3128
import jdk.vm.ci.meta.ResolvedJavaType;
3229

30+
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
31+
import static jdk.vm.ci.services.Services.IS_IN_NATIVE_IMAGE;
32+
3333
/**
3434
* Represents a constant non-{@code null} object reference, within the compiler and across the
3535
* compiler/runtime interface.
@@ -57,20 +57,11 @@ public boolean isCompressible() {
5757
return !compressed;
5858
}
5959

60-
@Override
61-
public abstract JavaConstant compress();
62-
63-
@Override
64-
public abstract JavaConstant uncompress();
65-
6660
@Override
6761
public HotSpotResolvedObjectType getType() {
6862
return runtime().reflection.getType(this);
6963
}
7064

71-
@Override
72-
public abstract int getIdentityHashCode();
73-
7465
private boolean isFullyInitializedConstantCallSite() {
7566
if (!runtime().getConstantCallSite().isInstance(this)) {
7667
return false;

src/jdk.internal.vm.ci/share/classes/jdk/vm/ci/meta/ConstantReflectionProvider.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -71,15 +71,15 @@ public interface ConstantReflectionProvider {
7171
/**
7272
* Converts the given {@link JavaKind#isPrimitive() primitive} constant to a boxed
7373
* {@link JavaKind#Object object} constant, according to the Java boxing rules. Returns
74-
* {@code null} if the source is is not a primitive constant, or the boxed value is not
74+
* {@code null} if the source is not a primitive constant, or the boxed value is not
7575
* available at this point.
7676
*/
7777
JavaConstant boxPrimitive(JavaConstant source);
7878

7979
/**
8080
* Converts the given {@link JavaKind#Object object} constant to a {@link JavaKind#isPrimitive()
8181
* primitive} constant, according to the Java unboxing rules. Returns {@code null} if the source
82-
* is is not an object constant that can be unboxed, or the unboxed value is not available at
82+
* is not an object constant that can be unboxed, or the unboxed value is not available at
8383
* this point.
8484
*/
8585
JavaConstant unboxPrimitive(JavaConstant source);
@@ -116,4 +116,15 @@ public interface ConstantReflectionProvider {
116116
* type representation which is typically stored in the object header.
117117
*/
118118
Constant asObjectHub(ResolvedJavaType type);
119+
120+
/**
121+
* Gets the identity hash code of the object value represented by {@code constant}.
122+
* <p>
123+
* For the {@link JavaConstant#isNull() null constant}, this method returns zero as specified by
124+
* {@link System#identityHashCode(Object)}.
125+
*
126+
* @throws NullPointerException if {@code constant == null}
127+
* @throws IllegalArgumentException if {@code constant.getJavaKind() != JavaKind.Object}
128+
*/
129+
Integer identityHashCode(JavaConstant constant);
119130
}

test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestConstantReflectionProvider.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@
4141
import java.lang.reflect.Array;
4242
import java.util.List;
4343

44-
import static org.junit.Assert.*;
44+
import static org.junit.Assert.assertEquals;
45+
import static org.junit.Assert.assertFalse;
46+
import static org.junit.Assert.assertNotNull;
47+
import static org.junit.Assert.assertNull;
48+
import static org.junit.Assert.assertThrows;
49+
import static org.junit.Assert.assertTrue;
50+
import static org.junit.Assert.fail;
4551

4652
/**
4753
* Tests for {@link ConstantReflectionProvider}. It assumes an implementation of the interface that
@@ -92,6 +98,31 @@ static class BoxedConstants {
9298
static final Boolean BOOL_CONST = true;
9399
}
94100

101+
@Test
102+
public void identityHashCodeTest() {
103+
try {
104+
constantReflection.identityHashCode(null);
105+
fail("Expected NullPointerException");
106+
} catch (NullPointerException e) {
107+
// Expected
108+
}
109+
assertEquals(Integer.valueOf(0), constantReflection.identityHashCode(JavaConstant.NULL_POINTER));
110+
for (ConstantValue cv : constants()) {
111+
if (cv.value.getJavaKind() != JavaKind.Object) {
112+
try {
113+
constantReflection.identityHashCode(cv.value);
114+
fail("Expected IllegalArgumentException for " + cv);
115+
} catch (IllegalArgumentException e) {
116+
// Expected
117+
}
118+
} else if (cv.boxed != cv.value) {
119+
int expect = System.identityHashCode(cv.boxed);
120+
int actual = constantReflection.identityHashCode(cv.value);
121+
assertEquals(cv.toString(), expect, actual);
122+
}
123+
}
124+
}
125+
95126
@Test
96127
public void boxTest() {
97128
for (ConstantValue cv : constants()) {

0 commit comments

Comments
 (0)