Skip to content

Commit

Permalink
Allow setting name of RequiresDirective to null
Browse files Browse the repository at this point in the history
According to the child descriptor property, it's optional,
so it makes sense that you should be able to set it to null.
The Javadoc also doesn't mention anything about null not being allowed.

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Jan 2, 2025
1 parent 07c7cda commit 2d24df1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,30 @@ public void testBug542106_since_9() throws Exception {
if (javaProject != null) deleteProject(javaProject);
}
}
public void testSetModuleNameNull_since_9() throws Exception {
IJavaProject javaProject = null;
try {
javaProject = createProject("P_9", JavaCore.VERSION_9);
IPackageFragmentRoot currentSourceFolder = getPackageFragmentRoot("P_9", "src");
IPackageFragment pack1= currentSourceFolder.getPackageFragment(Util.EMPTY_STRING);
String content =
"module first {\n" +
" requires module1;\n" +
"}";
ICompilationUnit cu= pack1.createCompilationUnit("module-info.java", content, false, null);
CompilationUnit astRoot= createAST(cu);
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
ModuleDeclaration moduleDeclaration = astRoot.getModule();
RequiresDirective requiresDirective = (RequiresDirective)moduleDeclaration.moduleStatements().get(0);
rewrite.remove(requiresDirective.getName(), null);
String preview= evaluateRewrite(cu, rewrite);
content =
"module first {\n" +
" requires ;\n" +
"}";
assertEqualString(preview, content);
} finally {
if (javaProject != null) deleteProject(javaProject);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,6 @@ public int getModifiers() {
* @return the module referenced
*/
public Name getName() {
if (this.name == null) {
// lazy init must be thread-safe for readers
synchronized (this) {
if (this.name == null) {
preLazyInit();
this.name = postLazyInit(
this.ast.newQualifiedName(new SimpleName(this.ast), new SimpleName(this.ast)),
NAME_PROPERTY);
}
}
}
return this.name;
}

Expand All @@ -224,9 +213,6 @@ public Name getName() {
* </ul>
*/
public void setName(Name name) {
if (name == null) {
throw new IllegalArgumentException();
}
ASTNode oldChild = this.name;
preReplaceChild(oldChild, name, NAME_PROPERTY);
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2417,8 +2417,11 @@ public boolean visit(RequiresDirective node) {
return doVisitUnchangedChildren(node);
}
int pos = getPosAfterToken(node.getStartPosition(), TerminalTokens.TokenNamerequires);
if (node.getName() != null) {
this.beforeRequiredSpaceIndex = node.getName().getStartPosition();
}
rewriteNodeList(node, RequiresDirective.MODIFIERS_PROPERTY, pos, String.valueOf(' '), String.valueOf(' '));
rewriteRequiredNode(node, RequiresDirective.NAME_PROPERTY);
rewriteNode(node, RequiresDirective.NAME_PROPERTY, this.beforeRequiredSpaceIndex, ASTRewriteFormatter.SPACE);
return false;
}

Expand Down

0 comments on commit 2d24df1

Please sign in to comment.