diff options
author | Stephan Herrmann | 2018-12-04 22:32:52 +0000 |
---|---|---|
committer | Manoj Palat | 2019-02-26 08:23:30 +0000 |
commit | 9a6c75cf8038040b323dac130925f5927eb35cc5 (patch) | |
tree | 7921b792ee18eb45ae44988c21b6ff7930eb9655 | |
parent | 51935ee9d486419f4f1f74060e0b14409482d023 (diff) | |
download | eclipse.jdt.core-9a6c75cf8038040b323dac130925f5927eb35cc5.tar.gz eclipse.jdt.core-9a6c75cf8038040b323dac130925f5927eb35cc5.tar.xz eclipse.jdt.core-9a6c75cf8038040b323dac130925f5927eb35cc5.zip |
Bug 542106 - [9][quick fix] "The fix 'Remove unused import' generated aI20190226-1800I20190226-1020I20190226-0715I20190226-0600
null change." in module-info.java
Change-Id: I4c566e1e273bcd59315275c178379b34f0bf93d3
Signed-off-by: Stephan Herrmann <stephan.herrmann@berlin.de>
2 files changed, 48 insertions, 11 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 1057346ead..58791869f0 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017 IBM Corporation and others. + * Copyright (c) 2017, 2019 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -364,4 +364,37 @@ public class ASTRewritingModuleDeclarationTest extends ASTRewritingTest { } } + public void testBug542106_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 = + "import java.util.*;\n" + + "import java.util.function.Consumer;\n" + + "module first {\n" + + "}"; + ICompilationUnit cu= pack1.createCompilationUnit("module-info.java", content, false, null); + CompilationUnit astRoot= createAST(cu); + ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST()); + AST ast= astRoot.getAST(); + ListRewrite listRewrite = rewrite.getListRewrite(astRoot, CompilationUnit.IMPORTS_PROPERTY); + { + listRewrite.remove((ImportDeclaration) astRoot.imports().get(0), null); + ImportDeclaration newImport = ast.newImportDeclaration(); + newImport.setName(ast.newName("java.io.Serializable")); + listRewrite.insertFirst(newImport, null); + } + String preview= evaluateRewrite(cu, rewrite); + content = + "import java.io.Serializable;\n" + + "import java.util.function.Consumer;\n" + + "module first {\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/internal/core/dom/rewrite/ASTRewriteAnalyzer.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java index 74dd7b3741..ace31ede86 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 @@ -1695,19 +1695,23 @@ public final class ASTRewriteAnalyzer extends ASTVisitor { if (!hasChildrenChanges(node)) { return doVisitUnchangedChildren(node); } - - if (node.getAST().apiLevel() >= JLS9_INTERNAL && node.getModule() != null) { - rewriteNode(node, CompilationUnit.MODULE_PROPERTY, 0, ASTRewriteFormatter.NONE); - return false; + int startPos = 0; + boolean isModuleInfo = node.getAST().apiLevel() >= JLS9_INTERNAL && node.getModule() != null; + + if (!isModuleInfo) { + startPos = rewriteNode(node, CompilationUnit.PACKAGE_PROPERTY, 0, ASTRewriteFormatter.NONE); + + if (getChangeKind(node, CompilationUnit.PACKAGE_PROPERTY) == RewriteEvent.INSERTED) { + doTextInsert(0, getLineDelimiter(), getEditGroup(node, CompilationUnit.PACKAGE_PROPERTY)); + } } - int startPos= rewriteNode(node, CompilationUnit.PACKAGE_PROPERTY, 0, ASTRewriteFormatter.NONE); - if (getChangeKind(node, CompilationUnit.PACKAGE_PROPERTY) == RewriteEvent.INSERTED) { - doTextInsert(0, getLineDelimiter(), getEditGroup(node, CompilationUnit.PACKAGE_PROPERTY)); + startPos = rewriteParagraphList(node, CompilationUnit.IMPORTS_PROPERTY, startPos, 0, 0, 2); + if (isModuleInfo) { + rewriteNode(node, CompilationUnit.MODULE_PROPERTY, startPos, ASTRewriteFormatter.NONE); + } else { + rewriteParagraphList(node, CompilationUnit.TYPES_PROPERTY, startPos, 0, -1, 2); } - - startPos= rewriteParagraphList(node, CompilationUnit.IMPORTS_PROPERTY, startPos, 0, 0, 2); - rewriteParagraphList(node, CompilationUnit.TYPES_PROPERTY, startPos, 0, -1, 2); return false; } |