Skip to content

Commit 3de8cb3

Browse files
SentryManrbygrave
andauthored
Remove Wildcards and package-private Imports (#257)
* remove wildcards and package private imports * Delete jsonb-generator/avaje-processors.txt * Update JsonbProcessor.java * Add unit tests for Util.validImportType() and refactor * Inline variable in Util.importDifferentPackage() --------- Co-authored-by: Rob Bygrave <robin.bygrave@gmail.com>
1 parent 9440f78 commit 3de8cb3

File tree

9 files changed

+63
-29
lines changed

9 files changed

+63
-29
lines changed

jsonb-generator/src/main/java/io/avaje/jsonb/generator/BeanReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface BeanReader {
99

1010
void read();
1111

12-
void writeImports(Append writer);
12+
void writeImports(Append writer, String packageName);
1313

1414
void writeFields(Append writer);
1515

jsonb-generator/src/main/java/io/avaje/jsonb/generator/ClassReader.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,11 @@ private Set<String> importTypes() {
165165
importTypes.add(Constants.REFLECT_TYPE);
166166
importTypes.add(Constants.PARAMETERIZED_TYPE);
167167
}
168-
importTypes.add(Constants.JSONB_WILD);
169168
importTypes.add(Constants.IOEXCEPTION);
170-
importTypes.add(Constants.JSONB_SPI);
171169
if (!hasSubTypes) {
172170
importTypes.add(Constants.METHODHANDLE);
173171
}
174-
if (Util.validImportType(type) && !ProcessingContext.isImported(beanType)) {
172+
if (!ProcessingContext.isImported(beanType)) {
175173
importTypes.add(type);
176174
}
177175
for (final FieldReader allField : allFields) {
@@ -183,19 +181,29 @@ private Set<String> importTypes() {
183181
if (implementation != null) {
184182
implementation.addImported(importTypes);
185183
}
184+
185+
if (supportsViewBuilder()) {
186+
importTypes.add("io.avaje.jsonb.spi.ViewBuilder");
187+
importTypes.add("io.avaje.jsonb.spi.ViewBuilderAware");
188+
}
189+
importTypes.add("io.avaje.jsonb.JsonAdapter");
190+
importTypes.add(Constants.JSONB);
191+
importTypes.add("io.avaje.jsonb.JsonReader");
192+
importTypes.add("io.avaje.jsonb.JsonWriter");
193+
importTypes.add("io.avaje.jsonb.Types");
194+
importTypes.add("io.avaje.jsonb.spi.Generated");
195+
importTypes.add("io.avaje.jsonb.spi.PropertyNames");
186196
return importTypes;
187197
}
188198

189199
private void addImported(Set<String> importTypes) {
190-
if (Util.validImportType(type)) {
191-
importTypes.add(type);
192-
}
200+
importTypes.add(type);
193201
}
194202

195203
@Override
196-
public void writeImports(Append writer) {
204+
public void writeImports(Append writer, String packageName) {
197205
for (final String importType : importTypes()) {
198-
if (Util.validImportType(importType)) {
206+
if (Util.validImportType(importType, packageName)) {
199207
writer.append("import %s;", Util.sanitizeImports(importType)).eol();
200208
}
201209
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/ComponentMetaData.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,14 @@ List<String> allFactories() {
6565
Collection<String> allImports() {
6666
Set<String> packageImports = new TreeSet<>();
6767
for (String adapterFullName : allTypes) {
68-
packageImports.add(Util.packageOf(adapterFullName) + ".*");
68+
packageImports.add(adapterFullName);
6969

7070
final String className = Util.baseTypeOfAdapter(adapterFullName);
7171
final int $index = className.indexOf("$");
7272
packageImports.add($index != -1 ? className.substring(0, $index) : className);
7373
}
7474

75-
for (final String adapterFullName : factoryTypes) {
76-
packageImports.add(Util.packageOf(adapterFullName) + ".*");
77-
}
75+
packageImports.addAll(factoryTypes);
7876
return packageImports;
7977
}
8078

jsonb-generator/src/main/java/io/avaje/jsonb/generator/Constants.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
final class Constants {
44

55
static final String META_INF_COMPONENT = "META-INF/services/io.avaje.jsonb.spi.JsonbExtension";
6-
static final String JSONB_WILD = "io.avaje.jsonb.*";
7-
static final String JSONB_SPI = "io.avaje.jsonb.spi.*";
86
static final String JSONB = "io.avaje.jsonb.Jsonb";
97
static final String JSON = "io.avaje.jsonb.Json";
108
static final String JSON_IMPORT = "io.avaje.jsonb.Json.Import";

jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleAdapterWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private void writeFields() {
124124
}
125125

126126
private void writeImports() {
127-
beanReader.writeImports(writer);
127+
beanReader.writeImports(writer, adapterPackage);
128128
}
129129

130130
private void writePackage() {

jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleComponentWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ private void writeMetaDataEntry(List<String> entries) {
108108

109109
private void writeImports() {
110110
importTypes.add(Constants.JSONB);
111-
importTypes.add(Constants.JSONB_SPI);
112111
importTypes.addAll(metaData.allImports());
112+
importTypes.add("io.avaje.jsonb.spi.Generated");
113+
importTypes.add("io.avaje.jsonb.spi.GeneratedComponent");
114+
importTypes.add("io.avaje.jsonb.spi.MetaData");
113115

114116
for (final String importType : importTypes) {
115-
if (Util.validImportType(importType)) {
117+
if (Util.validImportType(importType, metaData.packageName())) {
116118
writer.append("import %s;", importType).eol();
117119
}
118120
}

jsonb-generator/src/main/java/io/avaje/jsonb/generator/Util.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.util.ArrayList;
1212
import java.util.List;
13+
import java.util.function.Function;
1314
import java.util.regex.Matcher;
1415
import java.util.regex.Pattern;
1516

@@ -22,8 +23,19 @@ final class Util {
2223
private static final Pattern COMMA_PATTERN =
2324
Pattern.compile(", (?=(?:[^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)");
2425

25-
static boolean validImportType(String type) {
26-
return type.indexOf('.') > 0;
26+
static boolean validImportType(String type, String packageName) {
27+
return type.indexOf('.') > -1
28+
&& !type.startsWith("java.lang.")
29+
&& importDifferentPackage(type, packageName)
30+
|| importJavaLangSubpackage(type);
31+
}
32+
33+
private static boolean importDifferentPackage(String type, String packageName) {
34+
return type.replace(packageName + '.', "").indexOf('.') > 0;
35+
}
36+
37+
private static boolean importJavaLangSubpackage(String type) {
38+
return type.startsWith("java.lang.") && importDifferentPackage(type, "java.lang");
2739
}
2840

2941
public static String sanitizeImports(String type) {

jsonb-generator/src/main/java/io/avaje/jsonb/generator/ValueReader.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,27 @@ public TypeElement beanType() {
7373
}
7474

7575
private Set<String> importTypes() {
76-
importTypes.add(Constants.JSONB_WILD);
7776
importTypes.add(Constants.IOEXCEPTION);
78-
importTypes.add(Constants.JSONB_SPI);
7977
importTypes.add("java.util.EnumMap");
8078
importTypes.add("java.util.HashMap");
8179
importTypes.add("java.util.Map");
82-
if (Util.validImportType(type)) {
83-
importTypes.add(type);
84-
}
85-
importTypes.add(Constants.JSONB_SPI);
80+
importTypes.add(type);
8681
importTypes.add(element.asType().toString());
8782
importTypes.add(method.getReturnType().toString());
83+
importTypes.add("io.avaje.jsonb.spi.PropertyNames");
84+
importTypes.add("io.avaje.jsonb.JsonAdapter");
85+
importTypes.add("io.avaje.jsonb.JsonDataException");
86+
importTypes.add("io.avaje.jsonb.JsonReader");
87+
importTypes.add("io.avaje.jsonb.JsonWriter");
88+
importTypes.add("io.avaje.jsonb.spi.Generated");
89+
importTypes.add(Constants.JSONB);
8890
return importTypes;
8991
}
9092

9193
@Override
92-
public void writeImports(Append writer) {
94+
public void writeImports(Append writer, String packageName) {
9395
for (final String importType : importTypes()) {
94-
if (Util.validImportType(importType)) {
96+
if (Util.validImportType(importType, packageName)) {
9597
writer.append("import %s;", Util.sanitizeImports(importType)).eol();
9698
}
9799
}

jsonb-generator/src/test/java/io/avaje/jsonb/generator/UtilTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.List;
66

77
import static org.assertj.core.api.Assertions.assertThat;
8-
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.*;
99

1010
class UtilTest {
1111

@@ -50,4 +50,18 @@ void sanitizeImports() {
5050
assertEquals("org.foo.Bar", Util.sanitizeImports("org.foo.Bar[]"));
5151
assertEquals("org.foo.Bar", Util.sanitizeImports("@some.Nullable org.foo.Bar[]"));
5252
}
53+
54+
@Test
55+
void validImportType_expect_false() {
56+
assertFalse(Util.validImportType("int", "org.foo"));
57+
assertFalse(Util.validImportType("java.lang.Integer", "org.foo"));
58+
assertFalse(Util.validImportType("org.foo.Bar", "org.foo"));
59+
}
60+
61+
@Test
62+
void validImportType_expect_true() {
63+
assertTrue(Util.validImportType("java.lang.something.Foo", "org.foo"));
64+
assertTrue(Util.validImportType("org.foo.some.Bar", "org.foo"));
65+
assertTrue(Util.validImportType("org.other.Bar", "org.foo"));
66+
}
5367
}

0 commit comments

Comments
 (0)