From 3cf45e932b26f8e96caeecdae570cf7d9cd5e328 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Sun, 29 Nov 2009 16:44:05 +0100 Subject: Improved comment placement (newlines, whitespace) --- src/delombok/lombok/delombok/Comment.java | 20 ++++--- .../lombok/delombok/CommentCollectingScanner.java | 32 +++++++--- .../lombok/delombok/CommentPreservingParser.java | 4 +- .../lombok/delombok/PrettyCommentsPrinter.java | 69 ++++++++++++++++------ test/delombok/resource/after/Cast.java | 3 +- test/delombok/resource/after/WithComments.java | 1 + test/delombok/resource/before/WithComments.java | 1 + .../resource/after/CommentsInterspersed.java | 8 +-- 8 files changed, 94 insertions(+), 44 deletions(-) diff --git a/src/delombok/lombok/delombok/Comment.java b/src/delombok/lombok/delombok/Comment.java index 55a5c46d..170ffe22 100644 --- a/src/delombok/lombok/delombok/Comment.java +++ b/src/delombok/lombok/delombok/Comment.java @@ -22,22 +22,28 @@ package lombok.delombok; public final class Comment { + + public enum StartConnection { + START_OF_LINE, + ON_NEXT_LINE, + DIRECT_AFTER_PREVIOUS, + AFTER_PREVIOUS + } + final int pos; final int prevEndPos; final String content; final int endPos; - final boolean newLine; + final StartConnection start; + final boolean newLineAfter; - public Comment(int prevEndPos, int pos, int endPos, String content, boolean newLine) { + public Comment(int prevEndPos, int pos, int endPos, String content, StartConnection start, boolean newLineAfter) { this.pos = pos; this.prevEndPos = prevEndPos; this.endPos = endPos; this.content = content; - this.newLine = newLine; - } - - public boolean isConnected() { - return !newLine && prevEndPos == pos; + this.start = start; + this.newLineAfter = newLineAfter; } public String summary() { diff --git a/src/delombok/lombok/delombok/CommentCollectingScanner.java b/src/delombok/lombok/delombok/CommentCollectingScanner.java index 361d975c..8c77a0d3 100644 --- a/src/delombok/lombok/delombok/CommentCollectingScanner.java +++ b/src/delombok/lombok/delombok/CommentCollectingScanner.java @@ -23,6 +23,7 @@ package lombok.delombok; import java.nio.CharBuffer; +import lombok.delombok.Comment.StartConnection; import lombok.delombok.CommentPreservingParser.Comments; import com.sun.tools.javac.parser.Scanner; @@ -79,17 +80,32 @@ public class CommentCollectingScanner extends Scanner { protected void processComment(CommentStyle style) { int prevEndPos = prevEndPos(); int pos = pos(); - boolean newLine = containsNewLine(prevEndPos, pos); - String content = new String(getRawCharacters(pos, endPos())); - comments.add(prevEndPos, pos, endPos(), content, newLine); + int endPos = endPos(); + String content = new String(getRawCharacters(pos, endPos)); + boolean lineBreakAfter = isNewLine(getRawCharacters(endPos, endPos + 1)[0]); + StartConnection start = determineStartConnection(prevEndPos, pos); + Comment comment = new Comment(prevEndPos, pos, endPos, content, start, lineBreakAfter); + + comments.add(comment); } - private boolean containsNewLine(int from, int to) { - for (char c : getRawCharacters(from, to)) { - if (c == '\n' || c == '\r') { - return true; + private StartConnection determineStartConnection(int from, int to) { + if (from == to) { + return StartConnection.DIRECT_AFTER_PREVIOUS; + } + char[] between = getRawCharacters(from, to); + if (isNewLine(between[between.length - 1])) { + return StartConnection.START_OF_LINE; + } + for (char c : between) { + if (isNewLine(c)) { + return StartConnection.ON_NEXT_LINE; } } - return false; + return StartConnection.AFTER_PREVIOUS; + } + + private boolean isNewLine(char c) { + return c == '\n' || c == '\r'; } } diff --git a/src/delombok/lombok/delombok/CommentPreservingParser.java b/src/delombok/lombok/delombok/CommentPreservingParser.java index c776a224..2f1de8e5 100644 --- a/src/delombok/lombok/delombok/CommentPreservingParser.java +++ b/src/delombok/lombok/delombok/CommentPreservingParser.java @@ -101,8 +101,8 @@ public class CommentPreservingParser { static class Comments { List comments = List.nil(); - void add(int prevEndPos, int pos, int endPos, String content, boolean newLine) { - comments = comments.append(new Comment(prevEndPos, pos, endPos, content, newLine)); + void add(Comment comment) { + comments = comments.append(comment); } } diff --git a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java index f1ba0adf..695e94da 100644 --- a/src/delombok/lombok/delombok/PrettyCommentsPrinter.java +++ b/src/delombok/lombok/delombok/PrettyCommentsPrinter.java @@ -38,6 +38,8 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Map; +import lombok.delombok.Comment.StartConnection; + import com.sun.source.tree.Tree; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.Symbol; @@ -154,6 +156,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { private List comments; private final JCCompilationUnit cu; private boolean newLine = true; + private boolean indent = false; private boolean inParams = false; public PrettyCommentsPrinter(Writer out, JCCompilationUnit cu, List comments) { @@ -168,13 +171,16 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { private void consumeComments(int till) throws IOException { boolean shouldIndent = !newLine; - while (comments.nonEmpty() && comments.head.pos < till) { - if (newLine) { - align(); - } - print(comments.head.content); - println(); + boolean found = false; + Comment head = comments.head; + while (comments.nonEmpty() && head.pos < till) { + found = true; + printComment(head); comments = comments.tail; + head = comments.head; + } + if (found && !newLine && !indent) { + print(" "); } if (newLine && shouldIndent) { align(); @@ -186,18 +192,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { Comment head = comments.head; while (comments.nonEmpty() && head.prevEndPos == from) { from = head.endPos; - if (!head.isConnected()) { - if (head.newLine) { - if (!newLine) { - println(); - align(); - } - } - else { - print(" "); - } - } - print(head.content); + printComment(head); comments = comments.tail; head = comments.head; } @@ -206,6 +201,39 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { } } + private void printComment(Comment comment) throws IOException { + prepareComment(comment.start); + print(comment.content); + if (comment.newLineAfter) { + println(); + } + } + + private void prepareComment(StartConnection start) throws IOException { + switch (start) { + case DIRECT_AFTER_PREVIOUS: + break; + case AFTER_PREVIOUS: + if (!newLine && !indent) { + print(" "); + } + break; + case START_OF_LINE: + if (!newLine) { + println(); + } + break; + case ON_NEXT_LINE: + if (!newLine && !indent) { + println(); + } + if (!indent) { + align(); + } + break; + } + } + /** The output stream on which trees are printed. */ Writer out; @@ -227,6 +255,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { */ void align() throws IOException { newLine = false; + indent = true; for (int i = 0; i < lmargin; i++) out.write("\t"); } @@ -264,6 +293,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { */ public void print(Object s) throws IOException { newLine = false; + indent = false; out.write(Convert.escapeUnicode(s.toString())); } @@ -271,6 +301,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { */ public void println() throws IOException { newLine = true; + indent = false; out.write(lineSep); } @@ -306,7 +337,7 @@ public class PrettyCommentsPrinter extends JCTree.Visitor { tree.accept(this); int endPos = endPos(tree); consumeTrailingComments(endPos); - consumeComments(endPos); +// consumeComments(endPos); } } catch (UncheckedIOException ex) { IOException e = new IOException(ex.getMessage()); diff --git a/test/delombok/resource/after/Cast.java b/test/delombok/resource/after/Cast.java index a1b455f0..95237b0f 100644 --- a/test/delombok/resource/after/Cast.java +++ b/test/delombok/resource/after/Cast.java @@ -1,7 +1,6 @@ import java.util.*; public class Cast { public void test(List list) { - RandomAccess r = (/*before*/ - RandomAccess /*after*/)list; + RandomAccess r = (/*before*/ RandomAccess /*after*/)list; } } \ No newline at end of file diff --git a/test/delombok/resource/after/WithComments.java b/test/delombok/resource/after/WithComments.java index 684dcc8e..e4f078aa 100644 --- a/test/delombok/resource/after/WithComments.java +++ b/test/delombok/resource/after/WithComments.java @@ -1,4 +1,5 @@ // Cool Comments public class WithComments { // Also inside the body +// On start of line } \ No newline at end of file diff --git a/test/delombok/resource/before/WithComments.java b/test/delombok/resource/before/WithComments.java index 22d044b3..f75551ac 100644 --- a/test/delombok/resource/before/WithComments.java +++ b/test/delombok/resource/before/WithComments.java @@ -1,4 +1,5 @@ // Cool Comments public class WithComments { // Also inside the body +// On start of line } diff --git a/test/lombok/resource/after/CommentsInterspersed.java b/test/lombok/resource/after/CommentsInterspersed.java index f3841606..275f198f 100644 --- a/test/lombok/resource/after/CommentsInterspersed.java +++ b/test/lombok/resource/after/CommentsInterspersed.java @@ -1,13 +1,9 @@ -/* cmt */ -/* cmt2 */ -/* cmt3 */ -/*bla */ +/* cmt *//* cmt2 */ /* cmt3 */ /*bla */ public class CommentsInterspersed { /** javadoc for field */ private int x; - /* bla2 */ - private String test = "foo"; //$NON-NLS-1$ + /* bla2 */ private String test = "foo"; //$NON-NLS-1$ /** Javadoc on method */ public native void gwtTest(); /*-{ -- cgit From c646def5791e3a5c27cf77c373e5ce40017c165c Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 29 Nov 2009 17:23:19 +0100 Subject: 'ant test' will now run the lombok unit tests. --- build.xml | 4 + buildScripts/test.ant.xml | 79 +++++++++++++ test/core/src/lombok/DirectoryRunner.java | 2 +- test/core/src/lombok/RunTestsViaDelombok.java | 123 +++++++++++++++++++++ test/core/src/lombok/TestViaDelombok.java | 123 --------------------- .../src/lombok/delombok/TestLombokFiles.java | 4 +- 6 files changed, 209 insertions(+), 126 deletions(-) create mode 100644 buildScripts/test.ant.xml create mode 100644 test/core/src/lombok/RunTestsViaDelombok.java delete mode 100644 test/core/src/lombok/TestViaDelombok.java diff --git a/build.xml b/build.xml index 5fead285..006a546b 100644 --- a/build.xml +++ b/build.xml @@ -75,6 +75,10 @@ the common tasks and can be called on to run the main aspects of all the sub-scr + + + + diff --git a/buildScripts/test.ant.xml b/buildScripts/test.ant.xml new file mode 100644 index 00000000..742819c5 --- /dev/null +++ b/buildScripts/test.ant.xml @@ -0,0 +1,79 @@ + + + +This buildfile is part of projectlombok.org. It responsible for running unit tests. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + All tests successful. + + diff --git a/test/core/src/lombok/DirectoryRunner.java b/test/core/src/lombok/DirectoryRunner.java index 40b21aa0..1c8cfb2e 100644 --- a/test/core/src/lombok/DirectoryRunner.java +++ b/test/core/src/lombok/DirectoryRunner.java @@ -82,7 +82,7 @@ public class DirectoryRunner extends Runner { if (mustIgnore(file)) { return false; } - TestViaDelombok.compareFile(afterDirectory, file); + RunTestsViaDelombok.compareFile(afterDirectory, file); return true; } diff --git a/test/core/src/lombok/RunTestsViaDelombok.java b/test/core/src/lombok/RunTestsViaDelombok.java new file mode 100644 index 00000000..f2aedfa3 --- /dev/null +++ b/test/core/src/lombok/RunTestsViaDelombok.java @@ -0,0 +1,123 @@ +/* + * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; + +import lombok.delombok.Delombok; + +public class RunTestsViaDelombok { + private static Delombok delombok = new Delombok(); + private static volatile boolean printErrors = false; + + private static final String LINE_SEPARATOR = System.getProperty("line.separator"); + + public static void runComparison(File beforeDir, File afterDir) throws Throwable { + File[] listFiles = beforeDir.listFiles(); + + for (File file : listFiles) { + compareFile(afterDir, file); + } + } + + public static void compareFile(File afterDir, File file) throws Throwable { + delombok.setVerbose(false); + delombok.setForceProcess(true); + delombok.setCharset("UTF-8"); + StringWriter writer = new StringWriter(); + delombok.delombok(file.getAbsolutePath(), writer); + compare(file.getName(), readAfter(afterDir, file), writer.toString()); + } + + public static void printErrors(boolean print) { + printErrors = print; + } + + private static void compare(String name, String expectedFile, String actualFile) throws Throwable { + try { + compareContent(name, expectedFile, actualFile); + } + catch (Throwable e) { + if (printErrors) { + System.out.println("***** " + name + " *****"); + System.out.println(e.getMessage()); + System.out.println("**** Expected ******"); + System.out.println(expectedFile); + System.out.println("**** Actual ******"); + System.out.println(actualFile); + System.out.println("*******************"); + } + throw e; + } + } + + private static void compareContent(String name, String expectedFile, + String actualFile) { + String[] expectedLines = expectedFile.split("(\\r?\\n)"); + String[] actualLines = actualFile.split("(\\r?\\n)"); + if (actualLines[0].startsWith("// Generated by delombok at ")) { + actualLines[0] = ""; + } + expectedLines = removeBlanks(expectedLines); + actualLines = removeBlanks(actualLines); + int size = Math.min(expectedLines.length, actualLines.length); + for (int i = 0; i < size; i++) { + String expected = expectedLines[i]; + String actual = actualLines[i]; + assertEquals(String.format("Difference in %s on line %d", name, i + 1), expected, actual); + } + if (expectedLines.length > actualLines.length) { + fail(String.format("Missing line %d in generated %s: %s", size + 1, name, expectedLines[size])); + } + if (expectedLines.length < actualLines.length) { + fail(String.format("Extra line %d in generated %s: %s", size + 1, name, actualLines[size])); + } + } + + private static String readAfter(File afterDir, File file) throws IOException { + BufferedReader reader = new BufferedReader(new FileReader(new File(afterDir, file.getName()))); + StringBuilder result = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + result.append(line); + result.append(LINE_SEPARATOR); + } + reader.close(); + return result.toString(); + } + + private static String[] removeBlanks(String[] in) { + List out = new ArrayList(); + for (String s : in) { + if (!s.trim().isEmpty()) out.add(s); + } + return out.toArray(new String[0]); + } +} \ No newline at end of file diff --git a/test/core/src/lombok/TestViaDelombok.java b/test/core/src/lombok/TestViaDelombok.java deleted file mode 100644 index 63a8e1f8..00000000 --- a/test/core/src/lombok/TestViaDelombok.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package lombok; - -import static org.junit.Assert.*; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.List; - -import lombok.delombok.Delombok; - -public class TestViaDelombok { - private static Delombok delombok = new Delombok(); - private static volatile boolean printErrors = false; - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - - public static void runComparison(File beforeDir, File afterDir) throws Throwable { - File[] listFiles = beforeDir.listFiles(); - - for (File file : listFiles) { - compareFile(afterDir, file); - } - } - - public static void compareFile(File afterDir, File file) throws Throwable { - delombok.setVerbose(false); - delombok.setForceProcess(true); - delombok.setCharset("UTF-8"); - StringWriter writer = new StringWriter(); - delombok.delombok(file.getAbsolutePath(), writer); - compare(file.getName(), readAfter(afterDir, file), writer.toString()); - } - - public static void printErrors(boolean print) { - printErrors = print; - } - - private static void compare(String name, String expectedFile, String actualFile) throws Throwable { - try { - compareContent(name, expectedFile, actualFile); - } - catch (Throwable e) { - if (printErrors) { - System.out.println("***** " + name + " *****"); - System.out.println(e.getMessage()); - System.out.println("**** Expected ******"); - System.out.println(expectedFile); - System.out.println("**** Actual ******"); - System.out.println(actualFile); - System.out.println("*******************"); - } - throw e; - } - } - - private static void compareContent(String name, String expectedFile, - String actualFile) { - String[] expectedLines = expectedFile.split("(\\r?\\n)"); - String[] actualLines = actualFile.split("(\\r?\\n)"); - if (actualLines[0].startsWith("// Generated by delombok at ")) { - actualLines[0] = ""; - } - expectedLines = removeBlanks(expectedLines); - actualLines = removeBlanks(actualLines); - int size = Math.min(expectedLines.length, actualLines.length); - for (int i = 0; i < size; i++) { - String expected = expectedLines[i]; - String actual = actualLines[i]; - assertEquals(String.format("Difference in %s on line %d", name, i + 1), expected, actual); - } - if (expectedLines.length > actualLines.length) { - fail(String.format("Missing line %d in generated %s: %s", size + 1, name, expectedLines[size])); - } - if (expectedLines.length < actualLines.length) { - fail(String.format("Extra line %d in generated %s: %s", size + 1, name, actualLines[size])); - } - } - - private static String readAfter(File afterDir, File file) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(new File(afterDir, file.getName()))); - StringBuilder result = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - result.append(line); - result.append(LINE_SEPARATOR); - } - reader.close(); - return result.toString(); - } - - private static String[] removeBlanks(String[] in) { - List out = new ArrayList(); - for (String s : in) { - if (!s.trim().isEmpty()) out.add(s); - } - return out.toArray(new String[0]); - } -} \ No newline at end of file diff --git a/test/delombok/src/lombok/delombok/TestLombokFiles.java b/test/delombok/src/lombok/delombok/TestLombokFiles.java index 66e3580e..12c16d76 100644 --- a/test/delombok/src/lombok/delombok/TestLombokFiles.java +++ b/test/delombok/src/lombok/delombok/TestLombokFiles.java @@ -24,7 +24,7 @@ package lombok.delombok; import java.io.File; import lombok.DirectoryRunner; -import lombok.TestViaDelombok; +import lombok.RunTestsViaDelombok; import org.junit.runner.RunWith; @@ -32,7 +32,7 @@ import org.junit.runner.RunWith; public class TestLombokFiles { public static File getBeforeDirectory() { - TestViaDelombok.printErrors(true); + RunTestsViaDelombok.printErrors(true); return new File("test/lombok/resource/before"); } -- cgit From 78c1f1da1169522324b69079cb49a91b9e8d7b76 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Sun, 29 Nov 2009 17:28:27 +0100 Subject: Updated tests for Getter, Setter and Cleanup. Added Synchronized --- test/lombok/resource/after/CleanupPlain.java | 2 +- test/lombok/resource/after/GetterPlain.java | 2 +- test/lombok/resource/after/SetterPlain.java | 2 +- test/lombok/resource/after/SynchronizedPlain.java | 26 ++++++++++++++++++++++ test/lombok/resource/before/CleanupPlain.java | 3 +-- test/lombok/resource/before/GetterPlain.java | 2 +- test/lombok/resource/before/SetterPlain.java | 3 +-- test/lombok/resource/before/SynchronizedPlain.java | 17 ++++++++++++++ 8 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 test/lombok/resource/after/SynchronizedPlain.java create mode 100644 test/lombok/resource/before/SynchronizedPlain.java diff --git a/test/lombok/resource/after/CleanupPlain.java b/test/lombok/resource/after/CleanupPlain.java index 25c66ced..35d51543 100644 --- a/test/lombok/resource/after/CleanupPlain.java +++ b/test/lombok/resource/after/CleanupPlain.java @@ -1,5 +1,5 @@ import java.io.*; -class Cleanup { +class CleanupPlain { void test() throws Exception { InputStream in = new FileInputStream("in"); try { diff --git a/test/lombok/resource/after/GetterPlain.java b/test/lombok/resource/after/GetterPlain.java index 04944df8..5e8cc965 100644 --- a/test/lombok/resource/after/GetterPlain.java +++ b/test/lombok/resource/after/GetterPlain.java @@ -1,4 +1,4 @@ -class Getter { +class GetterPlain { int i; int foo; public int getI() { diff --git a/test/lombok/resource/after/SetterPlain.java b/test/lombok/resource/after/SetterPlain.java index 2570365c..8faad0ec 100644 --- a/test/lombok/resource/after/SetterPlain.java +++ b/test/lombok/resource/after/SetterPlain.java @@ -1,4 +1,4 @@ -class Setter { +class SetterPlain { int i; int foo; public void setI(final int i) { diff --git a/test/lombok/resource/after/SynchronizedPlain.java b/test/lombok/resource/after/SynchronizedPlain.java new file mode 100644 index 00000000..2c168368 --- /dev/null +++ b/test/lombok/resource/after/SynchronizedPlain.java @@ -0,0 +1,26 @@ +class SynchronizedPlain1 { + private final Object $lock = new Object[0]; + void test() { + synchronized ($lock) { + System.out.println("one"); + } + } + void test2() { + synchronized ($lock) { + System.out.println("two"); + } + } +} +class SynchronizedPlain2 { + private static final Object $LOCK = new Object[0]; + static void test() { + synchronized ($LOCK) { + System.out.println("three"); + } + } + static void test2() { + synchronized ($LOCK) { + System.out.println("four"); + } + } +} \ No newline at end of file diff --git a/test/lombok/resource/before/CleanupPlain.java b/test/lombok/resource/before/CleanupPlain.java index 6cf09062..a91ac3c1 100644 --- a/test/lombok/resource/before/CleanupPlain.java +++ b/test/lombok/resource/before/CleanupPlain.java @@ -1,7 +1,6 @@ -//ignore import lombok.Cleanup; import java.io.*; -class Cleanup { +class CleanupPlain { void test() throws Exception { @lombok.Cleanup InputStream in = new FileInputStream("in"); @Cleanup OutputStream out = new FileOutputStream("out"); diff --git a/test/lombok/resource/before/GetterPlain.java b/test/lombok/resource/before/GetterPlain.java index e6018972..d104f428 100644 --- a/test/lombok/resource/before/GetterPlain.java +++ b/test/lombok/resource/before/GetterPlain.java @@ -1,5 +1,5 @@ import lombok.Getter; -class Getter { +class GetterPlain { @lombok.Getter int i; @Getter int foo; } \ No newline at end of file diff --git a/test/lombok/resource/before/SetterPlain.java b/test/lombok/resource/before/SetterPlain.java index 85d3aad5..e1ac99f8 100644 --- a/test/lombok/resource/before/SetterPlain.java +++ b/test/lombok/resource/before/SetterPlain.java @@ -1,6 +1,5 @@ -//ignore import lombok.Setter; -class Setter { +class SetterPlain { @lombok.Setter int i; @Setter int foo; } \ No newline at end of file diff --git a/test/lombok/resource/before/SynchronizedPlain.java b/test/lombok/resource/before/SynchronizedPlain.java new file mode 100644 index 00000000..f903172c --- /dev/null +++ b/test/lombok/resource/before/SynchronizedPlain.java @@ -0,0 +1,17 @@ +import lombok.Synchronized; +class SynchronizedPlain1 { + @lombok.Synchonized void test() { + System.out.println("one"); + } + @Synchonized void test2() { + System.out.println("two"); + } +} +class SynchronizedPlain2 { + @lombok.Synchonized static void test() { + System.out.println("three"); + } + @Synchonized static void test2() { + System.out.println("four"); + } +} \ No newline at end of file -- cgit From 96c122a2679eeaa9993b3dac5fff11187a734d1c Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Sun, 29 Nov 2009 18:26:37 +0100 Subject: Fixed premature removal of imports --- src/core/lombok/core/AST.java | 15 ++++++++++++--- src/core/lombok/eclipse/EclipseAST.java | 11 +++-------- src/core/lombok/javac/JavacAST.java | 25 ++++++++++++------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/core/lombok/core/AST.java b/src/core/lombok/core/AST.java index e769ce34..07d6ec6c 100644 --- a/src/core/lombok/core/AST.java +++ b/src/core/lombok/core/AST.java @@ -30,6 +30,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.IdentityHashMap; import java.util.List; @@ -52,12 +53,16 @@ public abstract class AST, L extends LombokNode, private L top; private final String fileName; + private final String packageDeclaration; + private final Collection imports; Map identityDetector = new IdentityHashMap(); private Map nodeMap = new IdentityHashMap(); private boolean changed = false; - protected AST(String fileName) { + protected AST(String fileName, String packageDeclaration, Collection imports) { this.fileName = fileName == null ? "(unknown).java" : fileName; + this.packageDeclaration = packageDeclaration; + this.imports = Collections.unmodifiableCollection(new ArrayList(imports)); } protected void setChanged() { @@ -82,14 +87,18 @@ public abstract class AST, L extends LombokNode, * * Example: "java.util". */ - public abstract String getPackageDeclaration(); + public final String getPackageDeclaration() { + return packageDeclaration; + } /** * Return the contents of each non-static import statement on this AST's top (Compilation Unit) node. * * Example: "java.util.IOException". */ - public abstract Collection getImportStatements(); + public final Collection getImportStatements() { + return imports; + } /** * Puts the given node in the map so that javac/Eclipse's own internal AST object can be translated to diff --git a/src/core/lombok/eclipse/EclipseAST.java b/src/core/lombok/eclipse/EclipseAST.java index 7f436ddf..7ed83bfe 100644 --- a/src/core/lombok/eclipse/EclipseAST.java +++ b/src/core/lombok/eclipse/EclipseAST.java @@ -56,30 +56,25 @@ public class EclipseAST extends AST { * @param ast The compilation unit, which serves as the top level node in the tree to be built. */ public EclipseAST(CompilationUnitDeclaration ast) { - super(toFileName(ast)); + super(toFileName(ast), packageDeclaration(ast), imports(ast)); this.compilationUnitDeclaration = ast; setTop(buildCompilationUnit(ast)); this.completeParse = isComplete(ast); clearChanged(); } - /** {@inheritDoc} */ - @Override public String getPackageDeclaration() { - CompilationUnitDeclaration cud = (CompilationUnitDeclaration) top().get(); + private static String packageDeclaration(CompilationUnitDeclaration cud) { ImportReference pkg = cud.currentPackage; return pkg == null ? null : Eclipse.toQualifiedName(pkg.getImportName()); } - /** {@inheritDoc} */ - @Override public Collection getImportStatements() { + private static Collection imports(CompilationUnitDeclaration cud) { List imports = new ArrayList(); - CompilationUnitDeclaration cud = (CompilationUnitDeclaration) top().get(); if (cud.imports == null) return imports; for (ImportReference imp : cud.imports) { if (imp == null) continue; imports.add(Eclipse.toQualifiedName(imp.getImportName())); } - return imports; } diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java index e231f1d8..33c167f9 100644 --- a/src/core/lombok/javac/JavacAST.java +++ b/src/core/lombok/javac/JavacAST.java @@ -71,7 +71,7 @@ public class JavacAST extends AST { * @param top The compilation unit, which serves as the top level node in the tree to be built. */ public JavacAST(Messager messager, Context context, JCCompilationUnit top) { - super(top.sourcefile == null ? null : top.sourcefile.toString()); + super(sourceName(top), packageDeclaration(top), imports(top)); setTop(buildCompilationUnit(top)); this.context = context; this.messager = messager; @@ -81,30 +81,29 @@ public class JavacAST extends AST { this.symtab = Symtab.instance(context); clearChanged(); } - - public Context getContext() { - return context; + + private static String sourceName(JCCompilationUnit cu) { + return cu.sourcefile == null ? null : cu.sourcefile.toString(); } - /** {@inheritDoc} */ - @Override public String getPackageDeclaration() { - JCCompilationUnit unit = (JCCompilationUnit)top().get(); - return unit.pid instanceof JCFieldAccess ? unit.pid.toString() : null; + private static String packageDeclaration(JCCompilationUnit cu) { + return cu.pid instanceof JCFieldAccess ? cu.pid.toString() : null; } - /** {@inheritDoc} */ - @Override public Collection getImportStatements() { + private static Collection imports(JCCompilationUnit cu) { List imports = new ArrayList(); - JCCompilationUnit unit = (JCCompilationUnit)top().get(); - for (JCTree def : unit.defs) { + for (JCTree def : cu.defs) { if (def instanceof JCImport) { imports.add(((JCImport)def).qualid.toString()); } } - return imports; } + public Context getContext() { + return context; + } + /** * Runs through the entire AST, starting at the compilation unit, calling the provided visitor's visit methods * for each node, depth first. -- cgit From 8d9b4fdaca4c1f84385262d84c0c8db7cc222b1a Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Sun, 29 Nov 2009 18:30:58 +0100 Subject: Fixed the test code for Synchronized --- test/lombok/resource/after/SynchronizedPlain.java | 4 ++-- test/lombok/resource/before/SynchronizedPlain.java | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/lombok/resource/after/SynchronizedPlain.java b/test/lombok/resource/after/SynchronizedPlain.java index 2c168368..fcbe03f2 100644 --- a/test/lombok/resource/after/SynchronizedPlain.java +++ b/test/lombok/resource/after/SynchronizedPlain.java @@ -1,5 +1,4 @@ class SynchronizedPlain1 { - private final Object $lock = new Object[0]; void test() { synchronized ($lock) { System.out.println("one"); @@ -10,9 +9,9 @@ class SynchronizedPlain1 { System.out.println("two"); } } + private final java.lang.Object $lock = new java.lang.Object[0]; } class SynchronizedPlain2 { - private static final Object $LOCK = new Object[0]; static void test() { synchronized ($LOCK) { System.out.println("three"); @@ -23,4 +22,5 @@ class SynchronizedPlain2 { System.out.println("four"); } } + private static final java.lang.Object $LOCK = new java.lang.Object[0]; } \ No newline at end of file diff --git a/test/lombok/resource/before/SynchronizedPlain.java b/test/lombok/resource/before/SynchronizedPlain.java index f903172c..6f5f3b64 100644 --- a/test/lombok/resource/before/SynchronizedPlain.java +++ b/test/lombok/resource/before/SynchronizedPlain.java @@ -1,17 +1,17 @@ import lombok.Synchronized; class SynchronizedPlain1 { - @lombok.Synchonized void test() { + @lombok.Synchronized void test() { System.out.println("one"); } - @Synchonized void test2() { + @Synchronized void test2() { System.out.println("two"); } } class SynchronizedPlain2 { - @lombok.Synchonized static void test() { + @lombok.Synchronized static void test() { System.out.println("three"); } - @Synchonized static void test2() { + @Synchronized static void test2() { System.out.println("four"); } } \ No newline at end of file -- cgit From eb1a51588779ca55c9855651f9eefed9459f0874 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Sun, 29 Nov 2009 18:33:38 +0100 Subject: Make lock fields private --- src/core/lombok/javac/handlers/HandleSynchronized.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/lombok/javac/handlers/HandleSynchronized.java b/src/core/lombok/javac/handlers/HandleSynchronized.java index eaa0ad6d..3602e74b 100644 --- a/src/core/lombok/javac/handlers/HandleSynchronized.java +++ b/src/core/lombok/javac/handlers/HandleSynchronized.java @@ -87,7 +87,7 @@ public class HandleSynchronized implements JavacAnnotationHandler JCNewArray newObjectArray = maker.NewArray(chainDots(maker, methodNode, "java", "lang", "Object"), List.of(maker.Literal(TypeTags.INT, 0)), null); JCVariableDecl fieldDecl = maker.VarDef( - maker.Modifiers(Flags.FINAL | (isStatic ? Flags.STATIC : 0)), + maker.Modifiers(Flags.PRIVATE | Flags.FINAL | (isStatic ? Flags.STATIC : 0)), methodNode.toName(lockName), objectType, newObjectArray); injectField(methodNode.up(), fieldDecl); } -- cgit