diff --git a/java.source/src/org/netbeans/modules/java/source/save/DiffFacility.java b/java.source/src/org/netbeans/modules/java/source/save/DiffFacility.java --- a/java.source/src/org/netbeans/modules/java/source/save/DiffFacility.java +++ b/java.source/src/org/netbeans/modules/java/source/save/DiffFacility.java @@ -61,19 +61,19 @@ public DiffFacility(Collection diff) { this.gdiff = diff; } - + private static class Line { Line(String data, int start, int end) { this.start = start; this.end = end; this.data = data; } - + @Override public String toString() { return data.toString(); } - + @Override public boolean equals(Object o) { if (o instanceof Line) { @@ -82,17 +82,17 @@ return false; } } - + @Override public int hashCode() { return data.hashCode(); } - + String data; int end; int start; } - + private static List getLines(String text) { char[] chars = text.toCharArray(); List list = new ArrayList(); @@ -108,20 +108,20 @@ } return list; } - + public List makeListMatch(String text1, String text2, int offset) { List list1 = getLines(text1); List list2 = getLines(text2); Line[] lines1 = list1.toArray(new Line[list1.size()]); Line[] lines2 = list2.toArray(new Line[list2.size()]); - + List diffs = new ComputeDiff(lines1, lines2).diff(); for (Difference diff : diffs) { int delStart = diff.getDeletedStart(); int delEnd = diff.getDeletedEnd(); int addStart = diff.getAddedStart(); int addEnd = diff.getAddedEnd(); - + char type = delEnd != Difference.NONE && addEnd != Difference.NONE ? 'c' : (delEnd == Difference.NONE ? 'a' : 'd'); // addition @@ -134,14 +134,14 @@ delStart < lines1.length ? lines1[delStart].start + offset : (lines1.length != 0 ? lines1[lines1.length-1].end + offset : offset) : lines1[delEnd].end + offset, builder.toString())); - + } // deletion else if (type == 'd') { gdiff.add(Diff.delete(lines1[delStart].start + offset, lines1[delEnd].end + offset)); } - + // change else { // type == 'c' if (addEnd-addStart>delEnd-delStart) { @@ -185,7 +185,7 @@ } return null; } - + public List makeTokenListMatch(String text1, String text2, int currentPos) { TokenSequence seq1 = TokenHierarchy.create(text1, JavaTokenId.language()).tokenSequence(JavaTokenId.language()); TokenSequence seq2 = TokenHierarchy.create(text2, JavaTokenId.language()).tokenSequence(JavaTokenId.language()); @@ -203,7 +203,7 @@ lastId2 = seq2.token().id(); list2.add(new Line(data, seq2.offset(), seq2.offset() + data.length())); } - if (lastId1 != null && lastId1 == lastId2 && lastId1 == JavaTokenId.LINE_COMMENT) { + if (lastId1 != null && lastId1 == lastId2 && (lastId1 == JavaTokenId.LINE_COMMENT || lastId1 == JavaTokenId.WHITESPACE)) { list1.remove(list1.size() - 1); list2.remove(list2.size() - 1); } @@ -215,7 +215,7 @@ int delEnd = diff.getDeletedEnd(); int addStart = diff.getAddedStart(); int addEnd = diff.getAddedEnd(); - + char type = delEnd != Difference.NONE && addEnd != Difference.NONE ? 'c' : (delEnd == Difference.NONE ? 'a' : 'd'); // addition @@ -229,12 +229,12 @@ : lines1[delEnd].end), builder.toString())); } - + // deletion else if (type == 'd') { gdiff.add(Diff.delete(currentPos + lines1[delStart].start, currentPos + lines1[delEnd].end)); } - + // change else { // type == 'c' StringBuilder builder = new StringBuilder(); @@ -249,7 +249,7 @@ gdiff.add(Diff.insert(currentPos + (delEnd == Difference.NONE ? lines1[delStart].start : lines1[delEnd].end), builder.toString())); } - + } return null; } diff --git a/java.source/test/unit/src/org/netbeans/api/java/source/gen/GuardedBlockTest.java b/java.source/test/unit/src/org/netbeans/api/java/source/gen/GuardedBlockTest.java --- a/java.source/test/unit/src/org/netbeans/api/java/source/gen/GuardedBlockTest.java +++ b/java.source/test/unit/src/org/netbeans/api/java/source/gen/GuardedBlockTest.java @@ -107,7 +107,7 @@ /** * Regression tests for guarded exceptions. - * + * * @author Pavel Flaska */ public class GuardedBlockTest extends GeneratorTestMDRCompat { @@ -119,6 +119,7 @@ public static NbTestSuite suite() { NbTestSuite suite = new NbTestSuite(); // suite.addTestSuite(GuardedBlockTest.class); + suite.addTest(new GuardedBlockTest("testInsertMethodBeforeVariablesBug177824")); suite.addTest(new GuardedBlockTest("testAddMethodAfterVariables")); suite.addTest(new GuardedBlockTest("test119048")); suite.addTest(new GuardedBlockTest("test119962")); @@ -127,7 +128,7 @@ suite.addTest(new GuardedBlockTest("testComplex186754")); return suite; } - + /** * We need our own data loader to use guarded blocks. */ @@ -156,13 +157,86 @@ cacheFolder.mkdirs(); IndexUtil.setCacheFolder(cacheFolder); } - + + /** + * #177824: Guarded Exception + */ + public void testInsertMethodBeforeVariablesBug177824() throws Exception { + + String source = + "package test;\n" + + "\n" + + "import java.awt.event.ActionEvent;\n" + + "import java.awt.event.ActionListener;\n" + + "\n" + + "public class Guarded1 implements ActionListener {\n" + + " \n" + + " private void fooActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fooActionPerformed\n" + + " }//GEN-LAST:event_fooActionPerformed\n" + + "\n" + + " // Variables declaration - do not modify//GEN-BEGIN:variables\n" + + " private javax.swing.JButton jButton1;\n" + + " // End of variables declaration//GEN-END:variables\n" + + "}\n"; + + testFile = new File(getWorkDir(), "Test.java"); + TestUtilities.copyStringToFile(testFile, source); + DataObject dataObject = DataObject.find(FileUtil.toFileObject(testFile)); + EditorCookie editorCookie = ((GuardedDataObject) dataObject).getCookie(EditorCookie.class); + Document doc = editorCookie.openDocument(); + String golden = + "package test;\n" + + "\n" + + "import java.awt.event.ActionEvent;\n" + + "import java.awt.event.ActionListener;\n" + + "\n" + + "public class Guarded1 implements ActionListener {\n" + + " \n" + + " private void fooActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fooActionPerformed\n" + + " }//GEN-LAST:event_fooActionPerformed\n" + + "\n" + + " public String toString() {\n" + + " }\n" + + "\n" + + " // Variables declaration - do not modify//GEN-BEGIN:variables\n" + + " private javax.swing.JButton jButton1;\n" + + " // End of variables declaration//GEN-END:variables\n" + + "}\n"; + + JavaSource src = getJavaSource(testFile); + Task task = new Task() { + + public void run(WorkingCopy workingCopy) throws IOException { + workingCopy.toPhase(RESOLVED); + CompilationUnitTree cut = workingCopy.getCompilationUnit(); + TreeMaker make = workingCopy.getTreeMaker(); + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + MethodTree newMethod = make.Method( + make.Modifiers(Collections.singleton(Modifier.PUBLIC)), + "toString", + make.Type("java.lang.String"), + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + make.Block(Collections.emptyList(), false), + null // default value - not applicable + ); + ClassTree copy = make.insertClassMember(clazz, 2, newMethod); + workingCopy.rewrite(clazz, copy); + } + }; + src.runModificationTask(task).commit(); + editorCookie.saveDocument(); + String res = TestUtilities.copyFileToString(testFile); + assertEquals(golden, res); + } + /** * #90424: Guarded Exception */ public void testAddMethodAfterVariables() throws Exception { testFile = new File(getWorkDir(), "Test.java"); - TestUtilities.copyStringToFile(testFile, + TestUtilities.copyStringToFile(testFile, "package javaapplication5;\n" + "\n" + "import java.awt.event.ActionEvent;\n" + @@ -181,7 +255,7 @@ DataObject dataObject = DataObject.find(FileUtil.toFileObject(testFile)); EditorCookie editorCookie = ((GuardedDataObject) dataObject).getCookie(EditorCookie.class); Document doc = editorCookie.openDocument(); - String golden = + String golden = "package javaapplication5;\n" + "\n" + "import java.awt.event.ActionEvent;\n" + @@ -199,7 +273,7 @@ " public void actionPerformed(ActionEvent e) {\n" + " }\n" + "}\n"; - + JavaSource src = getJavaSource(testFile); Task task = new Task() { @@ -216,14 +290,14 @@ Collections.singletonList( make.Variable( make.Modifiers(Collections.emptySet()), - "e", - make.Identifier("ActionEvent"), + "e", + make.Identifier("ActionEvent"), null) ), Collections.emptyList(), make.Block(Collections.emptyList(), false), null // default value - not applicable - ); + ); ClassTree copy = make.addClassMember(clazz, newMethod); workingCopy.rewrite(clazz, copy); } @@ -240,7 +314,7 @@ */ public void test119048() throws Exception { testFile = new File(getWorkDir(), "Test.java"); - TestUtilities.copyStringToFile(testFile, + TestUtilities.copyStringToFile(testFile, "package javaapplication5;\n" + "\n" + "public class NewJFrame extends javax.swing.JFrame {\n" + @@ -255,7 +329,7 @@ DataObject dataObject = DataObject.find(FileUtil.toFileObject(testFile)); EditorCookie editorCookie = ((GuardedDataObject) dataObject).getCookie(EditorCookie.class); Document doc = editorCookie.openDocument(); - String golden = + String golden = "package javaapplication5;\n" + "\n" + "public class NewJFrame extends javax.swing.JFrame {\n" + @@ -267,7 +341,7 @@ " }//GEN-LAST:event_jButton1ActionPerformed\n" + "\n" + "}"; - + JavaSource src = getJavaSource(testFile); Task task = new Task() { @@ -293,13 +367,13 @@ System.err.println(res); assertEquals(golden, res); } - + /** * #119962: Guarded Exception */ public void test119962() throws Exception { testFile = new File(getWorkDir(), "Test.java"); - TestUtilities.copyStringToFile(testFile, + TestUtilities.copyStringToFile(testFile, "package test;\n" + "\n" + "public class NewJFrame extends javax.swing.JFrame {\n" + @@ -330,7 +404,7 @@ DataObject dataObject = DataObject.find(FileUtil.toFileObject(testFile)); EditorCookie editorCookie = ((GuardedDataObject) dataObject).getCookie(EditorCookie.class); Document doc = editorCookie.openDocument(); - String golden = + String golden = "package test;\n" + "\n" + "public class NewJFrame extends javax.swing.JFrame {\n" + @@ -360,7 +434,7 @@ " public void actionPerformed(ActionEvent e) {\n" + " }\n" + "}"; - + JavaSource src = getJavaSource(testFile); Task task = new Task() { @@ -377,14 +451,14 @@ Collections.singletonList( make.Variable( make.Modifiers(Collections.emptySet()), - "e", - make.Identifier("ActionEvent"), + "e", + make.Identifier("ActionEvent"), null) ), Collections.emptyList(), make.Block(Collections.emptyList(), false), null // default value - not applicable - ); + ); workingCopy.rewrite(clazz, make.addClassMember(clazz, newMethod)); } }; @@ -394,7 +468,7 @@ System.err.println(res); assertEquals(golden, res); } - + /** * #119345: Duplicated initComponents() when trying to rename in * the guarded. @@ -539,7 +613,7 @@ DataObject dataObject = DataObject.find(FileUtil.toFileObject(testFile)); EditorCookie editorCookie = ((GuardedDataObject) dataObject).getCookie(EditorCookie.class); Document doc = editorCookie.openDocument(); - String golden = + String golden = "package crystalball;\n" + "\n" + "import org.jdesktop.application.Action;\n" + @@ -673,7 +747,7 @@ " // End of variables declaration//GEN-END:variables\n" + " \n" + "}\n"; - + JavaSource src = getJavaSource(testFile); Task task = new Task() { @@ -694,7 +768,7 @@ mst = (MemberSelectTree) invocation.getArguments().get(0); mst = (MemberSelectTree) mst.getExpression(); workingCopy.rewrite(mst.getExpression(), make.Identifier("crystalball")); - + var = (VariableTree) stmts.get(16); invocation = (MethodInvocationTree) var.getInitializer(); mst = (MemberSelectTree) invocation.getArguments().get(0); @@ -713,10 +787,10 @@ System.err.println(res); assertEquals(golden, res); } - + public void testRenameTypeParameter125385() throws Exception { testFile = new File(getWorkDir(), "Test.java"); - TestUtilities.copyStringToFile(testFile, + TestUtilities.copyStringToFile(testFile, "package hierbas.del.litoral;\n" + "\n" + "public class MyList {\n" + @@ -733,12 +807,12 @@ " return null;\n" + " }\n" + "}\n"; - + DataObject dataObject = DataObject.find(FileUtil.toFileObject(testFile)); EditorCookie editorCookie = ((GuardedDataObject) dataObject).getCookie(EditorCookie.class); Document doc = editorCookie.openDocument(); JavaSource src = getJavaSource(testFile); - + Task task = new Task() { public void run(final WorkingCopy workingCopy) throws IOException { @@ -1008,7 +1082,7 @@ ((Environment) this.env).removeSaveCookie(); } - @Override + @Override protected CloneableEditor createCloneableEditor() { return new CloneableEditor(this); }