Skip to content

Commit d09f7f0

Browse files
committed
now mixin can import
1 parent 9992ad4 commit d09f7f0

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

blackbox-test/src/main/java/org/example/customer/mixin/CrewMateMixIn.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import io.avaje.jsonb.Json;
44

5-
@Json.Import(CrewMate.class)
65
@Json.MixIn(CrewMate.class)
76
public abstract class CrewMateMixIn {
87

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class BeanReader {
4646

4747
public BeanReader(
4848
TypeElement beanType,
49-
Map<String, Element> mixInFields,
49+
TypeElement mixInElement,
5050
ProcessingContext context) {
5151

5252
this.beanType = beanType;
@@ -55,7 +55,7 @@ public BeanReader(
5555
final NamingConventionReader ncReader = new NamingConventionReader(beanType);
5656
this.namingConvention = ncReader.get();
5757
this.typeProperty = ncReader.typeProperty();
58-
this.typeReader = new TypeReader(beanType, mixInFields, context, namingConvention);
58+
this.typeReader = new TypeReader(beanType, mixInElement, context, namingConvention);
5959
typeReader.process();
6060
this.nonAccessibleField = typeReader.nonAccessibleField();
6161
this.hasSubTypes = typeReader.hasSubTypes();

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
import java.io.IOException;
44
import java.util.ArrayList;
5-
import java.util.HashMap;
65
import java.util.HashSet;
76
import java.util.LinkedHashSet;
87
import java.util.List;
9-
import java.util.Map;
108
import java.util.Set;
119
import java.util.TreeSet;
12-
import java.util.stream.Collectors;
1310

1411
import javax.annotation.processing.AbstractProcessor;
1512
import javax.annotation.processing.ProcessingEnvironment;
@@ -27,6 +24,7 @@ public class Processor extends AbstractProcessor {
2724
private final ImportReader importReader = new ImportReader();
2825
private final List<BeanReader> allReaders = new ArrayList<>();
2926
private final Set<String> sourceTypes = new HashSet<>();
27+
private final Set<String> mixInImports = new HashSet<>();
3028

3129
private ProcessingContext context;
3230
private SimpleComponentWriter componentWriter;
@@ -71,9 +69,8 @@ private void readModule() {
7169
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment round) {
7270
readModule();
7371
writeAdapters(round.getElementsAnnotatedWith(Json.class));
74-
writeAdaptersForImported(
75-
round.getElementsAnnotatedWith(Json.Import.class),
76-
round.getElementsAnnotatedWith(Json.MixIn.class));
72+
writeAdaptersForMixInTypes(round.getElementsAnnotatedWith(Json.MixIn.class));
73+
writeAdaptersForImported(round.getElementsAnnotatedWith(Json.Import.class));
7774
initialiseComponent();
7875
cascadeTypes();
7976
writeComponent(round.processingOver());
@@ -120,27 +117,37 @@ private boolean ignoreType(String type) {
120117
|| sourceTypes.contains(type);
121118
}
122119

123-
/**
124-
* Elements that have a {@code @Json.Import} annotation.
125-
*/
126-
private void writeAdaptersForImported(
127-
Set<? extends Element> importedElements, Set<? extends Element> mixed) {
128-
final Map<String, TypeElement> mixinMap = new HashMap<>();
129-
for (final Element mixin : mixed) {
120+
/** Elements that have a {@code @Json.MixIn} annotation. */
121+
private void writeAdaptersForMixInTypes(Set<? extends Element> mixInElements) {
122+
123+
for (final Element mixin : mixInElements) {
130124
final String importType = importReader.readMixin(mixin);
131125
if (importType != null) {
132-
mixinMap.put(importType, context.element(mixin.asType().toString()));
126+
final TypeElement element = context.element(importType);
127+
if (element == null) {
128+
context.logError("Unable to find imported element " + importType);
129+
} else {
130+
mixInImports.add(importType);
131+
writeAdapterForMixInType(element, context.element(mixin.asType().toString()));
132+
}
133133
}
134134
}
135+
}
136+
137+
/** Elements that have a {@code @Json.Import} annotation. */
138+
private void writeAdaptersForImported(Set<? extends Element> importedElements) {
135139

136140
for (final Element importedElement : importedElements) {
137141
for (final String importType : importReader.read(importedElement)) {
142+
// if imported by mixin annotation skip
143+
if (mixInImports.contains(importType)) {
144+
continue;
145+
}
146+
138147
final TypeElement element = context.element(importType);
139148

140149
if (element == null) {
141150
context.logError("Unable to find imported element " + importType);
142-
} else if (mixinMap.containsKey(importType)) {
143-
writeAdapterForMixInType(element, mixinMap.get(importType));
144151
} else {
145152
writeAdapterForType(element);
146153
}
@@ -152,7 +159,7 @@ private void initialiseComponent() {
152159
metaData.initialiseFullName();
153160
try {
154161
componentWriter.initialise();
155-
} catch (IOException e) {
162+
} catch (final IOException e) {
156163
context.logError("Error creating writer for JsonbComponent", e);
157164
}
158165
}
@@ -162,7 +169,7 @@ private void writeComponent(boolean processingOver) {
162169
try {
163170
componentWriter.write();
164171
componentWriter.writeMetaInf();
165-
} catch (IOException e) {
172+
} catch (final IOException e) {
166173
context.logError("Error writing component", e);
167174
}
168175
}
@@ -172,7 +179,7 @@ private void writeComponent(boolean processingOver) {
172179
* Read the beans that have changed.
173180
*/
174181
private void writeAdapters(Set<? extends Element> beans) {
175-
for (Element element : beans) {
182+
for (final Element element : beans) {
176183
if (!(element instanceof TypeElement)) {
177184
context.logError("unexpected type [" + element + "]");
178185
} else {
@@ -187,11 +194,7 @@ private void writeAdapterForType(TypeElement typeElement) {
187194
}
188195

189196
private void writeAdapterForMixInType(TypeElement typeElement, TypeElement mixin) {
190-
final Map<String, Element> mixInFields =
191-
mixin.getEnclosedElements().stream()
192-
.filter(e -> e.getKind() == ElementKind.FIELD)
193-
.collect(Collectors.toMap(e -> e.getSimpleName().toString(), e -> e));
194-
final BeanReader beanReader = new BeanReader(typeElement, mixInFields, context);
197+
final BeanReader beanReader = new BeanReader(typeElement, mixin, context);
195198
writeAdapter(typeElement, beanReader);
196199
}
197200

@@ -204,12 +207,12 @@ private void writeAdapter(TypeElement typeElement, BeanReader beanReader) {
204207
return;
205208
}
206209
try {
207-
SimpleAdapterWriter beanWriter = new SimpleAdapterWriter(beanReader, context);
210+
final SimpleAdapterWriter beanWriter = new SimpleAdapterWriter(beanReader, context);
208211
metaData.add(beanWriter.fullName());
209212
beanWriter.write();
210213
allReaders.add(beanReader);
211214
sourceTypes.add(typeElement.getSimpleName().toString());
212-
} catch (IOException e) {
215+
} catch (final IOException e) {
213216
context.logError("Error writing JsonAdapter for %s %s", beanReader, e);
214217
}
215218
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import javax.lang.model.element.*;
66
import java.util.*;
7+
import java.util.stream.Collectors;
78

89
/**
910
* Read points for field injection and method injection
@@ -45,12 +46,15 @@ class TypeReader {
4546

4647
public TypeReader(
4748
TypeElement baseType,
48-
Map<String, Element> mixInFields,
49+
TypeElement mixInType,
4950
ProcessingContext context,
5051
NamingConvention namingConvention) {
5152

5253
this.baseType = baseType;
53-
this.mixInFields = mixInFields;
54+
this.mixInFields =
55+
mixInType.getEnclosedElements().stream()
56+
.filter(e -> e.getKind() == ElementKind.FIELD)
57+
.collect(Collectors.toMap(e -> e.getSimpleName().toString(), e -> e));
5458
this.context = context;
5559
this.namingConvention = namingConvention;
5660
this.hasJsonAnnotation = baseType.getAnnotation(Json.class) != null;

0 commit comments

Comments
 (0)