Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesper S Moller2013-03-15 15:30:43 +0000
committerssankaran2013-03-15 15:30:43 +0000
commitafe1aa1e23ce83b573f50388f1a50d564fe4b851 (patch)
treee35af0cab0e9016776a505d8b86839e92d04bf62
parentfe98ddbed4cd1f8bb449a78291ed05ed04f18196 (diff)
downloadeclipse.jdt.core-afe1aa1e23ce83b573f50388f1a50d564fe4b851.tar.gz
eclipse.jdt.core-afe1aa1e23ce83b573f50388f1a50d564fe4b851.tar.xz
eclipse.jdt.core-afe1aa1e23ce83b573f50388f1a50d564fe4b851.zip
Formatter308
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunAllJava8Tests.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunFormatterTests.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterJSR308Tests.java351
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testLambda/A_in.java44
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testLambda/A_out.java51
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testReferenceExpression/A_in.java21
-rw-r--r--org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testReferenceExpression/A_out.java21
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java336
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java52
9 files changed, 675 insertions, 205 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunAllJava8Tests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunAllJava8Tests.java
index cd1112eb44..3bd54f6e8c 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunAllJava8Tests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunAllJava8Tests.java
@@ -44,6 +44,7 @@ import org.eclipse.jdt.core.tests.dom.ASTConverterBugsTestJLS8;
import org.eclipse.jdt.core.tests.dom.ASTConverterTestAST8_2;
import org.eclipse.jdt.core.tests.dom.ConverterTestSetup;
import org.eclipse.jdt.core.tests.dom.TypeAnnotationsConverterTest;
+import org.eclipse.jdt.core.tests.formatter.FormatterJSR308Tests;
import org.eclipse.jdt.core.tests.formatter.FormatterJSR335Tests;
import org.eclipse.jdt.core.tests.rewrite.describing.ASTRewritingTest;
@@ -67,6 +68,7 @@ public class RunAllJava8Tests extends TestCase {
ExpressionContextTests.class,
FlowAnalysisTest8.class,
FormatterJSR335Tests.class,
+ FormatterJSR308Tests.class,
};
}
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunFormatterTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunFormatterTests.java
index 20da4d9149..e8e6aba074 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunFormatterTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/RunFormatterTests.java
@@ -12,6 +12,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Jesper S Moller - Contribution for bug 402173
+ * Contribution for bug 402892
*******************************************************************************/
package org.eclipse.jdt.core.tests;
@@ -37,6 +38,7 @@ public class RunFormatterTests extends junit.framework.TestCase {
TEST_SUITES.add(FormatterCommentsBugsTest.class);
TEST_SUITES.add(FormatterCommentsTests.class);
TEST_SUITES.add(FormatterJSR335Tests.class);
+ TEST_SUITES.add(FormatterJSR308Tests.class);
TEST_SUITES.add(FormatterCommentsClearBlankLinesTests.class);
TEST_SUITES.add(FormatterJavadocDontIndentTagsTests.class);
TEST_SUITES.add(FormatterJavadocDontIndentTagsDescriptionTests.class);
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterJSR308Tests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterJSR308Tests.java
new file mode 100644
index 0000000000..699bbe34cf
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterJSR308Tests.java
@@ -0,0 +1,351 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Jesper Steen Moller and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * This is an implementation of an early-draft specification developed under the Java
+ * Community Process (JCP) and is made available for testing and evaluation purposes
+ * only. The code is not compatible with any specification of the JCP.
+ *
+ * Contributors:
+ * Jesper Steen Moller - initial API and implementation */
+
+package org.eclipse.jdt.core.tests.formatter;
+
+import java.util.Map;
+
+import junit.framework.Test;
+
+import org.eclipse.core.resources.IWorkspaceDescription;
+import org.eclipse.jdt.core.ICompilationUnit;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.formatter.CodeFormatter;
+import org.eclipse.jdt.core.tests.model.AbstractJavaModelTests;
+import org.eclipse.jdt.core.tests.util.Util;
+import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
+import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
+import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
+import org.eclipse.text.edits.TextEdit;
+
+public class FormatterJSR308Tests extends AbstractJavaModelTests {
+
+ protected static IJavaProject JAVA_PROJECT;
+
+ public static final int UNKNOWN_KIND = 0;
+ public static final String IN = "_in";
+ public static final String OUT = "_out";
+ public static final boolean DEBUG = false;
+ static final String LINE_SEPARATOR = System.getProperty("line.separator");
+ private static final String PROJECT_NAME = "FormatterJSR308";
+ private long time;
+
+ DefaultCodeFormatterOptions formatterPrefs;
+ Map formatterOptions;
+
+ static {
+// TESTS_NUMBERS = new int[] { 783 };
+// TESTS_RANGE = new int[] { 734, -1 };
+// TESTS_NAMES = new String [] { "test000" };
+ }
+ public static Test suite() {
+ return buildModelTestSuite(FormatterJSR308Tests.class);
+ }
+
+ public FormatterJSR308Tests(String name) {
+ super(name);
+ }
+
+ String runFormatter(CodeFormatter codeFormatter, String source, int kind, int indentationLevel, int offset, int length, String lineSeparator, boolean repeat) {
+// long time = System.currentTimeMillis();
+ TextEdit edit = codeFormatter.format(kind, source, offset, length, indentationLevel, lineSeparator);//$NON-NLS-1$
+// System.out.println((System.currentTimeMillis() - time) + " ms");
+ if (edit == null) return null;
+// System.out.println(edit.getChildrenSize() + " edits");
+ String result = org.eclipse.jdt.internal.core.util.Util.editedString(source, edit);
+
+ if (repeat && length == source.length()) {
+// time = System.currentTimeMillis();
+ edit = codeFormatter.format(kind, result, 0, result.length(), indentationLevel, lineSeparator);//$NON-NLS-1$
+// System.out.println((System.currentTimeMillis() - time) + " ms");
+ if (edit == null) return null;
+// assertEquals("Should not have edits", 0, edit.getChildren().length);
+ final String result2 = org.eclipse.jdt.internal.core.util.Util.editedString(result, edit);
+ if (!result.equals(result2)) {
+ assertSourceEquals("Second formatting is different from first one!", Util.convertToIndependantLineDelimiter(result), Util.convertToIndependantLineDelimiter(result2));
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Init formatter preferences with Eclipse default settings.
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ this.formatterPrefs = DefaultCodeFormatterOptions.getEclipseDefaultSettings();
+ if (JAVA_PROJECT != null) {
+ this.formatterOptions = JAVA_PROJECT.getOptions(true);
+ }
+ this.formatterOptions.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_8);
+ this.formatterOptions.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_8);
+ this.formatterOptions.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_8);
+ }
+
+ /**
+ * Create project and set the jar placeholder.
+ */
+ public void setUpSuite() throws Exception {
+ // ensure autobuilding is turned off
+ IWorkspaceDescription description = getWorkspace().getDescription();
+ if (description.isAutoBuilding()) {
+ description.setAutoBuilding(false);
+ getWorkspace().setDescription(description);
+ }
+
+ if (JAVA_PROJECT == null) {
+ JAVA_PROJECT = setUpJavaProject(PROJECT_NAME, "1.8"); //$NON-NLS-1$
+ }
+
+ if (DEBUG) {
+ this.time = System.currentTimeMillis();
+ }
+ }
+
+ /**
+ * Reset the jar placeholder and delete project.
+ */
+ public void tearDownSuite() throws Exception {
+ deleteProject(JAVA_PROJECT); //$NON-NLS-1$
+ JAVA_PROJECT = null;
+ if (DEBUG) {
+ System.out.println("Time spent = " + (System.currentTimeMillis() - this.time));//$NON-NLS-1$
+ }
+ super.tearDownSuite();
+ }
+
+ private String getIn(String compilationUnitName) {
+ assertNotNull(compilationUnitName);
+ int dotIndex = compilationUnitName.indexOf('.');
+ assertTrue(dotIndex != -1);
+ return compilationUnitName.substring(0, dotIndex) + IN + compilationUnitName.substring(dotIndex);
+ }
+
+ private String getOut(String compilationUnitName) {
+ assertNotNull(compilationUnitName);
+ int dotIndex = compilationUnitName.indexOf('.');
+ assertTrue(dotIndex != -1);
+ return compilationUnitName.substring(0, dotIndex) + OUT + compilationUnitName.substring(dotIndex);
+ }
+
+ void assertLineEquals(String actualContents, String originalSource, String expectedContents, boolean checkNull) {
+ if (actualContents == null) {
+ assertTrue("actualContents is null", checkNull);
+ assertEquals(expectedContents, originalSource);
+ return;
+ }
+ assertSourceEquals("Different number of length", Util.convertToIndependantLineDelimiter(expectedContents), actualContents);
+ }
+
+ private void runTest(String packageName, String compilationUnitName) {
+ DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.formatterPrefs, this.formatterOptions);
+ runTest(codeFormatter, packageName, compilationUnitName, CodeFormatter.K_COMPILATION_UNIT, 0);
+ }
+
+ private void runTest(CodeFormatter codeFormatter, String packageName, String compilationUnitName, int kind, int indentationLevel) {
+ runTest(codeFormatter, packageName, compilationUnitName, kind, indentationLevel, false, 0, -1);
+ }
+ private void runTest(CodeFormatter codeFormatter, String packageName, String compilationUnitName, int kind, int indentationLevel, boolean checkNull, int offset, int length) {
+ runTest(codeFormatter, packageName, compilationUnitName, kind, indentationLevel, checkNull, offset, length, null);
+ }
+
+ private void runTest(CodeFormatter codeFormatter, String packageName, String compilationUnitName, int kind, int indentationLevel, boolean checkNull, int offset, int length, String lineSeparator) {
+ try {
+ ICompilationUnit sourceUnit = getCompilationUnit(PROJECT_NAME , "", packageName, getIn(compilationUnitName)); //$NON-NLS-1$ //$NON-NLS-2$
+ String source = sourceUnit.getSource();
+ assertNotNull(source);
+ ICompilationUnit outputUnit = getCompilationUnit(PROJECT_NAME , "", packageName, getOut(compilationUnitName)); //$NON-NLS-1$ //$NON-NLS-2$
+ assertNotNull(outputUnit);
+ String result;
+ if (length == -1) {
+ result = runFormatter(codeFormatter, source, kind, indentationLevel, offset, source.length(), lineSeparator, true);
+ } else {
+ result = runFormatter(codeFormatter, source, kind, indentationLevel, offset, length, lineSeparator, true);
+ }
+ assertLineEquals(result, source, outputUnit.getSource(), checkNull);
+ } catch (JavaModelException e) {
+ e.printStackTrace();
+ assertTrue(false);
+ }
+ }
+
+ public void testLambda() {
+ runTest("testLambda", "A.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void testReferenceExpression() {
+ runTest("testReferenceExpression", "A.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test000() {
+ runTest("test000", "I.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test001() {
+ runTest("test001", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test002() {
+ runTest("test002", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test003() {
+ runTest("test003", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test004() {
+ runTest("test004", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test005() {
+ runTest("test005", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test006() {
+ runTest("test006", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test007() {
+ runTest("test007", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test008() {
+ runTest("test008", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test009() {
+ runTest("test009", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test010() {
+ runTest("test010", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test011() {
+ runTest("test011", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test012() {
+ runTest("test012", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test013() {
+ runTest("test013", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test014() {
+ runTest("test014", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test015() {
+ runTest("test015", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test016() {
+ runTest("test016", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test017() {
+ runTest("test017", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test018() {
+ runTest("test018", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test019() {
+ runTest("test019", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test020() {
+ runTest("test020", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test021() {
+ runTest("test021", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test022() {
+ runTest("test022", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test023() {
+ runTest("test023", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test024() {
+ runTest("test024", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test025() {
+ runTest("test025", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test026() {
+ runTest("test026", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test027() {
+ runTest("test027", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test028() {
+ runTest("test028", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test029() {
+ runTest("test029", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test030() {
+ runTest("test030", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test031() {
+ runTest("test031", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test032() {
+ runTest("test032", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test033() {
+ runTest("test033", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test034() {
+ runTest("test034", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test035() {
+ runTest("test035", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test036() {
+ runTest("test036", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test037() {
+ runTest("test037", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test038() {
+ runTest("test038", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+
+ public void test039() {
+ runTest("test039", "X.java");//$NON-NLS-1$ //$NON-NLS-2$
+ }
+} \ No newline at end of file
diff --git a/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testLambda/A_in.java b/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testLambda/A_in.java
new file mode 100644
index 0000000000..39744d7a62
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testLambda/A_in.java
@@ -0,0 +1,44 @@
+/**
+ * Test
+ *
+ */
+package p1.p2.test;
+
+import java.io.Serializable;
+import java.io.IOException;
+
+public abstract class A extends java.lang.Object implements Runnable, Cloneable, Serializable {
+ public void run() {
+ }
+
+ public void lambdas() {
+ Runnable a = () -> {
+ thisIsCrazy();
+ iJustMet(you);
+ here.number.callMe();
+ };
+
+ Func idA = x -> x;
+
+ Func idB = x -> { return x; };
+
+ Func idC = x -> x;
+
+ Func id2 = x -> {
+ if (x == null)
+ {
+ return null;
+ }
+ else
+ return x;
+ };
+
+ ImaginableFunction<String, Integer> f =
+ (String input) -> input.length();
+
+ ImaginableFunction<String, Integer> f2 = (String input) -> { return input.length() + new LetsPretend() { int howMany() { return 42; } }
+
+.howMany(); };
+ }
+
+} \ No newline at end of file
diff --git a/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testLambda/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testLambda/A_out.java
new file mode 100644
index 0000000000..1bcd7c3422
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testLambda/A_out.java
@@ -0,0 +1,51 @@
+/**
+ * Test
+ *
+ */
+package p1.p2.test;
+
+import java.io.Serializable;
+import java.io.IOException;
+
+public abstract class A extends java.lang.Object implements Runnable,
+ Cloneable, Serializable {
+ public void run() {
+ }
+
+ public void lambdas() {
+ Runnable a = () -> {
+ thisIsCrazy();
+ iJustMet(you);
+ here.number.callMe();
+ };
+
+ Func idA = x -> x;
+
+ Func idB = x -> {
+ return x;
+ };
+
+ Func idC = x -> x;
+
+ Func id2 = x -> {
+ if (x == null) {
+ return null;
+ } else
+ return x;
+ };
+
+ ImaginableFunction<String, Integer> f = (String input) -> input
+ .length();
+
+ ImaginableFunction<String, Integer> f2 = (String input) -> {
+ return input.length() + new LetsPretend() {
+ int howMany() {
+ return 42;
+ }
+ }
+
+ .howMany();
+ };
+ }
+
+} \ No newline at end of file
diff --git a/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testReferenceExpression/A_in.java b/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testReferenceExpression/A_in.java
new file mode 100644
index 0000000000..4499467645
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testReferenceExpression/A_in.java
@@ -0,0 +1,21 @@
+/**
+ * Test
+ *
+ */
+package p1.p2.test;
+
+import java.io.Serializable;
+import java.io.IOException;
+
+public abstract class A extends java.lang.Object implements Runnable, Cloneable, Serializable {
+ public void run() {
+ }
+
+ private static final Functionalish<List<Integer>, Integer> WHATEVER = List< Integer > :: size ;
+
+ public void referenceExpression() {
+ ImaginableFunction<String, Integer> func = Klazz
+ :: method;
+ }
+
+} \ No newline at end of file
diff --git a/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testReferenceExpression/A_out.java b/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testReferenceExpression/A_out.java
new file mode 100644
index 0000000000..2652404a61
--- /dev/null
+++ b/org.eclipse.jdt.core.tests.model/workspace/FormatterJSR308/testReferenceExpression/A_out.java
@@ -0,0 +1,21 @@
+/**
+ * Test
+ *
+ */
+package p1.p2.test;
+
+import java.io.Serializable;
+import java.io.IOException;
+
+public abstract class A extends java.lang.Object implements Runnable,
+ Cloneable, Serializable {
+ public void run() {
+ }
+
+ private static final Functionalish<List<Integer>, Integer> WHATEVER = List<Integer>::size;
+
+ public void referenceExpression() {
+ ImaginableFunction<String, Integer> func = Klazz::method;
+ }
+
+} \ No newline at end of file
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java
index 3b49d3d973..40d58585fa 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java
@@ -15,6 +15,7 @@
* Nanda Firdausi - Contribution for bug 298844
* Jesper S Moller - Contribution for bug 402173
* Contribution for bug 402174
+ * Contribution for bug 402892
*******************************************************************************/
package org.eclipse.jdt.internal.formatter;
@@ -32,6 +33,7 @@ import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.AbstractVariableDeclaration;
import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
+import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Argument;
import org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression;
@@ -634,16 +636,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);
- /*
- * Check for extra dimensions
- */
- int extraDimensions = getDimensions();
- if (extraDimensions != 0) {
- for (int i = 0; i < extraDimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
- }
+ formatExtraDimensions(fieldDeclaration.type);
/*
* Field initialization
@@ -686,6 +679,41 @@ public class CodeFormatterVisitor extends ASTVisitor {
}
}
+ private void formatExtraDimensions(TypeReference typeReference) {
+ /*
+ * Check for extra dimensions
+ */
+ int extraDimensions = getDimensions();
+ int anchor = typeReference != null ? (typeReference.dimensions() - extraDimensions) : 0;
+ if (extraDimensions != 0) formatDimensions(typeReference, anchor, extraDimensions, true);
+ }
+
+ private void formatLeadingDimensions(TypeReference typeReference, boolean spaceBeforeAnnotation) {
+ int leadingDimensions = Math.min(getDimensions(), typeReference != null ? typeReference.dimensions() : 0);
+ if (leadingDimensions != 0) formatDimensions(typeReference, 0, leadingDimensions, spaceBeforeAnnotation);
+ }
+
+ private void formatDimensions(TypeReference typeReference, int anchor, int count, boolean spaceBeforeAnnotation) {
+ if (count != 0) {
+ if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) {
+ this.scribe.space();
+ }
+ Annotation[][] annotationsOnDimensions = typeReference != null ? typeReference.getAnnotationsOnDimensions() : null;
+ for (int i = 0; i < count; i++) {
+ int dimensionIndex = anchor + i;
+ if (annotationsOnDimensions != null && annotationsOnDimensions.length > dimensionIndex) {
+ boolean hadAnnotations = formatInlineAnnotations(annotationsOnDimensions[dimensionIndex], spaceBeforeAnnotation && i == 0);
+ if (hadAnnotations && this.preferences.insert_space_before_opening_bracket_in_array_type_reference) this.scribe.space();
+ }
+ this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
+ if (this.preferences.insert_space_between_brackets_in_array_type_reference) {
+ this.scribe.space();
+ }
+ this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
+ }
+ }
+ }
+
private void format(ImportReference importRef, boolean isLast) {
this.scribe.printNextToken(TerminalTokens.TokenNameimport);
if (!isLast) this.scribe.blank_lines_between_import_groups = this.preferences.blank_lines_between_import_groups;
@@ -763,16 +791,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, false);
}
- /*
- * Check for extra dimensions
- */
- int extraDimensions = getDimensions();
- if (extraDimensions != 0) {
- for (int index = 0; index < extraDimensions; index++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
- }
+ formatExtraDimensions(fieldDeclaration.type);
/*
* Field initialization
@@ -1677,16 +1696,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
*/
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, false);
}
- /*
- * Check for extra dimensions
- */
- int extraDimensions = getDimensions();
- if (extraDimensions != 0) {
- for (int index = 0; index < extraDimensions; index++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
- }
+ formatExtraDimensions(localDeclaration.type);
final Expression initialization = localDeclaration.initialization;
if (initialization != null) {
@@ -2340,6 +2350,8 @@ public class CodeFormatterVisitor extends ASTVisitor {
this.localScanner.resetTo(this.scribe.scanner.currentPosition, this.scribe.scannerEndPosition - 1);
int dimensions = 0;
int balance = 0;
+ boolean inAnnotation = false;
+ int parenBalance = 0; // for inline annotations
try {
int token;
loop: while ((token = this.localScanner.getNextToken()) != TerminalTokens.TokenNameEOF) {
@@ -2348,6 +2360,20 @@ public class CodeFormatterVisitor extends ASTVisitor {
dimensions++;
balance--;
break;
+ case TerminalTokens.TokenNameLPAREN:
+ parenBalance++;
+ break;
+ case TerminalTokens.TokenNameRPAREN:
+ if (! inAnnotation) break loop;
+ if (--parenBalance == 0) inAnnotation = false;
+ break;
+ case TerminalTokens.TokenNameAT :
+ inAnnotation = true;
+ break;
+ case TerminalTokens.TokenNameIdentifier :
+ case TerminalTokens.TokenNameEQUAL :
+ if (parenBalance == 0 && ! inAnnotation) break loop;
+ break;
case TerminalTokens.TokenNameCOMMENT_BLOCK :
case TerminalTokens.TokenNameCOMMENT_JAVADOC :
case TerminalTokens.TokenNameCOMMENT_LINE :
@@ -2356,7 +2382,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
balance++;
break;
default:
- break loop;
+ if (parenBalance == 0) break loop;
}
}
} catch(InvalidInputException e) {
@@ -2406,6 +2432,28 @@ public class CodeFormatterVisitor extends ASTVisitor {
return false;
}
+ private boolean isNextTokenPunctuation() {
+ this.localScanner.resetTo(this.scribe.scanner.currentPosition, this.scribe.scannerEndPosition - 1);
+ try {
+ int token = this.localScanner.getNextToken();
+ loop: while(true) {
+ switch(token) {
+ case TerminalTokens.TokenNameCOMMENT_BLOCK :
+ case TerminalTokens.TokenNameCOMMENT_JAVADOC :
+ case TerminalTokens.TokenNameCOMMENT_LINE :
+ token = this.localScanner.getNextToken();
+ continue loop;
+ default:
+ break loop;
+ }
+ }
+ return ! (Scanner.isLiteral(token) || Scanner.isKeyword(token) || Scanner.isIdentifier(token));
+ } catch(InvalidInputException e) {
+ // ignore
+ }
+ return false;
+ }
+
private boolean isClosingGenericToken() {
this.localScanner.resetTo(this.scribe.scanner.currentPosition, this.scribe.scannerEndPosition - 1);
try {
@@ -2624,8 +2672,15 @@ public class CodeFormatterVisitor extends ASTVisitor {
*/
int extraDimensions = annotationTypeMemberDeclaration.extendedDimensions;
if (extraDimensions != 0) {
+
+ if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) {
+ this.scribe.space();
+ }
for (int i = 0; i < extraDimensions; i++) {
this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
+ if (this.preferences.insert_space_between_brackets_in_array_type_reference) {
+ this.scribe.space();
+ }
this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
}
}
@@ -2681,18 +2736,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);
}
-
- /*
- * Check for extra dimensions
- */
- int extraDimensions = getDimensions();
- if (extraDimensions != 0) {
- for (int i = 0; i < extraDimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
- }
-
+ formatExtraDimensions(argument.type);
return false;
}
@@ -2714,6 +2758,9 @@ public class CodeFormatterVisitor extends ASTVisitor {
final Expression[] dimensions = arrayAllocationExpression.dimensions;
int dimensionsLength = dimensions.length;
for (int i = 0; i < dimensionsLength; i++) {
+ if (arrayAllocationExpression.annotationsOnDimensions != null) {
+ formatInlineAnnotations(arrayAllocationExpression.annotationsOnDimensions[i], i == 0);
+ }
if (this.preferences.insert_space_before_opening_bracket_in_array_allocation_expression) {
this.scribe.space();
}
@@ -2896,28 +2943,26 @@ public class CodeFormatterVisitor extends ASTVisitor {
return false;
}
+ private void formatArrayQualifiedTypeReference(ArrayQualifiedTypeReference arrayQualifiedTypeReference) {
+ final int numberOfParens = (arrayQualifiedTypeReference.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
+ if (numberOfParens > 0) {
+ manageOpeningParenthesizedExpression(arrayQualifiedTypeReference, numberOfParens);
+ }
+ formatQualifiedTypeReference(arrayQualifiedTypeReference);
+ formatLeadingDimensions(arrayQualifiedTypeReference, true);
+ if (numberOfParens > 0) {
+ manageClosingParenthesizedExpression(arrayQualifiedTypeReference, numberOfParens);
+ }
+ }
+
/**
* @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference, org.eclipse.jdt.internal.compiler.lookup.BlockScope)
*/
public boolean visit(
- ArrayQualifiedTypeReference arrayQualifiedTypeReference,
- BlockScope scope) {
+ ArrayQualifiedTypeReference arrayQualifiedTypeReference,
+ BlockScope scope) {
- final int numberOfParens = (arrayQualifiedTypeReference.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
- if (numberOfParens > 0) {
- manageOpeningParenthesizedExpression(arrayQualifiedTypeReference, numberOfParens);
- }
- this.scribe.printArrayQualifiedReference(arrayQualifiedTypeReference.tokens.length, arrayQualifiedTypeReference.sourceEnd);
- int dimensions = getDimensions();
- if (dimensions != 0) {
- for (int i = 0; i < dimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
- }
- if (numberOfParens > 0) {
- manageClosingParenthesizedExpression(arrayQualifiedTypeReference, numberOfParens);
- }
+ formatArrayQualifiedTypeReference(arrayQualifiedTypeReference);
return false;
}
@@ -2928,21 +2973,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
ArrayQualifiedTypeReference arrayQualifiedTypeReference,
ClassScope scope) {
- final int numberOfParens = (arrayQualifiedTypeReference.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
- if (numberOfParens > 0) {
- manageOpeningParenthesizedExpression(arrayQualifiedTypeReference, numberOfParens);
- }
- this.scribe.printArrayQualifiedReference(arrayQualifiedTypeReference.tokens.length, arrayQualifiedTypeReference.sourceEnd);
- int dimensions = getDimensions();
- if (dimensions != 0) {
- for (int i = 0; i < dimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
- }
- if (numberOfParens > 0) {
- manageClosingParenthesizedExpression(arrayQualifiedTypeReference, numberOfParens);
- }
+ formatArrayQualifiedTypeReference(arrayQualifiedTypeReference);
return false;
}
@@ -2981,20 +3012,18 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(arrayTypeReference, numberOfParens);
}
+ if (arrayTypeReference.annotations != null) formatInlineAnnotations(arrayTypeReference.annotations[0], false);
this.scribe.printNextToken(SINGLETYPEREFERENCE_EXPECTEDTOKENS);
int dimensions = getDimensions();
if (dimensions != 0) {
- if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) {
- this.scribe.space();
- }
- for (int i = 0; i < dimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- if (this.preferences.insert_space_between_brackets_in_array_type_reference) {
- this.scribe.space();
- }
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
+ formatDimensions(arrayTypeReference, 0, dimensions, true);
+ }
+ // dimensions the number of visible bracket-pairs. Ellipsis is an extra dimension
+ if ((arrayTypeReference.bits & ASTNode.IsVarArgs) != 0
+ && arrayTypeReference.annotationsOnDimensions != null
+ && arrayTypeReference.annotationsOnDimensions.length > dimensions) {
+ formatInlineAnnotations(arrayTypeReference.annotationsOnDimensions[dimensions], true);
}
if (numberOfParens > 0) {
manageClosingParenthesizedExpression(arrayTypeReference, numberOfParens);
@@ -3013,19 +3042,17 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(arrayTypeReference, numberOfParens);
}
+ if (arrayTypeReference.annotations != null) formatInlineAnnotations(arrayTypeReference.annotations[0], false);
this.scribe.printNextToken(SINGLETYPEREFERENCE_EXPECTEDTOKENS);
int dimensions = getDimensions();
if (dimensions != 0) {
- if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) {
- this.scribe.space();
- }
- for (int i = 0; i < dimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- if (this.preferences.insert_space_between_brackets_in_array_type_reference) {
- this.scribe.space();
- }
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
+ formatDimensions(arrayTypeReference, 0, dimensions, true);
+ }
+ // dimensions the number of visible bracket-pairs. Ellipsis is an extra dimension
+ if ((arrayTypeReference.bits & ASTNode.IsVarArgs) != 0
+ && arrayTypeReference.annotationsOnDimensions != null
+ && arrayTypeReference.annotationsOnDimensions.length > dimensions) {
+ formatInlineAnnotations(arrayTypeReference.annotationsOnDimensions[dimensions], true);
}
if (numberOfParens > 0) {
manageClosingParenthesizedExpression(arrayTypeReference, numberOfParens);
@@ -4520,16 +4547,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
this.preferences.insert_space_after_comma_in_method_declaration_parameters,
this.preferences.alignment_for_parameters_in_method_declaration);
- /*
- * Check for extra dimensions
- */
- int extraDimensions = getDimensions();
- if (extraDimensions != 0) {
- for (int i = 0; i < extraDimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
- }
+ formatExtraDimensions(methodDeclaration.returnType);
// Format throws
formatThrowsClause(
@@ -4665,6 +4683,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(parameterizedQualifiedTypeReference, numberOfParens);
}
+ if (parameterizedQualifiedTypeReference.annotations != null) formatInlineAnnotations(parameterizedQualifiedTypeReference.annotations[0], false);
TypeReference[][] typeArguments = parameterizedQualifiedTypeReference.typeArguments;
int length = typeArguments.length;
for (int i = 0; i < length; i++) {
@@ -4695,21 +4714,12 @@ public class CodeFormatterVisitor extends ASTVisitor {
}
if (i < length - 1) {
this.scribe.printNextToken(TerminalTokens.TokenNameDOT);
- }
- }
- int dimensions = getDimensions();
- if (dimensions != 0 && dimensions <= parameterizedQualifiedTypeReference.dimensions()) {
- if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) {
- this.scribe.space();
- }
- for (int i = 0; i < dimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- if (this.preferences.insert_space_between_brackets_in_array_type_reference) {
- this.scribe.space();
+ if (parameterizedQualifiedTypeReference.annotations != null && parameterizedQualifiedTypeReference.annotations.length > i+1) {
+ formatInlineAnnotations(parameterizedQualifiedTypeReference.annotations[i+1], false);
}
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
}
}
+ formatLeadingDimensions(parameterizedQualifiedTypeReference, true);
if (numberOfParens > 0) {
manageClosingParenthesizedExpression(parameterizedQualifiedTypeReference, numberOfParens);
}
@@ -4722,6 +4732,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(parameterizedQualifiedTypeReference, numberOfParens);
}
+ if (parameterizedQualifiedTypeReference.annotations != null) formatInlineAnnotations(parameterizedQualifiedTypeReference.annotations[0], false);
TypeReference[][] typeArguments = parameterizedQualifiedTypeReference.typeArguments;
int length = typeArguments.length;
for (int i = 0; i < length; i++) {
@@ -4753,20 +4764,11 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (i < length - 1) {
this.scribe.printNextToken(TerminalTokens.TokenNameDOT);
}
- }
- int dimensions = getDimensions();
- if (dimensions != 0 && dimensions <= parameterizedQualifiedTypeReference.dimensions()) {
- if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) {
- this.scribe.space();
- }
- for (int i = 0; i < dimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- if (this.preferences.insert_space_between_brackets_in_array_type_reference) {
- this.scribe.space();
- }
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
+ if (parameterizedQualifiedTypeReference.annotations != null && parameterizedQualifiedTypeReference.annotations.length > i+1) {
+ formatInlineAnnotations(parameterizedQualifiedTypeReference.annotations[i+1], false);
}
}
+ formatLeadingDimensions(parameterizedQualifiedTypeReference, true);
if (numberOfParens > 0) {
manageClosingParenthesizedExpression(parameterizedQualifiedTypeReference, numberOfParens);
}
@@ -4779,6 +4781,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(parameterizedSingleTypeReference, numberOfParens);
}
+ if (parameterizedSingleTypeReference.annotations != null) formatInlineAnnotations(parameterizedSingleTypeReference.annotations[0], false);
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier);
TypeReference[] typeArguments = parameterizedSingleTypeReference.typeArguments;
@@ -4804,19 +4807,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS);
}
- int dimensions = getDimensions();
- if (dimensions != 0 && dimensions <= parameterizedSingleTypeReference.dimensions()) {
- if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) {
- this.scribe.space();
- }
- for (int i = 0; i < dimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- if (this.preferences.insert_space_between_brackets_in_array_type_reference) {
- this.scribe.space();
- }
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
- }
+ formatLeadingDimensions(parameterizedSingleTypeReference, false);
if (numberOfParens > 0) {
manageClosingParenthesizedExpression(parameterizedSingleTypeReference, numberOfParens);
}
@@ -4829,6 +4820,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(parameterizedSingleTypeReference, numberOfParens);
}
+ if (parameterizedSingleTypeReference.annotations != null) formatInlineAnnotations(parameterizedSingleTypeReference.annotations[0], false);
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier);
TypeReference[] typeArguments = parameterizedSingleTypeReference.typeArguments;
@@ -4854,19 +4846,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
this.scribe.printNextToken(CLOSING_GENERICS_EXPECTEDTOKENS);
}
- int dimensions = getDimensions();
- if (dimensions != 0 && dimensions <= parameterizedSingleTypeReference.dimensions()) {
- if (this.preferences.insert_space_before_opening_bracket_in_array_type_reference) {
- this.scribe.space();
- }
- for (int i = 0; i < dimensions; i++) {
- this.scribe.printNextToken(TerminalTokens.TokenNameLBRACKET);
- if (this.preferences.insert_space_between_brackets_in_array_type_reference) {
- this.scribe.space();
- }
- this.scribe.printNextToken(TerminalTokens.TokenNameRBRACKET);
- }
- }
+ formatLeadingDimensions(parameterizedSingleTypeReference, false);
if (numberOfParens > 0) {
manageClosingParenthesizedExpression(parameterizedSingleTypeReference, numberOfParens);
}
@@ -5087,14 +5067,27 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(qualifiedTypeReference, numberOfParens);
}
- this.scribe.printQualifiedReference(qualifiedTypeReference.sourceEnd, numberOfParens>=0/*expect parenthesis*/);
-
+ formatQualifiedTypeReference(qualifiedTypeReference);
if (numberOfParens > 0) {
manageClosingParenthesizedExpression(qualifiedTypeReference, numberOfParens);
}
return false;
}
+ private void formatQualifiedTypeReference(QualifiedTypeReference qualifiedTypeReference) {
+ for (int i = 0; i < qualifiedTypeReference.tokens.length; ++i) {
+ if (i != 0) this.scribe.printNextToken(TerminalTokens.TokenNameDOT, false);
+ if (qualifiedTypeReference.annotations != null
+ && qualifiedTypeReference.annotations.length > i
+ && qualifiedTypeReference.annotations[i] != null) {
+ formatInlineAnnotations(qualifiedTypeReference.annotations[i], false);
+ this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, true);
+ } else {
+ this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier, false);
+ }
+ }
+ }
+
/**
* @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference, org.eclipse.jdt.internal.compiler.lookup.ClassScope)
*/
@@ -5106,7 +5099,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(qualifiedTypeReference, numberOfParens);
}
- this.scribe.printQualifiedReference(qualifiedTypeReference.sourceEnd, numberOfParens>=0/*expect parenthesis*/);
+ formatQualifiedTypeReference(qualifiedTypeReference);
if (numberOfParens > 0) {
manageClosingParenthesizedExpression(qualifiedTypeReference, numberOfParens);
@@ -5212,6 +5205,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(singleTypeReference, numberOfParens);
}
+ if (singleTypeReference.annotations != null) formatInlineAnnotations(singleTypeReference.annotations[0], false);
this.scribe.printNextToken(SINGLETYPEREFERENCE_EXPECTEDTOKENS);
if (numberOfParens > 0) {
@@ -5220,6 +5214,23 @@ public class CodeFormatterVisitor extends ASTVisitor {
return false;
}
+ private boolean formatInlineAnnotations(final Annotation[] annotations, boolean spaceBefore) {
+ if (annotations != null ) {
+ if (spaceBefore) this.scribe.space();
+
+ for (int i = 0; i < annotations.length; ++i) {
+ if (i != 0) this.scribe.space();
+ annotations[i].traverse(this, (BlockScope)null);
+ }
+ if (annotations.length > 0 && ! this.isNextTokenPunctuation()) {
+ this.scribe.space();
+ }
+ return true;
+ } else {
+ return false;
+ }
+ }
+
/**
* @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference, org.eclipse.jdt.internal.compiler.lookup.ClassScope)
*/
@@ -5231,6 +5242,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
if (numberOfParens > 0) {
manageOpeningParenthesizedExpression(singleTypeReference, numberOfParens);
}
+ if (singleTypeReference.annotations != null) formatInlineAnnotations(singleTypeReference.annotations[0], false);
this.scribe.printNextToken(SINGLETYPEREFERENCE_EXPECTEDTOKENS);
if (numberOfParens > 0) {
@@ -5688,6 +5700,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
return false;
}
public boolean visit(TypeParameter typeParameter, BlockScope scope) {
+ if (typeParameter.annotations != null) formatInlineAnnotations(typeParameter.annotations, false);
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier);
if (typeParameter.type != null) {
this.scribe.space();
@@ -5714,6 +5727,7 @@ public class CodeFormatterVisitor extends ASTVisitor {
return false;
}
public boolean visit(TypeParameter typeParameter, ClassScope scope) {
+ if (typeParameter.annotations != null) formatInlineAnnotations(typeParameter.annotations, false);
this.scribe.printNextToken(TerminalTokens.TokenNameIdentifier);
if (typeParameter.type != null) {
this.scribe.space();
@@ -5900,6 +5914,9 @@ public class CodeFormatterVisitor extends ASTVisitor {
return false;
}
public boolean visit(Wildcard wildcard, BlockScope scope) {
+ if (wildcard.annotations != null) {
+ if (formatInlineAnnotations(wildcard.annotations[0], false)) this.scribe.space();
+ }
this.scribe.printNextToken(TerminalTokens.TokenNameQUESTION, this.preferences.insert_space_before_question_in_wilcard);
switch(wildcard.kind) {
case Wildcard.SUPER :
@@ -5920,6 +5937,9 @@ public class CodeFormatterVisitor extends ASTVisitor {
return false;
}
public boolean visit(Wildcard wildcard, ClassScope scope) {
+ if (wildcard.annotations != null) {
+ if (formatInlineAnnotations(wildcard.annotations[0], false)) this.scribe.space();
+ }
this.scribe.printNextToken(TerminalTokens.TokenNameQUESTION, this.preferences.insert_space_before_question_in_wilcard);
switch(wildcard.kind) {
case Wildcard.SUPER :
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java
index ced5686af3..38ac389fc9 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/Scribe.java
@@ -5,9 +5,14 @@
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
+ * This is an implementation of an early-draft specification developed under the Java
+ * Community Process (JCP) and is made available for testing and evaluation purposes
+ * only. The code is not compatible with any specification of the JCP.
+ *
* Contributors:
* IBM Corporation - initial API and implementation
* Ray V. (voidstar@gmail.com) - Contribution for bug 282988
+ * Jesper S Moller - Contribution for bug 402892
*******************************************************************************/
package org.eclipse.jdt.internal.formatter;
@@ -4927,53 +4932,6 @@ public class Scribe implements IJavaDocTagConstants {
}
}
- public void printArrayQualifiedReference(int numberOfTokens, int sourceEnd) {
- int currentTokenStartPosition = this.scanner.currentPosition;
- int numberOfIdentifiers = 0;
- try {
- do {
- printComment(CodeFormatter.K_UNKNOWN, NO_TRAILING_COMMENT);
- switch(this.currentToken = this.scanner.getNextToken()) {
- case TerminalTokens.TokenNameEOF :
- return;
- case TerminalTokens.TokenNameWHITESPACE :
- addDeleteEdit(this.scanner.getCurrentTokenStartPosition(), this.scanner.getCurrentTokenEndPosition());
- currentTokenStartPosition = this.scanner.currentPosition;
- break;
- case TerminalTokens.TokenNameCOMMENT_BLOCK :
- case TerminalTokens.TokenNameCOMMENT_JAVADOC :
- printBlockComment(false);
- currentTokenStartPosition = this.scanner.currentPosition;
- break;
- case TerminalTokens.TokenNameCOMMENT_LINE :
- printLineComment();
- currentTokenStartPosition = this.scanner.currentPosition;
- break;
- case TerminalTokens.TokenNameIdentifier :
- print(this.scanner.currentPosition - this.scanner.startPosition, false);
- currentTokenStartPosition = this.scanner.currentPosition;
- if (++ numberOfIdentifiers == numberOfTokens) {
- this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
- return;
- }
- break;
- case TerminalTokens.TokenNameDOT :
- print(this.scanner.currentPosition - this.scanner.startPosition, false);
- currentTokenStartPosition = this.scanner.currentPosition;
- break;
- case TerminalTokens.TokenNameRPAREN:
- currentTokenStartPosition = this.scanner.startPosition;
- // $FALL-THROUGH$ - fall through default case...
- default:
- this.scanner.resetTo(currentTokenStartPosition, this.scannerEndPosition - 1);
- return;
- }
- } while (this.scanner.currentPosition <= sourceEnd);
- } catch(InvalidInputException e) {
- throw new AbortFormatting(e);
- }
- }
-
public void printQualifiedReference(int sourceEnd, boolean expectParenthesis) {
int currentTokenStartPosition = this.scanner.currentPosition;
try {

Back to the top