KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > reflect > CallbackOnEntity


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

19 package org.apache.cayenne.reflect;
20
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23
24 import org.apache.cayenne.CayenneRuntimeException;
25 import org.apache.cayenne.util.Util;
26
27 /**
28  * Defines a generic callback operation executed via reflection on a persistent object.
29  * Note that the method must be declared in the class itself. Callback will not look up
30  * the class hierarchy.
31  *
32  * @since 3.0
33  * @author Andrus Adamchik
34  */

35 class CallbackOnEntity extends AbstractCallback {
36
37     private Method JavaDoc callbackMethod;
38
39     CallbackOnEntity(Class JavaDoc objectClass, String JavaDoc methodName)
40             throws IllegalArgumentException JavaDoc {
41         this.callbackMethod = findMethod(objectClass, methodName);
42     }
43
44     public void performCallback(Object JavaDoc entity) {
45         try {
46             callbackMethod.invoke(entity, null);
47         }
48         catch (Exception JavaDoc e) {
49             throw new CayenneRuntimeException("Error invoking entity callback method "
50                     + callbackMethod.getName(), e);
51         }
52     }
53
54     private Method JavaDoc findMethod(Class JavaDoc objectClass, String JavaDoc methodName)
55             throws IllegalArgumentException JavaDoc {
56         Method JavaDoc[] methods = objectClass.getDeclaredMethods();
57         for (int i = 0; i < methods.length; i++) {
58             if (methodName.equals(methods[i].getName())) {
59
60                 // must be non-static, void, with no args
61
// JPA spec also requires it to be non-final, but we don't care
62
int modifiers = methods[i].getModifiers();
63                 if (!Modifier.isStatic(modifiers)
64                         && Void.TYPE.isAssignableFrom(methods[i].getReturnType())
65                         && methods[i].getParameterTypes().length == 0) {
66
67                     if (!Util.isAccessible(methods[i])) {
68                         methods[i].setAccessible(true);
69                     }
70
71                     return methods[i];
72                 }
73             }
74         }
75
76         throw new IllegalArgumentException JavaDoc("Class "
77                 + objectClass.getName()
78                 + " has no valid callback method '"
79                 + methodName
80                 + "'");
81     }
82 }
83
Popular Tags