de.beyondjava.examples.scopes.spring.LoggingAspect.java Source code

Java tutorial

Introduction

Here is the source code for de.beyondjava.examples.scopes.spring.LoggingAspect.java

Source

/**
 *  (C) 2013-2014 Stephan Rauh http://www.beyondjava.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.beyondjava.examples.scopes.spring;

import java.lang.reflect.Field;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Around("execution(public java.lang.String *.getCounter(..))")
    public Object logBefore(ProceedingJoinPoint joinPoint) throws Throwable {
        String msg = "(null) -> ";
        Object target = joinPoint.getTarget();
        try {
            Field counterField = target.getClass().getDeclaredField("counter");
            int counter = (int) counterField.get(target);
            msg = String.valueOf(counter) + " -> ";
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String s = (String) joinPoint.proceed();
        return msg + s;
    }

}