Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Logging constructor calls and exceptions

Hi Nils,

The following code advices both the constructor and the 'test' method.

Class:

public class Constructor {
	
	public Constructor(){
		
	}

	public static void main( String[] argv ){
		
		try{
			new Constructor().test();			
		}catch( Exception e ){
			
		}

		
	}
	
	public void test() throws Exception{
		throw new Exception();
	}
}


Aspect :

public aspect ConstructorAspect {

before(): execution(public Constructor.new(..)){
        if ( Boolean.TRUE ) {
                System.out.println("-> "
                                + thisJoinPoint.getSignature().toString()
                                + thisJoinPoint + " at "
                                + thisJoinPoint.getSourceLocation());
        }
	}
	
after() throwing (Exception e):  execution(* Constructor.test(..)){
        if ( Boolean.TRUE ) {
                System.out.println("-> "
                                + thisJoinPoint.getSignature().toString()
                                + thisJoinPoint + " at "
                                + thisJoinPoint.getSourceLocation());
        }
}	
}

Thanks,
Mohan

On Wed, Jun 9, 2010 at 3:18 PM, Nils Wilhelm <murphy@xxxxxxxxxxxxxxxx> wrote:
> Hi,
>
> i hope you can help me improving my aspect so that it logs constructor calls
> and exceptions too. What i already have is:
>
> public aspect LogAspect {
>
>       private static Logger communicationLogger = Logger
>                       .getLogger("communicationLogger");
>
>       private static Logger catchAllLogger = Logger.getRootLogger();
>
>       static {
>               PropertyConfigurator.configure("Logger.properties");
>       }
>
>       before(): execution(public * communication..*(..)){
>               if (communicationLogger.isDebugEnabled()) {
>                       communicationLogger.debug("-> "
>                                       +
> thisJoinPoint.getSignature().toString()
>                                       + createArgs(thisJoinPoint) + " at "
>                                       + thisJoinPoint.getSourceLocation());
>               }
>       }
>             before(): execution(public * *..*(..)){
>               if (catchAllLogger.isDebugEnabled()) {
>                       catchAllLogger.debug("-> "
>                                       +
> thisJoinPoint.getSignature().toString()
>                                       + createArgs(thisJoinPoint) + " at "
>                                       + thisJoinPoint.getSourceLocation());
>               }
>       }
>
>       after() returning(Object o): execution(public * communication..*(..)){
>               if (communicationLogger.isDebugEnabled())
>                       communicationLogger.debug("<- "
>                                       +
> thisJoinPoint.getSignature().toString() + " at "
>                                       + thisJoinPoint.getSourceLocation() +
> " returning: " + o);
>       }
>
>       after() returning(Object o): execution(public * *..*(..)){
>               if (catchAllLogger.isDebugEnabled())
>                       catchAllLogger.debug("<- "
>                                       +
> thisJoinPoint.getSignature().toString() + " at "
>                                       + thisJoinPoint.getSourceLocation() +
> " returning: " + o);
>       }
>
>       private String createArgs(JoinPoint jp) {
>               StringBuffer args = new StringBuffer();
>               args.append("(");
>               for (int i = 0; i < jp.getArgs().length; i++) {
>                       if (i > 0)
>                               args.append(",");
>                       args.append(jp.getArgs()[i].toString());
>               }
>               args.append(")");
>
>               return args.toString();
>       }
>
> }
>
> This logs my method calls and works fine. Now i want to log constuctor calls
> and exceptions too. If i use
>
> after() throwing (Exception e): execution(public * *..*(..)){
>               if (catchAllLogger.isDebugEnabled()) {
>                       catchAllLogger.debug("Error "
>                                       +
> thisJoinPoint.getSignature().toString()
>                                       + createArgs(thisJoinPoint) + " at "
>                                       + thisJoinPoint.getSourceLocation());
>               }
>       }
>
> for logging exceptions it is never applied. And if i use
>
> before(): execution(public new(..)){
>               if (catchAllLogger.isDebugEnabled()) {
>                       catchAllLogger.debug("-> "
>                                       +
> thisJoinPoint.getSignature().toString()
>                                       + createArgs(thisJoinPoint) + " at "
>                                       + thisJoinPoint.getSourceLocation());
>               }
>       }
> for logging constructor calls i get an NoAspectBoundException.
>
> Could you help me with that?
>
> Best regards
>
> Murphy
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>


Back to the top