Skip to content

Class cast is missing for the instance of private nested class #731

Closed
@alisevych

Description

@alisevych

Description

Class cast is missing for the Object of private nested class.

To Reproduce

Steps to reproduce the behavior:

  1. IntelliJ IDEA 2022.1.4 is running with UTBot plugin installed
  2. Open a project with JDK 8/11
  3. Add the following class to it:
public class ABC {

    private class NML {
        public NML[] array = new NML[2];

        NML() {
            array[0] = null;
            array[1] = null;
        }
    }

    NML obj;

    ABC() {
        obj = new NML();
    }

    public int size() {
        return obj.array.length;
    }

}
  1. Open the generated test

Expected behavior

Generated tests are supposed to be compilable.
Class cast must be added to the instance of the nested class, like

ABC.NML nml = ((ABC.NML) createInstance("ABC$NML"));

Actual behavior

Class cast is missing for the NML Object created by means of reflection.

Visual proofs (screenshots, logs, images)

The following test is present:

public void testSize_ThrowNullPointerException_1() throws Exception  {
        ABC abc = new ABC();
        Object nml = createInstance("ABC$NML");
        setField(nml, "array", null);
        abc.obj = nml;
        
        /* This test fails because method [ABC.size] produces [java.lang.NullPointerException]
            ABC.size(ABC.java:19) */
        abc.size();
    }

Environment

Windows 10 Pro
IntelliJ IDEA 2022.1.4
JDK 8

Additional context

If private modifier is removed - nested class is package-private or public - the class cast is made correctly.

Metadata

Metadata

Assignees

Labels

ctg-bugIssue is a bug

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions