01 /*
02 * Copyright 2010-2013 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;
17
18 import griffon.core.GriffonApplication;
19 import griffon.core.GriffonControllerClass;
20 import griffon.util.GriffonClassUtils;
21 import groovy.lang.Closure;
22 import org.codehaus.groovy.ast.ClassHelper;
23
24 import java.lang.reflect.Method;
25 import java.util.LinkedHashSet;
26 import java.util.Set;
27
28 /**
29 * @author Andres Almiray
30 * @since 0.9.1
31 */
32 public class DefaultGriffonControllerClass extends DefaultGriffonClass implements GriffonControllerClass {
33 protected final Set<String> actionsCache = new LinkedHashSet<String>();
34
35 public DefaultGriffonControllerClass(GriffonApplication app, Class<?> clazz) {
36 super(app, clazz, TYPE, TRAILING);
37 }
38
39 public void resetCaches() {
40 super.resetCaches();
41 actionsCache.clear();
42 }
43
44 public String[] getActionNames() {
45 if (actionsCache.isEmpty()) {
46 for (String propertyName : getPropertiesWithFields()) {
47 if (!STANDARD_PROPERTIES.contains(propertyName) &&
48 !actionsCache.contains(propertyName) &&
49 !GriffonClassUtils.isEventHandler(propertyName) &&
50 getPropertyValue(propertyName, Closure.class) != null) {
51 actionsCache.add(propertyName);
52 }
53 }
54 for (Method method : getClazz().getMethods()) {
55 String methodName = method.getName();
56 if (!actionsCache.contains(methodName) &&
57 GriffonClassUtils.isPlainMethod(method) &&
58 !GriffonClassUtils.isEventHandler(methodName) &&
59 hasVoidOrDefAsReturnType(method)) {
60 actionsCache.add(methodName);
61 }
62 }
63 }
64
65 return actionsCache.toArray(new String[actionsCache.size()]);
66 }
67
68 private boolean hasVoidOrDefAsReturnType(Method method) {
69 Class<?> returnType = method.getReturnType();
70 return returnType == ClassHelper.DYNAMIC_TYPE.getTypeClass() ||
71 returnType == ClassHelper.VOID_TYPE.getTypeClass();
72 }
73 }
|