Announcement Announcement Module
Collapse
No announcement yet.
Spring AOP - method's returned object's class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring AOP - method's returned object's class

    Hi

    I have encountered the following road block. I just want to clarify early-on that this is purely Spring AOP issue..though I mention about webservice just explain my issue in detail.

    I have a method like this.

    public Book getBook( String arg) throws ApplicationFault, Exception {
    ....
    ....
    }

    This is a SOAP webservice method which on-sucess returns Book object (since it is web service, it returns Book xml to caller using CXF)

    I have created Spring AOP pointcut like this

    @AfterThrowing(throwing="thr",pointcut="annotatedW ebServiceEndPoints()")
    public Response exceptionHandler(JoinPoint jp, Throwable thr) {
    .....
    .....
    log.info("thr.getClass()=" + thr.getClass().getCanonicalName());
    ....
    ....
    }

    Book.java extends Response
    ---------------------------
    public Book extends Response{
    private String bookName;

    public String getBookName(){
    }

    public void setBookName(String bookName){
    this.bookName= bookName;
    }

    }

    Response.java
    -------------
    Public Response {
    private String errorCode;

    public String getErrorCode(){
    }

    public void setErrorCode(String errorCode){
    this.errorCode = errorCode;
    }

    }

    In order to send back proper errorCode back to caller..in case of any exception.., I need to know whattype of object..the method is returning. Here in this case, returned object's class type is Book. So, if somehow..I know the *returned* object's class..then.., I can create that object (Book) using refections and populate errorCode on Book and return back from exceptionHandler. Since, JoinPoint does not have any facility to know the MethodSignature from which I can get the class type of *returned* object-class, I kind of struck here.

    Is there a way to get the class type of *method's returned* object class type in the case of @AfterThrowing.

    This is because my web services need the same object to be returned whatever in the case of error/exception with errorCode.

  • #2
    Hi

    After some more googling and research, I found the solution. I posting this so that it helps others if they have similar issue.

    MethodSignature ms = (org.aspectj.lang.reflect.MethodSignature) jp
    .getSignature();
    Class clazz = ms.getMethod().getReturnType();

    //clazz is returned object's class type

    Comment

    Working...
    X