|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ReflectiveVisitor.java | 0% | 0% | 0% | 0% |
|
1 | package org.drools.reteoo; | |
2 | ||
3 | /* | |
4 | * $Id: ReflectiveVisitor.java,v 1.5.2.1 2005/04/17 13:15:16 mproctor Exp $ | |
5 | * | |
6 | * Copyright 2004-2004 (C) The Werken Company. All Rights Reserved. | |
7 | * | |
8 | * Redistribution and use of this software and associated documentation | |
9 | * ("Software"), with or without modification, are permitted provided that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. (http://werken.com/) | |
28 | * | |
29 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
30 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
39 | * POSSIBILITY OF SUCH DAMAGE. | |
40 | * | |
41 | */ | |
42 | ||
43 | import java.lang.reflect.Method; | |
44 | ||
45 | import org.drools.Visitor; | |
46 | ||
47 | /** | |
48 | * Java Tip 98: Reflect on the Visitor design pattern. Implement visitors in | |
49 | * Java, using reflection. | |
50 | * http://www.javaworld.com/javaworld/javatips/jw-javatip98.html | |
51 | * | |
52 | * @author Jeremy Blosser | |
53 | */ | |
54 | public abstract class ReflectiveVisitor | |
55 | implements | |
56 | Visitor | |
57 | { | |
58 | static final String newline = System.getProperty( "line.separator" ); | |
59 | ||
60 | 0 | public void visit(Object object) |
61 | { | |
62 | 0 | try |
63 | { | |
64 | 0 | if ( object != null ) |
65 | { | |
66 | 0 | Method method = getMethod( object.getClass( ) ); |
67 | 0 | method.invoke( this, |
68 | new Object[]{object} ); | |
69 | } | |
70 | else | |
71 | { | |
72 | 0 | Method method = getClass( ).getMethod( "visitNull", |
73 | ( Class[] ) null ); | |
74 | 0 | method.invoke( this, |
75 | ( Object[] ) null ); | |
76 | } | |
77 | } | |
78 | catch ( Exception e ) | |
79 | { | |
80 | 0 | e.printStackTrace( ); |
81 | } | |
82 | } | |
83 | ||
84 | 0 | private Method getMethod(Class clazz) |
85 | { | |
86 | 0 | Class newClazz = clazz; |
87 | 0 | Method method = null; |
88 | ||
89 | // Try the superclasses | |
90 | 0 | while ( method == null && newClazz != Object.class ) |
91 | { | |
92 | 0 | String methodName = newClazz.getName( ); |
93 | 0 | methodName = "visit" + methodName.substring( methodName.lastIndexOf( '.' ) + 1 ); |
94 | 0 | try |
95 | { | |
96 | 0 | method = getClass( ).getMethod( methodName, |
97 | new Class[]{newClazz} ); | |
98 | } | |
99 | catch ( NoSuchMethodException e ) | |
100 | { | |
101 | 0 | newClazz = newClazz.getSuperclass( ); |
102 | } | |
103 | } | |
104 | ||
105 | // Try the interfaces. | |
106 | 0 | if ( newClazz == Object.class ) |
107 | { | |
108 | 0 | Class[] interfaces = clazz.getInterfaces( ); |
109 | 0 | for ( int i = 0; i < interfaces.length; i++ ) |
110 | { | |
111 | 0 | String methodName = interfaces[i].getName( ); |
112 | 0 | methodName = "visit" + methodName.substring( methodName.lastIndexOf( '.' ) + 1 ); |
113 | 0 | try |
114 | { | |
115 | 0 | method = getClass( ).getMethod( methodName, |
116 | new Class[]{interfaces[i]} ); | |
117 | } | |
118 | catch ( NoSuchMethodException e ) | |
119 | { | |
120 | // swallow | |
121 | } | |
122 | } | |
123 | } | |
124 | 0 | if ( method == null ) |
125 | { | |
126 | 0 | try |
127 | { | |
128 | 0 | method = getClass( ).getMethod( "visitObject", |
129 | new Class[]{Object.class} ); | |
130 | } | |
131 | catch ( Exception e ) | |
132 | { | |
133 | // Shouldn't happen as long as all Visitors extend this class | |
134 | // and this class continues to implement visitObject(Object). | |
135 | 0 | e.printStackTrace( ); |
136 | } | |
137 | } | |
138 | 0 | return method; |
139 | } | |
140 | ||
141 | 0 | public void visitObject(Object object) |
142 | { | |
143 | 0 | System.err.println( "no visitor implementation for : " + object.getClass( ) + " : " + object ); |
144 | } | |
145 | } |
|