Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-dev] Dynamically defining fields

Yegorov, Artem wrote:

In the example you have provided, what is the reason for defining the otherwise final static, keyword here being final, variable dynamically?

The purpose for the definition of the variable as final is so that it can be used in a switch statement. Technically, there is no need for it (in this case) to be public or static. However, as "constants" in Java have typically been public, static, and final, that was my choice.

As to why do they need to be defined dynamically, there are a couple of reasons for this:

1) The framework that we are developing already requires a certain naming convention to be used under certain (common in the framework) circumstances. Specifically, this is the definition of methods that begin with the word "action", end in anything that the user wants, and takes zero parameters.

2) The user can define any number of methods that adhere to the above convention


Are you expecting the waitForAction() to return upredictable results? Otherwise if you know the results before hand, why would you use an additional aspect layer to define such, unless you wanted to reuse such definition across classes which in other way are not related.
It depends on what you mean by unpredictable. The return value of waitForAction() will be one value of a known set. Specifically, it returns which of the actionXXX methods was invoked, from the set of all defined actionXXX methods. These return "codes" are certainly unrelated between classes (except when subclassing occurs).

If you can provide some more details on what is the puprose of what you are doing, one can analyze such and give you advice based on java's core functionality, as aspects do trap patterned code, redefining your code through aspects, either than enhancing it and providing another angle on reuse of your code would not be efficient and otherwise clear.
The Java language supports the use of events very well. However, take the following code example:

public class foo implements ActionListener {
 public init() {
    JButton b = new JButton("Button 1");
    b.addActionListener(this);

    b = new JButton("Button 2");
    b.addActionListener(this);
    ...
 }

 public void actionPerformed(ActionEvent ae) {
    if (ae.getActionCommand().equals("Button 1")) {
    }
    else if (ae.getActionCommand().equals("Button 2")) {
    }
 }
}

It is difficult for the user (in this example) to determine which of the two buttons was pressed. Of course, modern convieniences in the Java Language allow for anonymous inner classes where this is no longer required. Unfortunately, in our situation, anonymous inner classes are not appropriate for use, and so we are forced to use an approach similar to the above. This is because the above example is providing a distinction between two instance objects, whereas we want to provide a distinction between different method invocations within the same object.

Not being judgemental, but just convinced there is a clearer way do to what you need to do, possibly using aspects to generalize such logic, but not to redefine code.


We already utilize aspects to define pointcuts in all of the actionXXX methods, and add code before the method returns. We are looking for a convienient way (from the user's perspective) of determining what actionXXX method was invoked. The switch statement provides a convienient mechanism (versus a series of if-then-else statements). However, since you can't switch on a string, we need something else. Enums in 1.5 would be one way (eventually), but for now, final integers would suffice. Each actionXXX method would have it's own numeric representation (unlike the aspect example that I showed, where each variable was defined to have a value of "5").


Sincerely,

Artem D. Yegorov
Thanks for your help. I'm sorry if I can't go into too much detail, hopefully this provides enough insight into what (and why) we are trying to do!

--
Keven Ring               | "Oh no,  Not Again..."
The MITRE Corporation    |   Bowl of Petunias -
7515 Colshire Drive      |   The Hitchhikers Guide to the Galaxy
McLean VA 22102-7508     |
PH: (703)883-7026        |





Back to the top