2012-03-29 4 views

答えて

0

ポイントカット定義でif()を使用できます。

私は、次のパッケージ構造とEclipseで新しいAspectJのプロジェクトを作成しました:

package aspectj; 

@Aspect 
public class MyAspect { 

    public static boolean call = true; 
    public static boolean exec = true; 

    @Pointcut("if() && call(* *.*(..)) && !within(aspectj..*)") 
    public static boolean callPointcut(JoinPoint jp) { 
     return call; 
    } 

    @Pointcut("if() && execution(* *.*(..)) && !within(aspectj..*)") 
    public static boolean execPointcut(JoinPoint jp) { 
     return exec; 
    } 

    @After("execPointcut(jp) || callPointcut(jp)") 
    public void advice(JoinPoint jp){ 
     System.out.println(jp); 
     } 
} 

そしてGUI.java:

Package structure

MyAspect.ajは、次のコードを持っています

package main; 

public class GUI { 

    private JFrame frame; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        GUI window = new GUI(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public GUI() { 
     initialize(); 
    } 

    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 328, 109); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     final JCheckBox chckbxCall = new JCheckBox("Call"); 
     chckbxCall.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       MyAspect.call = chckbxCall.isSelected(); 
      } 
     }); 
     chckbxCall.setSelected(true); 
     chckbxCall.setBounds(6, 6, 128, 23); 
     frame.getContentPane().add(chckbxCall); 

     final JCheckBox chckbxExecution = new JCheckBox("Execution"); 
     chckbxExecution.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       MyAspect.exec = chckbxExecution.isSelected(); 
      } 
     }); 
     chckbxExecution.setSelected(true); 
     chckbxExecution.setBounds(6, 41, 128, 23); 
     frame.getContentPane().add(chckbxExecution); 

     JButton btnTestbutton = new JButton("testButton"); 
     btnTestbutton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       System.out.println("button pressed"); 
      } 
     }); 
     btnTestbutton.setBounds(172, 5, 117, 29); 
     frame.getContentPane().add(btnTestbutton); 
    } 
} 

希望の証拠として役立つ希望概念。

+0

こんにちは、ありがとうございます。私はそれをやろうとしましたが、チェックボックスを選択すると何も起こりません。私はロギングの出力を得ることはありません。ところで、チェックボックスのコードはどこに置くべきですか?それは側面にありますか? – user1082494

+0

完全に機能的な例を使って答えを更新しました。 – Fred

+0

こんにちはフレッド。機能コードを手伝ってくれてありがとう。これは私をたくさん助けます。しかし、私はこれを外部のjarファイルに適用したい。 EclipseのConfigure Build Pathオプションを使用してインポートする外部jarファイルで.ajファイルを織りたいとします。このコードはそこにも適用されますか?もしそうなら、それを私に見せてもらえますか? – user1082494

関連する問題