lang:java:aspectj
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| lang:java:aspectj [2016/12/15 22:52] – [Mots clés disponibles dans un advice] : Ajout d'un exemple pour comprendre l'utilité de chacune des 3 variables root | lang:java:aspectj [2020/04/26 22:39] (Version actuelle) – Conversion de <note> vers <WRAP> root | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | [[http:// | + | [[http:// |
| - | [[https:// | + | [[https:// |
| =====Déclaration d'un aspect===== | =====Déclaration d'un aspect===== | ||
| Ligne 25: | Ligne 25: | ||
| call(public void *.handleEvent()); | call(public void *.handleEvent()); | ||
| </ | </ | ||
| + | |||
| + | La dénomination exacte pour trouver le nom d'un méthode est : '' | ||
| + | |||
| Avec annotation | Avec annotation | ||
| Ligne 57: | Ligne 60: | ||
| La méthode '' | La méthode '' | ||
| - | [[http:// | + | [[http:// |
| * '' | * '' | ||
| Ligne 229: | Ligne 232: | ||
| C'est niet. | C'est niet. | ||
| - | <note warning>Il est interdit de mélanger l' | + | <WRAP center round alert 60%> |
| + | Il est interdit de mélanger l' | ||
| + | |||
| + | < | ||
| + | </WRAP> | ||
| <file java C.java> | <file java C.java> | ||
| Ligne 801: | Ligne 808: | ||
| } | } | ||
| </ | </ | ||
| - | <note>Il n'y a finalement pas de différence entre la méthode avec annotation et sans annotation, hormis que l' | + | |
| + | <WRAP center round info 60%> | ||
| + | Il n'y a finalement pas de différence entre la méthode avec annotation et sans annotation, hormis que l' | ||
| + | </WRAP> | ||
| =====Héritage et interface===== | =====Héritage et interface===== | ||
| <code java> | <code java> | ||
| Ligne 834: | Ligne 845: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | =====Aspect à l' | ||
| + | Pour cela, il faut un JAR contenant l' | ||
| + | ====La classe ==== | ||
| + | Créez un '' | ||
| + | <file java C.java> | ||
| + | package classe; | ||
| + | |||
| + | public class C { | ||
| + | public int i = 0; | ||
| + | |||
| + | public void incI(int x) { | ||
| + | i = i + x; | ||
| + | } | ||
| + | |||
| + | static public void main(String[] arg) { | ||
| + | for (String string : arg) { | ||
| + | System.out.println(string); | ||
| + | } | ||
| + | C c = new C(); | ||
| + | c.incI(50); | ||
| + | c.incI(1000); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | Puis en console allez dans le dossier '' | ||
| + | <code bash> | ||
| + | jar cf C.jar classe | ||
| + | </ | ||
| + | |||
| + | ====L' | ||
| + | Créez un '' | ||
| + | <file java A.aj> | ||
| + | package aspects; | ||
| + | |||
| + | import org.aspectj.lang.ProceedingJoinPoint; | ||
| + | import org.aspectj.lang.annotation.Around; | ||
| + | import org.aspectj.lang.annotation.Aspect; | ||
| + | import org.aspectj.lang.annotation.Pointcut; | ||
| + | |||
| + | import classe.C; | ||
| + | |||
| + | // Les deux implémentations marchent. | ||
| + | |||
| + | public aspect A { | ||
| + | static final int MAX = 1000; | ||
| + | |||
| + | before(int x, C c): call(void C.incI(int)) && target(c) && args(x) { | ||
| + | System.out.println(" | ||
| + | if (c.i + x > MAX) | ||
| + | throw new RuntimeException(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | /* | ||
| + | @Aspect | ||
| + | public class A { | ||
| + | private static final int MAX = 1000; | ||
| + | |||
| + | @Around(" | ||
| + | // AutourDeCoupureIncI renvoie le même type que C.incI. | ||
| + | public void AutourDeCoupureIncI(ProceedingJoinPoint joinPoint, int x) throws Throwable { | ||
| + | System.out.println(" | ||
| + | if (((C) joinPoint.getTarget()).i + x > MAX) | ||
| + | throw new RuntimeException(); | ||
| + | |||
| + | joinPoint.proceed(new Object[] { x }); | ||
| + | System.out.println(" | ||
| + | } | ||
| + | } | ||
| + | */ | ||
| + | </ | ||
| + | La classe dépendant de C, il ne faut pas oublie d' | ||
| + | |||
| + | Puis en console allez dans le dossier '' | ||
| + | <code bash> | ||
| + | jar cf A.jar aspects | ||
| + | </ | ||
| + | |||
| + | ====La classe exécutrice==== | ||
| + | Créez un '' | ||
| + | <file java Main.java> | ||
| + | package main; | ||
| + | |||
| + | import java.io.IOException; | ||
| + | import java.lang.reflect.InvocationTargetException; | ||
| + | import java.lang.reflect.Method; | ||
| + | import java.net.URL; | ||
| + | |||
| + | import org.aspectj.weaver.loadtime.WeavingURLClassLoader; | ||
| + | |||
| + | public class Main { | ||
| + | static public void main(String[] arg) throws ClassNotFoundException, | ||
| + | SecurityException, | ||
| + | try (WeavingURLClassLoader weaving = new WeavingURLClassLoader( | ||
| + | new URL[] { new URL(" | ||
| + | new URL[] { new URL(" | ||
| + | Thread.currentThread().getContextClassLoader())) { | ||
| + | Thread.currentThread().setContextClassLoader(weaving); | ||
| + | |||
| + | Class<?> | ||
| + | |||
| + | Method mainMethod = classC.getMethod(" | ||
| + | |||
| + | mainMethod.invoke(null, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Pensez à ajouter le jar '' | ||
| + | |||
| + | ====Exécution==== | ||
| + | Start | ||
| + | Before | ||
| + | Before | ||
| + | Exception in thread " | ||
| + | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | ||
| + | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: | ||
| + | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: | ||
| + | at java.lang.reflect.Method.invoke(Method.java: | ||
| + | at main.Main.main(Main.java: | ||
| + | Caused by: java.lang.RuntimeException | ||
| + | at aspects.A.ajc$before$aspects_A$1$ff7f72c0(A.aj: | ||
| + | at classe.C.main(C.java: | ||
| + | ... 5 more | ||
| + | |||
| + | ====Commentaires==== | ||
| + | Il est impératif que le projet '' | ||
| + | |||
| + | [[https:// | ||
lang/java/aspectj.1481838777.txt.gz · Dernière modification : (modification externe)
