diff options
Diffstat (limited to 'src/delombok')
-rwxr-xr-x | src/delombok/lombok/delombok/Delombok.java | 29 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/DelombokApp.java | 7 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/DocCommentIntegrator.java | 6 | ||||
-rw-r--r-- | src/delombok/lombok/delombok/PrettyPrinter.java | 62 |
4 files changed, 79 insertions, 25 deletions
diff --git a/src/delombok/lombok/delombok/Delombok.java b/src/delombok/lombok/delombok/Delombok.java index 6b745015..8d39f447 100755 --- a/src/delombok/lombok/delombok/Delombok.java +++ b/src/delombok/lombok/delombok/Delombok.java @@ -43,6 +43,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.IdentityHashMap; import java.util.LinkedHashMap; import java.util.List; @@ -51,6 +52,7 @@ import java.util.Map; import java.util.Set; import java.util.regex.Pattern; +import javax.annotation.processing.AbstractProcessor; import javax.tools.DiagnosticListener; import javax.tools.JavaFileManager; import javax.tools.JavaFileObject; @@ -58,6 +60,7 @@ import javax.tools.JavaFileObject; import lombok.Lombok; import lombok.javac.CommentCatcher; import lombok.javac.Javac; +import lombok.javac.JavacAugments; import lombok.javac.LombokOptions; import lombok.javac.apt.LombokProcessor; import lombok.permit.Permit; @@ -67,8 +70,11 @@ 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.tree.JCTree.JCCompilationUnit; +import com.sun.tools.javac.tree.JCTree.JCImport; 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; @@ -97,6 +103,7 @@ public class Delombok { 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>(); + private List<AbstractProcessor> additionalAnnotationProcessors = new ArrayList<AbstractProcessor>(); /** If null, output to standard out. */ private File output = null; @@ -165,6 +172,10 @@ public class Delombok { private boolean help; } + static { + LombokProcessor.addOpensForLombok(); + } + private static String indentAndWordbreak(String in, int indent, int maxLen) { StringBuilder out = new StringBuilder(); StringBuilder line = new StringBuilder(); @@ -642,6 +653,10 @@ public class Delombok { fileToBase.put(f, base); } + public void addAdditionalAnnotationProcessor(AbstractProcessor processor) { + additionalAnnotationProcessors.add(processor); + } + private static <T> com.sun.tools.javac.util.List<T> toJavacList(List<T> list) { com.sun.tools.javac.util.List<T> out = com.sun.tools.javac.util.List.nil(); ListIterator<T> li = list.listIterator(list.size()); @@ -718,7 +733,9 @@ public class Delombok { List<JCCompilationUnit> roots = new ArrayList<JCCompilationUnit>(); Map<JCCompilationUnit, File> baseMap = new IdentityHashMap<JCCompilationUnit, File>(); - Set<LombokProcessor> processors = Collections.singleton(new lombok.javac.apt.LombokProcessor()); + Set<AbstractProcessor> processors = new HashSet<AbstractProcessor>(); + processors.add(new lombok.javac.apt.LombokProcessor()); + processors.addAll(additionalAnnotationProcessors); if (Javac.getJavaCompilerVersion() >= 9) { JavaFileManager jfm_ = context.get(JavaFileManager.class); @@ -782,6 +799,16 @@ public class Delombok { if (verbose) feedback.printf("File: %s [%s]\n", unit.sourcefile.getName(), "unchanged (skipped)"); continue; } + ListBuffer<JCTree> newDefs = new ListBuffer<JCTree>(); + for (JCTree def : unit.defs) { + if (def instanceof JCImport) { + Boolean b = JavacAugments.JCImport_deletable.get((JCImport) def); + if (b == null || !b.booleanValue()) newDefs.append(def); + } else { + newDefs.append(def); + } + } + unit.defs = newDefs.toList(); if (verbose) feedback.printf("File: %s [%s%s]\n", unit.sourcefile.getName(), result.isChanged() ? "delomboked" : "unchanged", force && !options.isChanged(unit) ? " (forced)" : ""); Writer rawWriter; if (presetWriter != null) rawWriter = createUnicodeEscapeWriter(presetWriter); diff --git a/src/delombok/lombok/delombok/DelombokApp.java b/src/delombok/lombok/delombok/DelombokApp.java index 8467bf77..f5a5fc81 100644 --- a/src/delombok/lombok/delombok/DelombokApp.java +++ b/src/delombok/lombok/delombok/DelombokApp.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Project Lombok Authors. + * Copyright (C) 2009-2021 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 @@ -37,10 +37,9 @@ import java.util.jar.JarFile; import lombok.core.LombokApp; import lombok.permit.Permit; +import lombok.spi.Provides; -import org.mangosdk.spi.ProviderFor; - -@ProviderFor(LombokApp.class) +@Provides public class DelombokApp extends LombokApp { @Override public int runApp(List<String> args) throws Exception { try { diff --git a/src/delombok/lombok/delombok/DocCommentIntegrator.java b/src/delombok/lombok/delombok/DocCommentIntegrator.java index bab0abd8..e61968a5 100644 --- a/src/delombok/lombok/delombok/DocCommentIntegrator.java +++ b/src/delombok/lombok/delombok/DocCommentIntegrator.java @@ -89,7 +89,7 @@ public class DocCommentIntegrator { ((Map<JCTree, String>) map_).put(node, docCommentContent); return true; } else if (Javac.instanceOfDocCommentTable(map_)) { - CommentAttacher_8.attach(node, docCommentContent, map_); + CommentAttacher_8.attach(node, docCommentContent, cmt.pos, map_); return true; } @@ -98,7 +98,7 @@ public class DocCommentIntegrator { /* Container for code which will cause class loader exceptions on javac below 8. By being in a separate class, we avoid the problem. */ private static class CommentAttacher_8 { - static void attach(final JCTree node, String docCommentContent, Object map_) { + static void attach(final JCTree node, String docCommentContent, final int pos, Object map_) { final String docCommentContent_ = docCommentContent; ((DocCommentTable) map_).putComment(node, new Comment() { @Override public String getText() { @@ -106,7 +106,7 @@ public class DocCommentIntegrator { } @Override public int getSourcePos(int index) { - return -1; + return pos + index; } @Override public CommentStyle getStyle() { diff --git a/src/delombok/lombok/delombok/PrettyPrinter.java b/src/delombok/lombok/delombok/PrettyPrinter.java index 9f47e5ec..c46a3298 100644 --- a/src/delombok/lombok/delombok/PrettyPrinter.java +++ b/src/delombok/lombok/delombok/PrettyPrinter.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016-2020 The Project Lombok Authors. + * Copyright (C) 2016-2021 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 @@ -29,7 +29,6 @@ import static lombok.javac.JavacTreeMaker.TypeTag.typeTag; import java.io.IOException; import java.io.Writer; import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.HashMap; @@ -279,7 +278,6 @@ public class PrettyPrinter extends JCTree.Visitor { aligned = false; } - private void println() { try { out.write(LINE_SEP); @@ -516,10 +514,12 @@ public class PrettyPrinter extends JCTree.Visitor { boolean isInterface = (tree.mods.flags & INTERFACE) != 0; boolean isAnnotationInterface = isInterface && (tree.mods.flags & ANNOTATION) != 0; boolean isEnum = (tree.mods.flags & ENUM) != 0; + boolean isRecord = (tree.mods.flags & RECORD) != 0; if (isAnnotationInterface) print("@interface "); else if (isInterface) print("interface "); else if (isEnum) print("enum "); + else if (isRecord) print("record "); else print("class "); print(tree.name); @@ -542,6 +542,8 @@ public class PrettyPrinter extends JCTree.Visitor { print(tree.implementing, ", "); } + if (isRecord) printRecordConstructor(tree.defs); + println(" {"); indent++; printClassMembers(tree.defs, isEnum, isInterface); @@ -551,6 +553,23 @@ public class PrettyPrinter extends JCTree.Visitor { currentTypeName = prevTypeName; } + private void printRecordConstructor(List<JCTree> members) { + boolean first = true; + print("("); + for (JCTree member : members) { + if (member instanceof JCVariableDecl) { + JCVariableDecl variableDecl = (JCVariableDecl) member; + if ((variableDecl.mods.flags & GENERATED_MEMBER) != 0) { + if (!first) print(", "); + first = false; + printAnnotations(variableDecl.mods.annotations, false); + printVarDef0(variableDecl); + } + } + } + print(")"); + } + private void printClassMembers(List<JCTree> members, boolean isEnum, boolean isInterface) { Class<?> prefType = null; int typeOfPrevEnumMember = isEnum ? 3 : 0; // 1 = normal, 2 = with body, 3 = no enum field yet. @@ -580,6 +599,7 @@ public class PrettyPrinter extends JCTree.Visitor { JCTree init = ((JCVariableDecl) member).init; typeOfPrevEnumMember = init instanceof JCNewClass && ((JCNewClass) init).def != null ? 2 : 1; } else if (member instanceof JCVariableDecl) { + if ((((JCVariableDecl) member).mods.flags & GENERATED_MEMBER) != 0) continue; if (prefType != null && prefType != JCVariableDecl.class) println(); if (isInterface) flagMod = -1L & ~(PUBLIC | STATIC | FINAL); print(member); @@ -792,21 +812,28 @@ public class PrettyPrinter extends JCTree.Visitor { print(tree.name); } - boolean first = true; - print("("); - - JCVariableDecl recvparam = readObject(tree, "recvparam", null); - if (recvparam != null) { - printVarDefInline(recvparam); - first = false; + boolean argsLessConstructor = false; + if (isConstructor && (tree.mods.flags & COMPACT_RECORD_CONSTRUCTOR) != 0) { + argsLessConstructor = true; } - for (JCVariableDecl param : tree.params) { - if (!first) print(", "); - first = false; - printVarDefInline(param); + boolean first = true; + if (!argsLessConstructor) { + print("("); + + JCVariableDecl recvparam = readObject(tree, "recvparam", null); + if (recvparam != null) { + printVarDefInline(recvparam); + first = false; + } + + for (JCVariableDecl param : tree.params) { + if (!first) print(", "); + first = false; + printVarDefInline(param); + } + print(")"); } - print(")"); if (tree.thrown.nonEmpty()) { print(" throws "); @@ -1387,9 +1414,10 @@ public class PrettyPrinter extends JCTree.Visitor { } void printBindingPattern(JCTree tree) { - print((JCExpression) readObject(tree, "vartype", null)); + JCTree var = readObject(tree, "var", tree); + print((JCExpression) readObject(var, "vartype", null)); print(" "); - print((Name) readObject(tree, "name", null)); + print((Name) readObject(var, "name", null)); } @Override public void visitTry(JCTry tree) { |