From 1405884cb35c9f2acf4b8fdb2d69e32eaddcd80b Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 18 Dec 2024 17:33:28 -0500 Subject: [PATCH] Allow setting name of RequiresDirective to null 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 --- .../ASTRewritingModuleDeclarationTest.java | 26 +++++++++++++++++++ .../jdt/core/dom/RequiresDirective.java | 14 ---------- .../core/dom/rewrite/ASTRewriteAnalyzer.java | 5 +++- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingModuleDeclarationTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingModuleDeclarationTest.java index c5e40b32656..36d79627d91 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingModuleDeclarationTest.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingModuleDeclarationTest.java @@ -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); + } + } } \ No newline at end of file diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/RequiresDirective.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/RequiresDirective.java index e141d8c2583..54eb3d2d3d4 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/RequiresDirective.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/core/dom/RequiresDirective.java @@ -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; } @@ -224,9 +213,6 @@ public Name getName() { * */ public void setName(Name name) { - if (name == null) { - throw new IllegalArgumentException(); - } ASTNode oldChild = this.name; preReplaceChild(oldChild, name, NAME_PROPERTY); this.name = name; diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java index 94d00d72b08..5bcebb822a3 100644 --- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java +++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java @@ -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; }