javaee / jaxb-v2

Other
211 stars 101 forks source link

NPE because of the DummyListField.$get #1058

Open glassfishrobot opened 9 years ago

glassfishrobot commented 9 years ago

Today I've spent a few hours hunting an NPE bug.

I was trying to add the generateMixedExtensions="true" feature to a working project and started getting an NPE:

Caused by: java.lang.NullPointerException
    at com.sun.codemodel.JInvocation.generate(JInvocation.java:176)
    at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
    at com.sun.codemodel.JFormatter.g(JFormatter.java:363)
    at com.sun.codemodel.JInvocation.generate(JInvocation.java:185)
    at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
    at com.sun.codemodel.JAssignment.generate(JAssignment.java:65)
    at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
    at com.sun.codemodel.JAssignment.state(JAssignment.java:69)
    at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
    at com.sun.codemodel.JBlock.generateBody(JBlock.java:448)
    at com.sun.codemodel.JBlock.generate(JBlock.java:436)
    at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
    at com.sun.codemodel.JBlock.state(JBlock.java:464)
    at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
    at com.sun.codemodel.JBlock.generateBody(JBlock.java:448)
    at com.sun.codemodel.JBlock.generate(JBlock.java:436)
    at com.sun.codemodel.JFormatter.g(JFormatter.java:350)
    at com.sun.codemodel.JBlock.state(JBlock.java:464)
    at com.sun.codemodel.JFormatter.s(JFormatter.java:386)
    at com.sun.codemodel.JMethod.declare(JMethod.java:464)
    at com.sun.codemodel.JFormatter.d(JFormatter.java:376)
    at com.sun.codemodel.JDefinedClass.declareBody(JDefinedClass.java:832)
    at com.sun.codemodel.JDefinedClass.declare(JDefinedClass.java:803)
    at com.sun.codemodel.JFormatter.d(JFormatter.java:376)
    at com.sun.codemodel.JFormatter.write(JFormatter.java:406)
    at com.sun.codemodel.JPackage.build(JPackage.java:442)
    at com.sun.codemodel.JCodeModel.build(JCodeModel.java:311)
    at com.sun.codemodel.JCodeModel.build(JCodeModel.java:301)
    at org.jvnet.mjiip.v_2_2.XJC22Mojo.writeCode(XJC22Mojo.java:98)
    at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:42)
    at org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:28)

After some debugging I've found out that the NPE was thrown because the passed JMethod was null.

And that was because of the DummyListField.$get - a getter for the dummy list field. $get is a private field which is never initialized in the code, but used in the field accessor, for instance:

public void toRawValue(JBlock block, JVar $var) {
            // [RESULT]
            // $<var>.addAll(bean.getLIST());
            // list.toArray( array );
            block.assign($var,JExpr._new(codeModel.ref(ArrayList.class).narrow(exposedType.boxify())).arg(
$target.invoke($get)
            ));
        }

This leads to $target.invoke(...) with null and consequently to the NPE during code generation.

I think it was forgotten to initialize the $get field. Other list fields have something like:

$get = writer.declareMethod(listT,"get"+prop.getName(true));
        writer.javadoc().append(prop.javadoc);
        JBlock block = $get.body();
        fixNullRef(block);  // avoid using an internal getter
        block._return(acc.ref(true));

I think this appeared here because the generateMixedExtensions="true" feature probably creates a dummy field. Should it be dummy, actually? Anyway, somehow with that feature a DummyListField is created and it has a bug.

glassfishrobot commented 9 years ago

Reported by lexi

glassfishrobot commented 9 years ago

lexi said: I've made a temporal fix in form of a plugin:

https://github.com/highsource/jaxb2-basics/blob/master/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/fixjaxb1058/FixJAXB1058Plugin.java

glassfishrobot commented 9 years ago

Was assigned to yaroska

glassfishrobot commented 7 years ago

This issue was imported from java.net JIRA JAXB-1058