/** * Gets parameter types of the join point. * * @param joinPoint the join point * @return the parameter types for the method this object * represents */ public static Class[] getParameterTypes(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); return method.getParameterTypes(); }
@Override public Method getMethod() { Signature signature = jp.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; return methodSignature.getMethod(); }
@Aspect public class MyLogger { private Logger log = Logger.getLogger(getClass()); @After("execution(* com.example.web.HomeController.*(..))") public void log(JoinPoint point) { log.info(point.getSignature().getName() + " called..."); } }
public static String getMethodBody(JoinPoint pjp) { StringBuilder methodName = new StringBuilder(pjp.getSignature().getName()).append("("); MethodSignature signature = (MethodSignature) pjp.getSignature(); String[] names = signature.getParameterNames(); Class[] args = signature.getParameterTypes(); for (int i = 0, len = args.length; i < len; i++) { if (i != 0) { methodName.append(","); } methodName.append(args[i].getSimpleName()).append(" ").append(names[i]); } return methodName.append(")").toString(); }
@Before("execution(void *.additionalMethod(*))") public void log(JoinPoint jp) { System.out.println("Before " + jp.getSignature().getName()); } }
@Before("webLog()") public void doBefore(JoinPoint joinPoint) { // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 LOGGER.info("URL : " + request.getRequestURL().toString() + ",IP : " + request.getRemoteAddr() + ",CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName() + ",ARGS : " + Arrays.toString(joinPoint.getArgs())); }
public static Map<String, Object> getArgsMap(JoinPoint pjp) { MethodSignature signature = (MethodSignature) pjp.getSignature(); Map<String, Object> args = new LinkedHashMap<>(); String names[] = signature.getParameterNames(); for (int i = 0, len = names.length; i < len; i++) { args.put(names[i], pjp.getArgs()[i]); } return args; } }
public void checkAndDeleteCache(JoinPoint jp, Object retVal) throws Throwable { Signature signature = jp.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method.isAnnotationPresent(CacheDelete.class)) { CacheDelete cacheDelete = method.getAnnotation(CacheDelete.class); this.deleteCache(jp, cacheDelete, retVal); } }
public static <T extends Annotation> T findAnnotation(JoinPoint pjp, Class<T> annClass) { MethodSignature signature = (MethodSignature) pjp.getSignature(); Method m = signature.getMethod(); Class<?> targetClass = pjp.getTarget().getClass(); return findAnnotation(targetClass, m, annClass); }
public void needsJoinPoint(JoinPoint tjp) { this.collaborator.needsJoinPoint(tjp.getSignature().getName()); }
private static Method getAjcMethodFromTarget(JoinPoint joinPoint) { return getAjcMethodAroundAdvice(joinPoint.getTarget().getClass(), (MethodSignature) joinPoint.getSignature()); }
/** * Gets a {@link Method} object from target object (not proxy class). * * @param joinPoint the {@link JoinPoint} * @return a {@link Method} object or null if method doesn't exist or if the signature at a join point * isn't sub-type of {@link MethodSignature} */ public static Method getMethodFromTarget(JoinPoint joinPoint) { Method method = null; if (joinPoint.getSignature() instanceof MethodSignature) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); method = getDeclaredMethod(joinPoint.getTarget().getClass(), signature.getName(), getParameterTypes(joinPoint)); } return method; }
private static void exitMethod(JoinPoint joinPoint, Object result, long lengthMillis) { if (!enabled) return; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { Trace.endSection(); } Signature signature = joinPoint.getSignature(); Class<?> cls = signature.getDeclaringType(); String methodName = signature.getName(); boolean hasReturnType = signature instanceof MethodSignature && ((MethodSignature) signature).getReturnType() != void.class; StringBuilder builder = new StringBuilder("\u21E0 ") .append(methodName) .append(" [") .append(lengthMillis) .append("ms]"); if (hasReturnType) { builder.append(" = "); builder.append(Strings.toString(result)); } Log.v(asTag(cls), builder.toString()); }
/** * Performs the method interception of the before advice at the specified joint point. * * @param aJoinPoint The joint point to intercept. * @throws Throwable If an error occurs berforming the method invocation. */ protected void performBeforeInterception(JoinPoint aJoinPoint) throws Throwable { if (log.isTraceEnabled()) log.trace("#### Invoking a method decorated with a Shiro annotation" + "\n\tkind : " + aJoinPoint.getKind() + "\n\tjoinPoint : " + aJoinPoint + "\n\tannotations: " + Arrays.toString(((MethodSignature) aJoinPoint.getSignature()).getMethod().getAnnotations()) + "\n\ttarget : " + aJoinPoint.getTarget() ); // 1. Adapt the join point into a method invocation BeforeAdviceMethodInvocationAdapter mi = BeforeAdviceMethodInvocationAdapter.createFrom(aJoinPoint); // 2. Delegate the authorization of the method call to the super class super.invoke(mi); } }
Method correspondingGetter = getGetterFromSetter(((MethodSignature) jp.getSignature()).getMethod()); boolean modified = true; if (correspondingGetter != null) {
@AfterReturning(value = "anyPublicOperation() && execCommandOperation()", returning = "result") public void logAfter(JoinPoint joinPoint, boolean result) { Object[] args = joinPoint.getArgs(); OperationRecord record = new OperationRecord(); record.setOperator(getUsername()); record.setType(joinPoint.getSignature().getName()); record.setGroupName(args[0].toString()); record.setCommand(JSON.toJSONString(args[1])); int status = result ? 1 : 0; record.setStatus((byte) status); if (recordMapper == null) { LoggerUtil.accessLog(JSON.toJSONString(record)); } else { recordMapper.insertSelective(record); } }
MethodInvocationAdapter(JoinPoint jp) { this.jp = (ProceedingJoinPoint) jp; if (jp.getTarget() != null) { target = jp.getTarget(); } else { // SEC-1295: target may be null if an ITD is in use target = jp.getSignature().getDeclaringType(); } String targetMethodName = jp.getStaticPart().getSignature().getName(); Class<?>[] types = ((CodeSignature) jp.getStaticPart().getSignature()) .getParameterTypes(); Class<?> declaringType = jp.getStaticPart().getSignature().getDeclaringType(); method = findMethod(targetMethodName, declaringType, types); if (method == null) { throw new IllegalArgumentException( "Could not obtain target method from JoinPoint: '" + jp + "'"); } }
@Override public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { // makeEncSJP, although meant for computing the enclosing join point, // it serves our purpose here JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method); JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString()); assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString()); assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString()); assertEquals(aspectJVersionJp.toLongString(), jp.toLongString()); assertEquals(aspectJVersionJp.toShortString(), jp.toShortString()); assertEquals(aspectJVersionJp.toString(), jp.toString()); } });
assertSame(target, raw); assertSame(method.getName(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getName()); assertEquals(method.getModifiers(), AbstractAspectJAdvice.currentJoinPoint().getSignature().getModifiers()); MethodSignature msig = (MethodSignature) AbstractAspectJAdvice.currentJoinPoint().getSignature(); assertSame("Return same MethodSignature repeatedly", msig, AbstractAspectJAdvice.currentJoinPoint().getSignature()); assertSame("Return same JoinPoint repeatedly", AbstractAspectJAdvice.currentJoinPoint(), AbstractAspectJAdvice.currentJoinPoint()); assertEquals(method.getDeclaringClass(), msig.getDeclaringType());
@Override public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart(); assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart()); assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind()); assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature()); assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation()); } });