001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.guard;
013:
014: import java.lang.reflect.Constructor;
015: import java.lang.reflect.Method;
016:
017: import net.sf.oval.exception.ValidationFailedException;
018: import net.sf.oval.internal.Log;
019: import net.sf.oval.internal.util.Invocable;
020:
021: import org.aspectj.lang.ProceedingJoinPoint;
022: import org.aspectj.lang.annotation.Around;
023: import org.aspectj.lang.annotation.Aspect;
024: import org.aspectj.lang.annotation.DeclareParents;
025: import org.aspectj.lang.annotation.SuppressAjWarnings;
026: import org.aspectj.lang.reflect.ConstructorSignature;
027: import org.aspectj.lang.reflect.MethodSignature;
028:
029: /**
030: * This is an annotations based version of the GuardAspect aspect
031: *
032: * To workaround an AspectJ bug use the -XnoInline weave option, in case you are getting errors like:
033: * java.lang.VerifyError: (class: net/sf/oval/guard/GuardAspect2, method: ajc$inlineAccessMethod$net_sf_oval_guard_GuardAspect2$net_sf_oval_guard_Guard$guardMethodPost signature: (Lnet/sf/oval/guard/Guard;Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;Ljava/lang/Object;)V) Illegal use of nonvirtual function call
034: *
035: * @author Sebastian Thomschke
036: *
037: * @see Guard
038: */
039: @Aspect
040: public abstract class GuardAspect2 extends ApiUsageAuditor2 {
041: private final static Log LOG = Log.getLog(GuardAspect2.class);
042:
043: @SuppressWarnings("unused")
044: @DeclareParents("(@net.sf.oval.guard.Guarded *)")
045: private IsGuarded implementedInterface;
046:
047: private Guard guard;
048:
049: public GuardAspect2() {
050: this (new Guard());
051: getGuard().setParameterNameResolver(
052: new ParameterNameResolverAspectJImpl());
053: }
054:
055: public GuardAspect2(final Guard guard) {
056: LOG.info("Instantiated");
057:
058: setGuard(guard);
059: }
060:
061: @Around("execution((@net.sf.oval.guard.Guarded *).new(..))")
062: public Object allConstructors(
063: final ProceedingJoinPoint this JoinPoint) throws Throwable {
064: final ConstructorSignature SIGNATURE = (ConstructorSignature) this JoinPoint
065: .getSignature();
066:
067: LOG.debug("aroundConstructor() {}", SIGNATURE);
068:
069: final Constructor CONSTRUCTOR = SIGNATURE.getConstructor();
070: final Object[] args = this JoinPoint.getArgs();
071: final Object TARGET = this JoinPoint.getTarget();
072:
073: // pre conditions
074: {
075: guard.guardConstructorPre(TARGET, CONSTRUCTOR, args);
076: }
077:
078: final Object result = this JoinPoint.proceed();
079:
080: // post conditions
081: {
082: guard.guardConstructorPost(TARGET, CONSTRUCTOR, args);
083: }
084:
085: return result;
086: }
087:
088: @SuppressAjWarnings("adviceDidNotMatch")
089: @Around("execution(* (@net.sf.oval.guard.Guarded *).*(..))")
090: public Object allMethods(final ProceedingJoinPoint this JoinPoint)
091: throws Throwable {
092: final MethodSignature SIGNATURE = (MethodSignature) this JoinPoint
093: .getSignature();
094:
095: LOG.debug("aroundMethod() {}", SIGNATURE);
096:
097: final Method METHOD = SIGNATURE.getMethod();
098: final Object[] args = this JoinPoint.getArgs();
099: final Object TARGET = this JoinPoint.getTarget();
100:
101: return guard.guardMethod(TARGET, METHOD, args, new Invocable() {
102: public Object invoke() {
103: try {
104: return this JoinPoint.proceed();
105: } catch (final Throwable ex) {
106: throw new ValidationFailedException(
107: "Unexpected exception while invoking method.",
108: ex);
109: }
110: }
111: });
112: }
113:
114: /**
115: * @return the guard
116: */
117: public Guard getGuard() {
118: return guard;
119: }
120:
121: public final void setGuard(final Guard guard) {
122: this.guard = guard;
123: }
124: }
|