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

Problem with inner Class named '$' in JavaTemplate #3623

Open
MBoegers opened this issue Oct 16, 2023 · 5 comments
Open

Problem with inner Class named '$' in JavaTemplate #3623

MBoegers opened this issue Oct 16, 2023 · 5 comments
Labels
bug Something isn't working parser-java

Comments

@MBoegers
Copy link
Contributor

What version of OpenRewrite are you using?

I am using current main (Commit: d53b36f)
rewrite-java 8.5.0-SNAPSHOT

How are you running OpenRewrite?

Initially via tests in PR openrewrite/rewrite-migrate-java#285
Reproduced with test in rewrite-java directly.

What is the smallest, simplest way to reproduce the problem?

Reference an inner class with name $ inside a JavaTemplate

What did you expect to see?

JavaTemplate has to deal with $ as name of inner classes.
They resutl into A$$as FQN when defined as A.$.

What did you see instead?

__P__.<error>

What is the full stack trace of any errors you encountered?

none

Are you interested in contributing a fix to OpenRewrite?

Yes, as it blocks my PR :D

@MBoegers MBoegers added the bug Something isn't working label Oct 16, 2023
@MBoegers
Copy link
Contributor Author

@timtebeek timtebeek moved this to Backlog in OpenRewrite Oct 18, 2023
@MBoegers
Copy link
Contributor Author

MBoegers commented Jan 9, 2024

I may found the Spot, org.openrewrite.java.internal.template.Substitutions L155 replaces all $ which are used as delimiter

Same for JavaType#build Line 572

@timtebeek
Copy link
Contributor

Nice find! sorry this didn't get a lot of traction before; it's hard to get to everything! What do you want to do now? Do you need input on anything?

@MBoegers
Copy link
Contributor Author

MBoegers commented Jan 9, 2024

First of all, thanks an no worries. Its rarely used and I mentioned working on it ;).
I just documented my find as I try to understand the purpose of $ in OpenRewrite. It seems like $ is wider used in OpenRewrite than specified the JLS and that leads to sime wierd edge cases.
Would it be a hight value target to comply OpenRewrites usage of $ with the JLS?

@MBoegers
Copy link
Contributor Author

MBoegers commented Jan 9, 2024

I think I made some progress.

  • TypeUtils.toFullyQualifiedName seem to handle $ better (see TypeUtilsTest L 150 following)
  • All occurrences of String.replace("$", ".") in rewrite-java* now use TypeUtils.toFullyQualifiedName (namely Substitutions and RemoveUnusedImports) with green tests

I have a strange behavior in JavaTemplateSubstitutionTest.anyForInnerClass.
If I run the test I get

class A {
   void foo() {
-      $ test = new $();
+      $ test = new A.$();
     }
 //...
}

If I change Line 125 to new A.$() which also should be correct, I get

java.lang.IllegalStateException: AST contains missing or invalid type information
NewClass->NamedVariable->VariableDeclarations->Block->MethodDeclaration->Block->ClassDeclaration->CompilationUnit
/*~~(NewClass type is missing or malformed)~~>*/new A.$()
	at org.openrewrite.java.Assertions.assertValidTypes(Assertions.java:92)
	at org.openrewrite.java.Assertions.validateTypes(Assertions.java:62)
	at org.openrewrite.java.Assertions$$Lambda$440/0x00000008001ecfa8.accept(Unknown Source)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:466)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:132)
	at org.openrewrite.java.JavaTemplateSubstitutionsTest.anyForInnerClass(JavaTemplateSubstitutionsTest.java:89)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

That makes me feel like I miss a thing here. Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser-java
Projects
Status: Backlog
Development

No branches or pull requests

2 participants