package org.yzh.client.netty; import io.github.yezhihao.netmc.core.annotation.Endpoint; import io.github.yezhihao.netmc.core.annotation.Mapping; import io.github.yezhihao.netmc.util.ClassUtils; import lombok.SneakyThrows; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import java.util.Map; public class HandlerMapping { private final Map handlerMap = new HashMap<>(55); public HandlerMapping(String... packageNames) { for (String packageName : packageNames) { initial(packageName); } } private void initial(String packageName) { List handlerClassList = ClassUtils.getClassList(packageName, Endpoint.class); for (Class handlerClass : handlerClassList) { Method[] methods = handlerClass.getDeclaredMethods(); for (Method method : methods) { if (method.isAnnotationPresent(Mapping.class)) { Mapping annotation = method.getAnnotation(Mapping.class); String desc = annotation.desc(); int[] types = annotation.types(); Handler value = new Handler(newInstance(handlerClass), method, desc); for (int type : types) { handlerMap.put(type, value); } } } } } @SneakyThrows private Object newInstance(Class handlerClass) { return handlerClass.newInstance(); } public Handler getHandler(int messageId) { return handlerMap.get(messageId); } }