Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Handle Java Reserved Words/Keywords #234

Closed
adjokic opened this issue Apr 12, 2017 · 10 comments
Closed

Handle Java Reserved Words/Keywords #234

adjokic opened this issue Apr 12, 2017 · 10 comments

Comments

@adjokic
Copy link

adjokic commented Apr 12, 2017

Hi,
I am trying to find a way to deal with Java reserved words in the raml -> jax-rs generation.
This happens when I have a raml data type with, for example:

#%RAML 1.0 DataType
properties:
  supportedProperty:
    type: string
  class:
    type: string

this will fail with the following error during raml generation:
[ERROR] Failed to execute goal org.raml:raml-to-jaxrs-maven-plugin:2.0.0-RC2-SNAPSHOT:generate (default-cli) on project raml: Error generating Java classes from: null: not a valid name: class -> [Help 1]

Any ideas how to deal with this the best way? A couple of things:

  • I need to generate a JSON object from this generated PoJo, so ideally I would like to keep the property name in the RAML untouched
  • I am OK with changing the names of Java classes/fields/methods, so this would have to be dealt with in the generator.

Thanks

@jstoiko jstoiko added the 2.0.x label Apr 12, 2017
@jstoiko jstoiko modified the milestone: 2.0.1 Apr 21, 2017
@sichvoge
Copy link
Contributor

Maybe we can reserve some special character for these cases?

@adjokic
Copy link
Author

adjokic commented Apr 26, 2017

what I did was replace all occurrences of propertyInfo.getName() in SimpleInheritanceExtension.java with propertyInfoName and append "_property":

      final String propertyInfoName;
      if (!SourceVersion.isName(propertyInfo.getName())) {
        propertyInfoName = propertyInfo.getName() + "_property";
        System.out.println(propertyInfo.getName() + " is not a valid property name!");
      } else {
        propertyInfoName = propertyInfo.getName();
      }

This is using javax.lang.model.SourceVersion.isName(), which contains a set of Java reserved words.
I can create a PR showing these changes, thought it's not in its most elegant form. Perhaps we can add a method to PropertyInfo instead, e.g. getQualifiedName(), which does the same thing as above.

@jstoiko
Copy link
Contributor

jstoiko commented Apr 26, 2017

Thanks for sharing this.

I can create a PR showing these changes

please do! it would be very useful.

@adjokic
Copy link
Author

adjokic commented May 8, 2017

@jstoiko looks like I won't be able to sign the MuleSoft Contributor Agreement Acceptance [API Notebook] because of the permissions it asks for on my github account -- sorry about that. I won't be able to create a PR after all, but please feel free to use the above as a baseline, if it helps at all.

@sichvoge
Copy link
Contributor

sichvoge commented May 8, 2017

@adjokic can you let me know what the problem is with the signing process?

@adjokic
Copy link
Author

adjokic commented May 8, 2017

It asks to access my organizations in Github

@sichvoge
Copy link
Contributor

sichvoge commented May 8, 2017

that's very common, to be honest. not something you should be afraid about. but of course, it is absolutely your decision and either way is fine ;)

@jpbelang
Copy link
Contributor

jpbelang commented May 9, 2017

Variable names are now protected from being java keywords: I add "Property" to java keywords (classProperty) for example. I dropped the "_" to stay compliant to the java naming conventions and help people using sonar.

I also want to do a plugin to allow for a general strategy.

PS: having trouble committing. Should be solved soon.

PPS: Thanks @adjokic : hadn't seen the SourceVersion stuff. :-)

@adjokic
Copy link
Author

adjokic commented May 16, 2017

thanks @jpbelang
this works for me now. should I close the ticket or do you have a separate process for reviews?

@jstoiko
Copy link
Contributor

jstoiko commented May 16, 2017

thanks @adjokic you can leave this open for now

@jstoiko jstoiko closed this as completed Jul 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants