Skip to content
Permalink
Browse files
8275137: jdk.unsupported/sun.reflect.ReflectionFactory.readObjectNoDa…
…taForSerialization uses wrong signature

Reviewed-by: dfuchs
  • Loading branch information
FrauBoes committed Oct 26, 2021
1 parent 174f553 commit 4961373a676126cd557f92a2e7bbc8c66b2976b1
Showing 3 changed files with 22 additions and 24 deletions.
@@ -478,7 +478,7 @@ public final MethodHandle readObjectForSerialization(Class<?> cl) {
}

public final MethodHandle readObjectNoDataForSerialization(Class<?> cl) {
return findReadWriteObjectForSerialization(cl, "readObjectNoData", ObjectInputStream.class);
return findReadWriteObjectForSerialization(cl, "readObjectNoData", null);
}

public final MethodHandle writeObjectForSerialization(Class<?> cl) {
@@ -493,7 +493,8 @@ private final MethodHandle findReadWriteObjectForSerialization(Class<?> cl,
}

try {
Method meth = cl.getDeclaredMethod(methodName, streamClass);
Method meth = streamClass == null ? cl.getDeclaredMethod(methodName)
: cl.getDeclaredMethod(methodName, streamClass);
int mods = meth.getModifiers();
if (meth.getReturnType() != Void.TYPE ||
Modifier.isStatic(mods) ||
@@ -144,9 +144,9 @@ public final MethodHandle readObjectForSerialization(Class<?> cl) {
/**
* Returns a direct MethodHandle for the {@code readObjectNoData} method on
* a Serializable class.
* The first argument of {@link MethodHandle#invoke} is the serializable
* object and the second argument is the {@code ObjectInputStream} passed to
* {@code readObjectNoData}.
* The only argument of {@link MethodHandle#invoke} is the serializable
* object, which {@code readObjectNoData} is called on. No arguments are
* passed to the {@code readObjectNoData} method.
*
* @param cl a Serializable class
* @return a direct MethodHandle for the {@code readObjectNoData} method
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.OptionalDataException;
import java.io.Serializable;
import java.lang.invoke.MethodHandle;
@@ -45,11 +46,11 @@

/*
* @test
* @bug 8137058 8164908 8168980
* @run testng ReflectionFactoryTest
* @run testng/othervm/policy=security.policy ReflectionFactoryTest
* @bug 8137058 8164908 8168980 8275137
* @summary Basic test for the unsupported ReflectionFactory
* @modules jdk.unsupported
* @run testng ReflectionFactoryTest
* @run testng/othervm/policy=security.policy ReflectionFactoryTest
*/

public class ReflectionFactoryTest {
@@ -81,8 +82,7 @@ static void init() {
*/
@Test(dataProvider="ClassConstructors")
static void testConstructor(Class<?> type)
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException
throws InstantiationException, IllegalAccessException, InvocationTargetException
{
@SuppressWarnings("unchecked")
Constructor<?> c = factory.newConstructorForSerialization(type);
@@ -131,8 +131,8 @@ static void testNonSerializableConstructor(Class<?> cl,
}

Assert.assertEquals(((Foo)o).foo(), expectedFoo);
if (o instanceof Baz) {
Assert.assertEquals(((Baz)o).baz(), expectedBaz);
if (o instanceof Baz b) {
Assert.assertEquals(b.baz(), expectedBaz);
}
}

@@ -178,16 +178,15 @@ static class Baz extends Foo {
}

/**
* Test newConstructorForExternalization returns the constructor and it can be called.
* Tests that newConstructorForExternalization returns the constructor and it can be called.
* @throws NoSuchMethodException - error
* @throws InstantiationException - error
* @throws IllegalAccessException - error
* @throws InvocationTargetException - error
*/
@Test
static void newConstructorForExternalization()
throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException {
throws InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor<?> cons = factory.newConstructorForExternalization(Ext.class);
Ext ext = (Ext)cons.newInstance();
Assert.assertEquals(ext.ext, 1, "Constructor not run");
@@ -251,7 +250,7 @@ static void testReadWriteObjectForSerialization() throws Throwable {
Assert.assertFalse(ser2.readObjectNoDataCalled, "readObjectNoData should not be called");
Assert.assertFalse(ser2.readResolveCalled, "readResolve should not be called");

readObjectNoDataMethod.invoke(ser2, ois);
readObjectNoDataMethod.invoke(ser2);
Assert.assertTrue(ser2.readObjectCalled, "readObject should have been called");
Assert.assertTrue(ser2.readObjectNoDataCalled, "readObjectNoData not called");
Assert.assertFalse(ser2.readResolveCalled, "readResolve should not be called");
@@ -283,12 +282,12 @@ static class Ser implements Serializable {

public Ser() {}

private void readObject(ObjectInputStream ois) throws IOException {
private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
Assert.assertFalse(writeObjectCalled, "readObject called too many times");
readObjectCalled = ois.readBoolean();
}

private void readObjectNoData(ObjectInputStream ois) throws IOException {
private void readObjectNoData() throws ObjectStreamException {
Assert.assertFalse(readObjectNoDataCalled, "readObjectNoData called too many times");
readObjectNoDataCalled = true;
}
@@ -299,21 +298,21 @@ private void writeObject(ObjectOutputStream oos) throws IOException {
oos.writeBoolean(writeObjectCalled);
}

private Object writeReplace() {
private Object writeReplace() throws ObjectStreamException {
Assert.assertFalse(writeReplaceCalled, "writeReplace called too many times");
writeReplaceCalled = true;
return this;
}

private Object readResolve() {
private Object readResolve() throws ObjectStreamException {
Assert.assertFalse(readResolveCalled, "readResolve called too many times");
readResolveCalled = true;
return this;
}
}

/**
* Test the constructor of OptionalDataExceptions.
* Tests the constructor of OptionalDataExceptions.
*/
@Test
static void newOptionalDataException() {
@@ -324,8 +323,6 @@ static void newOptionalDataException() {

}



// Main can be used to run the tests from the command line with only testng.jar.
@SuppressWarnings("raw_types")
@Test(enabled = false)

1 comment on commit 4961373

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 4961373 Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.