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

Property name that is a java keyword generates java code with compile error #63

Closed
joelittlejohn opened this issue Jun 23, 2013 · 2 comments
Milestone

Comments

@joelittlejohn
Copy link
Owner

Original author: [email protected] (July 16, 2012 19:02:55)

What steps will reproduce the problem?

  1. Generate Java code from test.json:
    {
    "id" : "test",
    "type" : "object",
    "additionalProperties" : false,
    "properties" : {
    "name" : {
    "type": "string"
    },
    "enum" : {
    "type" : "array",
    "default" : [],
    "items" : { "type" : "string" }
    }
    }
    }
    2.Compile Generated java code.

What is the expected output?
Generated code compiles successfully.

What do you see instead?
Compile error.
Test.java:25: as of release 5, 'enum' is a keyword, and may not be used as an identifier
(use -source 1.4 or lower to use 'enum' as an identifier)
private List<String> enum = new ArrayList<String>();

What version of the product are you using?
0.3.1

On what Java version?
java version "1.6.0_30"

Adding this code before returning from (PropertyRule.getPropertyName(PropertyRule.java:158)) resolves the issue:
if( javax.lang.model.SourceVersion.isKeyword(nodeName) ){
nodeName = "__" + nodeName;
}

Original issue: http://code.google.com/p/jsonschema2pojo/issues/detail?id=63

@joelittlejohn
Copy link
Owner Author

From [email protected] on July 18, 2012 15:57:50
Instead of "__" which people associate with constants, maybe use a "$" instead.

e.g.

    if( javax.lang.model.SourceVersion.isKeyword(nodeName) ){
        nodeName = "$" + nodeName;
    }

@joelittlejohn
Copy link
Owner Author

From [email protected] on July 26, 2012 21:26:55
I've gone with appending an underscore for a couple of reasons:

  • It doesn't look as strange as a $ when reading the source. $ is generally associated with inner classes and confusing Java inter-op code from other languages.

  • It's not as intrusive as pre-pending when reading the source, e.g.

    private String enum_
    private String getEnum_
    private void setEnum_

vs

private String _enum
private String get_enum
private void set_enum

It's possible to avoid changing the method names but not currently practical. Since this is very much an edge case I'm happy with appending the underscore.

Thanks again for raising this John, there are many small areas where the plugin isn't as robust as it should be and this was a good one to fix.

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

No branches or pull requests

1 participant