AbstractDot.prettyPrint() incorrect

Issue #127 resolved
Johannes Mey created an issue

Disclaimer: I don't fully understand the AST stucture of array accesses, so I don't know if the following is described correctly.

class Test {
    public static void main(String[] args) {
        String[] test = new String[1];
        test[0].toString();
    }
}

is printed as

class Test {
    public static void main(String[] args) {
        String[] test = new String[1];
        test.[0].toString();
    }
}

probably because the AST contains two nested Dots, so needsDot() returns true incorrectly.

For my example class I fixed it with:

aspect FixedPrettyPrintUtil {
    refine PrettyPrintUtil eq AbstractDot.needsDot() {
        if (getRight() instanceof ArrayAccess) {
            return false;
        } else if (getRight() instanceof Dot) {
            return !(((Dot) getRight()).getLeft() instanceof ArrayAccess);
        } else {
            return true;
        }
    }
}

Comments (4)

  1. Jesper Öqvist

    Thank you for this bug report. You can use AbstractDot.rightSide() to access the expression to the immediate right of a dot, which means this should fix the issue:

    -  syn boolean AbstractDot.needsDot() = !(getRight() instanceof ArrayAccess);
    +  syn boolean AbstractDot.needsDot() = !(rightSide() instanceof ArrayAccess);
    
  2. Jesper Öqvist

    By the way, if you want to see the AST for a particular Java file you can use org.extendj.JavaDumpTree. For example:

    java -cp extendj.jar org.extendj.JavaDumpTree Test.java
    
  3. Log in to comment