diff options
Diffstat (limited to 'src/delombok')
-rw-r--r-- | src/delombok/lombok/delombok/Delombok.java | 175 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/ant/DelombokTask.java | 30 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/ant/DelombokTaskImpl.java | 4 |
3 files changed, 193 insertions, 16 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index f5372fbb..0b97b4be 100644 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2017 The Project Lombok Authors. + * Copyright (C) 2009-2018 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -26,14 +26,18 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintStream; +import java.io.UnsupportedEncodingException; import java.io.Writer; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URI; +import java.net.URLDecoder; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; import java.util.ArrayList; @@ -88,7 +92,7 @@ public class Delombok { private boolean noCopy; private boolean onlyChanged; private boolean force = false; - private String classpath, sourcepath, bootclasspath; + private String classpath, sourcepath, bootclasspath, modulepath; private LinkedHashMap<File, File> fileToBase = new LinkedHashMap<File, File>(); private List<File> filesToParse = new ArrayList<File>(); private Map<String, String> formatPrefs = new HashMap<String, String>(); @@ -138,6 +142,10 @@ public class Delombok { @Description("override Bootclasspath (analogous to javac -bootclasspath option)") private String bootclasspath; + @Description("Module path (analogous to javac --module-path option)") + @FullName("module-path") + private String modulepath; + @Description("Files to delombok. Provide either a file, or a directory. If you use a directory, all files in it (recursive) are delombok-ed") @Sequential private List<String> input = new ArrayList<String>(); @@ -182,21 +190,50 @@ public class Delombok { return out.toString(); } + static String getPathOfSelf() { + String url = Delombok.class.getResource("Delombok.class").toString(); + if (url.endsWith("lombok/delombok/Delombok.class")) { + url = urlDecode(url.substring(0, url.length() - "lombok/delombok/Delombok.class".length())); + } else if (url.endsWith("lombok/delombok/Delombok.SCL.lombok")) { + url = urlDecode(url.substring(0, url.length() - "lombok/delombok/Delombok.SCL.lombok".length())); + } else { + return null; + } + if (url.startsWith("jar:file:") && url.endsWith("!/")) return url.substring(9, url.length() - 2); + if (url.startsWith("file:")) return url.substring(5); + return null; + } + + private static String urlDecode(String in) { + try { + return URLDecoder.decode(in, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new InternalError("UTF-8 not supported"); + } + } + public static void main(String[] rawArgs) { + try { + rawArgs = fileExpand(rawArgs); + } catch (IOException e) { + System.out.println(e.getMessage()); + System.exit(1); + } + CmdReader<CmdArgs> reader = CmdReader.of(CmdArgs.class); CmdArgs args; try { args = reader.make(rawArgs); } catch (InvalidCommandLineException e) { System.err.println("ERROR: " + e.getMessage()); - System.err.println(reader.generateCommandLineHelp("delombok")); + System.err.println(cmdHelp(reader)); System.exit(1); return; } if (args.help || (args.input.isEmpty() && !args.formatHelp)) { if (!args.help) System.err.println("ERROR: no files or directories to delombok specified."); - System.err.println(reader.generateCommandLineHelp("delombok")); + System.err.println(cmdHelp(reader)); System.exit(args.help ? 0 : 1); return; } @@ -253,6 +290,7 @@ public class Delombok { if (args.classpath != null) delombok.setClasspath(args.classpath); if (args.sourcepath != null) delombok.setSourcepath(args.sourcepath); if (args.bootclasspath != null) delombok.setBootclasspath(args.bootclasspath); + if (args.modulepath != null) delombok.setModulepath(args.modulepath); try { for (String in : args.input) { @@ -273,15 +311,123 @@ public class Delombok { if (!args.quiet) { String msg = e.getMessage(); if (msg != null && msg.startsWith("DELOMBOK: ")) System.err.println(msg.substring("DELOMBOK: ".length())); - else { - e.printStackTrace(); - } + else e.printStackTrace(); System.exit(1); return; } } } + private static String cmdHelp(CmdReader<CmdArgs> reader) { + String x = reader.generateCommandLineHelp("delombok"); + int idx = x.indexOf('\n'); + return x.substring(0, idx) + "\n You can use @filename.args to read arguments from the file 'filename.args'.\n" + x.substring(idx); + } + + private static String[] fileExpand(String[] rawArgs) throws IOException { + String[] out = rawArgs; + int offset = 0; + for (int i = 0; i < rawArgs.length; i++) { + if (rawArgs[i].length() > 0 && rawArgs[i].charAt(0) == '@') { + String[] parts = readArgsFromFile(rawArgs[i].substring(1)); + String[] newOut = new String[out.length + parts.length - 1]; + System.arraycopy(out, 0, newOut, 0, i + offset); + System.arraycopy(parts, 0, newOut, i + offset, parts.length); + System.arraycopy(out, i + offset + 1, newOut, i + offset + parts.length, out.length - (i + offset + 1)); + offset += parts.length - 1; + out = newOut; + } + } + + return out; + } + + private static String[] readArgsFromFile(String file) throws IOException { + InputStream in = new FileInputStream(file); + StringBuilder s = new StringBuilder(); + try { + InputStreamReader isr = new InputStreamReader(in, "UTF-8"); + char[] c = new char[4096]; + while (true) { + int r = isr.read(c); + if (r == -1) break; + s.append(c, 0, r); + } + } finally { + in.close(); + } + + List<String> x = new ArrayList<String>(); + StringBuilder a = new StringBuilder(); + int state = 1; + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (state < 0) { + state = -state; + if (c != '\n') a.append(c); + continue; + } + if (state == 1) { + if (c == '\\') { + state = -1; + continue; + } + if (c == '"') { + state = 2; + continue; + } + if (c == '\'') { + state = 3; + continue; + } + if (Character.isWhitespace(c)) { + String aa = a.toString(); + if (!aa.isEmpty()) x.add(aa); + a.setLength(0); + continue; + } + a.append(c); + continue; + } + if (state == 2) { + if (c == '\\') { + state = -2; + continue; + } + if (c == '"') { + state = 1; + x.add(a.toString()); + a.setLength(0); + continue; + } + a.append(c); + continue; + } + if (state == 3) { + if (c == '\'') { + state = 1; + x.add(a.toString()); + a.setLength(0); + continue; + } + a.append(c); + continue; + } + } + if (state == 1) { + String aa = a.toString(); + if (!aa.isEmpty()) x.add(aa); + } else if (state < 0) { + throw new IOException("Unclosed backslash escape in @ file"); + } else if (state == 2) { + throw new IOException("Unclosed \" in @ file"); + } else if (state == 3) { + throw new IOException("Unclosed ' in @ file"); + } + + return x.toArray(new String[x.size()]); + } + public static class InvalidFormatOptionException extends Exception { public InvalidFormatOptionException(String msg) { super(msg); @@ -385,6 +531,10 @@ public class Delombok { this.output = null; } + public void setModulepath(String modulepath) { + this.modulepath = modulepath; + } + public void addDirectory(File base) throws IOException { addDirectory0(false, base, "", 0); } @@ -525,8 +675,19 @@ public class Delombok { argsList.add("-encoding"); argsList.add(charset.name()); } + String pathToSelfJar = getPathOfSelf(); + if (pathToSelfJar != null) { + argsList.add("--module-path"); + argsList.add((modulepath == null || modulepath.isEmpty()) ? pathToSelfJar : (pathToSelfJar + File.pathSeparator + modulepath)); + } else if (modulepath != null && !modulepath.isEmpty()) { + argsList.add("--module-path"); + argsList.add(modulepath); + } String[] argv = argsList.toArray(new String[0]); args.init("javac", argv); + options.put("diags.legacy", "TRUE"); + } else { + if (modulepath != null && !modulepath.isEmpty()) throw new IllegalStateException("DELOMBOK: Option --module-path requires usage of JDK9 or higher."); } CommentCatcher catcher = CommentCatcher.create(context); diff --git a/src/delombok/lombok/delombok/ant/DelombokTask.java b/src/delombok/lombok/delombok/ant/DelombokTask.java index 06bbe3e0..adaf43dd 100644 --- a/src/delombok/lombok/delombok/ant/DelombokTask.java +++ b/src/delombok/lombok/delombok/ant/DelombokTask.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2015 The Project Lombok Authors. + * Copyright (C) 2009-2018 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -78,6 +78,7 @@ class Tasks { private File fromDir, toDir; private Path classpath; private Path sourcepath; + private Path modulepath; private boolean verbose; private String encoding; private Path path; @@ -92,9 +93,7 @@ class Tasks { } public Path createClasspath() { - if (classpath == null) { - classpath = new Path(getProject()); - } + if (classpath == null) classpath = new Path(getProject()); return classpath.createPath(); } @@ -111,9 +110,7 @@ class Tasks { } public Path createSourcepath() { - if (sourcepath == null) { - sourcepath = new Path(getProject()); - } + if (sourcepath == null) sourcepath = new Path(getProject()); return sourcepath.createPath(); } @@ -121,6 +118,23 @@ class Tasks { createSourcepath().setRefid(r); } + public void setModulepath(Path modulepath) { + if (this.modulepath == null) { + this.modulepath = modulepath; + } else { + this.modulepath.append(modulepath); + } + } + + public Path createModulepath() { + if (modulepath == null) modulepath = new Path(getProject()); + return modulepath.createPath(); + } + + public void setModulepathRef(Reference r) { + createModulepath().setRefid(r); + } + public void setFrom(File dir) { this.fromDir = dir; } @@ -180,7 +194,7 @@ class Tasks { try { Object instance = shadowLoadClass("lombok.delombok.ant.DelombokTaskImpl").newInstance(); - for(Field selfField : getClass().getDeclaredFields()) { + for (Field selfField : getClass().getDeclaredFields()) { if (selfField.isSynthetic() || Modifier.isStatic(selfField.getModifiers())) continue; Field otherField = instance.getClass().getDeclaredField(selfField.getName()); otherField.setAccessible(true); diff --git a/src/delombok/lombok/delombok/ant/DelombokTaskImpl.java b/src/delombok/lombok/delombok/ant/DelombokTaskImpl.java index 470819cd..bd2f93e7 100644 --- a/src/delombok/lombok/delombok/ant/DelombokTaskImpl.java +++ b/src/delombok/lombok/delombok/ant/DelombokTaskImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009-2015 The Project Lombok Authors. + * Copyright (C) 2009-2018 The Project Lombok Authors. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -40,6 +40,7 @@ public class DelombokTaskImpl { private File fromDir, toDir; private Path classpath; private Path sourcepath; + private Path modulepath; private boolean verbose; private String encoding; private Path path; @@ -60,6 +61,7 @@ public class DelombokTaskImpl { if (classpath != null) delombok.setClasspath(classpath.toString()); if (sourcepath != null) delombok.setSourcepath(sourcepath.toString()); + if (modulepath != null) delombok.setModulepath(modulepath.toString()); try { delombok.setFormatPreferences(Delombok.formatOptionsToMap(formatOptions)); |