Skip to content

Commit

Permalink
[NETBEANS-892] Fix lambda expr and multi catch rewrite issues (apache…
Browse files Browse the repository at this point in the history
…#591)

* NETBEANS-892 : Fix lambda expr and multi catch rewrite issues

* Fixed testcase issue
  • Loading branch information
rtaneja1 authored and Geertjan Wielenga committed Jun 19, 2018
1 parent 6b33d35 commit fa374aa
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1452,11 +1452,13 @@ private int diffVarDef(JCVariableDecl oldT, JCVariableDecl newT, int[] bounds) {
localPointer = oldT.pos;
printer.suppressVariableType = suppressParameterTypes;
int l = printer.out.length();
printer.print(newT.vartype);
printer.suppressVariableType = false;
if (l < printer.out.length()) {
printer.print(" ");
if (!suppressParameterTypes) {
printer.print(newT.vartype);
if (l < printer.out.length()) {
printer.print(" ");
}
}
printer.suppressVariableType = false;
}
} else {
if (suppressParameterTypes) {
Expand Down Expand Up @@ -3760,7 +3762,8 @@ private int diffVarGroup(

protected int diffUnionType(JCTypeUnion oldT, JCTypeUnion newT, int[] bounds) {
int localPointer = bounds[0];
return diffParameterList(oldT.alternatives, newT.alternatives, null, localPointer, Measure.MEMBER, diffContext.style.spaceAroundBinaryOps(), diffContext.style.spaceAroundBinaryOps(), false, "|");
int pos = diffParameterList(oldT.alternatives, newT.alternatives, null, localPointer, Measure.MEMBER, diffContext.style.spaceAroundBinaryOps(), diffContext.style.spaceAroundBinaryOps(), false, "|");
return Math.min(pos, bounds[1]);
}

private boolean commaNeeded(ResultItem[] arr, ResultItem item) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,58 @@ public void run(final WorkingCopy workingCopy) throws IOException {
System.err.println(res);
assertEquals(golden, res);
}

public void testRemoveAddInMultiCatch() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n" +
"import java.io.*;\n" +
"import java.net.*;\n" +
"public class Test {\n" +
" public void taragui() {\n" +
" try {\n" +
" } catch (MalformedURLException | FileNotFoundException ex) {\n" +
" }\n" +
" }\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n" +
"import java.io.*;\n" +
"import java.net.*;\n" +
"public class Test {\n" +
" public void taragui() {\n" +
" try {\n" +
" } catch (IOException | RuntimeException ex) {\n" +
" }\n" +
" }\n" +
"}\n";
JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
Task task = new Task<WorkingCopy>() {

public void run(final WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
final TreeMaker make = workingCopy.getTreeMaker();

new ErrorAwareTreeScanner<Void, Void>() {
@Override public Void visitUnionType(UnionTypeTree node, Void p) {
List<Tree> alternatives = new ArrayList<Tree>(node.getTypeAlternatives());
alternatives.remove(0);
alternatives.remove(0);
alternatives.add(0, make.Identifier("IOException"));
alternatives.add(1, make.Identifier("RuntimeException"));
workingCopy.rewrite(node, make.UnionType(alternatives));
return null;
}
}.scan(workingCopy.getCompilationUnit(), null);
}

};
testSource.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}

String getGoldenPckg() {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,55 @@ public void test204444() throws Exception { // #204444 - Improve Move Refactorin
+ "}\n"));
}

public void testNETBEANS892() throws Exception { // #204444 - Improve Move Refactoring to support nested/inner classes
writeFilesAndWaitForScan(src,
new File("a/A.java", "package a;\n"
+ "import java.util.List;\n"
+ "import java.util.function.Function;\n"
+ "public class A {\n"
+ "public void v() {try{String bar = \"foo\";}catch (RuntimeException | AssertionError e){}}\n"
+ "public void breaks(){doStuff(x->x.substring(5));}\n"
+ "public void doStuff(Function<String, String> stuff){}\n"
+ "}\n"),
new File("a/B.java", "package a;\n"
+ "import java.util.List;\n"
+ "/** Class B */\n"
+ "public class B {\n"
+ " public int i = 42;\n"
+ " private List list;\n"
+ "}\n"),
new File("a/C.java", "package a;\n"
+ "import java.util.function.Function;\n"
+ "public class C {\n"
+ "public void v() {try{String bar = \"foo\";}catch (RuntimeException | AssertionError e){}}\n"
+ "public void breaks(){doStuff(x->x.substring(5));}\n"
+ "public void doStuff(Function<String, String> stuff){}\n"
+ "}\n"));
performMove(src.getFileObject("a/B.java"), 0, src.getFileObject("a/C.java"), 0);
verifyContent(src,
new File("a/A.java", "package a;\n"
+ "import java.util.List;\n"
+ "import java.util.function.Function;\n"
+ "public class A {\n"
+ "public void v() {try{String bar = \"foo\";}catch (RuntimeException | AssertionError e){}}\n"
+ "public void breaks(){doStuff(x->x.substring(5));}\n"
+ "public void doStuff(Function<String, String> stuff){}\n"
+ "}\n"),
new File("a/C.java", "package a;\n"
+ "import java.util.List;\n"
+ "import java.util.function.Function;\n"
+ "public class C {\n"
+ "public void v() {try{String bar = \"foo\";}catch (RuntimeException | AssertionError e){}}\n"
+ "public void breaks(){doStuff(x->x.substring(5));}\n"
+ "public void doStuff(Function<String, String> stuff){}\n"
+ "/** Class B */\n"
+ "public static class B {\n"
+ " public int i = 42;\n"
+ " private List list;\n"
+ "}\n"
+ "}\n"));
}

public void test243552() throws Exception { // #204444 - Improve Move Refactoring to support nested/inner classes
writeFilesAndWaitForScan(src,
new File("t/A.java", "package t;\n"
Expand Down

0 comments on commit fa374aa

Please sign in to comment.