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

A

Adam Wozniak

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
 
K

Knute Johnson

Adam said:
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?
 
T

Tom Anderson

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
 
A

Adam Wozniak

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
 
A

Adam Wozniak

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
 
A

Adam Wozniak

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
 
A

Adam Wozniak

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
 
A

Adam Wozniak

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
 
A

Adam Wozniak

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
 
K

Knute Johnson

Adam said:
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.
 
L

Lew

Adam said:
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!
 
L

Lew

Adam said:
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?
 
M

markspace

Adam said:
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.
 
T

Tom Anderson

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
 
T

Tom Anderson

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
 
T

Tom Anderson

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
 
A

Arved Sandstrom

Knute said:
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
 
A

Adam Wozniak

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
 
L

Lew

Tom said:
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.
 
K

Knute Johnson

Arved said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top