Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManoj Palat2013-03-01 15:51:10 +0000
committerMarkus Keller2013-03-01 15:51:50 +0000
commit6cb1a46319677820636b0fbe76d096b92fe1835a (patch)
tree1dc7f84a5f42b12ed8c10ee779d0976e5147997d
parent06b4da04864008da88066d5a5e945f628c432313 (diff)
downloadeclipse.jdt.core-6cb1a46319677820636b0fbe76d096b92fe1835a.tar.gz
eclipse.jdt.core-6cb1a46319677820636b0fbe76d096b92fe1835a.tar.xz
eclipse.jdt.core-6cb1a46319677820636b0fbe76d096b92fe1835a.zip
Bug 395663: [1.8][ast rewrite] Add rewrite support for annotations on
varargs
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingMethodDeclTest.java174
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteAnalyzer.java82
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java3
-rw-r--r--org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java1
4 files changed, 215 insertions, 45 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingMethodDeclTest.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingMethodDeclTest.java
index d52edf1ffd..d3ebd17702 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingMethodDeclTest.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingMethodDeclTest.java
@@ -2414,6 +2414,20 @@ public class ASTRewritingMethodDeclTest extends ASTRewritingTest {
buf.append(" }\n");
buf.append("}\n");
assertEqualString(preview, buf.toString());
+
+ this.project1.setOption(DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_BEFORE_ELLIPSIS, JavaCore.INSERT);
+
+ preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("public class DD {\n");
+ buf.append(" private void foo1(String format, Object args){\n");
+ buf.append(" }\n");
+ buf.append(" private void foo2(String format, int ... args) {\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
}
public void testAnnotationTypeMember_since_4() throws Exception {
@@ -2699,6 +2713,166 @@ public class ASTRewritingMethodDeclTest extends ASTRewritingTest {
assertEqualString(preview, buf.toString());
}
+ public void testVarArgsAnnotations_since_8() throws Exception {
+ IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
+ StringBuffer buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("@interface Marker {\n");
+ buf.append("}\n");
+ buf.append("public class DD {\n");
+ buf.append(" public void foo1(String format, Object @Marker... args){\n");
+ buf.append(" }\n");
+ buf.append(" public void foo2(Object... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo3(Object @Marker ... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo4(Object @Marker... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo5(Object... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo6(Object args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo7(Object @Marker... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo8(Object @Marker... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo9(@B @C int @A... a) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo10(Object args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo11(Object @Marker... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo12(Object... args) {\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ ICompilationUnit cu= pack1.createCompilationUnit("DD.java", buf.toString(), false, null);
+
+ CompilationUnit astRoot= createAST(cu, false);
+ AST ast= astRoot.getAST();
+ ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
+ TypeDeclaration type= findTypeDeclaration(astRoot, "DD");
+
+ {
+ // Remove annotation from first method args - boundary condition -
+ // - only one annotation should be present.
+ MethodDeclaration methodDecl= findMethodDeclaration(type, "foo1");
+ SingleVariableDeclaration param = (SingleVariableDeclaration) methodDecl.parameters().get(1);
+ rewrite.remove((ASTNode)param.varargsAnnotations().get(0), null);
+
+ // Add one annotation to the second method - boundary condition
+ // - no annotation should be present
+ methodDecl= findMethodDeclaration(type, "foo2");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ MarkerAnnotation markerAnnotation= ast.newMarkerAnnotation();
+ markerAnnotation.setTypeName(ast.newSimpleName("X"));
+ ListRewrite listRewrite= rewrite.getListRewrite(param, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY);
+ listRewrite.insertFirst(markerAnnotation, null);
+
+ // Remove the varargs property - annotation(s) should disappear
+ methodDecl= findMethodDeclaration(type, "foo3");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
+
+ // Remove the varargs property - annotation(s) should disappear
+ // - differs from the above due to the absence of a blank before ...
+ methodDecl= findMethodDeclaration(type, "foo4");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
+
+ // Remove the varargs property - Existing functionality unchanged without annotations
+ methodDecl= findMethodDeclaration(type, "foo5");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
+
+ // Add the varargs property and annotation
+ methodDecl= findMethodDeclaration(type, "foo6");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.TRUE, null);
+ markerAnnotation= ast.newMarkerAnnotation();
+ markerAnnotation.setTypeName(ast.newSimpleName("X"));
+ listRewrite= rewrite.getListRewrite(param, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY);
+ listRewrite.insertFirst(markerAnnotation, null);
+
+ // Replace annotation
+ methodDecl= findMethodDeclaration(type, "foo7");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ markerAnnotation= ast.newMarkerAnnotation();
+ markerAnnotation.setTypeName(ast.newSimpleName("X"));
+ rewrite.replace((ASTNode)param.varargsAnnotations().get(0), markerAnnotation, null);
+
+ // Reset and Set Varargs - output should not change.
+ methodDecl= findMethodDeclaration(type, "foo8");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
+ rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.TRUE, null);
+
+ // Add multiple (two) annotations, remove an existing annotation
+ methodDecl= findMethodDeclaration(type, "foo9");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ NormalAnnotation normalAnnotation = ast.newNormalAnnotation();
+ normalAnnotation.setTypeName(ast.newSimpleName("X"));
+ listRewrite= rewrite.getListRewrite(param, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY);
+ listRewrite.insertFirst(normalAnnotation, null);
+ markerAnnotation= ast.newMarkerAnnotation();
+ markerAnnotation.setTypeName(ast.newSimpleName("Y"));
+ listRewrite.insertFirst(markerAnnotation, null);
+ rewrite.remove((ASTNode)param.varargsAnnotations().get(0), null);
+
+ // Add the varargs property
+ methodDecl= findMethodDeclaration(type, "foo10");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.TRUE, null);
+
+ // Remove the annotations and varargs property as well.
+ methodDecl= findMethodDeclaration(type, "foo11");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ rewrite.remove((ASTNode)param.varargsAnnotations().get(0), null);
+ rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
+
+ // Add an annotation but remove the varargs property - should not add the annotation.
+ methodDecl= findMethodDeclaration(type, "foo12");
+ param = (SingleVariableDeclaration) methodDecl.parameters().get(0);
+ rewrite.set(param, SingleVariableDeclaration.VARARGS_PROPERTY, Boolean.FALSE, null);
+ markerAnnotation= ast.newMarkerAnnotation();
+ markerAnnotation.setTypeName(ast.newSimpleName("X"));
+ listRewrite= rewrite.getListRewrite(param, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY);
+ listRewrite.insertFirst(markerAnnotation, null);
+ }
+
+ String preview= evaluateRewrite(cu, rewrite);
+
+ buf= new StringBuffer();
+ buf.append("package test1;\n");
+ buf.append("@interface Marker {\n");
+ buf.append("}\n");
+ buf.append("public class DD {\n");
+ buf.append(" public void foo1(String format, Object... args){\n");
+ buf.append(" }\n");
+ buf.append(" public void foo2(Object @X... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo3(Object args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo4(Object args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo5(Object args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo6(Object @X... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo7(Object @X... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo8(Object @Marker... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo9(@B @C int @Y @X()... a) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo10(Object... args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo11(Object args) {\n");
+ buf.append(" }\n");
+ buf.append(" public void foo12(Object args) {\n");
+ buf.append(" }\n");
+ buf.append("}\n");
+ assertEqualString(preview, buf.toString());
+ }
public void testMethodDeclChangesBug77538() throws Exception {
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
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 a0b8c99a5a..b69f2c88b3 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
@@ -1499,7 +1499,10 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
RewriteEvent[] children= event.getChildren();
boolean isAllInsert= isAllOfKind(children, RewriteEvent.INSERTED);
boolean isAllRemove= isAllOfKind(children, RewriteEvent.REMOVED);
- if (isAllInsert || isAllRemove) {
+ String keyword= Util.EMPTY_STRING;
+ if (property == SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY) {
+ keyword= " "; //$NON-NLS-1$
+ } else if (isAllInsert || isAllRemove) {
// update pos
try {
pos= getScanner().getNextStartOffset(pos, false);
@@ -1509,23 +1512,23 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
}
Prefix formatterPrefix;
- if (property == SingleVariableDeclaration.MODIFIERS2_PROPERTY)
+ if (property == SingleVariableDeclaration.MODIFIERS2_PROPERTY || property == SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY)
formatterPrefix= this.formatter.PARAM_ANNOTATION_SEPARATION;
else
formatterPrefix= this.formatter.ANNOTATION_SEPARATION;
- int endPos= new ModifierRewriter(formatterPrefix).rewriteList(node, property, pos, Util.EMPTY_STRING, " "); //$NON-NLS-1$
+ int endPos= new ModifierRewriter(formatterPrefix).rewriteList(node, property, pos, keyword, " "); //$NON-NLS-1$
try {
int nextPos= getScanner().getNextStartOffset(endPos, false);
-
- boolean lastUnchanged= children[children.length - 1].getChangeKind() != RewriteEvent.UNCHANGED;
+ RewriteEvent lastChild = children[children.length - 1];
+ boolean lastUnchanged= lastChild.getChangeKind() != RewriteEvent.UNCHANGED;
if (isAllRemove) {
- doTextRemove(endPos, nextPos - endPos, getEditGroup(children[children.length - 1]));
+ doTextRemove(endPos, nextPos - endPos, getEditGroup(lastChild));
return nextPos;
- } else if (isAllInsert || (nextPos == endPos && lastUnchanged)) { // see bug 165654
- RewriteEvent lastChild= children[children.length - 1];
+ } else if ((isAllInsert || (nextPos == endPos && lastUnchanged)) // see bug 165654
+ && property != SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY) {
String separator;
if (lastChild.getNewValue() instanceof Annotation) {
separator= formatterPrefix.getPrefix(getIndent(pos));
@@ -1541,42 +1544,12 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
}
private int rewriteTypeAnnotations(ASTNode node, ChildListPropertyDescriptor property, int pos) {
- RewriteEvent event = getEvent(node, property);
- if (event == null || event.getChangeKind() == RewriteEvent.UNCHANGED) {
- return doVisit(node, property, pos);
- }
- RewriteEvent[] children = event.getChildren();
- boolean isAllInsert = isAllOfKind(children, RewriteEvent.INSERTED);
- boolean isAllRemove = isAllOfKind(children, RewriteEvent.REMOVED);
- if (isAllInsert || isAllRemove) {
- try {
- pos = getScanner().getNextStartOffset(pos, false);
- } catch (CoreException e) {
- handleException(e);
- }
- }
-
- Prefix formatterPrefix = this.formatter.ANNOTATION_SEPARATION;
-
- int endPos = new ModifierRewriter(formatterPrefix).rewriteList(node, property, pos, Util.EMPTY_STRING, " "); //$NON-NLS-1$
-
- try {
- int nextPos = getScanner().getNextStartOffset(endPos, false);
- RewriteEvent lastChild = children[children.length - 1];
- boolean lastUnchanged = lastChild.getChangeKind() != RewriteEvent.UNCHANGED;
-
- if (isAllRemove) {
- doTextRemove(endPos, nextPos - endPos, getEditGroup(lastChild));
- return nextPos;
- } else if (isAllInsert || (nextPos == endPos && lastUnchanged)) {
- doTextInsert(endPos, formatterPrefix.getPrefix(getIndent(pos)), getEditGroup(lastChild));
- }
- } catch (CoreException e) {
- handleException(e);
- }
- return endPos;
+ return rewriteModifiers2(node, property, pos);
}
+ private int rewriteVarargsAnnotations(ASTNode node, ChildListPropertyDescriptor property, int pos) {
+ return rewriteModifiers2(node, property, pos);
+ }
private void replaceOperation(int posBeforeOperation, String newOperation, TextEditGroup editGroup) {
try {
@@ -2876,16 +2849,35 @@ public final class ASTRewriteAnalyzer extends ASTVisitor {
pos= rewriteRequiredNode(node, SingleVariableDeclaration.TYPE_PROPERTY);
if (apiLevel >= JLS3_INTERNAL) {
if (isChanged(node, SingleVariableDeclaration.VARARGS_PROPERTY)) {
+ TextEditGroup editGroup = getEditGroup(node, SingleVariableDeclaration.VARARGS_PROPERTY);
if (getNewValue(node, SingleVariableDeclaration.VARARGS_PROPERTY).equals(Boolean.TRUE)) {
- doTextInsert(pos, "...", getEditGroup(node, SingleVariableDeclaration.VARARGS_PROPERTY)); //$NON-NLS-1$
+ if (apiLevel >= AST.JLS8) {
+ pos= rewriteVarargsAnnotations(node, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY, pos);
+ }
+ int indent= getIndent(node.getStartPosition());
+ String prefix= this.formatter.VARARGS.getPrefix(indent);
+ doTextInsert(pos, prefix, editGroup);
+ doTextInsert(pos, "...", editGroup); //$NON-NLS-1$
} else {
try {
- int ellipsisEnd= getScanner().getNextEndOffset(pos, true);
- doTextRemove(pos, ellipsisEnd - pos, getEditGroup(node, SingleVariableDeclaration.VARARGS_PROPERTY));
+ int ellipsisEnd;
+ int noOfAnnotations = apiLevel >= AST.JLS8 ? node.varargsAnnotations().size() : 0;
+ if (noOfAnnotations > 0) {
+ Annotation annotation= (Annotation) node.varargsAnnotations().get(noOfAnnotations - 1);
+ int annotationEndPosition= annotation.getStartPosition() + annotation.getLength();
+ ellipsisEnd= getScanner().getNextEndOffset(annotationEndPosition, true);
+ } else {
+ ellipsisEnd= getScanner().getNextEndOffset(pos, true);
+ }
+ doTextRemove(pos, ellipsisEnd - pos, editGroup);
} catch (CoreException e) {
handleException(e);
}
}
+ } else {
+ if (apiLevel >= AST.JLS8 && node.isVarargs()) {
+ pos = rewriteVarargsAnnotations(node, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY, pos);
+ }
}
if (!node.isVarargs()) {
ensureSpaceAfterReplace(node, SingleVariableDeclaration.TYPE_PROPERTY);
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
index a678af15d8..13d3eef4d5 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFlattener.java
@@ -820,6 +820,9 @@ public class ASTRewriteFlattener extends ASTVisitor {
visitList(node, SingleVariableDeclaration.MODIFIERS2_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' '));
}
getChildNode(node, SingleVariableDeclaration.TYPE_PROPERTY).accept(this);
+ if (node.getAST().apiLevel() >= AST.JLS8 && node.isVarargs()) {
+ visitList(node, SingleVariableDeclaration.VARARGS_ANNOTATIONS_PROPERTY, String.valueOf(' '), Util.EMPTY_STRING, String.valueOf(' '));
+ }
if (node.getAST().apiLevel() >= JLS3_INTERNAL) {
if (getBooleanAttribute(node, SingleVariableDeclaration.VARARGS_PROPERTY)) {
this.result.append("...");//$NON-NLS-1$
diff --git a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java
index aad5bf8dcc..a6dd2dc050 100644
--- a/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java
+++ b/org.eclipse.jdt.core/dom/org/eclipse/jdt/internal/core/dom/rewrite/ASTRewriteFormatter.java
@@ -554,6 +554,7 @@ import org.eclipse.text.edits.TextEdit;
public final Prefix FIRST_ENUM_CONST= new FormattingPrefix("enum E { X;}", "{ X" , CodeFormatter.K_COMPILATION_UNIT); //$NON-NLS-1$ //$NON-NLS-2$
public final Prefix ANNOTATION_SEPARATION= new FormattingPrefix("@A @B class C {}", "A @" , CodeFormatter.K_COMPILATION_UNIT); //$NON-NLS-1$ //$NON-NLS-2$
public final Prefix PARAM_ANNOTATION_SEPARATION= new FormattingPrefix("void foo(@A @B C p) { }", "A @" , CodeFormatter.K_CLASS_BODY_DECLARATIONS); //$NON-NLS-1$ //$NON-NLS-2$
+ public final Prefix VARARGS= new FormattingPrefix("void foo(A ... a) { }", "A ." , CodeFormatter.K_CLASS_BODY_DECLARATIONS); //$NON-NLS-1$ //$NON-NLS-2$
public final Prefix TRY_RESOURCES = new FormattingPrefix("try (A a = new A(); B b = new B()) {}", "; B" , CodeFormatter.K_STATEMENTS); //$NON-NLS-1$ //$NON-NLS-2$
public final Prefix TRY_RESOURCES_PAREN = new FormattingPrefix("try (A a = new A(); B b = new B()) {}", "y (" , CodeFormatter.K_STATEMENTS); //$NON-NLS-1$ //$NON-NLS-2$

Back to the top