This is an automated email from the ASF dual-hosted git repository. neilcsmith pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new d23e817 Fixing Java code generator to properly handle manipulations with annotations on enum constants. new ea4ed6a Merge pull request #2620 from jlahoda/fix-annotation-modifications-on-enum-constants d23e817 is described below commit d23e8173449625dc8969e5395f17a4f3ffa4981b Author: Jan Lahoda <jlah...@netbeans.org> AuthorDate: Wed Dec 30 08:52:17 2020 +0100 Fixing Java code generator to properly handle manipulations with annotations on enum constants. --- .../java/hints/declarative/remove-from-parent.hint | 3 + .../java/hints/declarative/remove-from-parent.test | 22 ++++ .../modules/java/source/save/CasualDiff.java | 5 + .../api/java/source/gen/AnnotationTest.java | 127 ++++++++++++++++++++- .../api/java/source/gen/GeneratorTestBase.java | 20 ++++ 5 files changed, 176 insertions(+), 1 deletion(-) diff --git a/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/remove-from-parent.hint b/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/remove-from-parent.hint index c9cc2b9..018b1dc 100644 --- a/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/remove-from-parent.hint +++ b/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/remove-from-parent.hint @@ -25,3 +25,6 @@ double d => <!error="err",remove-from-parent=true> ;; + @Deprecated +=> /*remove-from-parent*/ +;; diff --git a/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/remove-from-parent.test b/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/remove-from-parent.test index 7968c5e..eede1f0 100644 --- a/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/remove-from-parent.test +++ b/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/remove-from-parent.test @@ -43,3 +43,25 @@ public class Test { } %%=> remove-from-parent:f +%%TestCase pos-annotation +package test; +public class Test { + @Deprecated + private void g() {} +} +%%=> +package test; +public class Test { + private void g() {} +} +%%TestCase pos-annotation-enum-constant +package test; +public enum Test { + @Deprecated + A(); +} +%%=> +package test; +public enum Test { + A(); +} diff --git a/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java b/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java index b26570f..edbce4c 100644 --- a/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java +++ b/java/java.source.base/src/org/netbeans/modules/java/source/save/CasualDiff.java @@ -1421,6 +1421,11 @@ public class CasualDiff { int localPointer = bounds[0]; // check that it is not enum constant. If so, match it in special way if ((oldT.mods.flags & Flags.ENUM) != 0) { + JCModifiers mods = oldT.getModifiers(); + int startPos = mods.pos != Position.NOPOS ? getOldPos(mods) : getOldPos(parent); + + localPointer = diffAnnotationsLists(mods.getAnnotations(), newT.getModifiers().getAnnotations(), startPos, localPointer); + if (nameChanged(oldT.name, newT.name)) { copyTo(localPointer, oldT.pos); printer.print(newT.name); diff --git a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/AnnotationTest.java b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/AnnotationTest.java index 1f79fa0..abbcf6e 100644 --- a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/AnnotationTest.java +++ b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/AnnotationTest.java @@ -65,7 +65,7 @@ public class AnnotationTest extends GeneratorTestBase { public static NbTestSuite suite() { NbTestSuite suite = new NbTestSuite(); suite.addTestSuite(AnnotationTest.class); -// suite.addTest(new ConstructorRenameTest("testAnnotationRename1")); +// suite.addTest(new AnnotationTest("testRemoveAnnotationEnumConstant")); // suite.addTest(new ConstructorRenameTest("testAnnotationRename2")); // suite.addTest(new ConstructorRenameTest("testClassToAnnotation")); // suite.addTest(new ConstructorRenameTest("testAddDefaultValue")); @@ -1009,6 +1009,131 @@ public class AnnotationTest extends GeneratorTestBase { assertEquals(golden, res); } + public void testRemoveAnnotationEnumConstant() throws Exception { + fileModificationTest("package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " A();\n" + + "}\n", + workingCopy -> { + ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); + VariableTree constant = (VariableTree) clazz.getMembers().get(1); + ModifiersTree mods = constant.getModifiers(); + TreeMaker make = workingCopy.getTreeMaker(); + workingCopy.rewrite(mods, + make.addModifiersAnnotation(mods, make.Annotation(make.Identifier("Deprecated"), Collections.emptyList()))); + }, + "package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @Deprecated\n" + + " A();\n" + + "}\n"); + fileModificationTest("package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @Deprecated\n" + + " A();\n" + + "}\n", + workingCopy -> { + ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); + VariableTree constant = (VariableTree) clazz.getMembers().get(1); + ModifiersTree mods = constant.getModifiers(); + TreeMaker make = workingCopy.getTreeMaker(); + workingCopy.rewrite(mods, + make.insertModifiersAnnotation(mods, 0, make.Annotation(make.Identifier("A"), Collections.emptyList()))); + }, + "package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @A\n" + + " @Deprecated\n" + + " A();\n" + + "}\n"); + fileModificationTest("package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @Deprecated\n" + + " A();\n" + + "}\n", + workingCopy -> { + ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); + VariableTree constant = (VariableTree) clazz.getMembers().get(1); + ModifiersTree mods = constant.getModifiers(); + TreeMaker make = workingCopy.getTreeMaker(); + workingCopy.rewrite(mods, + make.insertModifiersAnnotation(mods, 1, make.Annotation(make.Identifier("A"), Collections.emptyList()))); + }, + "package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @Deprecated\n" + + " @A\n" + + " A();\n" + + "}\n"); + fileModificationTest("package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @A\n" + + " @Deprecated\n" + + " A();\n" + + "}\n", + workingCopy -> { + ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); + VariableTree constant = (VariableTree) clazz.getMembers().get(1); + ModifiersTree mods = constant.getModifiers(); + TreeMaker make = workingCopy.getTreeMaker(); + workingCopy.rewrite(mods, + make.removeModifiersAnnotation(mods, 0)); + }, + "package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @Deprecated\n" + + " A();\n" + + "}\n"); + fileModificationTest("package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @Deprecated\n" + + " @A\n" + + " A();\n" + + "}\n", + workingCopy -> { + ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); + VariableTree constant = (VariableTree) clazz.getMembers().get(1); + ModifiersTree mods = constant.getModifiers(); + TreeMaker make = workingCopy.getTreeMaker(); + workingCopy.rewrite(mods, + make.removeModifiersAnnotation(mods, 1)); + }, + "package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @Deprecated\n" + + " A();\n" + + "}\n"); + fileModificationTest("package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " @Deprecated\n" + + " A();\n" + + "}\n", + workingCopy -> { + ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); + VariableTree constant = (VariableTree) clazz.getMembers().get(1); + ModifiersTree mods = constant.getModifiers(); + TreeMaker make = workingCopy.getTreeMaker(); + workingCopy.rewrite(mods, + make.removeModifiersAnnotation(mods, 0)); + }, + "package hierbas.del.litoral;\n" + + "\n" + + "public enum Test {\n" + + " A();\n" + + "}\n"); + } + String getGoldenPckg() { return ""; } diff --git a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/GeneratorTestBase.java b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/GeneratorTestBase.java index a2d1fa5..fa3d840 100644 --- a/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/GeneratorTestBase.java +++ b/java/java.source.base/test/unit/src/org/netbeans/api/java/source/gen/GeneratorTestBase.java @@ -29,6 +29,7 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; +import java.util.function.Consumer; import javax.swing.JEditorPane; import org.netbeans.api.editor.mimelookup.MimePath; import org.netbeans.api.editor.mimelookup.test.MockMimeLookup; @@ -206,6 +207,25 @@ public abstract class GeneratorTestBase extends ClassIndexTestCase { in.close(); } + protected void fileModificationTest(String code, Consumer<WorkingCopy> modification, String expected) throws Exception { + testFile = new File(getWorkDir(), "Test.java"); + + TestUtilities.copyStringToFile(testFile, code); + + JavaSource src = getJavaSource(testFile); + Task task = new Task<WorkingCopy>() { + public void run(final WorkingCopy workingCopy) throws IOException { + workingCopy.toPhase(Phase.RESOLVED); + + modification.accept(workingCopy); + } + }; + src.runModificationTask(task).commit(); + String res = TestUtilities.copyFileToString(testFile); + //System.err.println(res); + assertEquals(expected, res); + } + abstract String getGoldenPckg(); abstract String getSourcePckg(); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists