diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-11-27 07:18:48 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-11-27 07:18:48 +0100 |
commit | 4316e77e1eb6654eec18d04780a6da78c22fb644 (patch) | |
tree | 6c879d4190ee36f674c740e3755b46fb55098704 | |
parent | 391db3dcecdd0d94eb76b656e655346891b02bb4 (diff) | |
download | lombok-4316e77e1eb6654eec18d04780a6da78c22fb644.tar.gz lombok-4316e77e1eb6654eec18d04780a6da78c22fb644.tar.bz2 lombok-4316e77e1eb6654eec18d04780a6da78c22fb644.zip |
Added a class that is easier to use than the parser itself for delombok, as well as code to process entire directories at a time.
Also removed duplication from the testcases.
-rw-r--r-- | .classpath | 1 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/Delombok.java | 155 | ||||
-rw-r--r-- | test/core/src/lombok/TestViaDelombok.java | 97 | ||||
-rw-r--r-- | test/delombok/src/lombok/delombok/TestLombokFiles.java | 67 | ||||
-rw-r--r-- | test/delombok/src/lombok/delombok/TestSourceFiles.java | 76 |
5 files changed, 261 insertions, 135 deletions
@@ -6,6 +6,7 @@ <classpathentry kind="src" path="src/delombok"/> <classpathentry kind="src" path="experimental/src"/> <classpathentry kind="src" path="test/delombok/src"/> + <classpathentry kind="src" path="test/core/src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="lib" path="deps/lombok/org.eclipse.jdt.core_3.5.0.v_963.jar"/> <classpathentry kind="lib" path="deps/lombok/org.eclipse.jdt.ui_3.5.1.r351_v20090821-0800.jar"/> diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java new file mode 100644 index 00000000..c6f9ecf9 --- /dev/null +++ b/src/delombok/lombok/delombok/Delombok.java @@ -0,0 +1,155 @@ +package lombok.delombok; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintStream; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.charset.UnsupportedCharsetException; + +import lombok.delombok.CommentPreservingParser.ParseResult; + +public class Delombok { + private Charset charset = Charset.defaultCharset(); + private CommentPreservingParser parser = new CommentPreservingParser(); + private PrintStream feedback = System.err; + private boolean verbose; + private boolean force = false; + + /** If null, output to standard out. */ + private File output = null; + + public void setCharset(String charsetName) throws UnsupportedCharsetException { + charset = Charset.forName(charsetName); + } + + public void setForceProcess(boolean force) { + this.force = force; + } + + public void setFeedback(PrintStream feedback) { + this.feedback = feedback; + } + + public void setVerbose(boolean verbose) { + this.verbose = verbose; + } + + public void setOutput(File dir) { + if (dir.isFile()) throw new IllegalArgumentException( + "DELOMBOK: delombok will only write to a directory. " + + "If you want to delombok a single file, use -p to output to standard output, then redirect this to a file:\n" + + "delombok MyJavaFile.java -p >MyJavaFileDelombok.java"); + output = dir; + } + + public void setOutputToStandardOut() { + this.output = null; + } + + public void delombok(File base) throws IOException { + delombok0(base, "", 0); + } + + private void delombok0(File base, String suffix, int loop) throws IOException { + File dir = suffix.isEmpty() ? base : new File(base, suffix); + String name = suffix + File.separator + dir.getName(); + + if (dir.isDirectory()) { + if (loop >= 100) { + feedback.printf("Over 100 subdirectories? I'm guessing there's a loop in your directory structure. Skipping: %s\n", suffix); + } else { + dir.mkdir(); + delombok0(base, name, loop + 1); + } + } else if (dir.isFile()) { + String extension = getExtension(dir); + if (extension.equals(".java")) delombok(base, name); + else if (extension.equals(".class")) skipClass(name); + else copy(base, name); + } else { + feedback.printf("Skipping %s because it is a special file type.\n", canonical(dir)); + } + } + + private void skipClass(String fileName) { + if (verbose) feedback.printf("Skipping class file: %s\n", fileName); + } + + private void copy(File base, String fileName) throws IOException { + if (output == null) { + feedback.printf("Skipping resource file: %s\n", fileName); + return; + } + if (verbose) feedback.printf("Copying resource file: %s\n", fileName); + byte[] b = new byte[65536]; + FileInputStream in = new FileInputStream(new File(base, fileName)); + try { + FileOutputStream out = new FileOutputStream(new File(output, fileName)); + try { + while (true) { + int r = in.read(b); + if (r == -1) break; + out.write(b, 0, r); + } + } finally { + out.close(); + } + } finally { + in.close(); + } + } + + public void delombok(String file, Writer writer) throws IOException { + ParseResult result = parser.parse(file, force); + + result.print(writer); + } + + public void delombok(File base, String fileName) throws IOException { + if (output != null && canonical(base).equals(canonical(output))) throw new IOException( + "DELOMBOK: Output file and input file refer to the same filesystem location. Specify a separate path for output."); + + ParseResult result = parser.parse(new File(base, fileName).getAbsolutePath(), force); + + if (verbose) feedback.printf("File: %s [%s]\n", fileName, result.isChanged() ? "delombok-ed" : "unchanged"); + + Writer rawWriter = output == null ? createStandardOutWriter() : createFileWriter(output, fileName); + BufferedWriter writer = new BufferedWriter(rawWriter); + + try { + result.print(writer); + } finally { + writer.close(); + } + } + + private static String canonical(File dir) { + try { + return dir.getCanonicalPath(); + } catch (Exception e) { + return dir.getAbsolutePath(); + } + } + + private static String getExtension(File dir) { + String name = dir.getName(); + int idx = name.lastIndexOf('.'); + return idx == -1 ? "" : name.substring(idx+1); + } + + private Writer createFileWriter(File base, String fileName) throws IOException { + File outFile = new File(base, fileName); + outFile.getParentFile().mkdirs(); + FileOutputStream out = new FileOutputStream(outFile); + return new OutputStreamWriter(out, charset); + } + + private Writer createStandardOutWriter() { + return new OutputStreamWriter(System.out, charset); + } +} diff --git a/test/core/src/lombok/TestViaDelombok.java b/test/core/src/lombok/TestViaDelombok.java new file mode 100644 index 00000000..e8070723 --- /dev/null +++ b/test/core/src/lombok/TestViaDelombok.java @@ -0,0 +1,97 @@ +/* + * 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.fail; + +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 final String LINE_SEPARATOR = System.getProperty("line.separator"); + + public static void runComparison(File beforeDir, File afterDir) throws IOException { + File[] listFiles = beforeDir.listFiles(); + + for (File file : listFiles) { + 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()); + } + } + + private static void compare(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]; + if (!expected.equals(actual)) { + fail(String.format("Difference in line %s(%d):\n`%s`\n`%s`\n", name, i, expected, actual)); + } + } + if (expectedLines.length > actualLines.length) { + fail(String.format("Missing line %s(%d): %s\n", name, size, expectedLines[size])); + } + if (expectedLines.length < actualLines.length) { + fail(String.format("Extra line %s(%d): %s\n", name, size, 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<String> out = new ArrayList<String>(); + for (String s : in) { + if (!s.trim().isEmpty()) out.add(s); + } + return out.toArray(new String[0]); + } +} diff --git a/test/delombok/src/lombok/delombok/TestLombokFiles.java b/test/delombok/src/lombok/delombok/TestLombokFiles.java index f04a0a34..1d93a4b2 100644 --- a/test/delombok/src/lombok/delombok/TestLombokFiles.java +++ b/test/delombok/src/lombok/delombok/TestLombokFiles.java @@ -21,77 +21,18 @@ */ package lombok.delombok; -import static org.junit.Assert.fail; -import static lombok.delombok.TestSourceFiles.removeBlanks; - -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.StringWriter; -import lombok.delombok.CommentPreservingParser.ParseResult; +import lombok.TestViaDelombok; -import org.junit.BeforeClass; import org.junit.Test; public class TestLombokFiles { - private static CommentPreservingParser parser; - - private static final File BEFORE_FOLDER = new File("test/lombok/resource/before"); - private static final File AFTER_FOLDER = new File("test/lombok/resource/after"); - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - - @BeforeClass - public static void init() { - parser = new CommentPreservingParser(); - } + private static final File BEFORE_DIR = new File("test/lombok/resource/before"); + private static final File AFTER_DIR = new File("test/lombok/resource/after"); @Test public void testSources() throws Exception { - File[] listFiles = BEFORE_FOLDER.listFiles(); - for (File file : listFiles) { - ParseResult parseResult = parser.parse(file.toString(), true); - StringWriter writer = new StringWriter(); - parseResult.print(writer); - compare(file.getName(), readAfter(file), writer.toString()); - } - } - - private void compare(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]; - if (!expected.equals(actual)) { - fail(String.format("Difference in line %s(%d):\n`%s`\n`%s`\n", name, i, expected, actual)); - } - } - if (expectedLines.length > actualLines.length) { - fail(String.format("Missing line %s(%d): %s\n", name, size, expectedLines[size])); - } - if (expectedLines.length < actualLines.length) { - fail(String.format("Extra line %s(%d): %s\n", name, size, actualLines[size])); - } - } - - private String readAfter(File file) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(new File(AFTER_FOLDER, 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(); + TestViaDelombok.runComparison(BEFORE_DIR, AFTER_DIR); } } diff --git a/test/delombok/src/lombok/delombok/TestSourceFiles.java b/test/delombok/src/lombok/delombok/TestSourceFiles.java index 91ace773..a5df5197 100644 --- a/test/delombok/src/lombok/delombok/TestSourceFiles.java +++ b/test/delombok/src/lombok/delombok/TestSourceFiles.java @@ -21,86 +21,18 @@ */ package lombok.delombok; -import static org.junit.Assert.fail; - -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.CommentPreservingParser.ParseResult; +import lombok.TestViaDelombok; -import org.junit.BeforeClass; import org.junit.Test; public class TestSourceFiles { - private static CommentPreservingParser parser; - - private static final File BEFORE_FOLDER = new File("test/delombok/resource/before"); - private static final File AFTER_FOLDER = new File("test/delombok/resource/after"); - - private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - - @BeforeClass - public static void init() { - parser = new CommentPreservingParser(); - } + private static final File BEFORE_DIR = new File("test/delombok/resource/before"); + private static final File AFTER_DIR = new File("test/delombok/resource/after"); @Test public void testSources() throws Exception { - File[] listFiles = BEFORE_FOLDER.listFiles(); - for (File file : listFiles) { - ParseResult parseResult = parser.parse(file.toString(), true); - StringWriter writer = new StringWriter(); - parseResult.print(writer); - compare(file.getName(), readAfter(file), writer.toString()); - } - } - - static String[] removeBlanks(String[] in) { - List<String> out = new ArrayList<String>(); - for (String s : in) { - if (!s.trim().isEmpty()) out.add(s); - } - return out.toArray(new String[0]); - } - - private void compare(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]; - if (!expected.equals(actual)) { - fail(String.format("Difference in line %s(%d):\nExpected `%s`\nGot `%s`\n", name, i, expected, actual)); - } - } - if (expectedLines.length > actualLines.length) { - fail(String.format("Missing line %s(%d): %s\n", name, size, expectedLines[size])); - } - if (expectedLines.length < actualLines.length) { - fail(String.format("Extra line %s(%d): %s\n", name, size, actualLines[size])); - } - } - - private String readAfter(File file) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(new File(AFTER_FOLDER, 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(); + TestViaDelombok.runComparison(BEFORE_DIR, AFTER_DIR); } } |