Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] StackOverflow error

Hello,

You have advised runMe() to execute your advice:

       @RunMultiple(counter = 1000)
       public void runMe(String greeting, String name) {
               if (logger.isDebugEnabled()) {
                       logger.debug(greeting + " " + name);
               }
       }

Your advice then says if count is less than the annotation value
counter, call runMe().  This will invoke the advised version of
runMe(greeting,name).  And then we will come back into the aspect and
do it again, and so on and so on. Did you really want to call the
advised version of runMe() - I guess you did from the aspect
structure.  This recursion only stops when count is finally greater
than counter.  The JVM can handle a certain level of recursion before
it just gives up and throws StackOverflow - and your 1000 number seems
to be the point where it is giving up.

I'd put the loop in the advice if I were you, rather than using
recursion in this way, and then guard the advice so it doesn't run
more than once in an execution stack.  This pointcut will avoid
running the advice more than once in a recursive call:

       @After("execution(@com.xxxx.util.annotation.RunMultiple *
*.*(..)) && !within(RunMultipleAspect) && !cflow(adviceexecution())")

The last part says 'do not run this advice if we are in the control
flow of some already running advice'.

But basically it is as you thought,  you are recursing because advice
is calling an advised method.

cheers,
Andy.

2008/7/2 jacknjill111 <jacknjill111@xxxxxxxxx>:
>
> Environment:
> AspectJ 1.6.0
> JDK 6u10
>
> Background info:
> I've coded an Aspect that would run a particular method a specific number of
> times. I've created an annotation that can be used to annotate any method
> that you want to run multiple times
>
> RunMultipleAspectTestApp is the driver class. When I set the
> RunMultiple.counter to something less than 900, it works great.
> But when I raise it to 1000 or more, I get a java.lang.StackOverflow error.
> I googled and saw that StackOverFlow error occur
> due to infinite recursion as noted in this document:
> http://www.eclipse.org/aspectj/doc/released/faq.html#q:infiniterecursion
> Do I have my pointcut defined incorrectly? Could someone please help me with
> the pointcut definition for the "repeat" advice?
>
>
> ************ RunMultipleAspectTestApp.java - START **************
>
> package com.xxxx.aop.util;
>
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
>
> import com.xxxx.util.annotation.RunMultiple;
>
> public class RunMultipleAspectTestApp {
>
>        private final Log logger = LogFactory.getLog(getClass());
>
>        public static void main(String[] args) {
>                RunMultipleAspectTestApp rmata = new RunMultipleAspectTestApp();
>
>                rmata.runMe();
>                rmata.runMe("Hello", "World");
>        }
>
>        @RunMultiple
>        public void runMe() {
>                if (logger.isDebugEnabled()) {
>                        logger.debug("Hello Jack");
>                }
>        }
>
>        @RunMultiple(counter = 950)
>        public void runMe(String greeting, String name) {
>                if (logger.isDebugEnabled()) {
>                        logger.debug(greeting + " " + name);
>                }
>        }
> }
>
> ************ RunMultipleAspectTestApp.java - END **************
>
>
> ************ RunMultiple.java - START **************
>
> package com.xxxx.util.annotation;
>
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Retention;
> import java.lang.annotation.RetentionPolicy;
> import java.lang.annotation.Target;
>
> @Retention(RetentionPolicy.RUNTIME)
> @Target(ElementType.METHOD)
> public @interface RunMultiple {
>        /**
>         * Number of times a method should be invoked
>         */
>        int counter() default 1;
> }
>
> ************ RunMultiple.java - END**************
>
> The RunMultipleAspect has a repeat (After) advice which is called on any
> method annotated with the RunMultiple annotation. RunMultipleAspect
> also intercepts object construction (AfterReturning) and caches the object
> that is created so it can reuse the object in the repeat advice.
> The repeat advice looks up the RunMultiple.counter value and then invokes
> the object method if 'RunMultipleAspect.count < RunMultiple.counter'
>
> ************ RunMultipleAspect.java - START **************
>
> package com.xxxx.aop.util;
>
> import java.lang.reflect.InvocationTargetException;
> import java.lang.reflect.Method;
>
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.aspectj.lang.JoinPoint;
> import org.aspectj.lang.ProceedingJoinPoint;
> import org.aspectj.lang.annotation.After;
> import org.aspectj.lang.annotation.AfterReturning;
> import org.aspectj.lang.annotation.Aspect;
> import org.aspectj.lang.reflect.CodeSignature;
> import org.aspectj.lang.reflect.MethodSignature;
>
> import com.xxxx.util.annotation.RunMultiple;
>
> /**
>  * Aspect to replace the following construct
>  *
>  *  ...
>  *  int counter = 10000;
>  *  for (int i=0; i < counter; i++) {
>  *    greet();
>  *  }
>  *
>  *   public void greet() {
>  *      ...
>  *      ...
>  *   }
>  *
>  *  with this construct:
>  *
>  *  @RunMultiple(counter=1000)
>  *  public void greet() {
>  *     ...
>  *  }
>  *
>  */
>
> @Aspect
> public class RunMultipleAspect {
>
>        private final Log logger = LogFactory.getLog(getClass());
>
>        private int count = 0; // keeps track of how many times a method has been
> executed
>        private String methodName; // keeps track of method name since we could
> have multiple (overloaded) methods annotated
>        private Object obj; // object instance that was created once
>
>        @After("execution(@com.xxxx.util.annotation.RunMultiple * *.*(..)) &&
> !within(RunMultipleAspect)")
>        public void repeat(JoinPoint jp) throws ClassNotFoundException,
>                        IllegalArgumentException, IllegalAccessException,
>                        InvocationTargetException, InstantiationException {
>                Method method = ((MethodSignature) jp.getSignature()).getMethod();
>
>                printArguments(jp);
>
>                // doing this to store the current method name
>                String tempMethodName = jp.getTarget().getClass().getName() + "."
>                                + method.getName() + this.getArgumentTypes(jp);
>                if (!tempMethodName.equals(methodName)) {
>                        this.methodName = jp.getTarget().getClass().getName() + "."
>                                        + method.getName() + this.getArgumentTypes(jp);
>                        this.count = 0;
>                }
>
>                RunMultiple rmAnnotation = method.getAnnotation(RunMultiple.class);
>                if (logger.isTraceEnabled()) {
>                        logger.trace("rmAnnotation.toString(): " + rmAnnotation.toString());
>                }
>
>                if (logger.isTraceEnabled()) {
>                        logger.trace("count: " + count + " :: " + rmAnnotation.counter());
>                }
>                if (++count < rmAnnotation.counter()) {
>                        if (logger.isTraceEnabled()) {
>                                logger.trace("using object " + obj.hashCode());
>                                logger.trace(obj.getClass().getName() + "." + method.getName());
>                        }
>                        method.invoke(obj, jp.getArgs());
>                }
>        }
>
>  /**
>   * Stores the object instance in 'obj' the first time it is created so I
> can
>   * call methods on that instance
>   */
>        @AfterReturning("execution(*.new(..)) && !within(RunMultipleAspect)")
>        public void setObjectAtConstruction(JoinPoint jp) {
>                if (logger.isTraceEnabled()) {
>                        logger.trace("jp.getTarget(): " + jp.getTarget().hashCode()
>                                        + ", jp.getThis(): " + jp.getThis().hashCode());
>                }
>
>                if (obj == null) {
>                        this.obj = jp.getTarget();
>                        if (logger.isTraceEnabled()) {
>                                logger.trace("creating object: " + obj.hashCode());
>                        }
>                } else {
>                        if (logger.isTraceEnabled()) {
>                                logger.trace("object created earlier: " + obj.hashCode());
>                        }
>                }
>        }
>
>
>        private String getArgumentTypes(JoinPoint jp) {
>                Class<?>[] argTypes = ((CodeSignature) jp.getSignature())
>                                .getParameterTypes();
>
>                if (argTypes != null && argTypes.length > 0) {
>                        StringBuilder sb = new StringBuilder("(");
>                        for (int ar = 0; ar < argTypes.length; ar++) {
>                                sb.append(argTypes[ar].getName());
>                                if (ar < argTypes.length - 1) {
>                                        sb.append(",");
>                                }
>                        }
>                        sb.append(")");
>
>                        return sb.toString();
>                }
>
>                return "";
>        }
>
>        private void printArguments(JoinPoint jp) {
>                Object[] args = jp.getArgs();
>                String[] argNames = ((CodeSignature) jp.getSignature())
>                                .getParameterNames();
>                Class<?>[] argTypes = ((CodeSignature) jp.getSignature())
>                                .getParameterTypes();
>
>                for (int i = 0; i < args.length; i++) {
>                        if (logger.isTraceEnabled()) {
>                                logger.trace(argTypes[i].getName() + "." + argNames[i] + "="
>                                                + args[i]);
>                        }
>                }
>
>        }
>
>
> }
>
> ************ RunMultipleAspect.java - END **************
>
> My aop.xml looks like this:
>
> <aspects>
>
>        <aspect name="com.xxxx.aop.util.RunMultipleAspect" />
>
>        <weaver options="-verbose -showWeaveInfo -debug">
>                <include within="com.xxxx.aop.util..*" />
>                <exclude within="com.xxxx.aop.util.RunMultipleAspect" />
>        </weaver>
>
> </aspects>
>
> ******************** Output/Error log: **************************
>
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> rmAnnotation.toString(): @com.xxxx.util.annotation.RunMultiple(counter=950)
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect - count: 929 :: 950
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect - using object
> 14779369
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe
> 2008/07/02 16:30:32:281 CDT [DEBUG] RunMultipleAspectTestApp - Hello World
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> java.lang.String.greeting=Hello
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> java.lang.String.name=World
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> rmAnnotation.toString(): @com.xxxx.util.annotation.RunMultiple(counter=950)
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect - count: 930 :: 950
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect - using object
> 14779369
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe
> 2008/07/02 16:30:32:281 CDT [DEBUG] RunMultipleAspectTestApp - Hello World
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> java.lang.String.greeting=Hello
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> java.lang.String.name=World
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> rmAnnotation.toString(): @com.xxxx.util.annotation.RunMultiple(counter=950)
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect - count: 931 :: 950
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect - using object
> 14779369
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe
> 2008/07/02 16:30:32:281 CDT [DEBUG] RunMultipleAspectTestApp - Hello World
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> java.lang.String.greeting=Hello
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> java.lang.String.name=World
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> rmAnnotation.toString(): @com.xxxx.util.annotation.RunMultiple(counter=950)
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect - count: 932 :: 950
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect - using object
> 14779369
> 2008/07/02 16:30:32:281 CDT [TRACE] RunMultipleAspect -
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe
> Exception in thread "main" java.lang.reflect.InvocationTargetException
>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>        at java.lang.reflect.Method.invoke(Unknown Source)
>        at com.xxxx.aop.util.RunMultipleAspect.repeat(RunMultipleAspect.java:142)
>        at
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe(RunMultipleAspectTestApp.java:52)
>        at
> com.xxxx.aop.util.RunMultipleAspectTestApp.main(RunMultipleAspectTestApp.java:16)
> Caused by: java.lang.reflect.InvocationTargetException
>        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>        at java.lang.reflect.Method.invoke(Unknown Source)
>        at com.xxxx.aop.util.RunMultipleAspect.repeat(RunMultipleAspect.java:142)
>        at
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe(RunMultipleAspectTestApp.java:52)
>        ... 7 more
> .......
> .......
> .......
> Caused by: java.lang.reflect.InvocationTargetException
>        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>        at java.lang.reflect.Method.invoke(Unknown Source)
>        at com.xxxx.aop.util.RunMultipleAspect.repeat(RunMultipleAspect.java:142)
>        at
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe(RunMultipleAspectTestApp.java:52)
>        ... 1012 more
> Caused by: java.lang.reflect.InvocationTargetException
>        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>        at java.lang.reflect.Method.invoke(Unknown Source)
>        at com.xxxx.aop.util.RunMultipleAspect.repeat(RunMultipleAspect.java:142)
>        at
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe(RunMultipleAspectTestApp.java:52)
>        ... 1017 more
> Caused by: java.lang.reflect.InvocationTargetException
>        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>        at java.lang.reflect.Method.invoke(Unknown Source)
>        at com.xxxx.aop.util.RunMultipleAspect.repeat(RunMultipleAspect.java:142)
>        at
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe(RunMultipleAspectTestApp.java:52)
>        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>        at java.lang.reflect.Method.invoke(Unknown Source)
>        at com.xxxx.aop.util.RunMultipleAspect.repeat(RunMultipleAspect.java:142)
>        at
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe(RunMultipleAspectTestApp.java:52)
>        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
> ........
> ........
> ........
> Caused by: java.lang.reflect.InvocationTargetException
>        ... 1024 more
> Caused by: java.lang.reflect.InvocationTargetException
>        ... 1024 more
> Caused by: java.lang.StackOverflowError
>        at java.text.DecimalFormat.subformat(Unknown Source)
>        at java.text.DecimalFormat.format(Unknown Source)
>        at java.text.DecimalFormat.format(Unknown Source)
>        at java.text.SimpleDateFormat.zeroPaddingNumber(Unknown Source)
>        at java.text.SimpleDateFormat.subFormat(Unknown Source)
>        at java.text.SimpleDateFormat.format(Unknown Source)
>        at java.text.SimpleDateFormat.format(Unknown Source)
>        at java.text.DateFormat.format(Unknown Source)
>        at org.apache.commons.logging.impl.SimpleLog.log(SimpleLog.java:294)
>        at org.apache.commons.logging.impl.SimpleLog.trace(SimpleLog.java:416)
>        at
> com.xxxx.aop.util.RunMultipleAspect.printArguments(RunMultipleAspect.java:175)
>        at com.xxxx.aop.util.RunMultipleAspect.repeat(RunMultipleAspect.java:115)
>        at
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe(RunMultipleAspectTestApp.java:52)
>        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>        at java.lang.reflect.Method.invoke(Unknown Source)
>        at com.xxxx.aop.util.RunMultipleAspect.repeat(RunMultipleAspect.java:142)
>        at
> com.xxxx.aop.util.RunMultipleAspectTestApp.runMe(RunMultipleAspectTestApp.java:52)
>        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
>        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>        at java.lang.reflect.Method.invoke(Unknown Source)
>        at com.xxxx.aop.util.RunMultipleAspect.repeat(RunMultipleAspect.java:142)
>
>
>
>
> Any help is greatly appreciated.
> --
> View this message in context: http://www.nabble.com/StackOverflow-error-tp18247723p18247723.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>


Back to the top