Skip to content

Commit

Permalink
Allow to call none public constructor on the parent class (close #33)
Browse files Browse the repository at this point in the history
  • Loading branch information
henri-tremblay committed Jun 20, 2017
1 parent 220161b commit b5da971
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public SunReflectionFactorySerializationInstantiator(Class<T> type) {
Constructor<? super T> nonSerializableAncestorConstructor;
try {
nonSerializableAncestorConstructor = nonSerializableAncestor
.getConstructor((Class[]) null);
.getDeclaredConstructor((Class[]) null);
}
catch(NoSuchMethodException e) {
throw new ObjenesisException(new NotSerializableException(type+" has no suitable superclass constructor"));
Expand Down
32 changes: 26 additions & 6 deletions main/src/test/java/org/objenesis/ExternalizableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,40 @@
*/
package org.objenesis;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;

import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import static org.junit.Assert.*;

/**
* This test makes sure an issue mentioned is not occurring.
* This test makes sure issue #33 is not occurring.
*
* @author Henri Tremblay
*/
public class ExternalizableTest {

public static class A extends ArrayList<String> implements Externalizable {
public static class C {

public int val = 33;

protected C() {}
}

public static class B extends C implements Serializable {
public B() {
fail("B constructor shouldn't be called");
}
}

public static class A extends B implements Externalizable {

public A() {
fail("A constructor shouldn't be called");
}

public void writeExternal(ObjectOutput out) throws IOException {
Expand All @@ -43,7 +63,7 @@ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundExcept
@Test
public void test() {
A a = ObjenesisHelper.newSerializableInstance(A.class);
// This call should work because the ArrayList constructor as been called. This is required by A implementing Externalizable
a.add("Test");
// The constructor from C should have been called
assertEquals(33, a.val);
}
}

0 comments on commit b5da971

Please sign in to comment.