package org.kj6682.kosimo.dm;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;

/**
 * Created by luigi on 24.04.16.
 */
@Aspect
@Component
public class FeatureAspect {

    @Around("execution(* AccountController.*(..))")
    public Object logServiceAccess(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println(">>> Before: " + joinPoint + "<<<");
        try {
            System.out.println(">>> Annotation : " + retrieveAnnotationFrom( joinPoint ).value());
            return joinPoint.proceed();
        }catch (IllegalArgumentException iae) {
            iae.printStackTrace();
            throw iae;
        }
    }

    private Feature retrieveAnnotationFrom(ProceedingJoinPoint joinPoint ) throws Throwable{
        return retrieveTargetMethodFrom( joinPoint ).getAnnotation( Feature.class );
    }

    private Method retrieveTargetMethodFrom(ProceedingJoinPoint joinPoint ) throws Throwable{
        Class targetClass = joinPoint.getTarget().getClass();
        return targetClass.getMethod( joinPoint.getSignature().getName(), retrieveArgTypesFrom( joinPoint ) );
    }

    private Class[] retrieveArgTypesFrom( ProceedingJoinPoint joinPoint ){
        Class[] argTypes = new Class[joinPoint.getArgs().length];
        for( int i=0; i<argTypes.length; i++ ){
            argTypes[i] = joinPoint.getArgs()[i].getClass();
        }
        return argTypes;
    }

}


@@ -28,43 +28,8 @@
 m;%0A%0A
-import org.aspectj.lang.JoinPoint;%0A
 impo
@@ -159,51 +159,8 @@
 ct;%0A
-import org.aspectj.lang.annotation.Before;%0A
 impo
@@ -256,71 +256,8 @@
 ent;
-%0Aimport org.springframework.web.bind.annotation.RequestMapping;
 %0A%0Aim
@@ -504,32 +504,33 @@
 throws Throwable
+ 
 %7B%0A        System
@@ -650,32 +650,23 @@
 %22 + 
-retrieveAnnotationFrom( 
+extractFeature(
 join
@@ -670,17 +670,16 @@
 oinPoint
- 
 ).value(
@@ -718,24 +718,26 @@
 .proceed();%0A
+%0A%0A
         %7Dcat
@@ -733,16 +733,17 @@
        %7D
+ 
 catch (I
@@ -872,30 +872,22 @@
 ure 
-retrieveAnnotationFrom
+extractFeature
 (Pro
@@ -904,33 +904,32 @@
 nPoint joinPoint
- 
 ) throws Throwab
@@ -922,32 +922,33 @@
 throws Throwable
+ 
 %7B%0A        return
@@ -952,34 +952,22 @@
 urn 
-retrieveTarge
+extrac
 tMethod
-From( 
+(
 join
@@ -971,17 +971,16 @@
 oinPoint
- 
 ).getAnn
@@ -987,17 +987,16 @@
 otation(
- 
 Feature.
@@ -1000,17 +1000,16 @@
 re.class
- 
 );%0A    %7D
@@ -1033,32 +1033,21 @@
 hod 
-retrieveTarge
+extrac
 tMethod
-From
 (Pro
@@ -1072,17 +1072,16 @@
 oinPoint
- 
 ) throws
@@ -1085,483 +1085,267 @@
 ows 
-Throwable%7B%0A        Class targetClass = joinPoint.getTarget().getClass();%0A        return targetClass.getMethod( joinPoint.getSignature().getName(), retrieveArgTypesFrom( joinPoint ) 
+NoSuchMethodException %7B%0A        MethodSignature signature = (MethodSignature) joinPoint.getSignature(
 );%0A    
-%7D%0A%0A
     
-private Class%5B%5D retrieveArgTypesFrom( ProceedingJoinPoint joinPoint )%7B%0A        Class%5B%5D argTypes = new Class%5BjoinPoint.getArgs().length%5D;%0A        for( int i=0; i%3CargTypes.length; i++ )%7B%0A            argTypes%5Bi%5D = joinPoint.getArgs()%5Bi%5D.getClass();%0A        %7D%0A        return argTypes;
+Class%3C?%3E targetClass = joinPoint.getTarget().getClass();%0A        return targetClass.getMethod(signature.getName(), signature.getParameterTypes());%0A
 %0A   

simplify Feature annotation extraction