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

convert to valid identifier before checking for collision #942

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Java convert before check
primoly authored Apr 28, 2024
commit b840296dbfef59258cb4c13cada90c10cae9080d
11 changes: 6 additions & 5 deletions crates/teavm-java/src/lib.rs
Original file line number Diff line number Diff line change
@@ -2273,17 +2273,18 @@ trait ToJavaIdent: ToOwned {

impl ToJavaIdent for str {
fn to_java_ident(&self) -> String {
// Escape Java keywords
// Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
match self {
let ident = self.to_lower_camel_case();
match ident.as_str() {
// Escape Java keywords
// Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
"abstract" | "continue" | "for" | "new" | "switch" | "assert" | "default" | "goto"
| "package" | "synchronized" | "boolean" | "do" | "if" | "private" | "this"
| "break" | "double" | "implements" | "protected" | "throw" | "byte" | "else"
| "import" | "public" | "throws" | "case" | "enum" | "instanceof" | "return"
| "transient" | "catch" | "extends" | "int" | "short" | "try" | "char" | "final"
| "interface" | "static" | "void" | "class" | "finally" | "long" | "strictfp"
| "volatile" | "const" | "float" | "native" | "super" | "while" => format!("{self}_"),
_ => self.to_lower_camel_case(),
| "volatile" | "const" | "float" | "native" | "super" | "while" => format!("{ident}_"),
_ => ident,
}
}
}