|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractPicoVisitor.java | 83.3% | 100% | 100% | 95.5% |
|
1 | /***************************************************************************** | |
2 | * Copyright (C) PicoContainer Organization. All rights reserved. * | |
3 | * ------------------------------------------------------------------------- * | |
4 | * The software in this package is published under the terms of the BSD * | |
5 | * style license a copy of which has been included with this distribution in * | |
6 | * the LICENSE.txt file. * | |
7 | *****************************************************************************/ | |
8 | package org.picocontainer.defaults; | |
9 | ||
10 | import org.picocontainer.PicoVisitor; | |
11 | ||
12 | import java.lang.reflect.InvocationTargetException; | |
13 | import java.lang.reflect.Method; | |
14 | ||
15 | /** | |
16 | * Abstract PicoVisitor implementation. A generic traverse method is implemented, that | |
17 | * accepts any object with a method named "accept", that takes a | |
18 | * {@link PicoVisitor} as argument and and invokes it. Additionally it provides the | |
19 | * {@link #checkTraversal()} method, that throws a {@link PicoVisitorTraversalException}, | |
20 | * if currently no traversal is running. | |
21 | * | |
22 | * @author Jörg Schaible | |
23 | * @since 1.1 | |
24 | */ | |
25 | public abstract class AbstractPicoVisitor implements PicoVisitor { | |
26 | private boolean traversal; | |
27 | ||
28 | 64 | public Object traverse(Object node) { |
29 | 64 | traversal = true; |
30 | 64 | try { |
31 | 64 | final Method accept = node.getClass().getMethod("accept", new Class[]{PicoVisitor.class}); |
32 | 62 | accept.invoke(node, new Object[]{this}); |
33 | 58 | return Void.TYPE; |
34 | } catch (NoSuchMethodException e) { | |
35 | } catch (IllegalAccessException e) { | |
36 | } catch (InvocationTargetException e) { | |
37 | 4 | Throwable cause = e.getTargetException(); |
38 | 4 | if (cause instanceof RuntimeException) { |
39 | 2 | throw (RuntimeException)cause; |
40 | 2 | } else if (cause instanceof Error) { |
41 | 2 | throw (Error)cause; |
42 | } | |
43 | } finally { | |
44 | 64 | traversal = false; |
45 | } | |
46 | 2 | throw new IllegalArgumentException(node.getClass().getName() + " is not a valid type for traversal"); |
47 | } | |
48 | ||
49 | /** | |
50 | * Checks the traversal flag, indicating a currently running traversal of the visitor. | |
51 | * @throws PicoVisitorTraversalException if no traversal is active. | |
52 | */ | |
53 | 234 | protected void checkTraversal() { |
54 | 234 | if (!traversal) { |
55 | 2 | throw new PicoVisitorTraversalException(this); |
56 | } | |
57 | } | |
58 | } |
|