aboutsummaryrefslogtreecommitdiff
path: root/src/delombok
diff options
context:
space:
mode:
Diffstat (limited to 'src/delombok')
-rw-r--r--src/delombok/lombok/delombok/Delombok.java88
-rw-r--r--src/delombok/lombok/delombok/ant/DelombokTask.java20
-rw-r--r--src/delombok/lombok/delombok/ant/Format.java35
3 files changed, 103 insertions, 40 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java
index e5b719f1..9cb78a82 100644
--- a/src/delombok/lombok/delombok/Delombok.java
+++ b/src/delombok/lombok/delombok/Delombok.java
@@ -195,8 +195,6 @@ public class Delombok {
}
}));
- Map<String, String> formatPrefs = new HashMap<String, String>();
-
if (args.formatHelp) {
System.out.println("Available format keys (to use, -f key:value -f key2:value2 -f ... ):");
for (Map.Entry<String, String> e : FormatPreferences.getKeysAndDescriptions().entrySet()) {
@@ -211,46 +209,14 @@ public class Delombok {
return;
}
- boolean prettyEnabled = false;
- for (String format : args.format) {
- int idx = format.indexOf(':');
- if (idx == -1) {
- if (format.equalsIgnoreCase("pretty")) {
- prettyEnabled = true;
- continue;
- } else {
- System.err.println("Format keys need to be 2 values separated with a colon. Try -f help.");
- System.exit(1);
- return;
- }
- }
- String key = format.substring(0, idx);
- String value = format.substring(idx + 1);
- boolean valid = false;
- for (String k : FormatPreferences.getKeysAndDescriptions().keySet()) {
- if (k.equalsIgnoreCase(key)) {
- valid = true;
- break;
- }
- }
- if (!valid) {
- System.err.println("Unknown format key: '" + key + "'. Try -f help.");
- System.exit(1);
- return;
- }
-
- formatPrefs.put(key.toLowerCase(), value);
- }
-
- if (prettyEnabled) {
- if (!formatPrefs.containsKey("suppresswarnings")) formatPrefs.put("suppresswarnings", "skip");
- if (!formatPrefs.containsKey("dancearoundidechecks")) formatPrefs.put("dancearoundidechecks", "skip");
- if (!formatPrefs.containsKey("generatedelombokcomment")) formatPrefs.put("generatedelombokcomment", "skip");
- if (!formatPrefs.containsKey("javalangasfqn")) formatPrefs.put("javalangasfqn", "skip");
+ try {
+ delombok.setFormatPreferences(formatOptionsToMap(args.format));
+ } catch (InvalidFormatOptionException e) {
+ System.out.println(e.getMessage() + " Try --format-help.");
+ System.exit(1);
+ return;
}
- delombok.setFormatPreferences(formatPrefs);
-
if (args.encoding != null) {
try {
delombok.setCharset(args.encoding);
@@ -301,6 +267,48 @@ public class Delombok {
}
}
+ public static class InvalidFormatOptionException extends Exception {
+ public InvalidFormatOptionException(String msg) {
+ super(msg);
+ }
+ }
+
+ public static Map<String, String> formatOptionsToMap(List<String> formatOptions) throws InvalidFormatOptionException {
+ boolean prettyEnabled = false;
+ Map<String, String> formatPrefs = new HashMap<String, String>();
+ for (String format : formatOptions) {
+ int idx = format.indexOf(':');
+ if (idx == -1) {
+ if (format.equalsIgnoreCase("pretty")) {
+ prettyEnabled = true;
+ continue;
+ } else {
+ throw new InvalidFormatOptionException("Format keys need to be 2 values separated with a colon.");
+ }
+ }
+ String key = format.substring(0, idx);
+ String value = format.substring(idx + 1);
+ boolean valid = false;
+ for (String k : FormatPreferences.getKeysAndDescriptions().keySet()) {
+ if (k.equalsIgnoreCase(key)) {
+ valid = true;
+ break;
+ }
+ }
+ if (!valid) throw new InvalidFormatOptionException("Unknown format key: '" + key + "'.");
+ formatPrefs.put(key.toLowerCase(), value);
+ }
+
+ if (prettyEnabled) {
+ if (!formatPrefs.containsKey("suppresswarnings")) formatPrefs.put("suppresswarnings", "skip");
+ if (!formatPrefs.containsKey("dancearoundidechecks")) formatPrefs.put("dancearoundidechecks", "skip");
+ if (!formatPrefs.containsKey("generatedelombokcomment")) formatPrefs.put("generatedelombokcomment", "skip");
+ if (!formatPrefs.containsKey("javalangasfqn")) formatPrefs.put("javalangasfqn", "skip");
+ }
+
+ return formatPrefs;
+ }
+
public void setFormatPreferences(Map<String, String> prefs) {
this.formatPrefs = prefs;
}
diff --git a/src/delombok/lombok/delombok/ant/DelombokTask.java b/src/delombok/lombok/delombok/ant/DelombokTask.java
index 2064e355..d5c17fe7 100644
--- a/src/delombok/lombok/delombok/ant/DelombokTask.java
+++ b/src/delombok/lombok/delombok/ant/DelombokTask.java
@@ -24,9 +24,12 @@ package lombok.delombok.ant;
import java.io.File;
import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
+import java.util.ArrayList;
import java.util.Iterator;
+import java.util.List;
import lombok.delombok.Delombok;
+import lombok.delombok.Delombok.InvalidFormatOptionException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
@@ -42,6 +45,7 @@ public class DelombokTask extends Task {
private boolean verbose;
private String encoding;
private Path path;
+ private List<Format> formatOptions = new ArrayList<Format>();
public void setClasspath(Path classpath) {
if (this.classpath == null) {
@@ -102,6 +106,10 @@ public class DelombokTask extends Task {
path.add(set);
}
+ public void addFormat(Format format) {
+ formatOptions.add(format);
+ }
+
@Override
public void execute() throws BuildException {
if (fromDir == null && path == null) throw new BuildException("Either 'from' attribute, or nested <fileset> tags are required.");
@@ -119,6 +127,18 @@ public class DelombokTask extends Task {
if (classpath != null) delombok.setClasspath(classpath.toString());
if (sourcepath != null) delombok.setSourcepath(sourcepath.toString());
+ try {
+ List<String> fo = new ArrayList<String>();
+ for (Format f : formatOptions) {
+ String v = f.getValue();
+ if (v == null) throw new BuildException("'value' property required for <format>");
+ fo.add(v);
+ }
+ delombok.setFormatPreferences(Delombok.formatOptionsToMap(fo));
+ } catch (InvalidFormatOptionException e) {
+ throw new BuildException(e.getMessage() + " Run java -jar lombok.jar --format-help for detailed format help.");
+ }
+
delombok.setOutput(toDir);
try {
if (fromDir != null) delombok.addDirectory(fromDir);
diff --git a/src/delombok/lombok/delombok/ant/Format.java b/src/delombok/lombok/delombok/ant/Format.java
new file mode 100644
index 00000000..bb2b5e87
--- /dev/null
+++ b/src/delombok/lombok/delombok/ant/Format.java
@@ -0,0 +1,35 @@
+package lombok.delombok.ant;
+
+public class Format {
+ private String value;
+
+ @Override public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((value == null) ? 0 : value.hashCode());
+ return result;
+ }
+
+ @Override public boolean equals(Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (getClass() != obj.getClass()) return false;
+ Format other = (Format) obj;
+ if (value == null) {
+ if (other.value != null) return false;
+ } else if (!value.equals(other.value)) return false;
+ return true;
+ }
+
+ @Override public String toString() {
+ return "FormatOption [value=" + value + "]";
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+}