Skip to content

Commit

Permalink
allow non-public subscribed methods in @EventBusSubscriber classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts committed Dec 11, 2024
1 parent a97a445 commit ecf2d45
Showing 1 changed file with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.tree.AnnotationNode;
Expand Down Expand Up @@ -54,16 +55,17 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)

final ClassReader cr = new ClassReader(basicClass);
final ClassNode cn = new ClassNode();
cr.accept(cn, ClassReader.SKIP_FRAMES | ClassReader.SKIP_DEBUG | ClassReader.SKIP_CODE);
cr.accept(cn, 0);

boolean changed = false;
// Processing all of this from the ASMDataTable is way too slow
for (MethodNode mn : cn.methods) {
Object2ObjectMap<String, AnnotationNode> usableAnnotations = getUsableAnnotations(mn.visibleAnnotations);
if (usableAnnotations.isEmpty()) continue;

if (!matchesSide(usableAnnotations.get(SIDEONLY_DESC))) {
if (DEBUG_EVENT_BUS) {
LOGGER.info("Skipping method {} due to side mismatch", transformedName);
LOGGER.info("Skipping method {} due to side mismatch", getMethodKey(transformedName, mn));
}
continue;
}
Expand All @@ -72,21 +74,19 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)
boolean condition = usableAnnotations.containsKey(CONDITION_DESC);
if ((mn.access & Opcodes.ACC_STATIC) == 0) {
if (!condition && subscribe != null) {
EventBusUtil.getInvalidMethods().add(
"Encountered unexpected non-static method: " + transformedName + " " + mn.name + mn.desc);
EventBusUtil.getInvalidMethods()
.add("Encountered unexpected non-static method: " + getMethodKey(transformedName, mn));
}
continue;
}

if (condition) {
if (mn.desc.equals("()Z")) {
EventBusUtil.getConditionsToCheck().put(transformedName, mn.name + mn.desc);
changed |= changeAccess(transformedName, mn);
} else {
EventBusUtil.getInvalidMethods().add(
"Invalid condition method: " + transformedName
+ " "
+ mn.name
+ mn.desc
"Invalid condition method: " + getMethodKey(transformedName, mn)
+ ". Condition method must have no parameters and return a boolean.");
}
continue;
Expand All @@ -96,11 +96,14 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)
if (DEBUG_EVENT_BUS) {
LOGGER.info(
"Skipping method {} with annotations {}. No @SubscribeEvent found.",
transformedName,
getMethodKey(transformedName, mn),
usableAnnotations.keySet());
}
continue;
}

changed |= changeAccess(transformedName, mn);

Object[] subscribeInfo = getSubscribeInfo(subscribe);
MethodInfo methodInfo = new MethodInfo(
transformedName,
Expand All @@ -127,6 +130,12 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)
}
}

if (changed) {
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return cw.toByteArray();
}

return basicClass;
}

Expand Down Expand Up @@ -171,4 +180,22 @@ private static Object[] getSubscribeInfo(AnnotationNode annotation) {
}
return info;
}

private static boolean changeAccess(String className, MethodNode mn) {
if ((mn.access & Opcodes.ACC_PUBLIC) == 0) {
if (DEBUG_EVENT_BUS) {
LOGGER.info(
"Encountered condition or subscriber method with non-public access {}. Making it public.",
getMethodKey(className, mn));
}
mn.access = (mn.access | Opcodes.ACC_PUBLIC) & ~(Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED);
return true;
}

return false;
}

private static String getMethodKey(String className, MethodNode mn) {
return className + " " + mn.name + mn.desc;
}
}

0 comments on commit ecf2d45

Please sign in to comment.