Skip to content

Commit 4dd150f

Browse files
authored
Fix validator by walking elements ourselves (#93)
1 parent 71546dc commit 4dd150f

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

eventbus-test/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies {
2323
testImplementation libs.junit.api
2424
testImplementation libs.jspecify.annotations
2525
testImplementation libs.compile.testing
26+
testImplementation libs.auto.value
2627
testImplementation libs.jetbrains.annotations
2728
testRuntimeOnly libs.bundles.junit.runtime
2829
}

eventbus-validator/src/main/java/net/minecraftforge/eventbus/validator/EventBusValidator.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
*/
55
package net.minecraftforge.eventbus.validator;
66

7-
import com.sun.source.tree.VariableTree;
8-
import com.sun.source.util.TreePathScanner;
9-
import com.sun.source.util.Trees;
10-
117
import javax.annotation.processing.ProcessingEnvironment;
128
import javax.annotation.processing.RoundEnvironment;
139
import javax.lang.model.element.Element;
@@ -17,40 +13,33 @@
1713
import javax.lang.model.type.DeclaredType;
1814
import javax.lang.model.type.TypeMirror;
1915
import javax.lang.model.util.Types;
16+
2017
import java.util.Set;
2118

2219
public final class EventBusValidator extends AbstractValidator {
23-
private Trees trees;
2420
private Types types;
2521

2622
@Override
2723
public void init(ProcessingEnvironment processingEnv) {
2824
super.init(processingEnv);
29-
trees = Trees.instance(processingEnv);
3025
types = processingEnv.getTypeUtils();
3126
}
3227

3328
@Override
3429
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
3530
for (var root : roundEnv.getRootElements()) {
36-
var path = trees.getPath(root);
37-
if (path != null) new BusFieldScanner().scan(path, null);
31+
process(root);
3832
}
3933

4034
return false; // allow other processors to run
4135
}
4236

43-
private final class BusFieldScanner extends TreePathScanner<Void, Void> {
44-
@Override
45-
public Void visitVariable(VariableTree varTree, Void ignored) {
46-
Element element = trees.getElement(getCurrentPath());
47-
if (element.getKind() != ElementKind.FIELD)
48-
return super.visitVariable(varTree, ignored); // only interested in fields
49-
37+
private void process(Element element) {
38+
if (element.getKind() == ElementKind.FIELD) {
5039
TypeMirror erasedFieldType = types.erasure(element.asType());
5140
var isStandardEventBus = types.isSameType(erasedFieldType, BusTypes.eventBus);
5241
if (!(isStandardEventBus || types.isSameType(erasedFieldType, BusTypes.cancellableEventBus)))
53-
return super.visitVariable(varTree, ignored); // only interested in (Cancellable)EventBus fields
42+
return; // only interested in (Cancellable)EventBus fields
5443

5544
// Show a warning if the bus field is not final
5645
if (!element.getModifiers().contains(Modifier.FINAL)) {
@@ -73,8 +62,11 @@ public Void visitVariable(VariableTree varTree, Void ignored) {
7362
);
7463
}
7564
}
65+
}
66+
7667

77-
return super.visitVariable(varTree, ignored);
68+
for (var child : element.getEnclosedElements()) {
69+
process(child);
7870
}
7971
}
8072
}

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dependencyResolutionManagement {
4848
library 'jmh-annotationProcessor', 'org.openjdk.jmh', 'jmh-generator-annprocess' versionRef 'jmh'
4949

5050
library 'compile-testing', 'com.google.testing.compile', 'compile-testing' version '0.21.0'
51+
library 'auto-value', 'com.google.auto.value', 'auto-value' version '1.11.0' // Need to bump this because compile-testing uses an old non-module compatible version
5152
library 'jetbrains-annotations', 'org.jetbrains', 'annotations' version '26.0.2'
5253
}
5354
//@formatter:on

0 commit comments

Comments
 (0)