77import java .util .List ;
88import java .util .Map ;
99import java .util .Objects ;
10+ import java .util .ServiceLoader ;
1011import java .util .Set ;
1112import java .util .TreeSet ;
1213import java .util .function .Consumer ;
1314import java .util .stream .Collectors ;
1415import javax .annotation .Nullable ;
16+ import com .google .common .collect .ImmutableList ;
17+ import org .immutables .extgenerator .GeneratedImportsModifier ;
1518import static java .lang .Character .isJavaIdentifierPart ;
1619import static java .lang .Character .isJavaIdentifierStart ;
1720import static java .lang .Character .isLowerCase ;
3538public class ImportRewriter {
3639 private final CharSequence source ;
3740
41+ static final ImmutableList <GeneratedImportsModifier > importsModifiers =
42+ ImmutableList .copyOf (ServiceLoader .load (GeneratedImportsModifier .class ,
43+ PostprocessingMachine .class .getClassLoader ()));
44+
3845 private final int len ;
3946 private int at ;
4047
@@ -82,15 +89,16 @@ private static final class Import extends Occurrence {
8289 final LinkedList <String > classSegments = new LinkedList <>();
8390 final StringBuilder name = new StringBuilder ();
8491 boolean isStatic ;
92+ boolean isStar ;
8593
8694 @ Override
8795 public String toString () {
88- return (packageSegments .isEmpty () ? "" : ("[" + String .join ("." , packageSegments ) + "]" ))
89- + String .join ("." , classSegments );
96+ return (isStatic ? "static " : "" ) + asKey () + (isStar ? ".*" : "" );
9097 }
9198
9299 String asKey () {
93- return (packageSegments .isEmpty () ? "" : (String .join ("." , packageSegments ) + '.' ))
100+ return (packageSegments .isEmpty ()
101+ ? "" : (String .join ("." , packageSegments ) + (classSegments .isEmpty () ? "" : "." )))
94102 + String .join ("." , classSegments );
95103 }
96104
@@ -155,6 +163,22 @@ private String replace() {
155163 // these blocks were kept inline to better see overall work done
156164 // and preserve context/state of result content and sourceAt pointer
157165
166+ // append useful (non-skipped) imports to the end of our newImports set
167+ for (Import imp : imports ) {
168+ if (imp .isStatic
169+ || imp .isStar
170+ || imp .classSegments .size () != 1
171+ || !imp .packageSegments .equals (JAVA_LANG )) {
172+
173+ newImports .add (imp .toString ());
174+ }
175+ }
176+
177+ // Run import modifiers if present on the classpath
178+ for (GeneratedImportsModifier modifier : importsModifiers ) {
179+ modifier .modify (thisPackage , newImports );
180+ }
181+
158182 // Insert new imports
159183 if (!newImports .isEmpty ()) {
160184 int insertAt = Math .max (beforeImportPosition , afterPackagePosition );
@@ -173,15 +197,11 @@ private String replace() {
173197 result .append ("\n " );
174198 }
175199
200+ // getting to the last of existing imports and setting position to the last one
201+ // it's ok to just iterate in this way, extracting the last one if present is more trouble
176202 for (Import imp : imports ) {
177- if (!imp .isStatic
178- && imp .classSegments .size () == 1
179- && imp .packageSegments .equals (JAVA_LANG )) {
180- // append everything since last position
181- result .append (source , sourceAt , imp .at );
182- // and skipping this import
183- sourceAt = imp .at + imp .len ;
184- }
203+ //result.append("//PARSED ").append(imp).append("\n");
204+ sourceAt = imp .at + imp .len ;
185205 }
186206
187207 // Replace rewritten
@@ -660,6 +680,24 @@ private boolean processImport() {
660680
661681 imp .range (begin , at - begin );
662682 addImport (imp );
683+ } else if (consume ('.' )) {
684+ // FIXME This case currently not working as consumeQualifiedName would go past the dot
685+ consumeWhitespace ();
686+ if (consume ('*' )) {
687+ consumeWhitespace ();
688+ if (consume (';' )) {
689+ imp .isStar = true ;
690+ imp .range (begin , at - begin );
691+ addImport (imp );
692+ }
693+ }
694+ } else if (consume ('*' )) {
695+ consumeWhitespace ();
696+ if (consume (';' )) {
697+ imp .isStar = true ;
698+ imp .range (begin , at - begin );
699+ addImport (imp );
700+ }
663701 }
664702 // if we've not added import, we still recognized it to some degree, so return true
665703 // and not backtracking to begin
@@ -671,8 +709,10 @@ private boolean processImport() {
671709
672710 private void addImport (Import imp ) {
673711 imports .add (imp );
674- usages .put (imp .local (), imp .asKey ());
675- //System.err.println("IMPORT " + (imp.isStatic ? "STATIC " : "") + imp);
712+ // we can use condition !imp.isStar, but we use more general check
713+ if (!imp .classSegments .isEmpty ()) {
714+ usages .put (imp .local (), imp .asKey ());
715+ }
676716 }
677717
678718 private boolean processRecord () {
0 commit comments