DefaultGriffonControllerClass.java
01 /*
02  * SPDX-License-Identifier: Apache-2.0
03  *
04  * Copyright 2008-2017 the original author or authors.
05  *
06  * Licensed under the Apache License, Version 2.0 (the "License");
07  * you may not use this file except in compliance with the License.
08  * You may obtain a copy of the License at
09  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.codehaus.griffon.runtime.core.artifact;
19 
20 import griffon.core.GriffonApplication;
21 import griffon.core.artifact.GriffonControllerClass;
22 import griffon.core.controller.ControllerAction;
23 
24 import javax.annotation.Nonnull;
25 import java.lang.reflect.Method;
26 import java.util.Set;
27 import java.util.TreeSet;
28 
29 import static griffon.util.AnnotationUtils.isAnnotatedWith;
30 import static griffon.util.GriffonClassUtils.isEventHandler;
31 import static griffon.util.GriffonClassUtils.isPlainMethod;
32 
33 /**
34  @author Andres Almiray
35  @since 2.0.0
36  */
37 public class DefaultGriffonControllerClass extends DefaultGriffonClass implements GriffonControllerClass {
38     protected final Set<String> actionsCache = new TreeSet<>();
39 
40     public DefaultGriffonControllerClass(@Nonnull GriffonApplication application, @Nonnull Class<?> clazz) {
41         super(application, clazz, TYPE, TRAILING);
42     }
43 
44     @Override
45     public void resetCaches() {
46         super.resetCaches();
47         actionsCache.clear();
48     }
49 
50     @Nonnull
51     public String[] getActionNames() {
52         if (actionsCache.isEmpty()) {
53             for (Method method : getClazz().getMethods()) {
54                 String methodName = method.getName();
55                 if (actionsCache.contains(methodName)) {
56                     continue;
57                 }
58 
59                 if (isPlainMethod(method&&
60                     !isEventHandler(methodName&&
61                     (isAnnotatedWith(method, ControllerAction.class, true|| method.getReturnType() == Void.TYPE)) {
62                     actionsCache.add(methodName);
63                 }
64             }
65         }
66 
67         return actionsCache.toArray(new String[actionsCache.size()]);
68     }
69 }