Skip to content

Commit a9cb69d

Browse files
committed
change sun.misc.Unsafe to jdk.internal.misc.Unsafe
1 parent 2dc3a5a commit a9cb69d

File tree

5 files changed

+63
-50
lines changed

5 files changed

+63
-50
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@
712712
<artifactId>maven-compiler-plugin</artifactId>
713713
<version>${version.plugin.maven.compiler}</version>
714714
<configuration>
715-
<release>${version.java.target}</release>
715+
<target>${version.java.target}</target>
716716
</configuration>
717717
</plugin>
718718
<plugin>

xstream/pom.xml

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@
258258

259259
<profiles>
260260
<profile>
261-
<id>jdk17</id>
261+
<id>jdk17-ge</id>
262262
<activation>
263-
<jdk>17</jdk>
263+
<jdk>[17,)</jdk>
264264
</activation>
265265
<build>
266266
<plugins>
@@ -274,47 +274,30 @@
274274
<excludePackageNames>com.thoughtworks.xstream.core.util</excludePackageNames>
275275
</configuration>
276276
</plugin>
277-
</plugins>
278-
</build>
279-
</profile>
280-
<profile>
281-
<id>jdk16-ge</id>
282-
<activation>
283-
<jdk>[16,)</jdk>
284-
</activation>
285-
<build>
286-
<plugins>
287277
<plugin>
288278
<groupId>org.apache.maven.plugins</groupId>
289279
<artifactId>maven-compiler-plugin</artifactId>
290-
<configuration>
291-
<testExcludes>
292-
<exclude>**/Record**</exclude>
293-
</testExcludes>
294-
</configuration>
295-
<executions>
296-
<execution>
297-
<id>compile-jdk16</id>
298-
<configuration>
299-
<release>16</release>
300-
<testExcludes combine.self="override" />
301-
<testIncludes>
302-
<include>**/Record**</include>
303-
</testIncludes>
304-
</configuration>
305-
<goals>
306-
<goal>testCompile</goal>
307-
</goals>
308-
</execution>
309-
</executions>
310-
</plugin>
280+
<executions>
281+
<execution>
282+
<id>compile-jdk17</id>
283+
<configuration>
284+
<target>17</target>
285+
<source>17</source>
286+
</configuration>
287+
<goals>
288+
<goal>testCompile</goal>
289+
</goals>
290+
</execution>
291+
</executions>
292+
</plugin>
311293
</plugins>
312294
</build>
313295
</profile>
296+
314297
<profile>
315-
<id>jdk15</id>
298+
<id>jdk15-ge</id>
316299
<activation>
317-
<jdk>15</jdk>
300+
<jdk>[15,17)</jdk>
318301
</activation>
319302
<build>
320303
<plugins>
@@ -330,11 +313,8 @@
330313
<execution>
331314
<id>compile-jdk15</id>
332315
<configuration>
333-
<release>15</release>
334-
<testExcludes combine.self="override" />
335-
<testIncludes>
336-
<include>**/Record**</include>
337-
</testIncludes>
316+
<target>15</target>
317+
<source>15</source>
338318
<compilerArgs>
339319
<arg>--enable-preview</arg>
340320
</compilerArgs>
@@ -370,6 +350,7 @@
370350
</plugins>
371351
</build>
372352
</profile>
353+
373354
<profile>
374355
<id>jdk11-ge</id>
375356
<activation>
@@ -381,8 +362,15 @@
381362
<groupId>org.apache.maven.plugins</groupId>
382363
<artifactId>maven-compiler-plugin</artifactId>
383364
<configuration>
365+
<target>11</target>
366+
<source>11</source>
367+
<testExcludes>
368+
<exclude>**/Record**</exclude>
369+
</testExcludes>
384370
<compilerArgs>
385371
<arg>-XDignore.symbol.file</arg>
372+
<arg>--add-exports</arg>
373+
<arg>java.base/jdk.internal.misc=ALL-UNNAMED</arg>
386374
</compilerArgs>
387375
</configuration>
388376
</plugin>
@@ -468,6 +456,7 @@
468456
--add-opens java.xml/com.sun.xml.internal.stream=ALL-UNNAMED
469457
--add-opens java.xml/com.sun.org.apache.xerces.internal.parsers=ALL-UNNAMED
470458
--add-opens java.xml/com.sun.org.apache.xerces.internal.util=ALL-UNNAMED
459+
--add-opens java.base/jdk.internal.misc=ALL-UNNAMED
471460
</surefire.argline>
472461
<surefire.preview.arg/>
473462
</properties>

xstream/src/java/com/thoughtworks/xstream/converters/reflection/SunLimitedUnsafeReflectionProvider.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
import com.thoughtworks.xstream.converters.ConversionException;
1313
import com.thoughtworks.xstream.converters.ErrorWritingException;
1414

15-
import sun.misc.Unsafe;
16-
15+
import jdk.internal.misc.Unsafe;
1716

1817
/**
1918
* Instantiates a new object bypassing the constructor using undocumented internal JDK features.
2019
* <p>
2120
* The code in the constructor will never be executed and parameters do not have to be known. This is the same method
22-
* used by the internals of standard Java serialization, but relies on internal code (sun.misc.Unsafe) that may not be
23-
* present on all JVMs.
21+
* used by the internals of standard Java serialization, but relies on internal code (jdk.internal.misc.Unsafe).
2422
* <p>
2523
* <p>
2624
* The implementation will use standard Java functionality to write any fields. This requires Java 5 as minimum runtime

xstream/src/java/com/thoughtworks/xstream/converters/reflection/SunUnsafeReflectionProvider.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111
*/
1212
package com.thoughtworks.xstream.converters.reflection;
1313

14+
import com.thoughtworks.xstream.core.JVM;
15+
1416
import java.lang.reflect.Field;
17+
import java.lang.reflect.InvocationTargetException;
18+
import java.lang.reflect.Method;
1519
import java.util.concurrent.ConcurrentHashMap;
1620
import java.util.concurrent.ConcurrentMap;
1721

18-
1922
/**
2023
* Instantiates a new object bypassing the constructor using undocumented internal JDK features.
2124
* <p>
2225
* The code in the constructor will never be executed and parameters do not have to be known. This is the same method
23-
* used by the internals of standard Java serialization, but relies on internal code (sun.misc.Unsafe) that may not be
24-
* present on all JVMs.
26+
* used by the internals of standard Java serialization, but relies on internal code (jdk.internal.misc.Unsafe).
2527
* <p>
2628
* <p>
2729
* The implementation will use the same internals to write into fields. This is a lot faster and was additionally the
@@ -38,6 +40,7 @@ public class SunUnsafeReflectionProvider extends SunLimitedUnsafeReflectionProvi
3840
// references to the Field key are kept in the FieldDictionary
3941
private transient ConcurrentMap<Field, Long> fieldOffsetCache;
4042

43+
private static final Method PUT_REFERENCE_UNSAFE_METHOD = getPutReferenceMethod();
4144
/**
4245
* @since 1.4.7
4346
*/
@@ -91,7 +94,7 @@ private void write(final Field field, final Object object, final Object value) {
9194
throw ex;
9295
}
9396
} else {
94-
unsafe.putObject(object, offset, value);
97+
putReferenceUnsafe(object, field, offset, value);
9598
}
9699

97100
} catch (final IllegalArgumentException e) {
@@ -116,6 +119,29 @@ private Object readResolve() {
116119
return this;
117120
}
118121

122+
private static Method getPutReferenceMethod() {
123+
Class<?> unsafeClass = unsafe.getClass();
124+
String methodName = "putObject";
125+
if(JVM.isVersion(12)) {
126+
methodName = "putReference";
127+
}
128+
try {
129+
return unsafeClass.getDeclaredMethod(methodName, Object.class, long.class, Object.class);
130+
} catch (NoSuchMethodException e) {
131+
throw new RuntimeException(e);
132+
}
133+
}
134+
135+
private void putReferenceUnsafe(Object object, Field field, long offset, Object value) {
136+
try {
137+
PUT_REFERENCE_UNSAFE_METHOD.invoke(unsafe, object, offset, value);
138+
} catch (IllegalAccessException | InvocationTargetException e) {
139+
final ObjectAccessException ex = new ObjectAccessException("Cannot set field", e);
140+
ex.add("field", object.getClass() + "." + field.getName());
141+
throw ex;
142+
}
143+
}
144+
119145
@Override
120146
protected void init() {
121147
super.init();

xstream/src/java/com/thoughtworks/xstream/core/JVM.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static class Test {
9595
boolean test = true;
9696
Object unsafe = null;
9797
try {
98-
final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
98+
final Class<?> unsafeClass = Class.forName("jdk.internal.misc.Unsafe");
9999
final Field unsafeField = unsafeClass.getDeclaredField("theUnsafe");
100100
unsafeField.setAccessible(true);
101101
unsafe = unsafeField.get(null);

0 commit comments

Comments
 (0)