Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Trying to implement dynamic inheritance in Java

Hello AspectJ users!
 
Let me introduce the context to the problem first, I
have been assigned the task of creating a Java
implementation of a Domain Language called Oasis.
Oasis is an OO based language best suited for the
domain of business/enterprise, it is used to produce a
formal specification of the problem which can be
feeded then to a code generator, very much 'a la MDA'.
My challenge in my implementation of Oasis has been in
substituting code generation for aspectizing, and
instead of producing hundreds of LOC, I try to produce
a very clean POJO model extended with annotations
which fill in all the information in the Oasis
metamodel which the Java metamodel lacks. There come
aspects, which read from these annotations (in
methods, fields and classes) and provide all the
needed behaviour, effectively replacing code
generation.
 
One of the extra things the Oasis metamodel supports
is dynamic inheritance, where classes have services
(methods in my Java impl) that produce specialization
of an object, and methods that produce generalization.
Thus an object can change types several times during
its lifetime.
 
Firstly I tried to solve this using a dynamic Proxy.
For example we have a superclass 'Customer', and two
specializations: 'TrustedCustomer' and
'ProblematicCustomer'. The dynamic Proxy will
implement all three interfaces, and will wrap an
instance of the appropriate type. Whenever it
intercepts a @TriggersSpecialization method (thanks
AspectJ 5!), it will replace the wrapped object for a
new one of the new type (taking care of maintaining
the state of course). Since the rest of the world
holds a reference to a proxy, thanks to this level of
indirection I can easily change the wrapped instance
for one of a different type. If a client tries to
access a method which the currently wrapped type
doesn't have, the proxy will detect this and complain.

 
This approach does the job, but I'm not totally happy
with it, as code like the following will stop working:
 
List customers = Factory.retrieveAllCustomers();
for (Customer c : customers) {
    if(customer instanceof ProblematicCustomers)
        cell.setBackgroundColor(red);
}
 
Any ideas of how to implement this with AOP instead of
the proxy? Any pointers to related efforts? As I see
it, static inter-type declarations cannot help here,
because of that 'static' in its name. However, if I
could find all the references to an object and use a
privileged aspect to replace all those references,
that would be something to start with. Or maybe a
mixed solution using a proxy and an aspect, perhaps
there is a way to intercept those type checks?
 
 
This is an academic project, so the solution isn't
expected to work in a real system. It's only a proof
of concept. I am aware of the magnitude of the
subjacent problem, providing dynamic inheritance to a
statically&strongly typed language as Java. However
I'm not trying nor can I do that, what concerns me is
to find the most elegant possible solution for the
scope of my problem.
 
Best regards
Pepe Iborra
Universidad Politécnica de Valencia



		
______________________________________________ 
Renovamos el Correo Yahoo!: ¡250 MB GRATIS! 
Nuevos servicios, más seguridad 
http://correo.yahoo.es


Back to the top