Skip to content

Commit 096010a

Browse files
committed
Reject unknown invisible annotations
1 parent 0333a4b commit 096010a

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

build.gradle

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ def annotationsToRemoveFromShadedJar = [
379379
'Lcom/google/j2objc/annotations/Weak;',
380380
'Ljavax/annotation/meta/TypeQualifierNickname;',
381381
] as Set<String>
382+
def graphqlJavaAnnotationDescriptorPrefix = 'Lgraphql/'
383+
def shadedDependencyAnnotationDescriptorPrefixes = [
384+
'Lgraphql/com/google/',
385+
'Lgraphql/org/antlr/',
386+
]
382387

383388
/**
384389
* Expands the shaded JAR into a clean working directory and removes any
@@ -452,21 +457,26 @@ assemble.dependsOn buildNewJar
452457

453458
/**
454459
* Scans every class in the completed published JAR with ASM and fails if any
455-
* configured annotation remains in a declaration or parameter invisible
456-
* annotation attribute. It also verifies that GraphQL Java's own
457-
* {@code graphql.Contract} annotation was preserved by the selective cleanup.
460+
* configured annotation remains, or if an unknown external annotation appears,
461+
* in a declaration or parameter invisible annotation attribute. GraphQL Java
462+
* annotations are allowed, while annotations in relocated dependency namespaces
463+
* are still considered external. The task also verifies that
464+
* {@code graphql.Contract} was preserved by the selective cleanup.
458465
*/
459466
tasks.register('verifyShadedClassAnnotations') {
460467
group = 'verification'
461-
description = 'Verify selected invisible annotations are absent from the published jar'
468+
description = 'Verify only GraphQL Java invisible annotations remain in the published jar'
462469
dependsOn buildNewJar
463470

464471
def shadedJar = layout.buildDirectory.file("libs/graphql-java-${project.version}.jar")
465472
inputs.file(shadedJar)
466473
inputs.property('annotationsToRemove', annotationsToRemoveFromShadedJar.toList().sort())
474+
inputs.property('graphqlJavaAnnotationPrefix', graphqlJavaAnnotationDescriptorPrefix)
475+
inputs.property('shadedDependencyAnnotationPrefixes', shadedDependencyAnnotationDescriptorPrefixes)
467476

468477
doLast {
469-
def offendingAnnotations = []
478+
def annotationsThatShouldHaveBeenRemoved = []
479+
def unexpectedExternalAnnotations = []
470480
boolean graphqlContractFound = false
471481

472482
new java.util.jar.JarFile(shadedJar.get().asFile).withCloseable { jarFile ->
@@ -480,7 +490,13 @@ tasks.register('verifyShadedClassAnnotations') {
480490
invisibleAnnotationLists(classNode).each { annotations ->
481491
annotations.each { annotation ->
482492
if (annotationsToRemoveFromShadedJar.contains(annotation.desc)) {
483-
offendingAnnotations.add("${entry.name}: ${annotation.desc}")
493+
annotationsThatShouldHaveBeenRemoved.add("${entry.name}: ${annotation.desc}")
494+
} else {
495+
boolean isGraphQLJavaAnnotation = annotation.desc.startsWith(graphqlJavaAnnotationDescriptorPrefix) &&
496+
!shadedDependencyAnnotationDescriptorPrefixes.any { annotation.desc.startsWith(it) }
497+
if (!isGraphQLJavaAnnotation) {
498+
unexpectedExternalAnnotations.add("${entry.name}: ${annotation.desc}")
499+
}
484500
}
485501
if (annotation.desc == 'Lgraphql/Contract;') {
486502
graphqlContractFound = true
@@ -490,11 +506,18 @@ tasks.register('verifyShadedClassAnnotations') {
490506
}
491507
}
492508

493-
if (!offendingAnnotations.isEmpty()) {
494-
throw new GradleException("Found annotations that should have been removed:\n${offendingAnnotations.join('\n')}")
509+
def verificationErrors = []
510+
if (!annotationsThatShouldHaveBeenRemoved.isEmpty()) {
511+
verificationErrors.add("Found annotations that should have been removed:\n${annotationsThatShouldHaveBeenRemoved.join('\n')}")
512+
}
513+
if (!unexpectedExternalAnnotations.isEmpty()) {
514+
verificationErrors.add("Found unexpected external invisible annotations; add them to annotationsToRemoveFromShadedJar:\n${unexpectedExternalAnnotations.join('\n')}")
495515
}
496516
if (!graphqlContractFound) {
497-
throw new GradleException('graphql.Contract was removed from the shaded jar')
517+
verificationErrors.add('graphql.Contract was removed from the shaded jar')
518+
}
519+
if (!verificationErrors.isEmpty()) {
520+
throw new GradleException(verificationErrors.join('\n\n'))
498521
}
499522
}
500523
}

0 commit comments

Comments
 (0)