From 435e516d51595cfdf3a2c6dd9a55954bafda4601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=94=E6=B0=91=E5=B0=8F=E9=95=87?= <262610965@qq.com> Date: Tue, 22 Oct 2024 15:21:43 +0800 Subject: [PATCH] :whale: Simplify the implementation class of ActionParserListener related to ProtoDataCodec. and #386 --- .../action/skeleton/core/ActionCommand.java | 14 ---- .../skeleton/core/ActionCommandParser.java | 33 ++++---- ...er.java => ActionParserListenerAbout.java} | 78 ++++++++++++------- .../skeleton/core/BarSkeletonBuilder.java | 3 + .../ProtobufCheckActionParserListener.java | 70 ----------------- 5 files changed, 72 insertions(+), 126 deletions(-) rename common/common-core/src/main/java/com/iohao/game/action/skeleton/core/{action/parser/ProtobufActionParserListener.java => ActionParserListenerAbout.java} (71%) delete mode 100644 common/common-core/src/main/java/com/iohao/game/action/skeleton/core/action/parser/ProtobufCheckActionParserListener.java diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommand.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommand.java index 7bd94ffa0..79c3b1364 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommand.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommand.java @@ -153,20 +153,6 @@ public T getAnnotation(Class annotationClass) { return actionMethod.getAnnotation(annotationClass); } - /** - * get class ConstructorAccess - * - * @return ConstructorAccess - * @since 21.19 - */ - public ConstructorAccess getActionControllerConstructorAccess() { - if (Objects.isNull(this.actionControllerConstructorAccess)) { - this.actionControllerConstructorAccess = ConstructorAccess.get(this.actionControllerClazz); - } - - return actionControllerConstructorAccess; - } - /** * {@link ActionCommand} 命令的构建器 *

diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommandParser.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommandParser.java index ff6077c2e..d4a08c448 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommandParser.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionCommandParser.java @@ -25,8 +25,6 @@ import com.iohao.game.action.skeleton.annotation.ActionMethod; import com.iohao.game.action.skeleton.core.action.parser.ActionParserContext; import com.iohao.game.action.skeleton.core.action.parser.ActionParserListener; -import com.iohao.game.action.skeleton.core.action.parser.ProtobufActionParserListener; -import com.iohao.game.action.skeleton.core.action.parser.ProtobufCheckActionParserListener; import com.iohao.game.action.skeleton.core.codec.ProtoDataCodec; import com.iohao.game.action.skeleton.core.doc.ActionCommandDoc; import com.iohao.game.action.skeleton.core.doc.ActionDoc; @@ -38,12 +36,12 @@ import lombok.Setter; import lombok.experimental.Accessors; import lombok.experimental.FieldDefaults; +import org.jctools.maps.NonBlockingHashMap; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Parameter; import java.util.*; -import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Stream; /** @@ -312,7 +310,7 @@ private void executeActionListeners() { // action command, actionMethod actionCommandRegion.getSubActionCommandMap().forEach((subCmd, command) -> { // action 构建时的上下文 - ActionParserContext context = new ActionParserContext() + var context = new ActionParserContext() .setBarSkeleton(barSkeleton) .setActionCommand(command); @@ -327,29 +325,34 @@ private void executeActionListeners() { @FieldDefaults(level = AccessLevel.PRIVATE) final class ActionParserListeners { - final List listeners = new CopyOnWriteArrayList<>(); + final Map, ActionParserListener> map = new NonBlockingHashMap<>(); void addActionParserListener(ActionParserListener listener) { - this.listeners.add(listener); + Objects.requireNonNull(listener); + this.map.putIfAbsent(listener.getClass(), listener); } void onActionCommand(ActionParserContext context) { - this.listeners.forEach(listener -> listener.onActionCommand(context)); + this.map.forEach((clazz, listener) -> listener.onActionCommand(context)); } void onAfter(BarSkeleton barSkeleton) { - this.listeners.forEach(listener -> listener.onAfter(barSkeleton)); + this.map.forEach((clazz, listener) -> listener.onAfter(barSkeleton)); } - public boolean isEmpty() { - return this.listeners.isEmpty(); - } - - public ActionParserListeners() { + ActionParserListeners() { // 监听器 - 预先创建协议代理类 if (DataCodecKit.getDataCodec() instanceof ProtoDataCodec) { - this.addActionParserListener(ProtobufActionParserListener.me()); - this.addActionParserListener(ProtobufCheckActionParserListener.me()); + this.addActionParserListener(new ProtobufActionParserListener()); + this.addActionParserListener(new ProtobufCheckActionParserListener()); } + + // prepared actionControllerConstructorAccess + this.addActionParserListener(context -> { + var actionCommand = context.getActionCommand(); + if (!actionCommand.isDeliveryContainer()) { + actionCommand.actionControllerConstructorAccess = ConstructorAccess.get(actionCommand.actionControllerClazz); + } + }); } } \ No newline at end of file diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/action/parser/ProtobufActionParserListener.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionParserListenerAbout.java similarity index 71% rename from common/common-core/src/main/java/com/iohao/game/action/skeleton/core/action/parser/ProtobufActionParserListener.java rename to common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionParserListenerAbout.java index 94211a2d1..4e4fa6ebb 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/action/parser/ProtobufActionParserListener.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/ActionParserListenerAbout.java @@ -16,15 +16,16 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -package com.iohao.game.action.skeleton.core.action.parser; +package com.iohao.game.action.skeleton.core; import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass; -import com.iohao.game.action.skeleton.core.ActionCommand; -import com.iohao.game.action.skeleton.core.BarSkeleton; +import com.iohao.game.action.skeleton.core.action.parser.ActionParserContext; +import com.iohao.game.action.skeleton.core.action.parser.ActionParserListener; import com.iohao.game.action.skeleton.protocol.wrapper.*; import com.iohao.game.common.kit.ProtoKit; import lombok.AccessLevel; import lombok.experimental.FieldDefaults; +import lombok.extern.slf4j.Slf4j; import org.jctools.maps.NonBlockingHashSet; import java.util.Objects; @@ -33,21 +34,38 @@ import java.util.function.Predicate; /** - * 预先生成 proto 协议代理类 + * Prepared action proto * * @author 渔民小镇 * @date 2024-05-01 * @since 21.7 */ @FieldDefaults(level = AccessLevel.PRIVATE) -public final class ProtobufActionParserListener implements ActionParserListener { - final Set> protoSet = new NonBlockingHashSet<>(); +final class ProtobufActionParserListener implements ActionParserListener { + static final Set> protoSet = new NonBlockingHashSet<>(); + + static { + // create a protobuf proxy class + ProtoKit.create(ByteValueList.class); + + ProtoKit.create(IntValue.class); + ProtoKit.create(IntValueList.class); + + ProtoKit.create(BoolValue.class); + ProtoKit.create(BoolValueList.class); + + ProtoKit.create(LongValue.class); + ProtoKit.create(LongValueList.class); + + ProtoKit.create(StringValue.class); + ProtoKit.create(StringValueList.class); + } @Override public void onActionCommand(ActionParserContext context) { // 添加了 ProtobufClass 注解的类 Predicate> protobufClassPredicate = c -> Objects.nonNull(c.getAnnotation(ProtobufClass.class)); - collect(context, protobufClassPredicate, this.protoSet); + collect(context, protobufClassPredicate, protoSet); } static void collect(ActionParserContext context, Predicate> protobufClassPredicate, Set> protoSet) { @@ -80,32 +98,38 @@ static void collect(ActionParserContext context, Predicate> protobufCla @Override public void onAfter(BarSkeleton barSkeleton) { - this.protoSet.forEach(ProtoKit::create); + protoSet.forEach(ProtoKit::create); } +} - private ProtobufActionParserListener() { - // create a protobuf proxy class - ProtoKit.create(ByteValueList.class); - ProtoKit.create(IntValue.class); - ProtoKit.create(IntValueList.class); - - ProtoKit.create(BoolValue.class); - ProtoKit.create(BoolValueList.class); - - ProtoKit.create(LongValue.class); - ProtoKit.create(LongValueList.class); +/** + * proto 协议类型添检测 + * + * @author 渔民小镇 + * @date 2024-05-02 + * @since 21.7 + */ +@Slf4j +final class ProtobufCheckActionParserListener implements ActionParserListener { + static final Set> protoSet = new NonBlockingHashSet<>(); - ProtoKit.create(StringValue.class); - ProtoKit.create(StringValueList.class); + @Override + public void onActionCommand(ActionParserContext context) { + // 添加了 ProtobufClass 注解的类 + Predicate> protobufClassPredicate = c -> c.getAnnotation(ProtobufClass.class) == null; + ProtobufActionParserListener.collect(context, protobufClassPredicate, protoSet); } - public static ProtobufActionParserListener me() { - return Holder.ME; - } + @Override + public void onAfter(BarSkeleton barSkeleton) { + if (protoSet.isEmpty()) { + return; + } - /** 通过 JVM 的类加载机制, 保证只加载一次 (singleton) */ - private static class Holder { - static final ProtobufActionParserListener ME = new ProtobufActionParserListener(); + log.error("======== 注意,协议类没有添加 ProtobufClass 注解 ========"); + for (Class protoClass : protoSet) { + log.error(protoClass.toString()); + } } } \ No newline at end of file diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/BarSkeletonBuilder.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/BarSkeletonBuilder.java index 90953457b..48ad298b3 100644 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/BarSkeletonBuilder.java +++ b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/BarSkeletonBuilder.java @@ -26,6 +26,7 @@ import com.iohao.game.action.skeleton.core.runner.Runner; import com.iohao.game.action.skeleton.core.runner.Runners; import com.iohao.game.common.kit.concurrent.executor.*; +import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; import lombok.experimental.Accessors; @@ -55,8 +56,10 @@ public final class BarSkeletonBuilder { /** action class */ final List> actionControllerClazzList = new LinkedList<>(); /** 错误码 */ + @Deprecated final List msgExceptionInfoList = new ArrayList<>(); /** action 构建时的钩子方法 */ + @Setter(AccessLevel.PRIVATE) ActionParserListeners actionParserListeners = new ActionParserListeners(); /** action工厂 */ ActionFactoryBean actionFactoryBean = new DefaultActionFactoryBean<>(); diff --git a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/action/parser/ProtobufCheckActionParserListener.java b/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/action/parser/ProtobufCheckActionParserListener.java deleted file mode 100644 index f3ba483fe..000000000 --- a/common/common-core/src/main/java/com/iohao/game/action/skeleton/core/action/parser/ProtobufCheckActionParserListener.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * ioGame - * Copyright (C) 2021 - present 渔民小镇 (262610965@qq.com、luoyizhu@gmail.com) . All Rights Reserved. - * # iohao.com . 渔民小镇 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package com.iohao.game.action.skeleton.core.action.parser; - -import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass; -import com.iohao.game.action.skeleton.core.BarSkeleton; -import lombok.extern.slf4j.Slf4j; -import org.jctools.maps.NonBlockingHashSet; - -import java.util.Set; -import java.util.function.Predicate; - -/** - * proto 协议类型添检测 - * - * @author 渔民小镇 - * @date 2024-05-02 - * @since 21.7 - */ -@Slf4j -public final class ProtobufCheckActionParserListener implements ActionParserListener { - final Set> protoSet = new NonBlockingHashSet<>(); - - @Override - public void onActionCommand(ActionParserContext context) { - // 添加了 ProtobufClass 注解的类 - Predicate> protobufClassPredicate = c -> c.getAnnotation(ProtobufClass.class) == null; - ProtobufActionParserListener.collect(context, protobufClassPredicate, protoSet); - } - - @Override - public void onAfter(BarSkeleton barSkeleton) { - if (this.protoSet.isEmpty()) { - return; - } - - log.error("======== 注意,协议类没有添加 ProtobufClass 注解 ========"); - for (Class protoClass : this.protoSet) { - log.error(protoClass.toString()); - } - } - - private ProtobufCheckActionParserListener() { - } - - public static ProtobufCheckActionParserListener me() { - return Holder.ME; - } - - /** 通过 JVM 的类加载机制, 保证只加载一次 (singleton) */ - private static class Holder { - static final ProtobufCheckActionParserListener ME = new ProtobufCheckActionParserListener(); - } -} \ No newline at end of file