@Override public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { SourceLocation sloc = AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(); assertEquals("Same source location must be returned on subsequent requests", sloc, AbstractAspectJAdvice.currentJoinPoint().getSourceLocation()); assertEquals(TestBean.class, sloc.getWithinType()); try { sloc.getLine(); fail("Can't get line number"); } catch (UnsupportedOperationException ex) { // Expected } try { sloc.getFileName(); fail("Can't get file name"); } catch (UnsupportedOperationException ex) { // Expected } } });
@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()); } });
@Override public SourceLocation getSourceLocation() { return this.joinpoint.getSourceLocation(); }
@Override public SourceLocation getSourceLocation() { return this.joinpoint.getSourceLocation(); }
@Aspect public class TraceAspect { @Pointcut("call(* java.io.PrintStream.*(..))") public void sysoutPointcut() { } @Pointcut("within(demo..*)") public void packagePointcut() { } @Before("sysoutPointcut() && packagePointcut()") public void beforeSysoutCallInsideDemoPackage(JoinPoint joinPoint) { System.out.print(joinPoint.getThis().getClass().getName() + ":" + joinPoint.getSourceLocation().getLine() + " - "); } }
/** * Checks that the given object is not null. Throws a {@link ConstraintViolationException} on failure. * * @param messageTemplate The message to use in the exception if needed. * @param paramIndex The index of the parameter under investigation (if applicable). * @param object The object to examine. * @param joinPoint The join point. */ private static void assertNonNull(String messageTemplate, Integer paramIndex, Object object, JoinPoint joinPoint) { if (object != null) { return; } final StringBuilder message; if (paramIndex != null) { message = new StringBuilder(String.format(messageTemplate, paramIndex)); } else { message = new StringBuilder(messageTemplate); } final String ls = System.lineSeparator(); message.append(ls).append("Method: ").append(joinPoint.getSignature().toLongString()).append(ls); if (joinPoint.getSourceLocation() != null) { message.append("Line: ").append(joinPoint.getSourceLocation().getLine()); } throw new IllegalArgumentException(message.toString()); }
/** * Logs method exception. * @param joinPoint JoinPoint automatically filled by aspectj */ @AfterThrowing(value = "@annotation(pl.touk.framework.logging.aop.LogMethodExceptionError)", throwing = "throwable") public void logMethodExceptionError(JoinPoint joinPoint, Throwable throwable) { Log log = this.logGetter.getLog(joinPoint); if (log.isErrorEnabled()) { StringBuilder sb = new StringBuilder(); sb.append("exception ---------------------\n"); sb.append(" method: ").append(joinPoint.getSignature().getName()).append("\n"); sb.append(" at: ").append(joinPoint.getSourceLocation().getWithinType()).append("\n"); sb.append(" message: ").append(throwable.getMessage()).append("\n"); sb.append(" ---------------------"); log.error(sb.toString(), throwable); } }
/** * Logs exit from annotated method. * @param joinPoint JoinPoint automatically filled by aspectj */ @AfterReturning(value = "@annotation(pl.touk.framework.logging.aop.LogMethodExitInfo)", returning = "retValue") public void logMethodExitInfo(JoinPoint joinPoint, Object retValue) { Log log = this.logGetter.getLog(joinPoint); if (log.isInfoEnabled()) { StringBuilder sb = new StringBuilder(); sb.append("leaving ---------------------\n"); sb.append(" method: ").append(joinPoint.getSignature().getName()).append("\n"); sb.append(" at: ").append(joinPoint.getSourceLocation().getWithinType()).append("\n"); sb.append(" value: ").append(retValue).append("\n"); sb.append(" ---------------------"); log.info(sb.toString()); } }
message.append(ls).append("Method: ").append(joinPoint.getSignature().toLongString()).append(ls); if (joinPoint.getSourceLocation() != null) { message.append("Line: ").append(joinPoint.getSourceLocation().getLine());
message.append(ls).append("Method: ").append(joinPoint.getSignature().toLongString()).append(ls); if (joinPoint.getSourceLocation() != null) { message.append("Line: ").append(joinPoint.getSourceLocation().getLine());
Class<?> type = jp.getSourceLocation().getWithinType(); this.classLoadHelper.processStaticInitialization(type);
private static String getLocation(final JoinPoint joinpoint) { SourceLocation loc = joinpoint.getSourceLocation(); return " (" + loc.getFileName() + ":" + loc.getLine() + ")"; }
sb.append(" at: ").append(joinPoint.getSourceLocation().getWithinType()).append("\n");
/** * Before outputting the message inject additional context. * * @param joinPoint The <code>JoinPoint</code>. */ @Before("call(* com.arpnetworking.steno.LogBuilder.log())") public void addToContextLineAndMethod(final JoinPoint joinPoint) { final SourceLocation sourceLocation = joinPoint.getSourceLocation(); final LogBuilder targetLogBuilder = (LogBuilder) joinPoint.getTarget(); targetLogBuilder.addContext("line", String.valueOf(sourceLocation.getLine())); targetLogBuilder.addContext("file", sourceLocation.getFileName()); targetLogBuilder.addContext("class", sourceLocation.getWithinType()); } }
public OperationTemplateCacheKey(JoinPoint jp) { Signature sig = jp.getSignature(); this.signatureName = sig.getName(); this.declaringTypeName = OperationCollectionUtil.resolveDeclaringTypeName(jp); this.paramTypes = (sig instanceof CodeSignature) ? ((CodeSignature) sig).getParameterTypes() : ArrayUtil.EMPTY_CLASSES; SourceLocation loc = jp.getSourceLocation(); this.line = loc.getLine(); this.withinTypeName = loc.getWithinType().getName(); this.fileName = loc.getFileName(); this.hashCode = makeHashCode(); }
@AfterReturning(pointcut = "call(*.new(..))", returning = "obj", argNames = "jp,obj") public void afterAllocation(final JoinPoint jp, final Object obj) { if (RECORD_OBJECT_SIZE) { RECORDER.getRecorder(jp.getSourceLocation().getWithinType()). record(InstrumentationHelper.getObjectSize(obj)); } else { RECORDER.getRecorder(jp.getSourceLocation().getWithinType()).record(1); } } }
public static final JoinPoint createMockJoinPoint (Method method) { SourceLocation source=createMockSourceLocation(method); Signature sig=createMockMethodSignature(method); JoinPoint.StaticPart staticPart=Mockito.mock(JoinPoint.StaticPart.class); Mockito.when(staticPart.getSignature()).thenReturn(sig); Mockito.when(staticPart.getSourceLocation()).thenReturn(source); JoinPoint jp=Mockito.mock(JoinPoint.class); Mockito.when(jp.getStaticPart()).thenReturn(staticPart); Mockito.when(jp.getSignature()).thenReturn(sig); Mockito.when(jp.getSourceLocation()).thenReturn(source); return jp; }