Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3052,6 +3052,11 @@ protected int diffAnnotation(JCAnnotation oldT, JCAnnotation newT, int[] bounds)
localPointer = diffTree(oldT.annotationType, newT.annotationType, annotationBounds);
JavaTokenId[] parens = null;
if (oldT.args.nonEmpty()) {
if (newT.args.isEmpty()) {
//non-empty to empty:
copyTo(localPointer, annotationBounds[1]);
return endPos(oldT);
}
copyTo(localPointer, localPointer = getOldPos(oldT.args.head));
} else {
// check, if there are already written parenthesis
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.sun.source.tree.*;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePathScanner;
import org.netbeans.api.java.source.support.ErrorAwareTreePathScanner;
import org.netbeans.api.java.source.support.ErrorAwareTreeScanner;
import java.io.File;
Expand Down Expand Up @@ -1134,6 +1135,96 @@ public void testRemoveAnnotationEnumConstant() throws Exception {
"}\n");
}

public void testAnnotationRemoveLastAttribute1() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n" +
"\n" +
Comment on lines +1141 to +1142
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

want to use code blocks for new tests?

"@Ann( 1 )\n" +
"public class Test {\n" +
"}\n" +
"public @interface Ann {\n" +
" public int value() default 0;\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n" +
"\n" +
"@Ann\n" +
"public class Test {\n" +
"}\n" +
"public @interface Ann {\n" +
" public int value() default 0;\n" +
"}\n";

JavaSource src = getJavaSource(testFile);
Task task = new Task<WorkingCopy>() {

public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
TreeMaker make = workingCopy.getTreeMaker();
CompilationUnitTree cut = workingCopy.getCompilationUnit();
new TreePathScanner<Void, Void>() {
@Override
public Void visitAnnotation(AnnotationTree node, Void p) {
workingCopy.rewrite(node,
make.Annotation(node.getAnnotationType(), List.of()));
return null;
}
}.scan(cut, null);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
//System.err.println(res);
assertEquals(golden, res);
}

public void testAnnotationRemoveLastAttribute2() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n" +
"\n" +
"@Ann( value = 1 )\n" +
"public class Test {\n" +
"}\n" +
"public @interface Ann {\n" +
" public int value() default 0;\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n" +
"\n" +
"@Ann\n" +
"public class Test {\n" +
"}\n" +
"public @interface Ann {\n" +
" public int value() default 0;\n" +
"}\n";

JavaSource src = getJavaSource(testFile);
Task task = new Task<WorkingCopy>() {

public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
TreeMaker make = workingCopy.getTreeMaker();
CompilationUnitTree cut = workingCopy.getCompilationUnit();
new TreePathScanner<Void, Void>() {
@Override
public Void visitAnnotation(AnnotationTree node, Void p) {
workingCopy.rewrite(node,
make.Annotation(node.getAnnotationType(), List.of()));
return null;
}
}.scan(cut, null);
}
};
src.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 @@ -722,7 +722,7 @@ public void test106543() throws Exception {
"/**\n" +
" *aa\n" +
" */\n" +
"@Annotation()\n" +
"@Annotation\n" +
"public class Test {\n" +
"}\n";
JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
Expand Down Expand Up @@ -1231,7 +1231,7 @@ public void testRemoveClassAnnotationAttribute4() throws Exception {
"\n" +
"import java.io.*;\n" +
"\n" +
"@Annotation()\n" +
"@Annotation\n" +
"public class Test {\n" +
" void alois() {\n" +
" }\n" +
Expand Down Expand Up @@ -1275,7 +1275,7 @@ public void testRemoveClassAnnotationAttribute5() throws Exception {
"\n" +
"import java.io.*;\n" +
"\n" +
"@Annotation()\n" +
"@Annotation\n" +
"public class Test {\n" +
" void alois() {\n" +
" }\n" +
Expand Down
Loading