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

Added [Feature]: a toString() method for enumerated values in Java #103

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/codegen/fromcto/java/javavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ class JavaVisitor {
property.accept(this, parameters);
});

parameters.fileWriter.writeLine(1, '@Override');
parameters.fileWriter.writeLine(1, 'public String toString() {');
parameters.fileWriter.writeLine(2, 'return name();');
parameters.fileWriter.writeLine(1, '}');

parameters.fileWriter.writeLine(0, '}' );

this.endClassFile(enumDeclaration, parameters);
Expand Down
32 changes: 32 additions & 0 deletions test/codegen/__snapshots__/codegen.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,10 @@ public enum State {
WA,
IL,
CA,
@Override
public String toString() {
return name();
}
}
",
}
Expand All @@ -990,6 +994,10 @@ public enum TShirtSizeType {
SMALL,
MEDIUM,
LARGE,
@Override
public String toString() {
return name();
}
}
",
}
Expand Down Expand Up @@ -1141,6 +1149,10 @@ public enum Department {
HR,
Engineering,
Design,
@Override
public String toString() {
return name();
}
}
",
}
Expand Down Expand Up @@ -1196,6 +1208,10 @@ import com.fasterxml.jackson.annotation.*;
public enum LaptopMake {
Apple,
Microsoft,
@Override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if enums in java store functions.

Copy link
Contributor Author

@subhajit20 subhajit20 Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it does, because I have created one java program where I declared a enum and inside it defined the toString function and it worked totally final without any issue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for clarification. I'm not much proficient in Java. @mttrbrts can you please review this?

Copy link
Contributor Author

@subhajit20 subhajit20 Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a sample code snippet

public class Main{
    public enum State {
        MA,
        NY,
        CO,
    
        // Override the toString() method for the entire enum type
        @Override
        public String toString() {
            return name();
        }
    }
    public static void main(String[] args) {
        // Using the enum constants
        State state1 = State.MA;
        State state2 = State.NY;
        String ma = state1.toString();  // This will call the overridden toString() method
        
        // Printing the enum constants
        System.out.println("State 1: " + ma);
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @sanketshevkar Have one question, How can I pass extra parameters like parameters.filewriter as show in the code.. I mean to say can we pass other parameter like this parameters.x ?

parameters.fileWriter.writeLine(0, 'public enum ' + enumDeclaration.getName() + ' {' );
enumDeclaration.getOwnProperties().forEach((property) => {
property.accept(this, parameters);
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you can. it is an options object on the visitor. (answered by @dselman on the Tech WG call)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you can. it is an options object on the visitor. (answered by @dselman on the Tech WG call)

Okay ... I am assuming is that something like this parameters.options

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! is there any update or any feedback on this pr? @dselman @mttrbrts

public String toString() {
return name();
}
}
",
}
Expand Down Expand Up @@ -7091,6 +7107,10 @@ public enum State {
WA,
IL,
CA,
@Override
public String toString() {
return name();
}
}
",
}
Expand All @@ -7108,6 +7128,10 @@ public enum TShirtSizeType {
SMALL,
MEDIUM,
LARGE,
@Override
public String toString() {
return name();
}
}
",
}
Expand Down Expand Up @@ -7259,6 +7283,10 @@ public enum Department {
HR,
Engineering,
Design,
@Override
public String toString() {
return name();
}
}
",
}
Expand Down Expand Up @@ -7314,6 +7342,10 @@ import com.fasterxml.jackson.annotation.*;
public enum LaptopMake {
Apple,
Microsoft,
@Override
public String toString() {
return name();
}
}
",
}
Expand Down
9 changes: 7 additions & 2 deletions test/codegen/fromcto/java/javavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,16 @@ describe('JavaVisitor', function () {
javaVisit.visitEnumDeclaration(mockEnumDeclaration, param);

mockStartClassFile.withArgs(mockEnumDeclaration, param).calledOnce.should.be.ok;
param.fileWriter.writeLine.callCount.should.deep.equal(4);
param.fileWriter.writeLine.callCount.should.deep.equal(8);

param.fileWriter.writeLine.getCall(0).args.should.deep.equal([0, 'import com.fasterxml.jackson.annotation.*;']);
param.fileWriter.writeLine.getCall(1).args.should.deep.equal([0, '@JsonIgnoreProperties({"$class"})']);
param.fileWriter.writeLine.getCall(2).args.should.deep.equal([0, 'public enum Bob {']);
param.fileWriter.writeLine.getCall(3).args.should.deep.equal([0, '}']);
param.fileWriter.writeLine.getCall(3).args.should.deep.equal([1, '@Override']);
param.fileWriter.writeLine.getCall(4).args.should.deep.equal([1, 'public String toString() {']);
param.fileWriter.writeLine.getCall(5).args.should.deep.equal([2, 'return name();']);
param.fileWriter.writeLine.getCall(6).args.should.deep.equal([1, '}']);
param.fileWriter.writeLine.getCall(7).args.should.deep.equal([0, '}']);
mockEndClassFile.withArgs(mockEnumDeclaration, param).calledOnce.should.be.ok;
});
});
Expand Down
Loading