Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Dynamic type casting

Venkat,

The question is not completely clear to me. But from what
I gather, your aspects and classes as shown here should
work just fine. When you return type "Object" from around
advice, AspectJ compiler will perform the necessary casting
(as well as wrapping and unwrapping, in case of primitive
types).

This information is a part of a free sample chapter of 
"AspectJ in Action" (chapter 3, section 3.2.7):
http://www.manning.com/laddad/

Try it and let us know if it works.

-Ramnivas


--- Venkat Dosapati <venkat@xxxxxxxxxxxxxxxxx> wrote:
> Sorry, my previous mail was missing the question. Here are the full
> details with the question:
> 
> Please see the question in the end.
> 
> ==============MyAspect.java====================
> public aspect MyAspect {
> 
>  Object around() : execution(* *.load(..)) {
>   Object receiver = thisJoinPoint.getThis();
>   String className = receiver.getClass().getName();
>   Object[] args = getArguments(thisJoinPoint);
>   System.out.println("Class:" + className + "::ARGS:" + args);
> 
>   Object returnValue = myapp.PersistenceHandler.load(className,
> args); /*
> Here I get common Object, but the Object is 'className' type. */
>   return returnValue;
>  }
> 
>  private Object[] getArguments(JoinPoint jp)
>  {
>   Object[] argumentValues = jp.getArgs();
>   return argumentValues;
>  }
> }
> ===============================================
> 
> 
> ================Class A=========================
> public class A {
> 
>     public A load(int id) {
>         // no implementation
>         return null;
>     }
> }
> ================================================
> 
> 
> ================Class B=========================
> public class B {
> 
>     public B load(int id) {
>         // no implementation
>         return null;
>     }
> }
> ===============================================
> 
> 
> 
> When I replace the load() method in 2 classes A & B with my aspect,
> the
> advice is returning the common Object. But the classes A & B should
> return
> their respective type object.
> 
> how can I type caste the Object to its original class in my aspect
> like
> this:
> 
> Object returnValue = myapp.PersistenceHandler.load(className, args);
> return (className) returnValue;  /* Here className is the variable
> for the
> class name */
> 
> [I think it is not possible with JAVA. Is it possible with AspectJ?]
> 
> 
> Thanks in advance!
> Venkat
> 
> 
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com


Back to the top