View Javadoc
1   /*******************************************************************************
2    * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian
3    * Thomschke.
4    * 
5    * All Rights Reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    * http://www.eclipse.org/legal/epl-v10.html
9    * 
10   * Contributors:
11   *     Sebastian Thomschke - initial implementation.
12   *******************************************************************************/
13  package net.sf.oval.guard;
14  
15  import java.lang.reflect.Constructor;
16  
17  import net.sf.oval.internal.Log;
18  import net.sf.oval.internal.util.Invocable;
19  
20  import org.aopalliance.intercept.ConstructorInterceptor;
21  import org.aopalliance.intercept.ConstructorInvocation;
22  import org.aopalliance.intercept.MethodInterceptor;
23  import org.aopalliance.intercept.MethodInvocation;
24  
25  /**
26   * AOP Alliance Interceptor implementation of the Guard aspect
27   * 
28   * @author Sebastian Thomschke
29   */
30  public class GuardInterceptor implements MethodInterceptor, ConstructorInterceptor
31  {
32  	protected static final class MethodInvocable implements Invocable
33  	{
34  		private final MethodInvocation methodInvocation;
35  
36  		protected MethodInvocable(final MethodInvocation methodInvocation)
37  		{
38  			this.methodInvocation = methodInvocation;
39  		}
40  
41  		/**
42  		 * {@inheritDoc}
43  		 */
44  		public Object invoke() throws Throwable
45  		{
46  			return methodInvocation.proceed();
47  		}
48  	}
49  
50  	private static final Log LOG = Log.getLog(GuardInterceptor.class);
51  
52  	private Guard guard;
53  
54  	public GuardInterceptor()
55  	{
56  		this(new Guard());
57  	}
58  
59  	public GuardInterceptor(final Guard guard)
60  	{
61  		LOG.info("Instantiated");
62  
63  		setGuard(guard);
64  	}
65  
66  	/**
67  	 * {@inheritDoc}
68  	 */
69  	public Object construct(final ConstructorInvocation constructorInvocation) throws Throwable
70  	{
71  		final Constructor< ? > ctor = constructorInvocation.getConstructor();
72  		final Object[] args = constructorInvocation.getArguments();
73  		final Object target = constructorInvocation.getThis();
74  
75  		// pre conditions
76  		{
77  			guard.guardConstructorPre(target, ctor, args);
78  		}
79  
80  		final Object result = constructorInvocation.proceed();
81  
82  		// post conditions
83  		{
84  			guard.guardConstructorPost(target, ctor, args);
85  		}
86  
87  		return result;
88  	}
89  
90  	/**
91  	 * @return the guard
92  	 */
93  	public Guard getGuard()
94  	{
95  		return guard;
96  	}
97  
98  	/**
99  	 * {@inheritDoc}
100 	 */
101 	public Object invoke(final MethodInvocation methodInvocation) throws Throwable
102 	{
103 		return guard.guardMethod(methodInvocation.getThis(), methodInvocation.getMethod(), methodInvocation
104 				.getArguments(), new MethodInvocable(methodInvocation));
105 	}
106 
107 	public void setGuard(final Guard guard)
108 	{
109 		this.guard = guard;
110 	}
111 }