Results 1 to 3 of 3
  1. #1
    DavidJohns is offline Member
    Join Date
    May 2008
    Posts
    1
    Rep Power
    0

    Default Need to dynamicaly get all parameters in a method call.

    While I am inside a method, I need a way to dynamically get the parameters for the method call that I am in. I need to put these in an array, map, or something that can be generically added to a custom exception.

    If anyone can let me know how to do this it would be greatly appreciated.

    For more info keep reading:

    Short Scenario:
    We have a SOA project. In our remote service layer all method calls contain a try catch block that catches any checked exception that we have already caught and logged and any Hibernate exception (is a standard method framework we use for all methods on this layer). However, if we get an error that we did not expect, we are going to create a separate dump file and log the name of the dump file.

    In the dump file we would like to include information about the parameters for the service. I have an exception object that can take a generic object, collection, or map.

    What I need to do:
    I would like a way to dynamically get the parameters for the method and put them in my exception object so that I can just include the same code in my exception clause instead of having to include each parameter by name in the exception.

    I have not been able to find a way to do this inside the method call.

    Sample Code Snippet:

    public PagingContext findAreacode(AreacodeTO areacodeTOcriteria, PagingContext pageContext) throws AjentException {
    ...

    try {
    ... do method stuff ...

    } catch (HibernateException e) {
    ... handle hibernate exception
    } catch (AjentException ae) {
    // All of our errors should extend AjentException
    ... Hanlde ajent exception
    } catch (Throwable e) {
    /* This is the code I want to replace with a dynamic method that will allow me to
    * determine what the parameters are to this call (findAreaCode(p1, p2).
    */
    Map paramatersMap = new HashMap();
    paramatersMap.put("areacodeTOcriteria", areacodeTOcriteria);
    paramatersMap.put("pageContext", pageContext);
    // End Parameter Entry Section.
    // Create exception & call logger
    AjilityServerDumpException asde = new AjilityServerDumpException(e, context, ctx, null, null, paramatersMap );
    ServerLogger.logErrorDump(asde, context, ctx, this.getClass(), ServerLogger.ADMIN_LOGGER, ServerLogger.ERROR);
    throw asde;
    } finally {
    ... do finally stuff
    }

    return pageContext;
    }



    David Johns

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Hi David,

    I am sorry but in Java you cannot access method parameters in a generic way. Reflection doesn't help you here. You can find information about classes (data members and method signatures) but you cannot find out even in what method you are. The parameters are loaded on the stack and unfortunately Java doesn't provide introspection there.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  3. #3
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Hi again David,

    You might be able to do something if you get out of straight Java and look into an aspect-oriented Java extension or some byte code manipulation with BCEL. Code generation might be the way to go too. So instead of writing the ugly code you might be able to generate it at build time based on some configuration maybe.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

Similar Threads

  1. Call Java Method
    By hussainzim in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-15-2008, 07:22 AM
  2. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM
  3. Call a Method Automatically
    By rhm54 in forum New To Java
    Replies: 4
    Last Post: 02-07-2008, 08:51 AM
  4. Replies: 9
    Last Post: 12-18-2007, 09:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •