Commit 61965517773a8e297947fddac908c0308258c83a
1 parent
b4c52de0
support handlers with return types in HandlerList, return null or zero
Showing
1 changed file
with
28 additions
and
7 deletions
java/common/com/mumfrey/liteloader/core/event/HandlerList.java
... | ... | @@ -754,15 +754,15 @@ public class HandlerList<T> extends LinkedList<T> implements FastIterableDeque<T |
754 | 754 | Type returnType = Type.getReturnType(method.desc); |
755 | 755 | Type[] args = Type.getArgumentTypes(method.desc); |
756 | 756 | |
757 | - if (returnType.equals(Type.VOID_TYPE)) | |
757 | + if (returnType.equals(Type.BOOLEAN_TYPE)) | |
758 | 758 | { |
759 | 759 | method.access = Opcodes.ACC_PUBLIC; |
760 | - this.populateVoidInvokationChain(classNode, method, args); | |
760 | + this.populateBooleanInvokationChain(classNode, method, args); | |
761 | 761 | } |
762 | - else if (returnType.equals(Type.BOOLEAN_TYPE)) | |
762 | + else | |
763 | 763 | { |
764 | 764 | method.access = Opcodes.ACC_PUBLIC; |
765 | - this.populateBooleanInvokationChain(classNode, method, args); | |
765 | + this.populateVoidInvokationChain(classNode, method, args, returnType); | |
766 | 766 | } |
767 | 767 | } |
768 | 768 | |
... | ... | @@ -771,14 +771,35 @@ public class HandlerList<T> extends LinkedList<T> implements FastIterableDeque<T |
771 | 771 | * @param method |
772 | 772 | * @param args |
773 | 773 | */ |
774 | - private void populateVoidInvokationChain(ClassNode classNode, MethodNode method, Type[] args) | |
774 | + private void populateVoidInvokationChain(ClassNode classNode, MethodNode method, Type[] args, Type returnType) | |
775 | 775 | { |
776 | + int returnSize = returnType.getSize(); | |
776 | 777 | for (int handlerIndex = 0; handlerIndex < this.size; handlerIndex++) |
777 | 778 | { |
778 | 779 | this.invokeHandler(handlerIndex, classNode, method, args); |
779 | - } | |
780 | + if (returnSize > 0) | |
781 | + { | |
782 | + method.instructions.add(new InsnNode(returnSize == 1 ? Opcodes.POP : Opcodes.POP2)); | |
783 | + } | |
784 | + } | |
785 | + | |
786 | + if (returnSize > 0) | |
787 | + { | |
788 | + if (returnType.getSort() == Type.OBJECT) | |
789 | + { | |
790 | + method.instructions.add(new InsnNode(Opcodes.ACONST_NULL)); | |
791 | + } | |
792 | + else if (returnSize == 1) | |
793 | + { | |
794 | + method.instructions.add(new InsnNode(Opcodes.ICONST_0)); | |
795 | + } | |
796 | + else if (returnSize == 2) | |
797 | + { | |
798 | + method.instructions.add(new InsnNode(Opcodes.DCONST_0)); | |
799 | + } | |
800 | + } | |
780 | 801 | |
781 | - method.instructions.add(new InsnNode(Opcodes.RETURN)); | |
802 | + method.instructions.add(new InsnNode(returnType.getOpcode(Opcodes.IRETURN))); | |
782 | 803 | |
783 | 804 | method.maxLocals = args.length + 1; |
784 | 805 | method.maxStack = args.length + 1; | ... | ... |