RunnableWithArgsMetaMethod.java
01 /*
02  * Copyright 2009-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 
17 package org.codehaus.griffon.runtime.util;
18 
19 import griffon.util.RunnableWithArgs;
20 import groovy.lang.MetaMethod;
21 import org.codehaus.groovy.reflection.CachedClass;
22 import org.codehaus.groovy.reflection.ReflectionCache;
23 
24 import java.lang.reflect.Modifier;
25 
26 /**
27  @author Andres Almiray
28  @since 1.1.0
29  */
30 public class RunnableWithArgsMetaMethod extends MetaMethod {
31     private final RunnableWithArgs runnable;
32     private final String name;
33     private final Class declaringClass;
34 
35     public RunnableWithArgsMetaMethod(String name, Class declaringClass, RunnableWithArgs runnable, Class[] parameterTypes) {
36         super(parameterTypes);
37         this.runnable = runnable;
38         this.name = name;
39         this.declaringClass = declaringClass;
40     }
41 
42     public int getModifiers() {
43         return Modifier.PUBLIC;
44     }
45 
46     public String getName() {
47         return name;
48     }
49 
50     public Class getReturnType() {
51         return Void.TYPE;
52     }
53 
54     public CachedClass getDeclaringClass() {
55         return ReflectionCache.getCachedClass(declaringClass);
56     }
57 
58     public Object invoke(Object object, Object[] arguments) {
59         arguments = coerceArgumentsToClasses(arguments);
60         runnable.setArgs(arguments);
61         runnable.run();
62         return null;
63     }
64 }