001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020
021 package org.apache.geronimo.j2ee.annotation;
022
023 import java.io.Serializable;
024 import java.lang.reflect.InvocationTargetException;
025 import java.lang.reflect.Method;
026
027 /**
028 * @version $Rev: 516141 $ $Date: 2007-03-08 13:45:05 -0500 (Thu, 08 Mar 2007) $
029 */
030 public class LifecycleMethod implements Serializable {
031
032 private final String targetClassName;
033 private final String methodName;
034
035
036 public LifecycleMethod(String targetClassName, String methodName) {
037 this.targetClassName = targetClassName;
038 this.methodName = methodName;
039 }
040
041 public String getTargetClassName() {
042 return targetClassName;
043 }
044
045 public String getMethodName() {
046 return methodName;
047 }
048
049 public void call(Object o, Class clazz) throws IllegalAccessException, InvocationTargetException {
050 if (clazz == null) {
051 clazz = o.getClass();
052 }
053 Method m = null;
054 while (m == null && clazz != null && clazz != Object.class) {
055 try {
056 m = clazz.getDeclaredMethod(methodName);
057 } catch (NoSuchMethodException e) {
058 clazz = clazz.getSuperclass();
059 }
060
061 }
062 if (m == null) {
063 throw new AssertionError("We checked that the class had this method at deploy time. expected class: " + targetClassName + ", actual class: " + clazz.getName() + ", method: " + methodName);
064 }
065 if (!m.isAccessible()) {
066 try {
067 m.setAccessible(true);
068 m.invoke(o);
069 } finally {
070 m.setAccessible(false);
071 }
072 } else {
073 m.invoke(o);
074 }
075 }
076 }