Outils pour utilisateurs

Outils du site


lang:java:aspectj

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
lang:java:aspectj [2016/12/12 23:57] – Ajout d'un commentaire pour l'utilisation combinée des annotations et sans annotation rootlang:java:aspectj [2020/04/26 22:39] (Version actuelle) – Conversion de <note> vers <WRAP> root
Ligne 1: Ligne 1:
-[[http://www.eclipse.org/aspectj/doc/next/progguide/printable.html|Manuel complet]]{{ :lang:java:aspectj:the_aspectjtm_programming_guide.html.maff |Archive}}+[[http://www.eclipse.org/aspectj/doc/next/progguide/printable.html|The AspectJTM Programming Guide]] {{ :lang:java:aspectj:the_aspectjtm_programming_guide_2020-04-26_10_31_44_pm_.html |Archive du 2003 le 26/04/2020}}
  
-[[https://eclipse.org/aspectj/doc/released/adk15notebook/printable.html#annotations|Complément Java 5]]{{ :lang:java:aspectj:the_aspectjtm_5_development_kit_developer_s_notebook.html.maff |Archive}}+[[https://eclipse.org/aspectj/doc/released/adk15notebook/printable.html|The AspectJTM Development Kit Developer's Notebook]] {{ :lang:java:aspectj:the_aspectjtm_5_development_kit_developer_s_notebook_2020-04-26_10_31_51_pm_.html |Archive du 2005 le 26/04/2020}}
  
 =====Déclaration d'un aspect===== =====Déclaration d'un aspect=====
 +Sans annotation
 <code java> <code java>
 public aspect Log { public aspect Log {
 +}
 +</code>
 +
 +Avec annotation
 +<code java>
 +import org.aspectj.lang.annotation.Aspect;
 +
 +@Aspect
 +public class Log {
 } }
 </code> </code>
  
 =====Déclaration d'un point de coupe===== =====Déclaration d'un point de coupe=====
 +Sans annotation
 <code java> <code java>
 pointcut evaluation(EventHandler h): target(h) && pointcut evaluation(EventHandler h): target(h) &&
   call(public void *.handleEvent());   call(public void *.handleEvent());
 +</code>
 +
 +La dénomination exacte pour trouver le nom d'un méthode est : ''package''.''classe''.''classes internes''.''methode''.
 +
 +
 +Avec annotation
 +<code java>
 +@Pointcut("call(void C.incI(int)) && args(x) && target(c)")
 +private void CoupureIncI(int x, C c) {
 +}
 </code> </code>
  
Ligne 39: Ligne 60:
 La méthode ''call'' est résolue à la compilation alors que la méthode ''execution'' est résolue à l'exécution. Pour que la méthode ''call'' marche, il faut de la classe appelante soit surveillée alors qu'avec la méthode ''execution'', seule la classe appelée doit être surveillée. La méthode ''call'' est résolue à la compilation alors que la méthode ''execution'' est résolue à l'exécution. Pour que la méthode ''call'' marche, il faut de la classe appelante soit surveillée alors qu'avec la méthode ''execution'', seule la classe appelée doit être surveillée.
  
-[[http://perfspy.blogspot.fr/2013/09/differences-between-aspectj-call-and.html|Source]]{{ :lang:java:aspectj:perfspy_differences_between_aspectj_call_and_execute_.html.maff |Archive}}+[[http://perfspy.blogspot.fr/2013/09/differences-between-aspectj-call-and.html|PerfSpy_ Differences between AspectJ call() and execute()]] {{ :lang:java:aspectj:perfspy_differences_between_aspectj_call_and_execute_2020-04-26_10_34_17_pm_.html |Archive du 02/09/2013 le 26/04/2020}}
  
    * ''(public void *.handleEvent())'' : la méthode à surveiller.    * ''(public void *.handleEvent())'' : la méthode à surveiller.
  
 Pour le constructeur, il utiliser la dénomination ''new''. Par exemple : ''C.new()''. Pour le constructeur, il utiliser la dénomination ''new''. Par exemple : ''C.new()''.
 +
 =====Déclaration d'un advice===== =====Déclaration d'un advice=====
 Utilisation d'un point de coupe (advice) : Utilisation d'un point de coupe (advice) :
Ligne 54: Ligne 76:
  
 <code java> <code java>
-around() : call(Display.update()) { if (! Display.disabled()) proceed();}+// Si la fonction update ne renvoie rien. 
 +around() : call(Display.update()) { 
 +  if (! Display.disabled()) 
 +    proceed(); 
 +
 + 
 +// Si la fonction update renvoie quelque chose. 
 +Object around() : call(Display.update()) { 
 +  if (! Display.disabled()) 
 +    return proceed(); 
 +  return null; 
 +}
 </code> </code>
 Ici, le point de coupe est directement directement défini dans l'advice. C'est à ''around'' de bien appeler ''proceed();'' qui permet l'exécution de la méthode ''Display.update()''. Ici, le point de coupe est directement directement défini dans l'advice. C'est à ''around'' de bien appeler ''proceed();'' qui permet l'exécution de la méthode ''Display.update()''.
  
-Exemple sans annotation : 
 <code java> <code java>
 +@Around("setAge(i)")
 +// Mettre void à la place de Object si la méthode setAge ne renvoie rien
 +public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint, int i) {
 +  // En cas de méthode static, pas besoin de joinPoint.getTarget().
 +  return thisJoinPoint.proceed(new Object[]{i*2, joinPoint.getTarget()}); //using Java 5 autoboxing
 +}
 +</code>
 +
 +Exemple sans annotation :
 +<file java Log.aj>
 public aspect Log { public aspect Log {
   pointcut evaluation(EventHandler h): target(h) &&   pointcut evaluation(EventHandler h): target(h) &&
     call(public void *.handleEvent());     call(public void *.handleEvent());
  
-  // Le pointcut et after ont la même signature. Je ne sais pas si c'est absolument +  // Le pointcut et after ont la même signature, sinon, ce n'est pas la peine 
-  // indispensable mais ça a toujours été le cas dans les exemples que j'ai trouvé.+  // de capturer des éléments dans le pointcut pour ne pas s'en servir.
   after(EventHandler h):evaluation(h) {   after(EventHandler h):evaluation(h) {
     System.out.println("Coucou");     System.out.println("Coucou");
   }   }
 } }
-</code>+</file>
  
-<code java>+<file java ProceedAspect.aj>
 public aspect ProceedAspect { public aspect ProceedAspect {
   pointcut setAge(int i): call(* setAge(..)) && args(i);   pointcut setAge(int i): call(* setAge(..)) && args(i);
  
   Object around(int i): setAge(i) {   Object around(int i): setAge(i) {
 +    // Les trois variables thisJoinPoint, thisJoinPointStaticPart et thisEnclosingJoinPointStaticPart
 +    // s'utilisent sans déclaration. Ce sont des mots clés.
     return proceed(i*2);     return proceed(i*2);
   }   }
 } }
-</code>+</file>
  
 Et en version annotation Java 5 : Et en version annotation Java 5 :
-<code java>+<file java Log.java>
 import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.After;
 import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
Ligne 89: Ligne 133:
 @Aspect @Aspect
 public class Log { public class Log {
-  @After("call(public void *.handleEvent()) && target(h)") +  @After("call(public void *.handleEvent())") 
-  public void LogHandleEvent(EventHandler h)+  // Chacun des champs est facultatif. 
 +  public void LogHandleEvent(JoinPoint thisJoinPoint, 
 +    JoinPoint.StaticPart thisJoinPointStaticPart, 
 +    JoinPoint.EnclosingStaticPart thisEnclosingJoinPointStaticPart)
   {   {
     System.out.println("Coucou");     System.out.println("Coucou");
   }   }
 } }
-</code>+</file>
  
-<code java>+<file java ProceedAspect.aj>
 @Aspect @Aspect
 public class ProceedAspect { public class ProceedAspect {
Ligne 105: Ligne 152:
  
   @Around("setAge(i)")   @Around("setAge(i)")
 +  // Dans le cas d'un Around, JoinPoint est en fait un ProceedingJoinPoint.
   public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint, int i) {   public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint, int i) {
-    return thisJoinPoint.proceed(new Object[]{i*2}); //using Java 5 autoboxing+    return thisJoinPoint.proceed(new Object[]{i*2});
   }   }
 } }
-</code>+</file>
  
-Séparation du point de coupure et de l'advice, il faut passer par une méthode vide. Pas pratique.+Séparation du point de coupure et de l'advice, il faut passer par une méthode vide. Pas pratique je trouve.
  
 <code java> <code java>
Ligne 132: Ligne 180:
 </code> </code>
  
 +=====Mots clés disponibles dans un advice=====
 +   * ''thisJoinPoint'' : that contains reflective information about the current join point for the advice to use,
 +   * ''thisJoinPointStaticPart'' : identique à ''thisJoinPoint.getStaticPart()''. Si seulement cette information est nécessaire, il est préférable de l'utiliser plutôt que de passer par ''thisJoinPoint'' pour des raisons de performance,
 +   * ''thisEnclosingJoinPointStaticPart'' : This only holds the static part of a join point, but only the enclosing join point.
 +
 +Exemple avec ''ProceedingJoinPoint'' (en annotation donc) :
 +  getArgs - 50 // Ici, un seul argument : un entier.
 +  getKind - method-call
 +  getTarget - test.C@30f39991
 +  getThis - null
 +  getSignature - void test.C.incI(int)
 +  toString - call(void test.C.incI(int))
 +  toLongString - call(void test.C.incI(int))
 +  toShortString - call(C.incI(..))
 +  getSourceLocation - C.java:18
 +
 +Exemple avec ''JoinPoint.StaticPart'' :
 +  getId - 0
 +  getKind - method-call
 +  toLongString - call(void test.C.incI(int))
 +  toShortString - call(C.incI(..))
 +  getClass - class org.aspectj.runtime.reflect.JoinPointImpl$StaticPartImpl
 +  getSignature - void test.C.incI(int)
 +  getSourceLocation - C.java:18
 +
 +Exemple avec ''JoinPoint.EnclosingStaticPart'' :
 +  getId - 1
 +  getKind - method-execution
 +  toLongString - execution(public static void test.C.main(java.lang.String[]))
 +  toShortString - execution(C.main(..))
 +  toString - execution(void test.C.main(String[]))
 +  getSignature - void test.C.main(String[])
 +  getSourceLocation - C.java:16
 =====Accéder aux champs privés===== =====Accéder aux champs privés=====
 Mot clé : ''privileged''. Cela n'existe pas en annotation et ne s'utilise pas en combiné avec l'annotation ''@Aspect''. Mot clé : ''privileged''. Cela n'existe pas en annotation et ne s'utilise pas en combiné avec l'annotation ''@Aspect''.
- 
-<note warning>Il est interdit de mélanger l'écriture de code avec annotation et sans annotation (cf [[http://stackoverflow.com/questions/25042972/aspectj-and-java8-bad-type-on-operand-stack|Source]], {{ :lang:java:aspectj:java_8_-_aspectj_and_java8_-_bad_type_on_operand_stack_-_stack_overflow.htm.maff |Archive}}). Il n'est donc pas possible d'utiliser la moindre annotation dans une classe nécessitant d'avoir des droits privilégiés.</note> 
  
 <code java> <code java>
Ligne 150: Ligne 229:
 </code> </code>
  
 +=====Mélange avec annotation et sans annotation=====
 +C'est niet.
 +
 +<WRAP center round alert 60%>
 +Il est interdit de mélanger l'écriture de code avec annotation et sans annotation. Il n'est donc pas possible d'utiliser la moindre annotation dans une classe nécessitant d'avoir des droits privilégiés.
 +
 +<cite>[[https://stackoverflow.com/questions/25042972/aspectj-and-java8-bad-type-on-operand-stack|java 8 - AspectJ and Java8 - bad type on operand stack - Stack Overflow]] {{ :lang:java:aspectj:java_8_-_aspectj_and_java8_-_bad_type_on_operand_stack_-_stack_overflow_2020-04-26_10_36_32_pm_.html |Archive du 30/07/2014 le 26/04/2020}}</cite>
 +</WRAP>
 +
 +<file java C.java>
 +public class C {
 +  public int i = 0;
 +
 +  void incI(int x) {
 +    System.out.println(i);
 +    i = i + x;
 +  }
 +
 +  static public void main(String[] args) {
 +    C c = new C();
 +    c.incI(50);
 +  }
 +}
 +</file>
 +
 +<file java A.aj>
 +import org.aspectj.lang.annotation.Around;
 +//import org.aspectj.lang.annotation.Aspect;
 +import org.aspectj.lang.annotation.Pointcut;
 +
 +//@Aspect
 +public aspect A {
 +  static final int MAX = 1000;
 +
 +  @Pointcut("call(void C.incI(int)) && args(x) && target(c)")
 +  private void CoupureIncI(int x, C c) {
 +  }
 +
 +  @Around("CoupureIncI(x, c)")
 +  public void incIA(int x, C c) {
 +    if (c.i + x > MAX)
 +      throw new RuntimeException();
 +  }
 +}
 +</file>
 +
 +Résultat à l'exécution :
 +  Error: A JNI error has occurred, please check your installation and try again
 +  Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
 +  Exception Details:
 +    Location:
 +      test/C.main([Ljava/lang/String;)V @21: invokestatic
 +    Reason:
 +      Type 'test/C' (current frame, stack[4]) is not assignable to integer
 +    Current Frame:
 +      bci: @21
 +      flags: { }
 +      locals: { '[Ljava/lang/String;', 'test/C', integer, 'test/C' }
 +      stack: { 'test/C', integer, 'test/A', integer, 'test/C', null }
 +    Bytecode:
 +      0x0000000: bb00 0159 b700 234c 2b10 323d 4e2d 1cb8
 +      0x0000010: 0036 1c2d 01b8 003a b1                 
 +  
 +        at java.lang.Class.getDeclaredMethods0(Native Method)
 +        at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
 +        at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
 +        at java.lang.Class.getMethod0(Class.java:3018)
 +        at java.lang.Class.getMethod(Class.java:1784)
 +        at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
 +        at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
 + 
 =====AspectJ en interne===== =====AspectJ en interne=====
 Le compilateur ajc utilise les points de coupe et les advices pour scanner le code source et trouver toutes les méthodes appelantes et les encadre avant par un appel des méthodes ''before'', et après par un appel des méthodes ''after''. Le compilateur ajc utilise les points de coupe et les advices pour scanner le code source et trouver toutes les méthodes appelantes et les encadre avant par un appel des méthodes ''before'', et après par un appel des méthodes ''after''.
  
-Code java :+====Exemple avec before==== 
 +===Code source===
 <file java C.java> <file java C.java>
 public class C { public class C {
Ligne 181: Ligne 332:
 </file> </file>
  
-Version décompilée avec l'advice sur la méthode ''call'' :+===Version décompilée avec l'advice sur la méthode ''call''===
 <file java C.java> <file java C.java>
 public class C public class C
Ligne 205: Ligne 356:
   public static void main(String[] arg)   public static void main(String[] arg)
   {   {
-    // Le code before entoure l'appel.+    // Le code before entoure chaque appel.
     C c = new C();     C c = new C();
     int j = 50;C localC1 = c;A.aspectOf().ajc$before$test_A$1$ff7f72c0(j, localC1);localC1.incI(j);     int j = 50;C localC1 = c;A.aspectOf().ajc$before$test_A$1$ff7f72c0(j, localC1);localC1.incI(j);
Ligne 260: Ligne 411:
 </file> </file>
  
-Version décompilée avec l'advice sur la méthode ''execution'' :+===Version décompilée avec l'advice sur la méthode ''execution''===
 <file java C.java> <file java C.java>
 public class C public class C
Ligne 278: Ligne 429:
   void incI(int x)   void incI(int x)
   {   {
-    // Le code before entoure la méthode surveillée+    // Le code before entoure l'intérieur de la méthode surveillée
     int j = x;A.aspectOf().ajc$before$test_A$1$74272596(j, this);this.i += x;     int j = x;A.aspectOf().ajc$before$test_A$1$74272596(j, this);this.i += x;
   }   }
Ligne 337: Ligne 488:
 } }
 </file> </file>
 +
 +====Exemple avec around====
 +===Code source===
 +<file java C.java>
 +public class C {
 +  public int i = 0;
 +
 +  void incI(int x) {
 +    System.out.println("incI" + i);
 +    i = i + x;
 +  }
 +
 +  void incI() {
 +    System.out.println("incI" + i);
 +    i = i + 1;
 +  }
 +
 +  static public void main(String[] arg) {
 +    C c = new C();
 +    c.incI(50);
 +    c.incI(500);
 +    c.incI(1500);
 +  }
 +}
 +</file>
 +
 +<file java A.aj>
 +public privileged aspect A {
 +  private static final int MAX = 1000;
 +
 +  void around(C c, int x): call(void C.incI(int)) && target(c) && args(x) {
 +    if (c.i + x > MAX)
 +      throw new RuntimeException();
 +    proceed(c, x);
 +  }
 +}
 +</file>
 +
 +<file java A2.aj>
 +import org.aspectj.lang.ProceedingJoinPoint;
 +import org.aspectj.lang.annotation.Around;
 +import org.aspectj.lang.annotation.Aspect;
 +import org.aspectj.lang.annotation.Pointcut;
 +
 +@Aspect
 +public class A2 {
 +  private static final int MAX = 1000;
 +
 +  @Pointcut("call(void C.incI(int)) && args(x)")
 +  private void CoupureIncI(int x) {
 +  }
 +
 +  @Around("CoupureIncI(x)")
 +  // AutourDeCoupureIncI renvoie le même type que C.incI.
 +  public void AutourDeCoupureIncI(ProceedingJoinPoint joinPoint, int x) throws Throwable {
 +    if (((C) joinPoint.getTarget()).i + x > MAX)
 +      throw new RuntimeException();
 +    // En cas de méthode static, pas besoin de joinPoint.getTarget().
 +    joinPoint.proceed(new Object[] { x, joinPoint.getTarget() });
 +  }
 +}
 +</file>
 +
 +===Version décompilée avec l'advice A seulement===
 +<file java C.java>
 +import java.io.PrintStream;
 +import org.aspectj.runtime.internal.AroundClosure;
 +
 +public class C
 +{
 +  public int i = 0;
 +  
 +  void incI(int x)
 +  {
 +    System.out.println("incI" + this.i);
 +    this.i += x;
 +  }
 +  
 +  private static final void incI_aroundBody1$advice(C target, int x, A ajc$aspectInstance, C c, int x, AroundClosure ajc$aroundClosure)
 +  {
 +    if (c.i + x > 1000) {
 +      throw new RuntimeException();
 +    }
 +    AroundClosure localAroundClosure = ajc$aroundClosure;int j = x;C localC = c;incI_aroundBody0(localC, j);
 +  }
 +  
 +  private static final void incI_aroundBody3$advice(C target, int x, A ajc$aspectInstance, C c, int x, AroundClosure ajc$aroundClosure)
 +  {
 +    if (c.i + x > 1000) {
 +      throw new RuntimeException();
 +    }
 +    AroundClosure localAroundClosure = ajc$aroundClosure;int j = x;C localC = c;incI_aroundBody2(localC, j);
 +  }
 +  
 +  private static final void incI_aroundBody5$advice(C target, int x, A ajc$aspectInstance, C c, int x, AroundClosure ajc$aroundClosure)
 +  {
 +    if (c.i + x > 1000) {
 +      throw new RuntimeException();
 +    }
 +    AroundClosure localAroundClosure = ajc$aroundClosure;int j = x;C localC = c;incI_aroundBody4(localC, j);
 +  }
 +  
 +  void incI()
 +  {
 +    System.out.println("incI" + this.i);
 +    this.i += 1;
 +  }
 +  
 +  private static final void incI_aroundBody0(C paramC, int paramInt)
 +  {
 +    paramC.incI(paramInt);
 +  }
 +  
 +  private static final void incI_aroundBody2(C paramC, int paramInt)
 +  {
 +    paramC.incI(paramInt);
 +  }
 +  
 +  public static void main(String[] arg)
 +  {
 +    C c = new C();
 +    int j = 50;C localC1 = c;incI_aroundBody1$advice(localC1, j, A.aspectOf(), localC1, j, null);
 +    int k = 500;C localC2 = c;incI_aroundBody3$advice(localC2, k, A.aspectOf(), localC2, k, null);
 +    int m = 1500;C localC3 = c;incI_aroundBody5$advice(localC3, m, A.aspectOf(), localC3, m, null);
 +  }
 +  
 +  private static final void incI_aroundBody4(C paramC, int paramInt)
 +  {
 +    paramC.incI(paramInt);
 +  }
 +}
 +</file>
 +
 +<file java A.aj>
 +import org.aspectj.internal.lang.annotation.ajcPrivileged;
 +import org.aspectj.lang.NoAspectBoundException;
 +import org.aspectj.lang.annotation.Around;
 +import org.aspectj.lang.annotation.Aspect;
 +import org.aspectj.runtime.internal.AroundClosure;
 +
 +@Aspect
 +@ajcPrivileged
 +public class A
 +{
 +  private static final int MAX = 1000;
 +  
 +  public static A aspectOf()
 +  {
 +    if (ajc$perSingletonInstance == null) {
 +      throw new NoAspectBoundException("test_A", ajc$initFailureCause);
 +    }
 +    return ajc$perSingletonInstance;
 +  }
 +  
 +  public static boolean hasAspect()
 +  {
 +    return ajc$perSingletonInstance != null;
 +  }
 +  
 +  static
 +  {
 +    try
 +    {
 +      
 +    }
 +    catch (Throwable localThrowable)
 +    {
 +      ajc$initFailureCause = localThrowable;
 +    }
 +  }
 +  
 +  @Around(value="(call(void C.incI(int)) && (target(c) && args(x)))", argNames="c,x,ajc$aroundClosure")
 +  public void ajc$around$test_A$1$ff7f72c0(C c, int x, AroundClosure ajc$aroundClosure)
 +  {
 +    if (c.i + x > 1000) {
 +      throw new RuntimeException();
 +    }
 +    ajc$around$test_A$1$ff7f72c0proceed(c, x, ajc$aroundClosure);
 +  }
 +}
 +</file>
 +
 +===Version décompilée avec l'advice A2 seulement===
 +<file java C.java>
 +import java.io.PrintStream;
 +import org.aspectj.lang.JoinPoint;
 +import org.aspectj.lang.JoinPoint.StaticPart;
 +import org.aspectj.lang.ProceedingJoinPoint;
 +import org.aspectj.runtime.internal.Conversions;
 +import org.aspectj.runtime.reflect.Factory;
 +
 +public class C
 +{
 +  private static void ajc$preClinit()
 +  {
 +    Factory localFactory = new Factory("C.java", C.class);ajc$tjp_0 = localFactory.makeSJP("method-call", localFactory.makeMethodSig("0", "incI", "test.C", "int", "x", "", "void"), 18);ajc$tjp_1 = localFactory.makeSJP("method-call", localFactory.makeMethodSig("0", "incI", "test.C", "int", "x", "", "void"), 19);ajc$tjp_2 = localFactory.makeSJP("method-call", localFactory.makeMethodSig("0", "incI", "test.C", "int", "x", "", "void"), 20);
 +  }
 +  
 +  public int i = 0;
 +  private static final JoinPoint.StaticPart ajc$tjp_0;
 +  private static final JoinPoint.StaticPart ajc$tjp_1;
 +  private static final JoinPoint.StaticPart ajc$tjp_2;
 +  
 +  void incI(int x)
 +  {
 +    System.out.println("incI" + this.i);
 +    this.i += x;
 +  }
 +  
 +  void incI()
 +  {
 +    System.out.println("incI" + this.i);
 +    this.i += 1;
 +  }
 +  
 +  private static final void incI_aroundBody0(C paramC, int paramInt, JoinPoint paramJoinPoint)
 +  {
 +    paramC.incI(paramInt);
 +  }
 +  
 +  private static final void incI_aroundBody2(C paramC, int paramInt, JoinPoint paramJoinPoint)
 +  {
 +    paramC.incI(paramInt);
 +  }
 +  
 +  public static void main(String[] arg)
 +  {
 +    C c = new C();
 +    int j = 50;C localC1 = c;JoinPoint localJoinPoint1 = Factory.makeJP(ajc$tjp_0, null, localC1, Conversions.intObject(j));incI_aroundBody1$advice(localC1, j, localJoinPoint1, A2.aspectOf(), (ProceedingJoinPoint)localJoinPoint1, j);
 +    int k = 500;C localC2 = c;JoinPoint localJoinPoint2 = Factory.makeJP(ajc$tjp_1, null, localC2, Conversions.intObject(k));incI_aroundBody3$advice(localC2, k, localJoinPoint2, A2.aspectOf(), (ProceedingJoinPoint)localJoinPoint2, k);
 +    int m = 1500;C localC3 = c;JoinPoint localJoinPoint3 = Factory.makeJP(ajc$tjp_2, null, localC3, Conversions.intObject(m));incI_aroundBody5$advice(localC3, m, localJoinPoint3, A2.aspectOf(), (ProceedingJoinPoint)localJoinPoint3, m);
 +  }
 +  
 +  private static final void incI_aroundBody4(C paramC, int paramInt, JoinPoint paramJoinPoint)
 +  {
 +    paramC.incI(paramInt);
 +  }
 +  
 +  private static final void incI_aroundBody1$advice(C target, int x, JoinPoint thisJoinPoint, A2 ajc$aspectInstance, ProceedingJoinPoint joinPoint, int x)
 +  {
 +    if (((C)joinPoint.getTarget()).i + x > 1000) {
 +      throw new RuntimeException();
 +    }
 +    Object[] arrayOfObject = { Integer.valueOf(x), joinPoint.getTarget() };ProceedingJoinPoint localProceedingJoinPoint = joinPoint;incI_aroundBody0(target, Conversions.intValue(arrayOfObject[0]), localProceedingJoinPoint);null;
 +  }
 +  
 +  private static final void incI_aroundBody3$advice(C target, int x, JoinPoint thisJoinPoint, A2 ajc$aspectInstance, ProceedingJoinPoint joinPoint, int x)
 +  {
 +    if (((C)joinPoint.getTarget()).i + x > 1000) {
 +      throw new RuntimeException();
 +    }
 +    Object[] arrayOfObject = { Integer.valueOf(x), joinPoint.getTarget() };ProceedingJoinPoint localProceedingJoinPoint = joinPoint;incI_aroundBody2(target, Conversions.intValue(arrayOfObject[0]), localProceedingJoinPoint);null;
 +  }
 +  
 +  private static final void incI_aroundBody5$advice(C target, int x, JoinPoint thisJoinPoint, A2 ajc$aspectInstance, ProceedingJoinPoint joinPoint, int x)
 +  {
 +    if (((C)joinPoint.getTarget()).i + x > 1000) {
 +      throw new RuntimeException();
 +    }
 +    Object[] arrayOfObject = { Integer.valueOf(x), joinPoint.getTarget() };ProceedingJoinPoint localProceedingJoinPoint = joinPoint;incI_aroundBody4(target, Conversions.intValue(arrayOfObject[0]), localProceedingJoinPoint);null;
 +  }
 +  
 +  static {}
 +}
 +</file>
 +
 +<file java A2.aj>
 +import org.aspectj.lang.NoAspectBoundException;
 +import org.aspectj.lang.ProceedingJoinPoint;
 +import org.aspectj.lang.annotation.Around;
 +import org.aspectj.lang.annotation.Aspect;
 +
 +@Aspect
 +public class A2
 +{
 +  private static final int MAX = 1000;
 +  private static Throwable ajc$initFailureCause;
 +  public static final A2 ajc$perSingletonInstance;
 +  
 +  static
 +  {
 +    try
 +    {
 +      ajc$postClinit();
 +    }
 +    catch (Throwable localThrowable)
 +    {
 +      ajc$initFailureCause = localThrowable;
 +    }
 +  }
 +  
 +  private static void ajc$postClinit()
 +  {
 +    ajc$perSingletonInstance = new A2();
 +  }
 +  
 +  public static boolean hasAspect()
 +  {
 +    return ajc$perSingletonInstance != null;
 +  }
 +  
 +  public static A2 aspectOf()
 +  {
 +    if (ajc$perSingletonInstance == null) {
 +      throw new NoAspectBoundException("test.A2", ajc$initFailureCause);
 +    }
 +    return ajc$perSingletonInstance;
 +  }
 +  
 +  @Around("CoupureIncI(x)")
 +  public void AutourDeCoupureIncI(ProceedingJoinPoint joinPoint, int x)
 +    throws Throwable
 +  {
 +    if (((C)joinPoint.getTarget()).i + x > 1000) {
 +      throw new RuntimeException();
 +    }
 +    joinPoint.proceed(new Object[] { Integer.valueOf(x), joinPoint.getTarget() });
 +  }
 +}
 +</file>
 +
 +<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'ordre d'écriture des méthodes n'est pas la même (et bien sûr l'utilisation de ''ProceedingJoinPoint'' pour une annotation ''around'' et ''JoinPoint'' pour ''before'' et ''after'').
 +</WRAP>
 +
 =====Héritage et interface===== =====Héritage et interface=====
 <code java> <code java>
Ligne 369: Ligne 845:
 } }
 </code> </code>
 +
 +=====Aspect à l'exécution=====
 +Pour cela, il faut un JAR contenant l'aspect, un JAR contenant la classe et un programme fusionnant les 2.
 +====La classe ====
 +Créez un ''Java project''.
 +<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);
 +  }
 +}
 +</file>
 +Puis en console allez dans le dossier ''bin'' et tapez :
 +<code bash>
 +jar cf C.jar classe
 +</code>
 +
 +====L'aspect====
 +Créez un ''AspectJ project''.
 +<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("Before");
 +    if (c.i + x > MAX)
 +      throw new RuntimeException();
 +  }
 +}
 +
 +/*
 +@Aspect
 +public class A {
 +  private static final int MAX = 1000;
 +
 +  @Around("execution(void C.incI(int)) && args(x)")
 +  // AutourDeCoupureIncI renvoie le même type que C.incI.
 +  public void AutourDeCoupureIncI(ProceedingJoinPoint joinPoint, int x) throws Throwable {
 +    System.out.println("début");
 +    if (((C) joinPoint.getTarget()).i + x > MAX)
 +      throw new RuntimeException();
 +
 +    joinPoint.proceed(new Object[] { x });
 +    System.out.println("Fin");
 +  }
 +}
 +*/
 +</file>
 +La classe dépendant de C, il ne faut pas oublie d'ajouter le chemin vers le premier projet pour que la classe compile : Menu ''Project|Properties'', catégorie ''Java Build Path'', onglet ''Projects'' puis ''Add'' et cochez la case du projet Java ''classe''.
 +
 +Puis en console allez dans le dossier ''bin'' et tapez :
 +<code bash>
 +jar cf A.jar aspects
 +</code>
 +
 +====La classe exécutrice====
 +Créez un ''AspectJ project''.
 +<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, NoSuchMethodException,
 +      SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
 +    try (WeavingURLClassLoader weaving = new WeavingURLClassLoader(
 +        new URL[] { new URL("file:///tmp/java/classe/bin/C.jar"), new URL("file:///tmp/java/aspects/bin/A.jar") },
 +        new URL[] { new URL("file:///tmp/java/aspects/bin/A.jar") },
 +        Thread.currentThread().getContextClassLoader())) {
 +      Thread.currentThread().setContextClassLoader(weaving);
 +
 +      Class<?> classC = weaving.loadClass("classe.C");
 +
 +      Method mainMethod = classC.getMethod("main", new Class[] { String[].class });
 +
 +      mainMethod.invoke(null, (Object)new String[] { "Start" });
 +    }
 +  }
 +}
 +</file>
 +
 +Pensez à ajouter le jar ''org.aspectj.weaver'' dans le ''Build Path'', onglet ''Libraries''.
 +
 +====Exécution====
 +  Start
 +  Before
 +  Before
 +  Exception in thread "main" java.lang.reflect.InvocationTargetException
 +    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 +    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 +    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 +    at java.lang.reflect.Method.invoke(Method.java:498)
 +    at main.Main.main(Main.java:24)
 +  Caused by: java.lang.RuntimeException
 +    at aspects.A.ajc$before$aspects_A$1$ff7f72c0(A.aj:18)
 +    at classe.C.main(C.java:16)
 +    ... 5 more
 +
 +====Commentaires====
 +Il est impératif que le projet ''main'' ne possède pas le projet ''classe'' dans son ''Build Path'', onglet ''Projects''. Sinon la réflexion ne passera pas par l'aspect.
 +
 +[[https://raw.githubusercontent.com/kilim/kilim/master/src/kilim/tools/Kilim.java|Kilim.java]], {{ :lang:java:aspectj:kilim.zip |Archive kilim.zip}}
lang/java/aspectj.1481583456.txt.gz · Dernière modification : 2016/12/12 23:57 de root