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