Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Is it possible to define a scope of methods accepting at least one non primitive argument?

Thanks, it works. Here is the aspect code:
=======================================================
package com.shunra.poc;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import com.shunra.poc.logging.LoggerAspect;

public aspect NotNullAspect {
  pointcut nullArg0(Object o): execution(* *(@NotNull (*),..)) &&
args(o,..);
  pointcut nullArg1(Object o): execution(* *(*,@NotNull (*),..)) &&
args(o,..);
  pointcut nullArg2(Object o): execution(* *(*,*,@NotNull (*),..)) &&
args(o,..);
  pointcut nullArg3(Object o): execution(* *(*,*,*,@NotNull (*),..)) &&
args(o,..);
  pointcut nullArg4(Object o): execution(* *(*,*,*,*,@NotNull (*),..)) &&
args(o,..);
  pointcut nullArg5(Object o): execution(* *(*,*,*,*,*,@NotNull (*),..)) &&
args(o,..);

  before(Object o): nullArg0(o) { assertNotNull(o, 0,
thisJoinPointStaticPart); }
  before(Object o): nullArg1(o) { assertNotNull(o, 1,
thisJoinPointStaticPart); }
  before(Object o): nullArg2(o) { assertNotNull(o, 2,
thisJoinPointStaticPart); }
  before(Object o): nullArg3(o) { assertNotNull(o, 3,
thisJoinPointStaticPart); }
  before(Object o): nullArg4(o) { assertNotNull(o, 4,
thisJoinPointStaticPart); }
  before(Object o): nullArg5(o) { assertNotNull(o, 5,
thisJoinPointStaticPart); }
  
  protected void assertNotNull(Object o, int index, JoinPoint.StaticPart jp)
{
    if (o == null) {
      MethodSignature ms = (MethodSignature)jp.getSignature();
      throw new IllegalArgumentException("The '" +
ms.getParameterNames()[index] + "' parameter of the method '" + ms + "'
cannot be null (" + jp.getSourceLocation() + ").");
    }
  }
}
=======================================================

Is it indeed the most compact way to write such an aspect?

Thanks.

--
View this message in context: http://aspectj.2085585.n4.nabble.com/Is-it-possible-to-define-a-scope-of-methods-accepting-at-least-one-non-primitive-argument-tp4195341p4201936.html
Sent from the AspectJ - users mailing list archive at Nabble.com.


Back to the top