Skip to content

Commit 83042b4

Browse files
authored
fix mixins (#253)
1 parent 5b61e51 commit 83042b4

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,9 @@ final class AdapterName {
99
final String fullName;
1010

1111
AdapterName(TypeElement origin) {
12-
String originName = origin.getQualifiedName().toString();
13-
String name = origin.getSimpleName().toString();
14-
String originPackage = Util.packageOf(originName);
15-
if (origin.getNestingKind().isNested()) {
16-
String parent = Util.shortName(originPackage);
17-
originPackage = Util.packageOf(originPackage);
18-
shortName = parent + "$" + name;
19-
} else {
20-
shortName = name;
21-
}
12+
String originPackage = APContext.elements().getPackageOf(origin).toString();
13+
var name = shortName(origin);
14+
shortName = name.substring(0, name.length() - 1);
2215
if ("".equals(originPackage)) {
2316
this.adapterPackage = "jsonb";
2417
} else {
@@ -27,6 +20,14 @@ final class AdapterName {
2720
this.fullName = adapterPackage + "." + shortName + "JsonAdapter";
2821
}
2922

23+
private String shortName(TypeElement origin) {
24+
var sb = new StringBuilder();
25+
if (origin.getNestingKind().isNested()) {
26+
sb.append(shortName((TypeElement) origin.getEnclosingElement()));
27+
}
28+
return sb.append(Util.shortName(origin.getSimpleName().toString())).append("$").toString();
29+
}
30+
3031
String shortName() {
3132
return shortName;
3233
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private void addImported(ImportPrism importPrism) {
239239
for (final TypeMirror importType : importPrism.value()) {
240240
// if imported by mixin annotation skip
241241
if (mixInImports.contains(importType.toString())) {
242-
return;
242+
continue;
243243
}
244244
writeAdapterForImportedType(asTypeElement(importType), implementationType(importPrism));
245245
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.lang.model.element.*;
44
import javax.lang.model.type.TypeMirror;
55
import javax.lang.model.util.ElementFilter;
6+
67
import java.util.*;
78
import java.util.function.Supplier;
89
import java.util.stream.Collectors;
@@ -170,7 +171,23 @@ void read(TypeElement type) {
170171

171172
private void readField(Element element, List<FieldReader> localFields) {
172173
final Element mixInField = mixInFields.get(element.getSimpleName().toString());
173-
if (mixInField != null && mixInField.asType().equals(element.asType())) {
174+
if (mixInField != null && APContext.types().isSameType(mixInField.asType(), element.asType())) {
175+
176+
var mixinModifiers = new HashSet<>(mixInField.getModifiers());
177+
var modifiers = new HashSet<>(mixInField.getModifiers());
178+
179+
Arrays.stream(Modifier.values())
180+
.filter(m -> m != Modifier.PRIVATE || m != Modifier.PROTECTED || m != Modifier.PUBLIC)
181+
.forEach(
182+
m -> {
183+
modifiers.remove(m);
184+
mixinModifiers.remove(m);
185+
});
186+
187+
if (!modifiers.equals(mixinModifiers)) {
188+
APContext.logError(mixInField, "mixIn fields must have the same modifiers as the target class");
189+
}
190+
174191
element = mixInField;
175192
}
176193
if (element.asType().toString().contains("java.util.Optional")) {

0 commit comments

Comments
 (0)