All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.geronimo.j2ee.annotation.LifecycleMethod Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


package org.apache.geronimo.j2ee.annotation;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @version $Rev: 516141 $ $Date: 2007-03-09 02:45:05 +0800 (Fri, 09 Mar 2007) $
 */
public class LifecycleMethod implements Serializable {

    private final String targetClassName;
    private final String methodName;


    public LifecycleMethod(String targetClassName, String methodName) {
        this.targetClassName = targetClassName;
        this.methodName = methodName;
    }

    public String getTargetClassName() {
        return targetClassName;
    }

    public String getMethodName() {
        return methodName;
    }

    public void call(Object o, Class clazz) throws IllegalAccessException, InvocationTargetException {
        if (clazz == null) {
            clazz = o.getClass();
        }
        Method m = null;
        while (m == null && clazz != null && clazz != Object.class) {
            try {
                m = clazz.getDeclaredMethod(methodName);
            } catch (NoSuchMethodException e) {
                clazz = clazz.getSuperclass();
            }

        }
        if (m == null) {
            throw new AssertionError("We checked that the class had this method at deploy time. expected class: " + targetClassName + ", actual class: " + clazz.getName() + ", method: " + methodName);
        }
        if (!m.isAccessible()) {
            try {
                m.setAccessible(true);
                m.invoke(o);
            } finally {
                m.setAccessible(false);
            }
        } else {
            m.invoke(o);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy