C
Conrad Eaglehill
Hi,
I'm trying to use reflection with AspectJ to identify when a needed
aspect is not in the build. For example, if I have two aspects
MenuBarAspect (with a variable JMenuBar Menubar) and EditMenuAspect
(with a variable JMenu Edit), and I remove MenuBarAspect from the
build, I don't want a compilation error when code within
EditMenuAspect tries to call Menubar.add(...).
The example that was given to me worked perfectly from another article
in this newsgroup (which was much appreciated, by the way!), works
perfectly in every way expected, and the code I'm using below is based
on it:
--------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Notepad extends JFrame {
JScrollPane scrollpane;
public Notepad(){
setSize(800,600);
Container Notepad.cp = getContentPane();
cp.add(n.textArea = new JTextArea());
cp.add(n.scrollpane = new JScrollPane(n.textArea));
center.nCenter();
}
public static void main(String[] args){
new Notepad();
}
}
--------------------------------------
import javax.swing.*;
public aspect MenuBarAspect {
JMenuBar Notepad.Menubar = null;
pointcut menubar() : set(* Notepad.scrollpane);
before() : menubar() {
Notepad n = (Notepad) thisJoinPoint.getTarget();
if (n.Menubar == null)
n.setJMenuBar(n.Menubar = new JMenuBar());
}
}
--------------------------------------
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public aspect EditMenuAspect {
JMenu Notepad.ediT = null;
pointcut editMenu() : call(void Notepad.setJMenuBar(..)) &&
if(check(Notepad.class, "Menubar",
JMenuBar.class));
after() : editMenu() {
Notepad n = (Notepad) thisJoinPoint.getTarget();
if (n.ediT == null)
n.Menubar.add(n.ediT = new JMenu("Edit"));
}
static boolean check(Class c, String memberName, Class memberType) {
try {
Field f = c.getField(memberName);
return f.getType().equals(memberType);
} catch (SecurityException e) { e.printStackTrace();
} catch (NoSuchFieldException e) { System.out.println("Check NSFE");
}
return false;
}
}
--------------------------------------
The code compiles without warning/error. When I run the program,
however, I get a NoSuchFieldException when check(..) is executed in
EditMenuAspect; the Menubar appears, but the Edit Menu does not.
Clearly (?), Menubar is declared, everything is spelled correctly, and
the aspects are in the right order. What am I missing?
Thanks in advance for any help,
Conrad Eaglehill
I'm trying to use reflection with AspectJ to identify when a needed
aspect is not in the build. For example, if I have two aspects
MenuBarAspect (with a variable JMenuBar Menubar) and EditMenuAspect
(with a variable JMenu Edit), and I remove MenuBarAspect from the
build, I don't want a compilation error when code within
EditMenuAspect tries to call Menubar.add(...).
The example that was given to me worked perfectly from another article
in this newsgroup (which was much appreciated, by the way!), works
perfectly in every way expected, and the code I'm using below is based
on it:
--------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Notepad extends JFrame {
JScrollPane scrollpane;
public Notepad(){
setSize(800,600);
Container Notepad.cp = getContentPane();
cp.add(n.textArea = new JTextArea());
cp.add(n.scrollpane = new JScrollPane(n.textArea));
center.nCenter();
}
public static void main(String[] args){
new Notepad();
}
}
--------------------------------------
import javax.swing.*;
public aspect MenuBarAspect {
JMenuBar Notepad.Menubar = null;
pointcut menubar() : set(* Notepad.scrollpane);
before() : menubar() {
Notepad n = (Notepad) thisJoinPoint.getTarget();
if (n.Menubar == null)
n.setJMenuBar(n.Menubar = new JMenuBar());
}
}
--------------------------------------
import java.lang.reflect.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public aspect EditMenuAspect {
JMenu Notepad.ediT = null;
pointcut editMenu() : call(void Notepad.setJMenuBar(..)) &&
if(check(Notepad.class, "Menubar",
JMenuBar.class));
after() : editMenu() {
Notepad n = (Notepad) thisJoinPoint.getTarget();
if (n.ediT == null)
n.Menubar.add(n.ediT = new JMenu("Edit"));
}
static boolean check(Class c, String memberName, Class memberType) {
try {
Field f = c.getField(memberName);
return f.getType().equals(memberType);
} catch (SecurityException e) { e.printStackTrace();
} catch (NoSuchFieldException e) { System.out.println("Check NSFE");
}
return false;
}
}
--------------------------------------
The code compiles without warning/error. When I run the program,
however, I get a NoSuchFieldException when check(..) is executed in
EditMenuAspect; the Menubar appears, but the Edit Menu does not.
Clearly (?), Menubar is declared, everything is spelled correctly, and
the aspects are in the right order. What am I missing?
Thanks in advance for any help,
Conrad Eaglehill