Re: AspectJ: solution to Java's repetitiveness?
Mark Space wrote:
EricF wrote:
AspectJ will not address those issues. It's best for cross-cutting
concerns, code that might be common across methods - logging, timing,
and transactions are frequent examples.
I've never worked with any type of aspect oriented programming, although
I've seen it described. At first blush I would have thought that it
might address the OP's needs.
So, when you wave code, are you weaving static methods? Or does the
code you weave in have access to a "this" pointer?
Because if "this" is accessible that could allow some pretty substantial
and useful stuff. If it's just static methods, then I guess it would be
pretty limited.
I just read a bit about AspectJ and introductions.
Yes - AspectJ can add this kind of functionality.
Example:
package test;
public interface AutoToString {
String toString();
}
and
package test;
import java.lang.reflect.*;
public aspect AutoToStringImpl {
declare parents: test.* implements AutoToString;
public String AutoToString.toString() {
try {
StringBuilder sb = new StringBuilder();
Method[] m = getClass().getMethods();
for(int i = 0; i < m.length; i++) {
if(m[i].getName().startsWith("get") &&
m[i].getDeclaringClass() == getClass()) {
if(i > 0) {
sb.append(",");
}
sb.append(m[i].invoke(this, new Object[0]));
}
}
return sb.toString();
} catch(IllegalAccessException e) {
return "Ooops";
} catch(InvocationTargetException e) {
return "Ooops";
}
}
}
Arne
PS: No - I don't think many will want to use this code.