Skip to main content

Bug(?) in java.bean.Statement

No replies
lidierth
Offline
Joined: 2010-11-04
Points: 0

This issue was reported before, but has disappeared from the Oracle bug site (bug_id=7195727).

Is it a bug?

The java.lang.Statement invoke (or more recently invokeInternal) method states that

221        if (target instanceof Class) {
222            /*
223            For class methods, simluate the effect of a meta class
224            by taking the union of the static methods of the
225            actual class, with the instance methods of "Class.class"
226            and the overloaded "newInstance" methods defined by the
227            constructors.
228            This way "System.class", for example, will perform both
229            the static method getProperties() and the instance method
230            getSuperclass() defined in "Class.class".
231            */
232            if (methodName.equals("new")) {
233                methodName = "newInstance";
234            }

The result is that where I would expect the null constructor to be called, a static "newInstance" call will be made for a class containing such a method. So for this xml:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_11" class="java.beans.XMLDecoder">
<object class="kcl.waterloo.test.TestClass"/>
</java>

XMLDecoder invokes the static newInstance method not the null constructor for the following test class:

public class TestClass {



public TestClass() {
}

public static TestClass newInstance() {
    System.err.println("Creating instance using static 'newInstance' methed");
    return new TestClass();
}


//Create a test file
public static void doTestSave() throws FileNotFoundException, IOException {
    BufferedOutputStream buffer = new BufferedOutputStream(new java.io.FileOutputStream("Test.xml"));
    XMLEncoder e = new XMLEncoder(buffer);
    Thread.currentThread().setContextClassLoader(TestClass.class.getClassLoader());
    TestClass t = new TestClass();
    e.writeObject(t);
    e.close();
    buffer.close();
}

// Deserialize the test file
public static Object loadTestFile() throws FileNotFoundException, IOException {
    BufferedInputStream buffer = new BufferedInputStream(new FileInputStream("Test.xml"));
    XMLDecoder e = new XMLDecoder(buffer);
    e.close();
    buffer.close();
    return e.readObject();
}

}

Is this a bug?