Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Sanitize function name when building methods #690

Closed
iikirilov opened this issue Nov 7, 2018 · 4 comments
Closed

Sanitize function name when building methods #690

iikirilov opened this issue Nov 7, 2018 · 4 comments

Comments

@iikirilov
Copy link

iikirilov commented Nov 7, 2018

When writing code generators to wrap constructs from another language to java we sometimes hit issues that some constructs are given names which are reserved in the java language.

I propose that MethodSpec.methodBuilder() can be overloaded like this:

MethodSpec.Builder methodBuilder =
                MethodSpec.methodBuilder(functionName, sanitizingChar, append)
                        .addModifiers(Modifier.PUBLIC);

which will append (or prepend if set to false) the sanitizingChar to the method name if SourceVersion.isName(functionName) returns false.

See: hyperledger-web3j/web3j#658

@JakeWharton
Copy link
Collaborator

JakeWharton commented Nov 8, 2018 via email

@iikirilov
Copy link
Author

Thank you for the suggestion. I would like to make it more configurable like being able to define your own "sanitizing character" and whether it should be prepended or appended. Makes sense?

@JakeWharton
Copy link
Collaborator

JakeWharton commented Nov 10, 2018 via email

@iikirilov
Copy link
Author

OK - I decided to just use

        if (!SourceVersion.isName(functionName)) {
            functionName = "_" + functionName;
        }
```
because we will never have a case that the name will come up twice in the same code generation process so using `NameAllocator` is overkill.

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

No branches or pull requests

2 participants