-
Notifications
You must be signed in to change notification settings - Fork 720
SONARJAVA-5984 Support Module Import Declarations #5403
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
Changes from all commits
ecd5c5c
e6be949
332963f
e1af38b
947d87c
0a1ea69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -575,20 +575,32 @@ private JavaTree.CompilationUnitTreeImpl convertCompilationUnit(CompilationUnit | |
| for (int i = 0; i < e.imports().size(); i++) { | ||
| ImportDeclaration e2 = (ImportDeclaration) e.imports().get(i); | ||
| ExpressionTree name = convertImportName(e2.getName()); | ||
| if (e2.isOnDemand()) { | ||
| name = new MemberSelectExpressionTreeImpl( | ||
| name, | ||
| lastTokenIn(e2, TerminalToken.TokenNameDOT), | ||
| new IdentifierTreeImpl(lastTokenIn(e2, TerminalToken.TokenNameMULTIPLY)) | ||
| ); | ||
|
|
||
| boolean isModuleImport = org.eclipse.jdt.core.dom.Modifier.isModule(e2.getModifiers()); | ||
|
|
||
| // "on demand" means `import pkg.*;` | ||
| if (e2.isOnDemand() && !isModuleImport) { | ||
| InternalSyntaxToken dotToken = lastTokenIn(e2, TerminalToken.TokenNameDOT); | ||
| InternalSyntaxToken identifierToken = lastTokenIn(e2, TerminalToken.TokenNameMULTIPLY); | ||
| name = new MemberSelectExpressionTreeImpl(name, dotToken, new IdentifierTreeImpl(identifierToken)); | ||
| } | ||
|
|
||
| InternalSyntaxToken staticKeyword = e2.isStatic() ? firstTokenIn(e2, TerminalToken.TokenNamestatic) : null; | ||
| InternalSyntaxToken moduleKeyword = isModuleImport ? firstTokenIn(e2, TerminalToken.TokenNamemodule) : null; | ||
|
|
||
| JavaTree.ImportTreeImpl t = new JavaTree.ImportTreeImpl( | ||
| firstTokenIn(e2, TerminalToken.TokenNameimport), | ||
| e2.isStatic() ? firstTokenIn(e2, TerminalToken.TokenNamestatic) : null, | ||
| staticKeyword, | ||
| moduleKeyword, | ||
| name, | ||
| lastTokenIn(e2, TerminalToken.TokenNameSEMICOLON) | ||
| ); | ||
| t.binding = e2.resolveBinding(); | ||
|
|
||
| if (!isModuleImport) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we want to skip resolution of IModuleBinding when it is a module ? Asking it because we don't skip resolution of IPackageBinding when
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My understanding is that it does not make sense for a module. In fact, when the condition is removed, we are getting type cast exception in jdt. |
||
| // There is no method to resolve bindings for a module import. | ||
| t.binding = e2.resolveBinding(); | ||
| } | ||
|
|
||
| imports.add(t); | ||
|
|
||
| int tokenIndex = tokenManager.lastIndexIn(e2, TerminalToken.TokenNameSEMICOLON); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to mix an on-demand import and a module import? I am not sure the JLS allows this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is not allowed: https://docs.oracle.com/javase/specs/jls/se25/html/jls-7.html#jls-7.5 On demend imports exist only in the context of type and static imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, because we have coverage, I will not question this one too much but it looks little weird to me