1515import com .ericlam .mc .eldgui .view .BukkitRedirectView ;
1616import com .ericlam .mc .eldgui .view .BukkitView ;
1717import com .ericlam .mc .eldgui .view .LoadingView ;
18- import com .ericlam .mc .eldgui .view .View ;
1918import com .google .inject .Injector ;
2019import com .google .inject .TypeLiteral ;
2120import org .bukkit .Bukkit ;
3938
4039public final class ELDGUI {
4140
41+ private static final Map <Class <?>, Method []> declaredMethodMap = new ConcurrentHashMap <>();
4242 private static final Logger LOGGER = LoggerFactory .getLogger (ELDGUI .class );
4343
4444
@@ -49,7 +49,6 @@ public final class ELDGUI {
4949 private final LifeCycleManager lifeCycleManager ;
5050
5151 private final Class <?> controllerCls ;
52- private final Object controller ;
5352 private final Injector injector ;
5453 private final UISession session ;
5554 private final Player owner ;
@@ -61,6 +60,7 @@ public final class ELDGUI {
6160 private final Consumer <Player > onDestroy ;
6261 private final ViewJumper goTo ;
6362 private final BukkitView <? extends LoadingView , Void > loadingView ;
63+ private final Method [] declaredMethods ;
6464
6565
6666 private ELDGView <?> currentView ;
@@ -79,7 +79,6 @@ public ELDGUI(
7979 ) {
8080
8181 this .session = session ;
82- this .controller = controller ;
8382 this .injector = injector ;
8483 this .owner = owner ;
8584 this .onDestroy = onDestroy ;
@@ -91,16 +90,22 @@ public ELDGUI(
9190 methodParseManager = managerFactory .buildParseManager (this ::initMethodParseManager );
9291 returnTypeManager = managerFactory .buildReturnTypeManager (this ::initReturnTypeManager );
9392 this .lifeCycleManager = new LifeCycleManager (controller , methodParseManager );
93+ this .controllerCls = controller .getClass ();
94+
95+ if (declaredMethodMap .containsKey (controllerCls )) {
96+ this .declaredMethods = declaredMethodMap .get (controllerCls );
97+ } else {
98+ this .declaredMethods = controllerCls .getDeclaredMethods ();
99+ declaredMethodMap .put (controllerCls , declaredMethods );
100+ }
94101
95102 var customQualifier = eldgmvcInstallation .getQualifierMap ();
96- this .eventHandlerMap .put (InventoryClickEvent .class , new ELDGClickEventHandler (controller , methodParseManager , returnTypeManager , customQualifier ));
97- this .eventHandlerMap .put (InventoryDragEvent .class , new ELDGDragEventHandler (controller , methodParseManager , returnTypeManager , customQualifier ));
103+ this .eventHandlerMap .put (InventoryClickEvent .class , new ELDGClickEventHandler (controller , methodParseManager , returnTypeManager , customQualifier , declaredMethods ));
104+ this .eventHandlerMap .put (InventoryDragEvent .class , new ELDGDragEventHandler (controller , methodParseManager , returnTypeManager , customQualifier , declaredMethods ));
98105 this .itemGetterMap .put (InventoryClickEvent .class .getSimpleName (), e -> ((InventoryClickEvent ) e ).getCurrentItem ());
99106 this .itemGetterMap .put (InventoryDragEvent .class .getSimpleName (), e -> ((InventoryDragEvent ) e ).getOldCursor ());
100107
101108
102- this .controllerCls = controller .getClass ();
103-
104109 this .lifeCycleManager .onLifeCycle (PostConstruct .class );
105110
106111 Optional <Class <? extends LoadingView >> loadingViewOpt = Optional .ofNullable (this .controllerCls .getAnnotation (AsyncLoadingView .class )).map (AsyncLoadingView ::value );
@@ -140,7 +145,7 @@ private synchronized void jumpToController(BukkitRedirectView redirectView) {
140145 public void initIndexView (Object controller ) {
141146 LOGGER .debug ("initializing index view" ); // debug
142147 try {
143- Optional <Method > indexMethod = Arrays .stream (controllerCls . getDeclaredMethods () ).filter (m -> m .getName ().equalsIgnoreCase ("index" )).findAny ();
148+ Optional <Method > indexMethod = Arrays .stream (declaredMethods ).filter (m -> m .getName ().equalsIgnoreCase ("index" )).findAny ();
144149 if (indexMethod .isEmpty ())
145150 throw new IllegalStateException ("cannot find index method from " + controllerCls );
146151 Method index = indexMethod .get ();
@@ -203,7 +208,7 @@ private void initMethodParseManager(MethodParseManager parser) {
203208 FromPattern pattern = (FromPattern ) Arrays .stream (annotations ).filter (a -> a .annotationType () == FromPattern .class ).findAny ().orElseThrow (() -> new IllegalStateException ("cannot find @FromPattern in List<ItemStack> parameters" ));
204209 if (t instanceof ParameterizedType ) {
205210 var parat = (ParameterizedType ) t ;
206- if (parat .getActualTypeArguments ()[0 ] == ItemStack .class && parat .getRawType () == List .class ){
211+ if (parat .getActualTypeArguments ()[0 ] == ItemStack .class && parat .getRawType () == List .class ) {
207212 return this .currentView .getEldgContext ().getItems (pattern .value ());
208213 }
209214 }
@@ -244,8 +249,8 @@ private void initMethodParseManager(MethodParseManager parser) {
244249 MapAttribute attribute = (MapAttribute ) Arrays .stream (annotations ).filter (a -> a .annotationType () == MapAttribute .class ).findAny ().orElseThrow (() -> new IllegalStateException ("cannot find MapAttribute annotation" ));
245250 var context = this .currentView .getEldgContext ();
246251 boolean isMap = false ;
247- if (type instanceof ParameterizedType ){
248- var parat = (ParameterizedType )type ;
252+ if (type instanceof ParameterizedType ) {
253+ var parat = (ParameterizedType ) type ;
249254 isMap = parat .getRawType () == Map .class && parat .getActualTypeArguments ()[0 ] == String .class && parat .getActualTypeArguments ()[1 ] == Object .class ;
250255 }
251256
@@ -315,7 +320,12 @@ private void handleException(Exception ex) {
315320 Class <? extends ExceptionViewHandler > exceptionViewHandler = exceptionViewHandlerOpt .orElseGet (eldgmvcInstallation ::getDefaultExceptionHandler );
316321 ExceptionViewHandler viewHandlerIns = injector .getInstance (exceptionViewHandler );
317322 UIController fromController = controllerCls .getAnnotation (UIController .class );
318- Arrays .stream (exceptionViewHandler .getDeclaredMethods ())
323+ Method [] declaredMethods = Optional .ofNullable (declaredMethodMap .get (exceptionViewHandler )).orElseGet (() -> {
324+ var methods = exceptionViewHandler .getDeclaredMethods ();
325+ declaredMethodMap .put (exceptionViewHandler , methods );
326+ return methods ;
327+ });
328+ Arrays .stream (declaredMethods )
319329 .filter (m -> m .isAnnotationPresent (HandleException .class ))
320330 .filter (m -> Arrays .stream (m .getAnnotation (HandleException .class ).value ()).anyMatch (v -> {
321331 Class <?> superCls = ex .getClass ();
0 commit comments