001 // Copyright 2011 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.internal.plastic;
016
017 import org.apache.tapestry5.plastic.MethodAdvice;
018 import org.apache.tapestry5.plastic.MethodDescription;
019
020 import java.lang.reflect.Method;
021
022 /**
023 * Bundles together the fixed (same for all instances) information needed by a {@link MethodInvocationBundle}.
024 */
025 public class MethodInvocationBundle
026 {
027 public final String className;
028
029 public final MethodDescription methodDescription;
030
031 public final MethodAdvice[] advice;
032
033 private volatile Method method;
034
035 public MethodInvocationBundle(String className, MethodDescription methodDescription, MethodAdvice[] advice)
036 {
037 this.className = className;
038 this.methodDescription = methodDescription;
039 this.advice = advice;
040 }
041
042 public Method getMethod(Object instance)
043 {
044 if (method == null)
045 method = findMethod(instance.getClass().getClassLoader());
046
047 return method;
048 }
049
050 @SuppressWarnings("unchecked")
051 private Method findMethod(ClassLoader loader)
052 {
053 try
054 {
055 Class[] types = new Class[methodDescription.argumentTypes.length];
056
057 for (int i = 0; i < types.length; i++)
058 {
059 types[i] = PlasticInternalUtils.toClass(loader, methodDescription.argumentTypes[i]);
060 }
061
062 Class clazz = PlasticInternalUtils.toClass(loader, className);
063
064 return clazz.getDeclaredMethod(methodDescription.methodName, types);
065 } catch (Exception ex)
066 {
067 throw new RuntimeException(String.format("Unable to locate Method %s: %s", methodDescription,
068 PlasticInternalUtils.toMessage(ex)), ex);
069 }
070 }
071
072 }