Soyez le premier à donner votre avis sur cette source.
Vue 4 404 fois - Téléchargée 179 fois
package ccm.kx.aop; /** * @author KX */ public class MathUtil { public static long factorial(int n) { if (n < 0) throw new IllegalArgumentException("Can't compute " + n + "!"); return n > 1 ? n * factorial(n - 1) : 1; } public static void main(String[] args) { System.out.println("3! = " + factorial(3)); try { System.out.println("-2! = " + factorial(-2)); } catch (RuntimeException e) { System.err.println("-2! throws an error"); } } }
3! = 6 -2! throws an error
déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsBefore INFOS: void ccm.kx.aop.MathUtil.main(String[]) starts with [[Ljava.lang.String;@1234567] params déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsBefore INFOS: long ccm.kx.aop.MathUtil.factorial(int) starts with [3] params déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsBefore INFOS: long ccm.kx.aop.MathUtil.factorial(int) starts with [2] params déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsBefore INFOS: long ccm.kx.aop.MathUtil.factorial(int) starts with [1] params déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAfterReturning INFOS: long ccm.kx.aop.MathUtil.factorial(int) finishes with [1] params and returns 1 déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAfterReturning INFOS: long ccm.kx.aop.MathUtil.factorial(int) finishes with [2] params and returns 2 déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAfterReturning INFOS: long ccm.kx.aop.MathUtil.factorial(int) finishes with [3] params and returns 6 3! = 6 déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsBefore INFOS: long ccm.kx.aop.MathUtil.factorial(int) starts with [-2] params déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAfterThrowing AVERTISSEMENT: long ccm.kx.aop.MathUtil.factorial(int) fails with [-2] params java.lang.IllegalArgumentException: Can t compute -2! at ccm.kx.aop.MathUtil.factorial(MathUtil.java:10) at ccm.kx.aop.MathUtil.main(MathUtil.java:17) -2! throws an error déc. 15, 2018 1:23:46 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAfterReturning INFOS: void ccm.kx.aop.MathUtil.main(String[]) finishes with [[Ljava.lang.String;@1234567] params and returns null
package ccm.kx.aop; import java.util.*; import java.util.logging.*; import org.aspectj.lang.*; import org.aspectj.lang.annotation.*; /** * @author KX */ @Aspect public class AutomaticLogsAspect { private static final Level LEVEL_BEFORE = Level.INFO; private static final Level LEVEL_AFTER_RETURNING = Level.INFO; private static final Level LEVEL_AFTER_THROWING = Level.WARNING; @Pointcut("execution(* *(..))") // all methods public void allMethodsPointcut() { } @Before("allMethodsPointcut()") public void automaticLogsAllMethodsBefore(JoinPoint joinPoint) throws Throwable { Logger logger = Logger.getLogger(joinPoint.getSourceLocation().getWithinType().getName()); if (logger.isLoggable(LEVEL_BEFORE)) { logger.log(LEVEL_BEFORE, joinPoint.getSignature() + " starts with " + Arrays.toString(joinPoint.getArgs()) + " params"); } } @AfterReturning(pointcut = "allMethodsPointcut()", returning = "result") public void automaticLogsAllMethodsAfterReturning(JoinPoint joinPoint, Object result) throws Throwable { Logger logger = Logger.getLogger(joinPoint.getSourceLocation().getWithinType().getName()); if (logger.isLoggable(LEVEL_AFTER_RETURNING)) { logger.log(LEVEL_AFTER_RETURNING, joinPoint.getSignature() + " finishes with " + Arrays.toString(joinPoint.getArgs()) + " params and returns " + result); } } @AfterThrowing(pointcut = "allMethodsPointcut()", throwing = "exception") public void automaticLogsAllMethodsAfterThrowing(JoinPoint joinPoint, Throwable exception) throws Throwable { Logger logger = Logger.getLogger(joinPoint.getSourceLocation().getWithinType().getName()); if (logger.isLoggable(LEVEL_AFTER_THROWING)) { logger.log(LEVEL_AFTER_THROWING, joinPoint.getSignature() + " fails with " + Arrays.toString(joinPoint.getArgs()) + " params", exception); } } }
<properties> <java.source.version>1.5</java.source.version> <aspectj.version>1.9.2</aspectj.version> </properties> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.11</version> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> <configuration> <source>${java.source.version}</source> <target>${java.source.version}</target> <complianceLevel>${java.source.version}</complianceLevel> </configuration> <executions> <execution> <goals> <goal>compile</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
package ccm.kx.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint.StaticPart; import org.aspectj.lang.Signature; import org.aspectj.runtime.internal.Conversions; import org.aspectj.runtime.reflect.Factory; public class MathUtil { private static StaticPart ajc$tjp_0; private static StaticPart ajc$tjp_1; static { ajc$preClinit(); } private static void ajc$preClinit() { final Factory factory = new Factory("MathUtil.java", MathUtil.class); ajc$tjp_0 = factory.makeSJP("method-execution", factory.makeMethodSig("9", "factorial", "ccm.kx.aop.MathUtil", "int", "n", "", "long"), 8); ajc$tjp_1 = factory.makeSJP("method-execution", factory.makeMethodSig("9", "main", "ccm.kx.aop.MathUtil", "[Ljava.lang.String;", "args", "", "void"), 14); } public static long factorial(final int n) { final JoinPoint jp = Factory.makeJP(MathUtil.ajc$tjp_0, null, null, Conversions.intObject(n)); try { AutomaticLogsAspect.aspectOf().automaticLogsAllMethodsBefore(jp); if (n < 0) { throw new IllegalArgumentException("Can't compute " + n + "!"); } final long n2 = (n > 1) ? (n * factorial(n - 1)) : 1L; AutomaticLogsAspect.aspectOf().automaticLogsAllMethodsAfterReturning(jp, Conversions.longObject(n2)); return n2; } catch (Throwable exception) { AutomaticLogsAspect.aspectOf().automaticLogsAllMethodsAfterThrowing(jp, exception); throw exception; } } public static void main(String args[]) { final JoinPoint joinpoint = Factory.makeJP(ajc$tjp_1, null, null, args); try { AutomaticLogsAspect.aspectOf().automaticLogsAllMethodsBefore(joinpoint); System.out.println((new StringBuilder("3! = ")).append(factorial(3)).toString()); try { System.out.println((new StringBuilder("-2! = ")).append(factorial(-2)).toString()); } catch (RuntimeException _ex) { System.err.println("-2! throws an error"); } AutomaticLogsAspect.aspectOf().automaticLogsAllMethodsAfterReturning(joinpoint, null); return; } catch (Throwable throwable) { AutomaticLogsAspect.aspectOf().automaticLogsAllMethodsAfterThrowing(joinpoint, throwable); throw throwable; } } }
@Around("allMethodsPointcut()") // Do not use with @Before or @After public Object automaticLogsAllMethodsAround(ProceedingJoinPoint joinPoint) throws Throwable { Logger logger = Logger.getLogger(joinPoint.getSourceLocation().getWithinType().getName()); if (logger.isLoggable(LEVEL_BEFORE)) { logger.log(LEVEL_BEFORE, joinPoint.getSignature() + " starts with " + Arrays.toString(joinPoint.getArgs()) + " params"); } Object result = null; Throwable throwable = null; double time = System.nanoTime(); try { result = joinPoint.proceed(); } catch (Throwable t) { throwable = t; } finally { time = System.nanoTime() - time; } if (throwable != null) { if (logger.isLoggable(LEVEL_AFTER_THROWING)) { logger.log(LEVEL_AFTER_THROWING, joinPoint.getSignature() + " fails with " + Arrays.toString(joinPoint.getArgs()) + " params after " + time / 1000000 + "ms", throwable); } throw throwable; } else { if (logger.isLoggable(LEVEL_AFTER_RETURNING)) { logger.log(LEVEL_AFTER_RETURNING, joinPoint.getSignature() + " finishes with " + Arrays.toString(joinPoint.getArgs()) + " params after " + time / 1000000 + "ms and returns " + result); } return result; } }
déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround INFOS: void ccm.kx.aop.MathUtil.main(String[]) starts with [[Ljava.lang.String;@1234567] params déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround INFOS: long ccm.kx.aop.MathUtil.factorial(int) starts with [3] params déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround INFOS: long ccm.kx.aop.MathUtil.factorial(int) starts with [2] params déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround INFOS: long ccm.kx.aop.MathUtil.factorial(int) starts with [1] params déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround INFOS: long ccm.kx.aop.MathUtil.factorial(int) finishes with [1] params after 0.04ms and returns 1 déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround INFOS: long ccm.kx.aop.MathUtil.factorial(int) finishes with [2] params after 8.55ms and returns 2 déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround INFOS: long ccm.kx.aop.MathUtil.factorial(int) finishes with [3] params after 17.00ms and returns 6 3! = 6 déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround INFOS: long ccm.kx.aop.MathUtil.factorial(int) starts with [-2] params déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround AVERTISSEMENT: long ccm.kx.aop.MathUtil.factorial(int) fails with [-2] params after 0.08ms java.lang.IllegalArgumentException: Can t compute -2! at ccm.kx.aop.MathUtil.factorial_aroundBody0(MathUtil.java:10) at ccm.kx.aop.MathUtil$AjcClosure1.run(MathUtil.java:1) at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:149) at ccm.kx.aop.AutomaticLogsAspect.automaticLogsAllMethodsAround(AutomaticLogsAspect.java:56) at ccm.kx.aop.MathUtil.factorial(MathUtil.java:8) at ccm.kx.aop.MathUtil.main_aroundBody2(MathUtil.java:17) at ccm.kx.aop.MathUtil$AjcClosure3.run(MathUtil.java:1) at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:149) at ccm.kx.aop.AutomaticLogsAspect.automaticLogsAllMethodsAround(AutomaticLogsAspect.java:56) at ccm.kx.aop.MathUtil.main(MathUtil.java:14) -2! throws an error déc. 15, 2018 1:23:45 PM ccm.kx.aop.AutomaticLogsAspect automaticLogsAllMethodsAround INFOS: void ccm.kx.aop.MathUtil.main(String[]) finishes with [[Ljava.lang.String;@1234567] params after 49.98ms and returns null
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.