Skip to content

Commit 8a43db9

Browse files
committed
#1575 restoring GeneratedImportsModifier functionality
1 parent a036232 commit 8a43db9

3 files changed

Lines changed: 61 additions & 18 deletions

File tree

generator/src/org/immutables/extgenerator/GeneratedImportsModifier.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@ public interface GeneratedImportsModifier {
3232
/**
3333
* Modify imports set after import processing/extraction but before writing generated files.
3434
* Important to note that there are no way to change classnames as other parts of source file
35-
* refers to class by simple name. However package-name part of imports may be rewritten.
35+
* refers to class by simple name. However, package-name part of imports may be rewritten.
36+
* <p>
37+
* <em>Note: Before fully qualified class name there may be {@code static} keyword for static
38+
* imports. Also import might contain {@code .*} suffix, while not typical, some templates
39+
* might have these. But all automatic imports extracted by import rewriting tool will be
40+
* just plain, non-static class imports and their import lines will be just fully qualified class names</em>
3641
* @param packageOfGeneratedFile informative package name of the generated file, may be used to
3742
* employ different strategies for different packages.
38-
* @param imports modifiables set of imports
43+
* @param imports modifiable set of imports
3944
*/
4045
void modify(String packageOfGeneratedFile, Set<String> imports);
4146
}

generator/src/org/immutables/generator/ImportRewriter.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import java.util.List;
88
import java.util.Map;
99
import java.util.Objects;
10+
import java.util.ServiceLoader;
1011
import java.util.Set;
1112
import java.util.TreeSet;
1213
import java.util.function.Consumer;
1314
import java.util.stream.Collectors;
1415
import javax.annotation.Nullable;
16+
import com.google.common.collect.ImmutableList;
17+
import org.immutables.extgenerator.GeneratedImportsModifier;
1518
import static java.lang.Character.isJavaIdentifierPart;
1619
import static java.lang.Character.isJavaIdentifierStart;
1720
import static java.lang.Character.isLowerCase;
@@ -35,6 +38,10 @@
3538
public 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() {

generator/src/org/immutables/generator/PostprocessingMachine.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public final class PostprocessingMachine {
4343

4444
private static final Joiner JOINER = Joiner.on("");
4545

46-
private static final ImmutableList<GeneratedImportsModifier> importsModifiers =
47-
ImmutableList.copyOf(ServiceLoader.load(GeneratedImportsModifier.class,
48-
PostprocessingMachine.class.getClassLoader()));
46+
private static final ImmutableList<GeneratedImportsModifier> importsModifiers = ImportRewriter.importsModifiers;
4947

5048
private PostprocessingMachine() {}
5149

0 commit comments

Comments
 (0)