Skip to content

Commit 45072d6

Browse files
committed
feat: keep member declarations annotated with @keep
even if member does not exist in target type to support backwards compatibility
1 parent 45d438d commit 45072d6

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

eea-generator/src/main/java/com/vegardit/no_npe/eea_generator/EEAFile.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,18 @@ public void applyAnnotationsAndCommentsFrom(final EEAFile their, final boolean o
472472
}
473473
}
474474

475+
boolean newMembersAdded = false;
475476
for (final ClassMember theirMember : their.members) {
476477
final ClassMember ourMember = findMatchingClassMember(theirMember);
477478
if (ourMember == null) {
478-
if (addNewMembers) {
479+
// add non-existing fields/method declarations if they are annotated with @Keep
480+
// for compatibility to support older versions of a class
481+
if (addNewMembers || theirMember.annotatedSignature.comment.contains("@Keep")) {
482+
if (!newMembersAdded) {
483+
addEmptyLine();
484+
}
479485
addMember(theirMember.clone());
486+
newMembersAdded = true;
480487
}
481488
} else {
482489
ourMember.applyAnnotationsAndCommentsFrom(theirMember, overrideOnConflict);

eea-generator/src/main/java/com/vegardit/no_npe/eea_generator/EEAGenerator.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,9 @@ public static long generateEEAFiles(final Config cfg) throws IOException {
684684
// do nothing
685685
} else {
686686
assert inheritedFrom != null;
687-
if (inheritableAnnotatedSignature.value.equals(member.annotatedSignature.value)) {
687+
if (member.annotatedSignature.comment.contains("@Keep")) {
688+
// do nothing
689+
} else if (inheritableAnnotatedSignature.value.equals(member.annotatedSignature.value)) {
688690
if (setInheritedAnnotatedSignature(member, inheritableAnnotatedSignature, inheritedFrom.getName())) {
689691
recomputeInheritance.set(true);
690692
}
@@ -843,12 +845,18 @@ public static long validateEEAFiles(final Config config) throws IOException {
843845
// ensure the EEA file does not contain declarations of non-existing fields/methods
844846
parsedEEAFile.getClassMembers().forEach(parsedMember -> {
845847
if (computedEEAFile.findMatchingClassMember(parsedMember) == null) {
848+
849+
// allow non-existing fields/method declarations if they are annotated with @Keep
850+
// for compatibility to support older versions of a class
851+
if (parsedMember.annotatedSignature.comment.contains("@Keep"))
852+
return;
853+
846854
final var candidates = computedEEAFile.getClassMembers() //
847855
.filter(m -> m.name.equals(parsedMember.name)) //
848856
.map(m -> m.name + "\n" + " " + m.originalSignature) //
849857
.collect(Collectors.joining("\n"));
850858
throw new IllegalStateException("Unknown member declaration found in [" + path + "]:\n" + parsedMember + (candidates
851-
.length() > 0 ? "\nPotential candidates: \n" + candidates : ""));
859+
.isEmpty() ? "" : "\nPotential candidates: \n" + candidates));
852860
}
853861
});
854862
});

0 commit comments

Comments
 (0)