to get values passed to a method as its arguments - how?

Discussion in 'Java' started by Adam Wozniak, Aug 14, 2009.

  1. Adam Wozniak

    Adam Wozniak Guest

    Hello

    Is it possible to get (automatically / magically) values passed to a
    method as its arguments?

    Example:
    void myMethod(int a, int b)
    {
    String arguments = “All arguments: “ + getAllArguments();
    System.out.println();
    }

    Output:
    All arguments: int a=1, int b=2

    Somebody knows how to implement getAllArguments method?

    Kind regards,
    Adam Wozniak
     
    Adam Wozniak, Aug 14, 2009
    #1
    1. Advertising

  2. Adam Wozniak wrote:
    > Hello
    >
    > Is it possible to get (automatically / magically) values passed to a
    > method as its arguments?


    No. And why would you want that?

    > Example:
    > void myMethod(int a, int b)
    > {
    > String arguments = “All arguments: “ + getAllArguments();
    > System.out.println();
    > }
    >
    > Output:
    > All arguments: int a=1, int b=2
    >
    > Somebody knows how to implement getAllArguments method?
    >
    > Kind regards,
    > Adam Wozniak


    You wrote myMethod(), just use the arguments.

    What are you really trying to do?

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Aug 14, 2009
    #2
    1. Advertising

  3. Adam Wozniak

    Tom Anderson Guest

    On Fri, 14 Aug 2009, Adam Wozniak wrote:

    > Is it possible to get (automatically / magically) values passed to a
    > method as its arguments?
    >
    > Example:
    > void myMethod(int a, int b)
    > {
    > String arguments = ?All arguments: ? + getAllArguments();
    > System.out.println();
    > }


    No.

    However, you can write a method that has what we call 'varargs':

    void myMethod(int... args) {
    // in here, args is an int[]
    String arguments = "All arguments: " + args;
    System.out.println(arguments);
    }

    You can call that with any number of arguments:

    myMethod();
    myMethod(1);
    myMethod(1, 2, 3, 4, 5);

    That might be a way of getting what you want.

    tom

    PS: Note that the above method won't actually print anything very
    interesting, because arrays have a crap toString. A better version would
    be:

    void myMethod(Integer... args) {
    // in here, args is an int[]
    String arguments = "All arguments: " + Arrays.asList(args);
    System.out.println(arguments);
    }

    But that has cruft which would get in the way of my didactic purpose.

    --
    Imagine a city where graffiti wasn't illegal, a city where everybody
    could draw wherever they liked. Where every street was awash with a
    million colours and little phrases. Where standing at a bus stop was never
    boring. A city that felt like a living breathing thing which belonged to
    everybody, not just the estate agents and barons of big business. Imagine
    a city like that and stop leaning against the wall - it's wet. -- Banksy
     
    Tom Anderson, Aug 14, 2009
    #3
  4. Adam Wozniak

    Adam Wozniak Guest

    On 14 Sie, 19:42, Knute Johnson <>
    wrote:
    > You wrote myMethod(), just use the arguments.
    >
    > What are you really trying to do?


    Yep, there is a reason why I would be glad to have such possibility -
    creating Exception messages in more automatically manner.
    I would like to have a simple routine to make a string with all
    arguments concatenated in it.

    Example:

    void myMethod(int a, int b, int b)
    {
    try{
    // ...
    }catch(SomeException e){
    throw MyException("myMethod: place 1. Arguments: " +
    getAllArguments(), e);
    }

    // ...

    try{
    // ...
    }catch(SomeException e){
    throw MyOtherException("myMethod: place 2. Arguments: " +
    getAllArguments(), e);
    }

    // an so on...
    }

    I just hate to repeat myself and writing some boring and repeating
    pieces of code, like this:

    throw MyOtherException(
    "myMethod: place 1. Arguments: "
    + "a: " + a
    + ", b: " + b
    , e);

    Note, that when I add another parameter to a method (int c) I must
    modify all places, where I am concatenating their values into
    Exception message string. I don't like it :(

    Any clue?

    Kind regards,
    Adam Wozniak / Poland
     
    Adam Wozniak, Aug 15, 2009
    #4
  5. Adam Wozniak

    Adam Wozniak Guest

    On 14 Sie, 19:42, "Scott A. Hightower" <>
    wrote:
    > Reflection.


    Reflection?
    But how exactly you want to use reflection in this case? I don't get
    it :(
    Any piece of code or a hint?

    Kind regards,
    Adam Wozniak
     
    Adam Wozniak, Aug 15, 2009
    #5
  6. Adam Wozniak

    Adam Wozniak Guest

    On 14 Sie, 19:42, "Scott A. Hightower" <>
    wrote:
    > Reflection.


    Reflection?
    But how exactly you want to use reflection in this case? I don't get
    it :(
    Any piece of code or a hint?

    Kind regards,
    Adam Wozniak
     
    Adam Wozniak, Aug 15, 2009
    #6
  7. Adam Wozniak

    Adam Wozniak Guest

    On 14 Sie, 20:33, Tom Anderson <> wrote:
    > However, you can write a method that has what we call 'varargs':
    >
    > void myMethod(int... args) {
    >         // in here, args is an int[]
    >         String arguments = "All arguments: " + args;
    >         System.out.println(arguments);
    >
    > }
    >
    > You can call that with any number of arguments:
    >
    > myMethod();
    > myMethod(1);
    > myMethod(1, 2, 3, 4, 5);
    >
    > That might be a way of getting what you want.


    Yep, your hint works perfectly, but "only" for methods, where all
    parameters (args) have the same type. I guess that I would be able to
    "enhance" your hint and write something like this:

    void myMethod(Object... args) {
    ^^^^^^

    .... but I cannot (well: I absolutely don't want) to lose information
    about parameter types (for obvious reasons).


    Thanks and take care,
    Adam Wozniak
     
    Adam Wozniak, Aug 15, 2009
    #7
  8. Adam Wozniak

    Adam Wozniak Guest

    On 14 Sie, 20:33, Tom Anderson <> wrote:
    > However, you can write a method that has what we call 'varargs':
    >
    > void myMethod(int... args) {
    >         // in here, args is an int[]
    >         String arguments = "All arguments: " + args;
    >         System.out.println(arguments);
    >
    > }
    >
    > You can call that with any number of arguments:
    >
    > myMethod();
    > myMethod(1);
    > myMethod(1, 2, 3, 4, 5);
    >
    > That might be a way of getting what you want.


    Yep, your hint works perfectly, but "only" for methods, where all
    parameters (args) have the same type. I guess that I would be able to
    "enhance" your hint and write something like this:

    void myMethod(Object... args) {
    ^^^^^^

    .... but I cannot (well: I absolutely don't want) to lose information
    about parameter types (for obvious reasons).


    Thanks and take care,
    Adam Wozniak
     
    Adam Wozniak, Aug 15, 2009
    #8
  9. Adam Wozniak

    Adam Wozniak Guest

    On 14 Sie, 20:33, Tom Anderson <> wrote:
    > However, you can write a method that has what we call 'varargs':
    >
    > void myMethod(int... args) {
    >         // in here, args is an int[]
    >         String arguments = "All arguments: " + args;
    >         System.out.println(arguments);
    >
    > }
    >
    > You can call that with any number of arguments:
    >
    > myMethod();
    > myMethod(1);
    > myMethod(1, 2, 3, 4, 5);
    >
    > That might be a way of getting what you want.


    Yep, your hint works perfectly, but "only" for methods, where all
    parameters (args) have the same type. I guess that I would be able to
    "enhance" your hint and write something like this:

    void myMethod(Object... args) {
    ^^^^^^

    .... but I cannot (well: I absolutely don't want) to lose information
    about parameter types (for obvious reasons).


    Thanks and take care,
    Adam Wozniak
     
    Adam Wozniak, Aug 15, 2009
    #9
  10. Adam Wozniak wrote:
    > On 14 Sie, 19:42, Knute Johnson <>
    > wrote:
    >> You wrote myMethod(), just use the arguments.
    >>
    >> What are you really trying to do?

    >
    > Yep, there is a reason why I would be glad to have such possibility -
    > creating Exception messages in more automatically manner.
    > I would like to have a simple routine to make a string with all
    > arguments concatenated in it.
    >
    > Example:
    >
    > void myMethod(int a, int b, int b)
    > {
    > try{
    > // ...
    > }catch(SomeException e){
    > throw MyException("myMethod: place 1. Arguments: " +
    > getAllArguments(), e);
    > }
    >
    > // ...
    >
    > try{
    > // ...
    > }catch(SomeException e){
    > throw MyOtherException("myMethod: place 2. Arguments: " +
    > getAllArguments(), e);
    > }
    >
    > // an so on...
    > }
    >
    > I just hate to repeat myself and writing some boring and repeating
    > pieces of code, like this:
    >
    > throw MyOtherException(
    > "myMethod: place 1. Arguments: "
    > + "a: " + a
    > + ", b: " + b
    > , e);
    >
    > Note, that when I add another parameter to a method (int c) I must
    > modify all places, where I am concatenating their values into
    > Exception message string. I don't like it :(
    >
    > Any clue?
    >
    > Kind regards,
    > Adam Wozniak / Poland


    That's just silly.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Aug 15, 2009
    #10
  11. Adam Wozniak

    Lew Guest

    Adam Wozniak wrote:
    > I just hate to repeat myself and writing some boring and repeating
    > pieces of code, like this:


    Yes, because typing is such *hard* physical labor!

    You poor, poor child. I hope you don't strain a finger!

    --
    Lew
     
    Lew, Aug 15, 2009
    #11
  12. Adam Wozniak

    Lew Guest

    Adam Wozniak wrote:
    > On 14 Sie, 20:33, Tom Anderson <> wrote:
    >> However, you can write a method that has what we call 'varargs':
    >>
    >> void myMethod(int... args) {
    >> // in here, args is an int[]
    >> String arguments = "All arguments: " + args;
    >> System.out.println(arguments);
    >>
    >> }
    >>
    >> You can call that with any number of arguments:
    >>
    >> myMethod();
    >> myMethod(1);
    >> myMethod(1, 2, 3, 4, 5);
    >>
    >> That might be a way of getting what you want.

    >
    > Yep, your hint works perfectly, but "only" for methods, where all
    > parameters (args) have the same type. I guess that I would be able to
    > "enhance" your hint and write something like this:
    >
    > void myMethod(Object... args) {
    > ^^^^^^
    >
    > ... but I cannot (well: I absolutely don't want) to lose information
    > about parameter types (for obvious reasons).


    Do you think you might restrict yourself to a single reply to a post at a time?

    --
    Lew
     
    Lew, Aug 15, 2009
    #12
  13. Adam Wozniak

    markspace Guest

    Adam Wozniak wrote:

    >
    > Any clue?


    What you suggest isn't actually a bad idea, but unfortunately nothing
    comes to mind. What you are asking for just isn't possible.

    You might have a slightly easier time with the String.format() method,
    rather than using the + operator.

    throw new IllegalArgumentException( String.format(
    "Illegal argument, a must be > 0 (a=%d, b=%d, c=%d)",
    a, b, c ) );

    Or something to that effect.
     
    markspace, Aug 15, 2009
    #13
  14. Adam Wozniak

    Tom Anderson Guest

    On Fri, 14 Aug 2009, Adam Wozniak wrote:

    > On 14 Sie, 20:33, Tom Anderson <> wrote:
    >> However, you can write a method that has what we call 'varargs':
    >>
    >> void myMethod(int... args) {
    >>         // in here, args is an int[]
    >>         String arguments = "All arguments: " + args;
    >>         System.out.println(arguments);
    >>
    >> }
    >>
    >> You can call that with any number of arguments:
    >>
    >> myMethod();
    >> myMethod(1);
    >> myMethod(1, 2, 3, 4, 5);
    >>
    >> That might be a way of getting what you want.

    >
    > Yep, your hint works perfectly, but "only" for methods, where all
    > parameters (args) have the same type. I guess that I would be able to
    > "enhance" your hint and write something like this:
    >
    > void myMethod(Object... args) {
    > ^^^^^^
    >
    > ... but I cannot (well: I absolutely don't want) to lose information
    > about parameter types (for obvious reasons).


    Your reasons aren't at all obvious, to me at least. You say:

    Yep, there is a reason why I would be glad to have such possibility -
    creating Exception messages in more automatically manner. I would like to
    have a simple routine to make a string with all arguments concatenated in
    it.

    Which doesn't require keeping the type of the parameter (note that you
    only lose the declared types, not the runtime classes of the objects -
    objects always have those). I wrote a very similar method yesterday, for
    logging:

    private void log(Object... msg) {
    StringBuilder sb = new StringBuilder();
    for (Object msgPart: msg) {
    sb.append(msgPart);
    }
    return sb.toString();
    }

    Used like:

    log("downloading page ", pageName, " from host ", hostName);

    tom

    --
    Argumentative and pedantic, oh, yes. Although it's properly called
    "correct" -- Huge
     
    Tom Anderson, Aug 15, 2009
    #14
  15. Adam Wozniak

    Tom Anderson Guest

    On Fri, 14 Aug 2009, Lew wrote:

    > Adam Wozniak wrote:
    >> I just hate to repeat myself and writing some boring and repeating
    >> pieces of code, like this:

    >
    > Yes, because typing is such *hard* physical labor!


    You really like this 'typing' strawman, don't you, Lew? The problem is not
    having to type, the problem is having to *remember* to type.

    It would be very easy to forget to add the extra parameter to the
    exception message when changing the code. On any serious project, you have
    more than enough things to remember already, so being able to forget even
    one of them is helpful.

    There is no way to do what the OP wants in plain java. It strikes me as
    the kind of thing that might be doable with AOP, but i've never used AOP
    myself, so i can't be sure.

    tom

    --
    Argumentative and pedantic, oh, yes. Although it's properly called
    "correct" -- Huge
     
    Tom Anderson, Aug 15, 2009
    #15
  16. Adam Wozniak

    Tom Anderson Guest

    On Sat, 15 Aug 2009, Tom Anderson wrote:

    > On Fri, 14 Aug 2009, Adam Wozniak wrote:
    >
    >> ... but I cannot (well: I absolutely don't want) to lose information
    >> about parameter types (for obvious reasons).

    >
    > Your reasons aren't at all obvious, to me at least.


    Because what you meant was that you don't want to lose information about
    parameter *names*. Fair enough.

    tom

    --
    Argumentative and pedantic, oh, yes. Although it's properly called
    "correct" -- Huge
     
    Tom Anderson, Aug 15, 2009
    #16
  17. Knute Johnson wrote:
    > Adam Wozniak wrote:
    >> On 14 Sie, 19:42, Knute Johnson <>
    >> wrote:
    >>> You wrote myMethod(), just use the arguments.
    >>>
    >>> What are you really trying to do?

    >>
    >> Yep, there is a reason why I would be glad to have such possibility -
    >> creating Exception messages in more automatically manner.
    >> I would like to have a simple routine to make a string with all
    >> arguments concatenated in it.
    >>
    >> Example:
    >>
    >> void myMethod(int a, int b, int b)
    >> {
    >> try{
    >> // ...
    >> }catch(SomeException e){
    >> throw MyException("myMethod: place 1. Arguments: " +
    >> getAllArguments(), e);
    >> }
    >>
    >> // ...
    >>
    >> try{
    >> // ...
    >> }catch(SomeException e){
    >> throw MyOtherException("myMethod: place 2. Arguments: " +
    >> getAllArguments(), e);
    >> }
    >>
    >> // an so on...
    >> }
    >>
    >> I just hate to repeat myself and writing some boring and repeating
    >> pieces of code, like this:
    >>
    >> throw MyOtherException(
    >> "myMethod: place 1. Arguments: "
    >> + "a: " + a
    >> + ", b: " + b
    >> , e);
    >>
    >> Note, that when I add another parameter to a method (int c) I must
    >> modify all places, where I am concatenating their values into
    >> Exception message string. I don't like it :(
    >>
    >> Any clue?
    >>
    >> Kind regards,
    >> Adam Wozniak / Poland

    >
    > That's just silly.
    >

    Why is it silly? If creating an exception message inside a method it
    makes good sense to be interested in what the method arguments are. If
    this was silly then a large part of black-box testing would be silly
    too...why bother to test a method then with all sorts of permutations of
    arguments?

    Given this argument (the OP's argument) for having a getAllArguments
    method, it makes sense to me. It also makes sense for debugging code in
    a variety of situations.

    AHS
     
    Arved Sandstrom, Aug 15, 2009
    #17
  18. Adam Wozniak

    Adam Wozniak Guest

    On 15 Sie, 13:20, Tom Anderson <> wrote:
    > >> ... but I cannot (well: I absolutely don't want) to lose information
    > >> about parameter types (for obvious reasons).

    >
    > > Your reasons aren't at all obvious, to me at least.

    >
    > Because what you meant was that you don't want to lose information about
    > parameter *names*. Fair enough.


    Yes, you are correct. I would like to have a full automatic routine to
    collect *names* and values of all arguments passed to my method.
    In particular: after adding another parameter to my method I don't
    want to remember, that I must add this parameter to all exception
    messages.

    I like to automatize things which are the same for most cases. For
    example, in my Java project, I automatized toString method. For almost
    all my classes, toString method is just a one line of code:

    @Override public String toString()
    {
    return ToStringBuilder.objectToStringInOneLine(this);
    }

    .... where ToStringBuilder class is a static util class using
    org.apache.commons.lang.builder.ReflectionToStringBuilder:

    public class ToStringBuilder
    {
    public static String objectToStringInOneLine(final Object obj)
    {
    final String str = ToStringBuilder.objectToString(obj,
    ToStringStyle.SHORT_PREFIX_STYLE);
    return str;
    }

    public static String objectToString(final Object obj, final
    ToStringStyle toStringStyle)
    {
    return doObjectToString(obj, null, toStringStyle);
    }

    // ...

    private static String doObjectToString(
    final Object obj
    , final String[] excludeFieldNamesParam // can be null.
    , final ToStringStyle toStringStyle
    )
    {
    final ReflectionToStringBuilder rtsb =
    new ReflectionToStringBuilder(obj, toStringStyle)
    { /* empty block */ };

    if(excludeFieldNamesParam != null)
    rtsb.setExcludeFieldNames(excludeFieldNamesParam);

    return rtsb.toString();
    }
    }

    Of course there *must* be a way to obtain arguments names and their
    (runtime) values because all Java debuggers can *see* those things
    when I'm standing on a breakpoint in Debug mode.

    Thanks!
    Adam Wozniak
     
    Adam Wozniak, Aug 15, 2009
    #18
  19. Adam Wozniak

    Lew Guest

    Tom Anderson wrote:
    > On Fri, 14 Aug 2009, Lew wrote:
    >
    >> Adam Wozniak wrote:
    >>> I just hate to repeat myself and writing some boring and repeating
    >>> pieces of code, like this:

    >>
    >> Yes, because typing is such *hard* physical labor!

    >
    > You really like this 'typing' strawman, don't you, Lew? The problem is
    > not having to type, the problem is having to *remember* to type.


    That's not what the OP said. The OP said he hates to write repetitive code;
    he never said he had trouble remembering to type it. In order for my reply to
    be a straw man, it would've had to address a different issue than the one he
    mentioned, such as you did.

    > It would be very easy to forget to add the extra parameter to the
    > exception message when changing the code. On any serious project, you
    > have more than enough things to remember already, so being able to
    > forget even one of them is helpful.
    >
    > There is no way to do what the OP wants in plain java. It strikes me as
    > the kind of thing that might be doable with AOP, but i've never used AOP
    > myself, so i can't be sure.
    >
    > tom
    >



    --
    Lew
     
    Lew, Aug 15, 2009
    #19
  20. Arved Sandstrom wrote:
    > Knute Johnson wrote:
    >> Adam Wozniak wrote:
    >>> On 14 Sie, 19:42, Knute Johnson <>
    >>> wrote:
    >>>> You wrote myMethod(), just use the arguments.
    >>>>
    >>>> What are you really trying to do?
    >>>
    >>> Yep, there is a reason why I would be glad to have such possibility -
    >>> creating Exception messages in more automatically manner.
    >>> I would like to have a simple routine to make a string with all
    >>> arguments concatenated in it.
    >>>
    >>> Example:
    >>>
    >>> void myMethod(int a, int b, int b)
    >>> {
    >>> try{
    >>> // ...
    >>> }catch(SomeException e){
    >>> throw MyException("myMethod: place 1. Arguments: " +
    >>> getAllArguments(), e);
    >>> }
    >>>
    >>> // ...
    >>>
    >>> try{
    >>> // ...
    >>> }catch(SomeException e){
    >>> throw MyOtherException("myMethod: place 2. Arguments: " +
    >>> getAllArguments(), e);
    >>> }
    >>>
    >>> // an so on...
    >>> }
    >>>
    >>> I just hate to repeat myself and writing some boring and repeating
    >>> pieces of code, like this:
    >>>
    >>> throw MyOtherException(
    >>> "myMethod: place 1. Arguments: "
    >>> + "a: " + a
    >>> + ", b: " + b
    >>> , e);
    >>>
    >>> Note, that when I add another parameter to a method (int c) I must
    >>> modify all places, where I am concatenating their values into
    >>> Exception message string. I don't like it :(
    >>>
    >>> Any clue?
    >>>
    >>> Kind regards,
    >>> Adam Wozniak / Poland

    >>
    >> That's just silly.
    >>

    > Why is it silly? If creating an exception message inside a method it
    > makes good sense to be interested in what the method arguments are. If
    > this was silly then a large part of black-box testing would be silly
    > too...why bother to test a method then with all sorts of permutations of
    > arguments?
    >
    > Given this argument (the OP's argument) for having a getAllArguments
    > method, it makes sense to me. It also makes sense for debugging code in
    > a variety of situations.
    >
    > AHS


    I should have been more specific. It's the "I hate just to repeat
    myself and write some boring pieces of code" that I find silly. But
    then I use a text editor to write my code so I must really like writing
    boring pieces of code.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Aug 15, 2009
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Storm
    Replies:
    1
    Views:
    459
    Chris Uppal
    May 1, 2004
  2. jmborr
    Replies:
    1
    Views:
    427
    Stargaming
    Nov 3, 2007
  3. thunk
    Replies:
    1
    Views:
    323
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    496
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    630
    thunk
    Apr 3, 2010
Loading...

Share This Page