1000 lines
41 KiB
Java
1000 lines
41 KiB
Java
package lombok.delombok;
|
|
|
|
import com.adjust.sdk.Constants;
|
|
import com.sun.tools.javac.code.Symbol;
|
|
import com.sun.tools.javac.code.Symtab;
|
|
import com.sun.tools.javac.comp.Todo;
|
|
import com.sun.tools.javac.file.BaseFileManager;
|
|
import com.sun.tools.javac.main.Arguments;
|
|
import com.sun.tools.javac.main.JavaCompiler;
|
|
import com.sun.tools.javac.tree.JCTree;
|
|
import com.sun.tools.javac.util.Context;
|
|
import com.sun.tools.javac.util.ListBuffer;
|
|
import com.zwitserloot.cmdreader.CmdReader;
|
|
import com.zwitserloot.cmdreader.Description;
|
|
import com.zwitserloot.cmdreader.Excludes;
|
|
import com.zwitserloot.cmdreader.FullName;
|
|
import com.zwitserloot.cmdreader.InvalidCommandLineException;
|
|
import com.zwitserloot.cmdreader.Mandatory;
|
|
import com.zwitserloot.cmdreader.Sequential;
|
|
import com.zwitserloot.cmdreader.Shorthand;
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
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;
|
|
import java.util.Collections;
|
|
import java.util.HashMap;
|
|
import java.util.IdentityHashMap;
|
|
import java.util.Iterator;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.ListIterator;
|
|
import java.util.Map;
|
|
import java.util.Queue;
|
|
import java.util.Set;
|
|
import java.util.regex.Pattern;
|
|
import javax.tools.DiagnosticListener;
|
|
import javax.tools.JavaFileManager;
|
|
import javax.tools.JavaFileObject;
|
|
import lombok.Lombok;
|
|
import lombok.javac.CommentCatcher;
|
|
import lombok.javac.Javac;
|
|
import lombok.javac.LombokOptions;
|
|
import lombok.javac.apt.LombokProcessor;
|
|
import lombok.permit.Permit;
|
|
/* loaded from: com.discord-118106.apk:lombok/delombok/Delombok.SCL.lombok */
|
|
public class Delombok {
|
|
private Writer presetWriter;
|
|
private boolean verbose;
|
|
private boolean noCopy;
|
|
private boolean onlyChanged;
|
|
private boolean disablePreview;
|
|
private String classpath;
|
|
private String sourcepath;
|
|
private String bootclasspath;
|
|
private String modulepath;
|
|
private static final Field MODULE_FIELD = getModuleField();
|
|
private static Method attributeMethod;
|
|
private static Method flowMethod;
|
|
private Charset charset = Charset.defaultCharset();
|
|
private Context context = new Context();
|
|
private PrintStream feedback = System.err;
|
|
private boolean force = false;
|
|
private LinkedHashMap<File, File> fileToBase = new LinkedHashMap<>();
|
|
private List<File> filesToParse = new ArrayList();
|
|
private Map<String, String> formatPrefs = new HashMap();
|
|
private File output = null;
|
|
|
|
/* renamed from: lombok.delombok.Delombok$1 reason: invalid class name */
|
|
/* loaded from: com.discord-118106.apk:lombok/delombok/Delombok$1.SCL.lombok */
|
|
class AnonymousClass1 extends OutputStream {
|
|
AnonymousClass1() {
|
|
}
|
|
|
|
@Override // java.io.OutputStream
|
|
public void write(int i) throws IOException {
|
|
}
|
|
}
|
|
|
|
/* loaded from: com.discord-118106.apk:lombok/delombok/Delombok$CmdArgs.SCL.lombok */
|
|
private static class CmdArgs {
|
|
@Excludes({"quiet"})
|
|
@Description("Print the name of each file as it is being delombok-ed.")
|
|
@Shorthand({"v"})
|
|
private boolean verbose;
|
|
@FullName("format-help")
|
|
private boolean formatHelp;
|
|
@Excludes({"verbose"})
|
|
@Description("No warnings or errors will be emitted to standard error")
|
|
@Shorthand({"q"})
|
|
private boolean quiet;
|
|
@Description("Sets the encoding of your source files. Defaults to the system default charset. Example: \"UTF-8\"")
|
|
@Shorthand({"e"})
|
|
private String encoding;
|
|
@Description("Print delombok-ed code to standard output instead of saving it in target directory")
|
|
@Shorthand({"p"})
|
|
private boolean print;
|
|
@Description("Directory to save delomboked files to")
|
|
@Mandatory(onlyIfNot = {"print", "help", "format-help"})
|
|
@Shorthand({"d"})
|
|
private String target;
|
|
@Description("Classpath (analogous to javac -cp option)")
|
|
@Shorthand({"c"})
|
|
private String classpath;
|
|
@Description("Sourcepath (analogous to javac -sourcepath option)")
|
|
@Shorthand({"s"})
|
|
private String sourcepath;
|
|
@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("Lombok will only delombok source files. Without this option, non-java, non-class files are copied to the target directory.")
|
|
@Shorthand({"n"})
|
|
private boolean nocopy;
|
|
@Description("Output only changed files (implies -n)")
|
|
private boolean onlyChanged;
|
|
@Description("By default lombok enables preview features if available (introduced in JDK 12). With this option, lombok won't do that.")
|
|
@FullName("disable-preview")
|
|
private boolean disablePreview;
|
|
private boolean help;
|
|
@Description("Sets formatting rules. Use --format-help to list all available rules. Unset format rules are inferred by scanning the source for usages.")
|
|
@Shorthand({"f"})
|
|
private List<String> format = new ArrayList();
|
|
@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();
|
|
|
|
private CmdArgs() {
|
|
}
|
|
|
|
static /* synthetic */ boolean access$0(CmdArgs cmdArgs) {
|
|
return cmdArgs.help;
|
|
}
|
|
|
|
static /* synthetic */ List access$1(CmdArgs cmdArgs) {
|
|
return cmdArgs.input;
|
|
}
|
|
|
|
static /* synthetic */ boolean access$2(CmdArgs cmdArgs) {
|
|
return cmdArgs.formatHelp;
|
|
}
|
|
|
|
static /* synthetic */ boolean access$3(CmdArgs cmdArgs) {
|
|
return cmdArgs.quiet;
|
|
}
|
|
|
|
static /* synthetic */ List access$4(CmdArgs cmdArgs) {
|
|
return cmdArgs.format;
|
|
}
|
|
|
|
static /* synthetic */ String access$5(CmdArgs cmdArgs) {
|
|
return cmdArgs.encoding;
|
|
}
|
|
|
|
static /* synthetic */ boolean access$6(CmdArgs cmdArgs) {
|
|
return cmdArgs.verbose;
|
|
}
|
|
|
|
static /* synthetic */ boolean access$7(CmdArgs cmdArgs) {
|
|
return cmdArgs.nocopy;
|
|
}
|
|
|
|
static /* synthetic */ boolean access$8(CmdArgs cmdArgs) {
|
|
return cmdArgs.onlyChanged;
|
|
}
|
|
|
|
static /* synthetic */ boolean access$9(CmdArgs cmdArgs) {
|
|
return cmdArgs.disablePreview;
|
|
}
|
|
|
|
static /* synthetic */ boolean access$10(CmdArgs cmdArgs) {
|
|
return cmdArgs.print;
|
|
}
|
|
|
|
static /* synthetic */ String access$11(CmdArgs cmdArgs) {
|
|
return cmdArgs.target;
|
|
}
|
|
|
|
static /* synthetic */ String access$12(CmdArgs cmdArgs) {
|
|
return cmdArgs.classpath;
|
|
}
|
|
|
|
static /* synthetic */ String access$13(CmdArgs cmdArgs) {
|
|
return cmdArgs.sourcepath;
|
|
}
|
|
|
|
static /* synthetic */ String access$14(CmdArgs cmdArgs) {
|
|
return cmdArgs.bootclasspath;
|
|
}
|
|
|
|
static /* synthetic */ String access$15(CmdArgs cmdArgs) {
|
|
return cmdArgs.modulepath;
|
|
}
|
|
}
|
|
|
|
/* loaded from: com.discord-118106.apk:lombok/delombok/Delombok$InvalidFormatOptionException.SCL.lombok */
|
|
public static class InvalidFormatOptionException extends Exception {
|
|
public InvalidFormatOptionException(String str) {
|
|
super(str);
|
|
}
|
|
}
|
|
|
|
public void setWriter(Writer writer) {
|
|
this.presetWriter = writer;
|
|
}
|
|
|
|
private static String indentAndWordbreak(String str, int i, int i2) {
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb2 = new StringBuilder();
|
|
StringBuilder sb3 = new StringBuilder();
|
|
int length = str.length();
|
|
int i3 = 0;
|
|
while (i3 < length + 1) {
|
|
char charAt = i3 == length ? ' ' : str.charAt(i3);
|
|
if (charAt == ' ') {
|
|
if (sb2.length() + sb3.length() < i2) {
|
|
sb2.append((CharSequence) sb3);
|
|
} else {
|
|
if (sb.length() > 0) {
|
|
sb.append("\n");
|
|
}
|
|
for (int i4 = 0; i4 < i; i4++) {
|
|
sb.append(" ");
|
|
}
|
|
sb.append((CharSequence) sb2);
|
|
sb2.setLength(0);
|
|
sb2.append(sb3.toString().trim());
|
|
}
|
|
sb3.setLength(0);
|
|
}
|
|
if (i3 < length) {
|
|
sb3.append(charAt);
|
|
}
|
|
i3++;
|
|
}
|
|
if (sb2.length() > 0) {
|
|
if (sb.length() > 0) {
|
|
sb.append("\n");
|
|
}
|
|
for (int i5 = 0; i5 < i; i5++) {
|
|
sb.append(" ");
|
|
}
|
|
sb.append((CharSequence) sb2);
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
static String getPathOfSelf() {
|
|
String str;
|
|
String url = Delombok.class.getResource("Delombok.class").toString();
|
|
if (url.endsWith("lombok/delombok/Delombok.class")) {
|
|
str = urlDecode(url.substring(0, url.length() - "lombok/delombok/Delombok.class".length()));
|
|
} else if (!url.endsWith("lombok/delombok/Delombok.SCL.lombok")) {
|
|
return null;
|
|
} else {
|
|
str = urlDecode(url.substring(0, url.length() - "lombok/delombok/Delombok.SCL.lombok".length()));
|
|
}
|
|
if (str.startsWith("jar:file:") && str.endsWith("!/")) {
|
|
return str.substring(9, str.length() - 2);
|
|
}
|
|
if (str.startsWith("file:")) {
|
|
return str.substring(5);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private static String urlDecode(String str) {
|
|
try {
|
|
return URLDecoder.decode(str, Constants.ENCODING);
|
|
} catch (UnsupportedEncodingException unused) {
|
|
throw new InternalError("UTF-8 not supported");
|
|
}
|
|
}
|
|
|
|
public static void main(String[] strArr) {
|
|
try {
|
|
strArr = fileExpand(strArr);
|
|
} catch (IOException e) {
|
|
System.out.println(e.getMessage());
|
|
System.exit(1);
|
|
}
|
|
CmdReader of = CmdReader.of(CmdArgs.class);
|
|
try {
|
|
CmdArgs cmdArgs = (CmdArgs) of.make(strArr);
|
|
if (CmdArgs.access$0(cmdArgs) || (CmdArgs.access$1(cmdArgs).isEmpty() && !CmdArgs.access$2(cmdArgs))) {
|
|
if (!CmdArgs.access$0(cmdArgs)) {
|
|
System.err.println("ERROR: no files or directories to delombok specified.");
|
|
}
|
|
System.err.println(cmdHelp(of));
|
|
System.exit(CmdArgs.access$0(cmdArgs) ? 0 : 1);
|
|
return;
|
|
}
|
|
Delombok delombok = new Delombok();
|
|
if (CmdArgs.access$3(cmdArgs)) {
|
|
delombok.setFeedback(new PrintStream(new AnonymousClass1()));
|
|
}
|
|
if (CmdArgs.access$2(cmdArgs)) {
|
|
System.out.println("Available format keys (to use, -f key:value -f key2:value2 -f ... ):");
|
|
Iterator<Map.Entry<String, String>> it = FormatPreferences.getKeysAndDescriptions().entrySet().iterator();
|
|
while (it.hasNext()) {
|
|
Map.Entry<String, String> next = it.next();
|
|
System.out.print(" ");
|
|
System.out.print(next.getKey());
|
|
System.out.println(":");
|
|
System.out.println(indentAndWordbreak(next.getValue(), 4, 70));
|
|
}
|
|
System.out.println("Example: -f indent:4 -f emptyLines:indent");
|
|
System.out.println("The '-f pretty' option is shorthand for '-f suppressWarnings:skip -f generated:skip -f danceAroundIdeChecks:skip -f generateDelombokComment:skip -f javaLangAsFQN:skip'");
|
|
System.exit(0);
|
|
return;
|
|
}
|
|
try {
|
|
delombok.setFormatPreferences(formatOptionsToMap(CmdArgs.access$4(cmdArgs)));
|
|
if (CmdArgs.access$5(cmdArgs) != null) {
|
|
try {
|
|
delombok.setCharset(CmdArgs.access$5(cmdArgs));
|
|
} catch (UnsupportedCharsetException unused) {
|
|
System.err.println("ERROR: Not a known charset: " + CmdArgs.access$5(cmdArgs));
|
|
System.exit(1);
|
|
return;
|
|
}
|
|
}
|
|
if (CmdArgs.access$6(cmdArgs)) {
|
|
delombok.setVerbose(true);
|
|
}
|
|
if (CmdArgs.access$7(cmdArgs) || CmdArgs.access$8(cmdArgs)) {
|
|
delombok.setNoCopy(true);
|
|
}
|
|
if (CmdArgs.access$9(cmdArgs)) {
|
|
delombok.setDisablePreview(true);
|
|
}
|
|
if (CmdArgs.access$8(cmdArgs)) {
|
|
delombok.setOnlyChanged(true);
|
|
}
|
|
if (CmdArgs.access$10(cmdArgs)) {
|
|
delombok.setOutputToStandardOut();
|
|
} else {
|
|
delombok.setOutput(new File(CmdArgs.access$11(cmdArgs)));
|
|
}
|
|
if (CmdArgs.access$12(cmdArgs) != null) {
|
|
delombok.setClasspath(CmdArgs.access$12(cmdArgs));
|
|
}
|
|
if (CmdArgs.access$13(cmdArgs) != null) {
|
|
delombok.setSourcepath(CmdArgs.access$13(cmdArgs));
|
|
}
|
|
if (CmdArgs.access$14(cmdArgs) != null) {
|
|
delombok.setBootclasspath(CmdArgs.access$14(cmdArgs));
|
|
}
|
|
if (CmdArgs.access$15(cmdArgs) != null) {
|
|
delombok.setModulepath(CmdArgs.access$15(cmdArgs));
|
|
}
|
|
try {
|
|
Iterator it2 = CmdArgs.access$1(cmdArgs).iterator();
|
|
while (it2.hasNext()) {
|
|
File absoluteFile = new File((String) it2.next()).getAbsoluteFile();
|
|
if (absoluteFile.isFile()) {
|
|
delombok.addFile(absoluteFile.getParentFile(), absoluteFile.getName());
|
|
} else if (absoluteFile.isDirectory()) {
|
|
delombok.addDirectory(absoluteFile);
|
|
} else if (!absoluteFile.exists()) {
|
|
if (!CmdArgs.access$3(cmdArgs)) {
|
|
System.err.println("WARNING: does not exist - skipping: " + absoluteFile);
|
|
}
|
|
} else if (!CmdArgs.access$3(cmdArgs)) {
|
|
System.err.println("WARNING: not a standard file or directory - skipping: " + absoluteFile);
|
|
}
|
|
}
|
|
delombok.delombok();
|
|
} catch (Exception e2) {
|
|
if (!CmdArgs.access$3(cmdArgs)) {
|
|
String message = e2.getMessage();
|
|
if (message == null || !message.startsWith("DELOMBOK: ")) {
|
|
e2.printStackTrace();
|
|
} else {
|
|
System.err.println(message.substring("DELOMBOK: ".length()));
|
|
}
|
|
System.exit(1);
|
|
}
|
|
}
|
|
} catch (InvalidFormatOptionException e3) {
|
|
System.out.println(String.valueOf(e3.getMessage()) + " Try --format-help.");
|
|
System.exit(1);
|
|
}
|
|
} catch (InvalidCommandLineException e4) {
|
|
System.err.println("ERROR: " + e4.getMessage());
|
|
System.err.println(cmdHelp(of));
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
private static String cmdHelp(CmdReader<CmdArgs> cmdReader) {
|
|
String generateCommandLineHelp = cmdReader.generateCommandLineHelp("delombok");
|
|
int indexOf = generateCommandLineHelp.indexOf(10);
|
|
return String.valueOf(generateCommandLineHelp.substring(0, indexOf)) + "\n You can use @filename.args to read arguments from the file 'filename.args'.\n" + generateCommandLineHelp.substring(indexOf);
|
|
}
|
|
|
|
private static String[] fileExpand(String[] strArr) throws IOException {
|
|
String[] strArr2 = strArr;
|
|
int i = 0;
|
|
for (int i2 = 0; i2 < strArr.length; i2++) {
|
|
if (strArr[i2].length() > 0 && strArr[i2].charAt(0) == '@') {
|
|
String[] readArgsFromFile = readArgsFromFile(strArr[i2].substring(1));
|
|
String[] strArr3 = new String[(strArr2.length + readArgsFromFile.length) - 1];
|
|
System.arraycopy(strArr2, 0, strArr3, 0, i2 + i);
|
|
System.arraycopy(readArgsFromFile, 0, strArr3, i2 + i, readArgsFromFile.length);
|
|
System.arraycopy(strArr2, i2 + i + 1, strArr3, i2 + i + readArgsFromFile.length, strArr2.length - ((i2 + i) + 1));
|
|
i += readArgsFromFile.length - 1;
|
|
strArr2 = strArr3;
|
|
}
|
|
}
|
|
return strArr2;
|
|
}
|
|
|
|
/* JADX WARN: Finally extract failed */
|
|
private static String[] readArgsFromFile(String str) throws IOException {
|
|
FileInputStream fileInputStream = new FileInputStream(str);
|
|
StringBuilder sb = new StringBuilder();
|
|
try {
|
|
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, Constants.ENCODING);
|
|
char[] cArr = new char[4096];
|
|
while (true) {
|
|
int read = inputStreamReader.read(cArr);
|
|
if (read == -1) {
|
|
break;
|
|
}
|
|
sb.append(cArr, 0, read);
|
|
}
|
|
inputStreamReader.close();
|
|
fileInputStream.close();
|
|
ArrayList arrayList = new ArrayList();
|
|
StringBuilder sb2 = new StringBuilder();
|
|
int i = 1;
|
|
for (int i2 = 0; i2 < sb.length(); i2++) {
|
|
char charAt = sb.charAt(i2);
|
|
if (i < 0) {
|
|
i = -i;
|
|
if (charAt != '\n') {
|
|
sb2.append(charAt);
|
|
}
|
|
} else if (i == 1) {
|
|
if (charAt == '\\') {
|
|
i = -1;
|
|
} else if (charAt == '\"') {
|
|
i = 2;
|
|
} else if (charAt == '\'') {
|
|
i = 3;
|
|
} else if (Character.isWhitespace(charAt)) {
|
|
String sb3 = sb2.toString();
|
|
if (!sb3.isEmpty()) {
|
|
arrayList.add(sb3);
|
|
}
|
|
sb2.setLength(0);
|
|
} else {
|
|
sb2.append(charAt);
|
|
}
|
|
} else if (i == 2) {
|
|
if (charAt == '\\') {
|
|
i = -2;
|
|
} else if (charAt == '\"') {
|
|
i = 1;
|
|
arrayList.add(sb2.toString());
|
|
sb2.setLength(0);
|
|
} else {
|
|
sb2.append(charAt);
|
|
}
|
|
} else if (i == 3) {
|
|
if (charAt == '\'') {
|
|
i = 1;
|
|
arrayList.add(sb2.toString());
|
|
sb2.setLength(0);
|
|
} else {
|
|
sb2.append(charAt);
|
|
}
|
|
}
|
|
}
|
|
if (i == 1) {
|
|
String sb4 = sb2.toString();
|
|
if (!sb4.isEmpty()) {
|
|
arrayList.add(sb4);
|
|
}
|
|
} else if (i < 0) {
|
|
throw new IOException("Unclosed backslash escape in @ file");
|
|
} else if (i == 2) {
|
|
throw new IOException("Unclosed \" in @ file");
|
|
} else if (i == 3) {
|
|
throw new IOException("Unclosed ' in @ file");
|
|
}
|
|
return (String[]) arrayList.toArray(new String[0]);
|
|
} catch (Throwable th) {
|
|
fileInputStream.close();
|
|
throw th;
|
|
}
|
|
}
|
|
|
|
public static Map<String, String> formatOptionsToMap(List<String> list) throws InvalidFormatOptionException {
|
|
boolean z2 = false;
|
|
HashMap hashMap = new HashMap();
|
|
Iterator<String> it = list.iterator();
|
|
while (it.hasNext()) {
|
|
String next = it.next();
|
|
int indexOf = next.indexOf(58);
|
|
if (indexOf != -1) {
|
|
String substring = next.substring(0, indexOf);
|
|
String substring2 = next.substring(indexOf + 1);
|
|
boolean z3 = false;
|
|
Iterator<String> it2 = FormatPreferences.getKeysAndDescriptions().keySet().iterator();
|
|
while (true) {
|
|
if (it2.hasNext()) {
|
|
if (it2.next().equalsIgnoreCase(substring)) {
|
|
z3 = true;
|
|
break;
|
|
}
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
if (!z3) {
|
|
throw new InvalidFormatOptionException("Unknown format key: '" + substring + "'.");
|
|
}
|
|
hashMap.put(substring.toLowerCase(), substring2);
|
|
} else if (next.equalsIgnoreCase("pretty")) {
|
|
z2 = true;
|
|
} else {
|
|
throw new InvalidFormatOptionException("Format keys need to be 2 values separated with a colon.");
|
|
}
|
|
}
|
|
if (z2) {
|
|
if (!hashMap.containsKey("suppresswarnings")) {
|
|
hashMap.put("suppresswarnings", "skip");
|
|
}
|
|
if (!hashMap.containsKey("generated")) {
|
|
hashMap.put("generated", "skip");
|
|
}
|
|
if (!hashMap.containsKey("dancearoundidechecks")) {
|
|
hashMap.put("dancearoundidechecks", "skip");
|
|
}
|
|
if (!hashMap.containsKey("generatedelombokcomment")) {
|
|
hashMap.put("generatedelombokcomment", "skip");
|
|
}
|
|
if (!hashMap.containsKey("javalangasfqn")) {
|
|
hashMap.put("javalangasfqn", "skip");
|
|
}
|
|
}
|
|
return hashMap;
|
|
}
|
|
|
|
public void setFormatPreferences(Map<String, String> map) {
|
|
this.formatPrefs = map;
|
|
}
|
|
|
|
public void setCharset(String str) throws UnsupportedCharsetException {
|
|
if (str == null) {
|
|
this.charset = Charset.defaultCharset();
|
|
} else {
|
|
this.charset = Charset.forName(str);
|
|
}
|
|
}
|
|
|
|
public void setDiagnosticsListener(DiagnosticListener<JavaFileObject> diagnosticListener) {
|
|
if (diagnosticListener != null) {
|
|
this.context.put(DiagnosticListener.class, diagnosticListener);
|
|
}
|
|
}
|
|
|
|
public void setForceProcess(boolean z2) {
|
|
this.force = z2;
|
|
}
|
|
|
|
public void setFeedback(PrintStream printStream) {
|
|
this.feedback = printStream;
|
|
}
|
|
|
|
public void setClasspath(String str) {
|
|
this.classpath = str;
|
|
}
|
|
|
|
public void setSourcepath(String str) {
|
|
this.sourcepath = str;
|
|
}
|
|
|
|
public void setBootclasspath(String str) {
|
|
this.bootclasspath = str;
|
|
}
|
|
|
|
public void setVerbose(boolean z2) {
|
|
this.verbose = z2;
|
|
}
|
|
|
|
public void setNoCopy(boolean z2) {
|
|
this.noCopy = z2;
|
|
}
|
|
|
|
public void setDisablePreview(boolean z2) {
|
|
this.disablePreview = z2;
|
|
}
|
|
|
|
public void setOnlyChanged(boolean z2) {
|
|
this.onlyChanged = z2;
|
|
}
|
|
|
|
public void setOutput(File file) {
|
|
if (file.isFile() || (!file.isDirectory() && file.getName().endsWith(".java"))) {
|
|
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:\ndelombok MyJavaFile.java -p >MyJavaFileDelombok.java");
|
|
}
|
|
this.output = file;
|
|
}
|
|
|
|
public void setOutputToStandardOut() {
|
|
this.output = null;
|
|
}
|
|
|
|
public void setModulepath(String str) {
|
|
this.modulepath = str;
|
|
}
|
|
|
|
public void addDirectory(File file) throws IOException {
|
|
addDirectory0(false, file, "", 0);
|
|
}
|
|
|
|
public void addDirectory1(boolean z2, File file, String str) throws IOException {
|
|
File file2 = new File(file, str);
|
|
if (file2.isFile()) {
|
|
String extension = getExtension(file2);
|
|
if (extension.equals("java")) {
|
|
addFile(file, str);
|
|
} else if (extension.equals("class")) {
|
|
skipClass(str);
|
|
} else {
|
|
copy(z2, file, str);
|
|
}
|
|
} else if (!file2.exists()) {
|
|
this.feedback.printf("Skipping %s because it does not exist.\n", canonical(file2));
|
|
} else if (!file2.isDirectory()) {
|
|
this.feedback.printf("Skipping %s because it is a special file type.\n", canonical(file2));
|
|
}
|
|
}
|
|
|
|
private void addDirectory0(boolean z2, File file, String str, int i) throws IOException {
|
|
File file2 = str.isEmpty() ? file : new File(file, str);
|
|
if (file2.isDirectory()) {
|
|
boolean z3 = !z2 && new File(canonical(file2)).getName().startsWith(".");
|
|
if (i >= 100) {
|
|
this.feedback.printf("Over 100 subdirectories? I'm guessing there's a loop in your directory structure. Skipping: %s\n", str);
|
|
return;
|
|
}
|
|
File[] listFiles = file2.listFiles();
|
|
if (listFiles.length > 0) {
|
|
if (z3 && !this.noCopy && this.output != null) {
|
|
this.feedback.printf("Only processing java files (not copying non-java files) in %s because it's a hidden directory.\n", canonical(file2));
|
|
}
|
|
int length = listFiles.length;
|
|
for (int i2 = 0; i2 < length; i2++) {
|
|
addDirectory0(z2 || z3, file, String.valueOf(str) + (str.isEmpty() ? "" : File.separator) + listFiles[i2].getName(), i + 1);
|
|
}
|
|
} else if (!z3 && !this.noCopy && !z2 && this.output != null && !str.isEmpty()) {
|
|
File file3 = new File(this.output, str);
|
|
file3.mkdirs();
|
|
if (this.verbose) {
|
|
this.feedback.printf("Creating empty directory: %s\n", canonical(file3));
|
|
}
|
|
}
|
|
} else {
|
|
addDirectory1(!z2 && !this.noCopy, file, str);
|
|
}
|
|
}
|
|
|
|
private void skipClass(String str) {
|
|
if (this.verbose) {
|
|
this.feedback.printf("Skipping class file: %s\n", str);
|
|
}
|
|
}
|
|
|
|
private void copy(boolean z2, File file, String str) throws IOException {
|
|
if (this.output == null) {
|
|
this.feedback.printf("Skipping resource file: %s\n", str);
|
|
} else if (z2) {
|
|
if (this.verbose) {
|
|
this.feedback.printf("Copying resource file: %s\n", str);
|
|
}
|
|
byte[] bArr = new byte[65536];
|
|
FileInputStream fileInputStream = new FileInputStream(new File(file, str));
|
|
try {
|
|
File file2 = new File(this.output, str);
|
|
file2.getParentFile().mkdirs();
|
|
FileOutputStream fileOutputStream = new FileOutputStream(file2);
|
|
while (true) {
|
|
int read = fileInputStream.read(bArr);
|
|
if (read == -1) {
|
|
fileOutputStream.close();
|
|
return;
|
|
}
|
|
fileOutputStream.write(bArr, 0, read);
|
|
}
|
|
} finally {
|
|
fileInputStream.close();
|
|
}
|
|
} else if (this.verbose) {
|
|
this.feedback.printf("Skipping resource file: %s\n", str);
|
|
}
|
|
}
|
|
|
|
public void addFile(File file, String str) throws IOException {
|
|
if (this.output == null || !canonical(file).equals(canonical(this.output))) {
|
|
File file2 = new File(file, str);
|
|
this.filesToParse.add(file2);
|
|
this.fileToBase.put(file2, file);
|
|
return;
|
|
}
|
|
throw new IOException("DELOMBOK: Output file and input file refer to the same filesystem location. Specify a separate path for output.");
|
|
}
|
|
|
|
private static <T> com.sun.tools.javac.util.List<T> toJavacList(List<T> list) {
|
|
com.sun.tools.javac.util.List<T> nil = com.sun.tools.javac.util.List.nil();
|
|
ListIterator<T> listIterator = list.listIterator(list.size());
|
|
while (listIterator.hasPrevious()) {
|
|
nil = nil.prepend(listIterator.previous());
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
private static Field getModuleField() {
|
|
try {
|
|
return Permit.getField(JCTree.JCCompilationUnit.class, "modle");
|
|
} catch (NoSuchFieldException unused) {
|
|
return null;
|
|
} catch (SecurityException unused2) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public boolean delombok() throws IOException {
|
|
JavaCompiler javaCompiler;
|
|
LombokOptions delombokOptions = LombokOptionsFactory.getDelombokOptions(this.context);
|
|
delombokOptions.deleteLombokAnnotations();
|
|
delombokOptions.putJavacOption("ENCODING", this.charset.name());
|
|
if (this.classpath != null) {
|
|
delombokOptions.putJavacOption("CLASSPATH", unpackClasspath(this.classpath));
|
|
}
|
|
if (this.sourcepath != null) {
|
|
delombokOptions.putJavacOption("SOURCEPATH", this.sourcepath);
|
|
}
|
|
if (this.bootclasspath != null) {
|
|
delombokOptions.putJavacOption("BOOTCLASSPATH", unpackClasspath(this.bootclasspath));
|
|
}
|
|
delombokOptions.setFormatPreferences(new FormatPreferences(this.formatPrefs));
|
|
delombokOptions.put("compilePolicy", "check");
|
|
if (Javac.getJavaCompilerVersion() >= 9) {
|
|
Arguments instance = Arguments.instance(this.context);
|
|
ArrayList arrayList = new ArrayList();
|
|
if (this.classpath != null) {
|
|
arrayList.add("--class-path");
|
|
arrayList.add(delombokOptions.get("--class-path"));
|
|
}
|
|
if (this.sourcepath != null) {
|
|
arrayList.add("--source-path");
|
|
arrayList.add(delombokOptions.get("--source-path"));
|
|
}
|
|
if (this.bootclasspath != null) {
|
|
arrayList.add("--boot-class-path");
|
|
arrayList.add(delombokOptions.get("--boot-class-path"));
|
|
}
|
|
if (this.charset != null) {
|
|
arrayList.add("-encoding");
|
|
arrayList.add(this.charset.name());
|
|
}
|
|
String pathOfSelf = getPathOfSelf();
|
|
if (pathOfSelf != null) {
|
|
arrayList.add("--module-path");
|
|
arrayList.add((this.modulepath == null || this.modulepath.isEmpty()) ? pathOfSelf : String.valueOf(pathOfSelf) + File.pathSeparator + this.modulepath);
|
|
} else if (this.modulepath != null && !this.modulepath.isEmpty()) {
|
|
arrayList.add("--module-path");
|
|
arrayList.add(this.modulepath);
|
|
}
|
|
if (!this.disablePreview && Javac.getJavaCompilerVersion() >= 11) {
|
|
arrayList.add("--enable-preview");
|
|
}
|
|
if (Javac.getJavaCompilerVersion() < 15) {
|
|
instance.init("javac", (String[]) arrayList.toArray(new String[0]));
|
|
} else {
|
|
instance.init("javac", arrayList);
|
|
}
|
|
delombokOptions.put("diags.legacy", "TRUE");
|
|
delombokOptions.put("allowStringFolding", "FALSE");
|
|
} else if (this.modulepath != null && !this.modulepath.isEmpty()) {
|
|
throw new IllegalStateException("DELOMBOK: Option --module-path requires usage of JDK9 or higher.");
|
|
}
|
|
CommentCatcher create = CommentCatcher.create(this.context, Javac.getJavaCompilerVersion() >= 13);
|
|
JavaCompiler compiler = create.getCompiler();
|
|
ArrayList arrayList2 = new ArrayList();
|
|
IdentityHashMap identityHashMap = new IdentityHashMap();
|
|
Set singleton = Collections.singleton(new LombokProcessor());
|
|
if (Javac.getJavaCompilerVersion() >= 9) {
|
|
BaseFileManager baseFileManager = (JavaFileManager) this.context.get(JavaFileManager.class);
|
|
if (baseFileManager instanceof BaseFileManager) {
|
|
Arguments instance2 = Arguments.instance(this.context);
|
|
baseFileManager.setContext(this.context);
|
|
baseFileManager.handleOptions(instance2.getDeferredFileManagerOptions());
|
|
}
|
|
}
|
|
if (Javac.getJavaCompilerVersion() < 9) {
|
|
compiler.initProcessAnnotations(singleton);
|
|
} else {
|
|
compiler.initProcessAnnotations(singleton, Collections.emptySet(), Collections.emptySet());
|
|
}
|
|
Symbol.ModuleSymbol moduleSymbol = null;
|
|
if (Javac.getJavaCompilerVersion() >= 9) {
|
|
moduleSymbol = Symtab.instance(this.context).unnamedModule;
|
|
}
|
|
Iterator<File> it = this.filesToParse.iterator();
|
|
while (it.hasNext()) {
|
|
File next = it.next();
|
|
JCTree.JCCompilationUnit parse = compiler.parse(next.getAbsolutePath());
|
|
if (Javac.getJavaCompilerVersion() >= 9) {
|
|
try {
|
|
MODULE_FIELD.set(parse, moduleSymbol);
|
|
} catch (IllegalAccessException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
identityHashMap.put(parse, this.fileToBase.get(next));
|
|
arrayList2.add(parse);
|
|
}
|
|
if (compiler.errorCount() > 0) {
|
|
return false;
|
|
}
|
|
Iterator it2 = arrayList2.iterator();
|
|
while (it2.hasNext()) {
|
|
JCTree.JCCompilationUnit jCCompilationUnit = (JCTree.JCCompilationUnit) it2.next();
|
|
create.setComments(jCCompilationUnit, new DocCommentIntegrator().integrate(create.getComments(jCCompilationUnit), jCCompilationUnit));
|
|
}
|
|
if (Javac.getJavaCompilerVersion() >= 9) {
|
|
compiler.initModules(com.sun.tools.javac.util.List.from((JCTree.JCCompilationUnit[]) arrayList2.toArray(new JCTree.JCCompilationUnit[0])));
|
|
}
|
|
com.sun.tools.javac.util.List enterTrees = compiler.enterTrees(toJavacList(arrayList2));
|
|
if (Javac.getJavaCompilerVersion() < 9) {
|
|
javaCompiler = compiler.processAnnotations(enterTrees, com.sun.tools.javac.util.List.nil());
|
|
} else {
|
|
javaCompiler = compiler;
|
|
compiler.processAnnotations(enterTrees, com.sun.tools.javac.util.List.nil());
|
|
}
|
|
callFlowMethodOnJavaCompiler(javaCompiler, callAttributeMethodOnJavaCompiler(javaCompiler, javaCompiler.todo));
|
|
FormatPreferences formatPreferences = new FormatPreferences(this.formatPrefs);
|
|
Iterator it3 = arrayList2.iterator();
|
|
while (it3.hasNext()) {
|
|
JCTree.JCCompilationUnit jCCompilationUnit2 = (JCTree.JCCompilationUnit) it3.next();
|
|
DelombokResult delombokResult = new DelombokResult(create.getComments(jCCompilationUnit2), create.getTextBlockStarts(jCCompilationUnit2), jCCompilationUnit2, this.force || delombokOptions.isChanged(jCCompilationUnit2), formatPreferences);
|
|
if (!this.onlyChanged || delombokResult.isChanged() || delombokOptions.isChanged(jCCompilationUnit2)) {
|
|
if (this.verbose) {
|
|
PrintStream printStream = this.feedback;
|
|
Object[] objArr = new Object[3];
|
|
objArr[0] = jCCompilationUnit2.sourcefile.getName();
|
|
objArr[1] = delombokResult.isChanged() ? "delomboked" : "unchanged";
|
|
objArr[2] = (!this.force || delombokOptions.isChanged(jCCompilationUnit2)) ? "" : " (forced)";
|
|
printStream.printf("File: %s [%s%s]\n", objArr);
|
|
}
|
|
BufferedWriter bufferedWriter = new BufferedWriter(this.presetWriter != null ? createUnicodeEscapeWriter(this.presetWriter) : this.output == null ? createStandardOutWriter() : createFileWriter(this.output, (File) identityHashMap.get(jCCompilationUnit2), jCCompilationUnit2.sourcefile.toUri()));
|
|
try {
|
|
delombokResult.print(bufferedWriter);
|
|
if (this.output != null) {
|
|
bufferedWriter.close();
|
|
} else {
|
|
bufferedWriter.flush();
|
|
}
|
|
} catch (Throwable th) {
|
|
if (this.output != null) {
|
|
bufferedWriter.close();
|
|
} else {
|
|
bufferedWriter.flush();
|
|
}
|
|
throw th;
|
|
}
|
|
} else if (this.verbose) {
|
|
this.feedback.printf("File: %s [%s]\n", jCCompilationUnit2.sourcefile.getName(), "unchanged (skipped)");
|
|
}
|
|
}
|
|
javaCompiler.close();
|
|
return true;
|
|
}
|
|
|
|
private String unpackClasspath(String str) {
|
|
String[] split = str.split(Pattern.quote(File.pathSeparator));
|
|
StringBuilder sb = new StringBuilder();
|
|
for (String str2 : split) {
|
|
if (!str2.endsWith("*")) {
|
|
if (sb.length() > 0) {
|
|
sb.append(File.pathSeparator);
|
|
}
|
|
sb.append(str2);
|
|
} else {
|
|
File[] listFiles = new File(str2.substring(0, str2.length() - 2)).listFiles();
|
|
if (listFiles != null) {
|
|
for (File file : listFiles) {
|
|
if (file.isFile()) {
|
|
if (sb.length() > 0) {
|
|
sb.append(File.pathSeparator);
|
|
}
|
|
sb.append((CharSequence) str2, 0, str2.length() - 1);
|
|
sb.append(file.getName());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
private static Object callAttributeMethodOnJavaCompiler(JavaCompiler javaCompiler, Todo todo) {
|
|
if (attributeMethod == null) {
|
|
try {
|
|
attributeMethod = Permit.getMethod(JavaCompiler.class, "attribute", Queue.class);
|
|
} catch (NoSuchMethodException unused) {
|
|
try {
|
|
attributeMethod = Permit.getMethod(JavaCompiler.class, "attribute", ListBuffer.class);
|
|
} catch (NoSuchMethodException e) {
|
|
throw Lombok.sneakyThrow(e);
|
|
}
|
|
}
|
|
}
|
|
try {
|
|
return attributeMethod.invoke(javaCompiler, todo);
|
|
} catch (Exception e2) {
|
|
if (e2 instanceof InvocationTargetException) {
|
|
throw Lombok.sneakyThrow(e2.getCause());
|
|
}
|
|
throw Lombok.sneakyThrow(e2);
|
|
}
|
|
}
|
|
|
|
private static void callFlowMethodOnJavaCompiler(JavaCompiler javaCompiler, Object obj) {
|
|
if (flowMethod == null) {
|
|
try {
|
|
flowMethod = Permit.getMethod(JavaCompiler.class, "flow", Queue.class);
|
|
} catch (NoSuchMethodException unused) {
|
|
try {
|
|
flowMethod = Permit.getMethod(JavaCompiler.class, "flow", com.sun.tools.javac.util.List.class);
|
|
} catch (NoSuchMethodException e) {
|
|
throw Lombok.sneakyThrow(e);
|
|
}
|
|
}
|
|
}
|
|
try {
|
|
flowMethod.invoke(javaCompiler, obj);
|
|
} catch (Exception e2) {
|
|
if (e2 instanceof InvocationTargetException) {
|
|
throw Lombok.sneakyThrow(e2.getCause());
|
|
}
|
|
throw Lombok.sneakyThrow(e2);
|
|
}
|
|
}
|
|
|
|
private static String canonical(File file) {
|
|
try {
|
|
return file.getCanonicalPath();
|
|
} catch (Exception unused) {
|
|
return file.getAbsolutePath();
|
|
}
|
|
}
|
|
|
|
private static String getExtension(File file) {
|
|
String name = file.getName();
|
|
int lastIndexOf = name.lastIndexOf(46);
|
|
return lastIndexOf == -1 ? "" : name.substring(lastIndexOf + 1);
|
|
}
|
|
|
|
private Writer createFileWriter(File file, File file2, URI uri) throws IOException {
|
|
URI uri2 = file2.toURI();
|
|
URI relativize = uri2.relativize(uri2.resolve(uri));
|
|
File file3 = relativize.isAbsolute() ? new File(file, new File(relativize).getName()) : new File(file, relativize.getPath());
|
|
file3.getParentFile().mkdirs();
|
|
return createUnicodeEscapeWriter(new FileOutputStream(file3));
|
|
}
|
|
|
|
private Writer createStandardOutWriter() {
|
|
return createUnicodeEscapeWriter(System.out);
|
|
}
|
|
|
|
private Writer createUnicodeEscapeWriter(Writer writer) {
|
|
return new UnicodeEscapeWriter(writer, this.charset);
|
|
}
|
|
|
|
private Writer createUnicodeEscapeWriter(OutputStream outputStream) {
|
|
return new UnicodeEscapeWriter(new OutputStreamWriter(outputStream, this.charset), this.charset);
|
|
}
|
|
}
|