Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: DefaultJavaPrettyPrinter misses line breaks with enums #4129

Open
SirYwell opened this issue Aug 25, 2021 · 1 comment
Open

bug: DefaultJavaPrettyPrinter misses line breaks with enums #4129

SirYwell opened this issue Aug 25, 2021 · 1 comment
Labels

Comments

@SirYwell
Copy link
Collaborator

SirYwell commented Aug 25, 2021

I found some issues when writing enums:

  1. If an enum only contains enum values but no other type members, code like
enum SimpleEnum {
    CONSTANT
}

is transformed into

enum SimpleEnum {

    CONSTANT;}

This comes from the printList call here:

elementPrinterHelper.printList(ctEnum.getEnumValues(),

It writes a line break before each enum value but does not add one after the ;. This isn't a problem if there are any other type members to write because ElementPrinterHelper#writeElementList will add the line break (although there is no empty line then, but that breaks a test here due to the expected output:
"public enum ENUM {" + nl + nl
+ " E1(TypeIdentifierCollision.globalField, TypeIdentifierCollision.ENUM.E1);" + nl
+ " final int NUM;" + nl + nl
+ " final Enum<?> e;" + nl + nl
+ " private ENUM(int num, Enum<?> e) {" + nl
+ " NUM = num;" + nl
+ " this.e = e;" + nl
+ " }" + nl
+ "}";
)

Only writing a new line if enum values were written but no type members makes it work.

  1. Using preserveLineNumbers writes an initial code
enum AlmostEmptyEnum {
    ;
    @Override
    public void toString() {
        return name().toLowerCase();
    }
}

as

enum AlmostEmptyEnum {
    ;
    @Override
    public void toString() {
        return name().toLowerCase();
    }}

This only happens with that option, without the option it looks like

enum AlmostEmptyEnum {
    ;

    @Override
    public void toString() {
        return name().toLowerCase();
    }
}

Note that without preserving line numbers, a new line is added before the method. This line is not added with that option but due to

if (!env.isPreserveLineNumbers()) {
printer.writeln();
}
the last line break is still skipped (this might be an issue with normal classes too(?)).

Another - rather unrelated - issue I encountered with printing of enum values is that an enum

enum SimpleEnum {
    CONSTANT {

    }
}

is written as

enum SimpleEnum {

    CONSTANT() {};}

The added () and the ; also appear with the Sniper printer.


Edit

The added () come from the !isImplicit check here

CtConstructorCall<?> constructorCall = (CtConstructorCall<?>) enumValue.getDefaultExpression();
if (!constructorCall.isImplicit()) {
elementPrinterHelper.printList(constructorCall.getArguments(), null, false, "(", false, false, ",", true, false, ")", expr -> scan(expr));
which indicates that the default expression is not implicit but it is handled as if the constructor is not implicit.

@slarse
Copy link
Collaborator

slarse commented Aug 28, 2021

I've also noted some funky printing of enums at times. This clearly needs some work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants