Hi Are-
> I'm wondering if it's possible in AspectJ to add inter-type member
declarations based on pointcuts?
No. Your case is classic generative programming, of applying a rule
to create code.
AspectJ if anything is moving away from that. ITMD's since AspectJ
1.1 have their type
resolved at compile-time, mainly to support incremental weaving. We
thought that it
would be better to strongly separate generative programming from AspectJ
than
to continue or expand support for it.
You might get as much convenience from a utility - variants:
String.valueOf(x)
Renderer.render(int x)
getRenderer(y).render(y.x, "fieldname")
AspectJ could parameterize around the field name for lazy
initialization:
String around(Item item) : get(String *AsString) &&
target(Item) {
String result = proceed(item);
if (result == null) {
// reflectively set field,
based on utility call, keyed off field signature
}
return result;
}
Or perhaps for each domain element viewable on a web page:
interface IWeb { String fieldAsString(String fieldName);
}
aspect ImplementingIWeb {
declare parents {domainClasses}
implement IWeb;
public String
IWeb.fieldAsString(String fieldName) {
//
default, reflective implementation
}
}
/** override default implementation for Item */
aspect ImplementingIWebForItem {
declare precedence
ImplementingIWebForItem, ImplementingIWeb;
public String
Item.fieldAsString(String fieldName) {
//
custom Item implementation
}
}
(I personally don't like any of these options, but they might be worth
considering.)
Hope this helps -
Wes
------------Original Message------------
From: "Are Meisfjord" <are@xxxxxxxxxxxxx>
To: aspectj-users@xxxxxxxxxxx
Date: Mon, Aug-9-2004 2:10 AM
Subject: [aspectj-users] Newbie question on inter-type member
declarations
Hello!
I'm wondering if it's possible in AspectJ to add
inter-type member declarations based on pointcuts? A little example to
illustrate what I mean:
We have a web application using Struts. For
convenience we have introduced a
naming convention requiring certain properties in our domain
model classes to have extra ...AsString properties that formats the
properties correctly for presentation in web pages (using Struts taglibs). So
if we have a property 'salary' in the Employee class we will also have a
property 'salaryAsString' that returns the salary (originally a double) as a
string with thousand separators and two decimal precision.
Is it possible to enforce this convention
throughout the application with an aspect? Of course, We could list
each ...AsString method as an inter-type member declaration in the
aspect, but that won't be much of an improvement wrt programming effort. It
would be much better if we could make a pointcut selecting all get
and set methods of interest and then apply an advice on them that added the
extra methods...
Regards,
Are Meisfjord
|