Search in sources :

Example 6 with JoinPoint

use of org.aspectj.lang.JoinPoint in project spring-framework by spring-projects.

the class AbstractAspectJAdvice method currentJoinPoint.

/**
	 * Lazily instantiate joinpoint for the current invocation.
	 * Requires MethodInvocation to be bound with ExposeInvocationInterceptor.
	 * <p>Do not use if access is available to the current ReflectiveMethodInvocation
	 * (in an around advice).
	 * @return current AspectJ joinpoint, or through an exception if we're not in a
	 * Spring AOP invocation.
	 */
public static JoinPoint currentJoinPoint() {
    MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
    if (!(mi instanceof ProxyMethodInvocation)) {
        throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
    }
    ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi;
    JoinPoint jp = (JoinPoint) pmi.getUserAttribute(JOIN_POINT_KEY);
    if (jp == null) {
        jp = new MethodInvocationProceedingJoinPoint(pmi);
        pmi.setUserAttribute(JOIN_POINT_KEY, jp);
    }
    return jp;
}
Also used : ProxyMethodInvocation(org.springframework.aop.ProxyMethodInvocation) MethodInvocation(org.aopalliance.intercept.MethodInvocation) ProxyMethodInvocation(org.springframework.aop.ProxyMethodInvocation) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Example 7 with JoinPoint

use of org.aspectj.lang.JoinPoint in project weixin-java-mp-multi-demo by binarywang.

the class ControllerLogAspect method writeParams.

@Before("controller()")
public void writeParams(JoinPoint jp) {
    String[] names = ((CodeSignature) jp.getSignature()).getParameterNames();
    Object[] args = jp.getArgs();
    if (ArrayUtils.isEmpty(names)) {
        return;
    }
    StringBuilder sb = new StringBuilder("Arguments: ");
    for (int i = 0; i < names.length; i++) {
        sb.append(names[i] + " = " + args[i] + ",");
    }
    debugInController(jp, sb.toString());
}
Also used : CodeSignature(org.aspectj.lang.reflect.CodeSignature) JoinPoint(org.aspectj.lang.JoinPoint) Before(org.aspectj.lang.annotation.Before)

Example 8 with JoinPoint

use of org.aspectj.lang.JoinPoint in project Corgi by kevinYin.

the class AdminLoggerAspect method getJointPointParam.

/**
 * 获取连接点对应方法的请求参数列表(参数名-参数值)
 *
 * @param jp
 *            - 切面连接点
 * @return
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 */
public static Map<String, Object> getJointPointParam(JoinPoint jp) throws IllegalArgumentException, IllegalAccessException {
    MethodSignature ms = (MethodSignature) jp.getSignature();
    Method method = ms.getMethod();
    List<Class<?>> paramTypes = Arrays.asList(method.getParameterTypes());
    List<String> paramNames = getMethodParamNames(method);
    Object[] args = jp.getArgs();
    int size = paramNames.size();
    int length = args.length;
    Map<String, Object> params = new HashMap<String, Object>();
    for (int i = 0; i < size && i < length; i++) {
        Class<?> clazz = paramTypes.get(i);
        String name = paramNames.get(i);
        Object value = args[i];
        if (isJavaClass(clazz)) {
            params.put(name, value);
        } else {
            if ((value instanceof HttpServletResponse)) {
                continue;
            }
            Map<String, Object> param = getClassAttrValueMap(clazz, name, value);
            params.putAll(param);
        }
    }
    return params;
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) HttpServletResponse(javax.servlet.http.HttpServletResponse) Method(java.lang.reflect.Method) JoinPoint(org.aspectj.lang.JoinPoint)

Example 9 with JoinPoint

use of org.aspectj.lang.JoinPoint in project spf4j by zolyfarkas.

the class SamplingAllocationMonitorAspect method afterAllocation.

@AfterReturning(pointcut = "call(*.new(..))", returning = "obj", argNames = "jp,obj")
public void afterAllocation(final JoinPoint jp, final Object obj) {
    MutableInteger counter = ThreadLocalCounter.get();
    int value = counter.getValue();
    if (value < SAMPLE_COUNT) {
        counter.setValue(value + 1);
    } else {
        // the stack trace get and the object size method are expensive to be done at every allocation...
        counter.setValue(0);
        StackTrace st = StackTrace.from(Thread.currentThread().getStackTrace(), 2);
        RECORDER.getRecorder(st).record(InstrumentationHelper.getObjectSize(obj));
    }
}
Also used : StackTrace(org.spf4j.stackmonitor.StackTrace) MutableInteger(org.spf4j.base.MutableInteger) JoinPoint(org.aspectj.lang.JoinPoint) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Example 10 with JoinPoint

use of org.aspectj.lang.JoinPoint in project herd by FINRAOS.

the class NamespaceSecurityAdviceTest method checkPermissionAssertNoExceptionWhenMultipleAnnotationsAndAllPermissionsValid.

/**
 * Test where a method is annotated with multiple NamespacePermission annotations. Asserts that the user will all permissions do not throw an exception.
 */
@Test
public void checkPermissionAssertNoExceptionWhenMultipleAnnotationsAndAllPermissionsValid() throws Exception {
    // Mock a join point of the method call
    // mockMethodMultipleAnnotations("foo", "bar");
    JoinPoint joinPoint = mock(JoinPoint.class);
    MethodSignature methodSignature = mock(MethodSignature.class);
    Method method = NamespaceSecurityAdviceTest.class.getDeclaredMethod("mockMethodMultipleAnnotations", String.class, String.class);
    when(methodSignature.getParameterNames()).thenReturn(new String[] { "namespace1", "namespace2" });
    when(methodSignature.getMethod()).thenReturn(method);
    when(joinPoint.getSignature()).thenReturn(methodSignature);
    when(joinPoint.getArgs()).thenReturn(new Object[] { "foo", "bar" });
    String userId = "userId";
    ApplicationUser applicationUser = new ApplicationUser(getClass());
    applicationUser.setUserId(userId);
    applicationUser.setNamespaceAuthorizations(new HashSet<>());
    applicationUser.getNamespaceAuthorizations().add(new NamespaceAuthorization("foo", Arrays.asList(NamespacePermissionEnum.READ)));
    applicationUser.getNamespaceAuthorizations().add(new NamespaceAuthorization("bar", Arrays.asList(NamespacePermissionEnum.WRITE)));
    SecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken(new SecurityUserWrapper(userId, "", false, false, false, false, Arrays.asList(), applicationUser), null));
    try {
        namespaceSecurityAdvice.checkPermission(joinPoint);
    } catch (AccessDeniedException e) {
        fail();
    }
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) AccessDeniedException(org.springframework.security.access.AccessDeniedException) MethodSignature(org.aspectj.lang.reflect.MethodSignature) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) Method(java.lang.reflect.Method) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) JoinPoint(org.aspectj.lang.JoinPoint) AbstractServiceTest(org.finra.herd.service.AbstractServiceTest) Test(org.junit.Test)

Aggregations

JoinPoint (org.aspectj.lang.JoinPoint)59 MethodSignature (org.aspectj.lang.reflect.MethodSignature)31 Method (java.lang.reflect.Method)30 Test (org.junit.Test)29 AccessDeniedException (org.springframework.security.access.AccessDeniedException)26 AbstractServiceTest (org.finra.herd.service.AbstractServiceTest)25 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)25 SecurityUserWrapper (org.finra.herd.model.dto.SecurityUserWrapper)22 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)21 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)14 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)11 Before (org.aspectj.lang.annotation.Before)5 CodeSignature (org.aspectj.lang.reflect.CodeSignature)4 Annotation (java.lang.annotation.Annotation)3 ArrayList (java.util.ArrayList)3 IView (com.yydcdut.note.views.IView)2 IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2