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

B

Bill McCleary

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

It's actually painful watching people struggle with these deficiencies
of the Java language, when there's a tool within arm's reach that
removes all of these problems: Lisp, and particularly the Lisp macro.
It's easy to make your own DEFUN-like macro that wraps DEFUN but stores
argument names and values into a list that can be spat out in an error
message with FORMAT. It can even go further and define a local function
LOGERR that closes over the list of argument names and values, takes a
diagnostic message parameter, and FORMATs a message to standard output
containing all of this stuff. So you can do:

(my-defun my-divide (numer denom)
(if (eq 0 denom)
(logerr "division by zero")
(/ numer denom)))

if MY_DEFUN is defined appropriately and get
CL-USER> (my-divide 1 0)
MY-DIVIDE NUMER=1 DENOM=0: division by zero
NIL
CL-USER>
say.

Even the standard DEFUN lets you capture the argument names and values
as a list, like varargs except you can still also have some or all of
them assigned individual names, if you make it a keyword function:

(defun foo (&rest all &key x y z)
(format t "x is: ~a~%" x)
(format t "y is: ~a~%" y)
(format t "z is: ~a~%" z)
(format t "all: ~a~%" all))

CL-USER> (foo :y 3 :x 7 :z 2)
x is: 7
y is: 3
z is: 2
all: :)Y 3 :X 7 :Z 2)
 
J

Joshua Cranmer

Adam said:
Somebody knows how to implement getAllArguments method?

This is 100% impossible, since, without compiling with debug
information, all of the argument *names* are not present at run-time.
All that is saved are the types and order of the variables [1]. Even if
you compile with debug symbols, you would either have to get creative
with class loading hooks or drag yourself through a debugging API to get
the requisite information.

[1] Strictly speaking, parameter annotations and variable arity is also
retained in the class file as well. The latter one can consider to be
part of the type and the former would involve a lot more maintenance
than you are presumably willing to use.
 
L

Lew

Bill said:
It's actually painful watching people struggle with these deficiencies
of the Java language, when there's a tool within arm's reach that
removes all of these problems: Lisp, and particularly the Lisp macro.

It's actually painful watching trolls jump into the computer-language
newsgroups and try to start yet another Language War, particularly the dweebs
who desperately try to promote the unreadable execration known as Lisp.

Switching to Lisp because you're too incompetent to use Java correctly is like
amputating your arm because your nose itches.
 
J

Joshua Cranmer

Bill said:
It's actually painful watching people struggle with these deficiencies
of the Java language, when there's a tool within arm's reach that
removes all of these problems: Lisp, and particularly the Lisp macro.

And what does the advantages of another language have to do with a Java
program? Java, for better or worse, is not LISP, and the fact that LISP
has this functionality is completely nontopical to this newsgroup.
 
B

Bill McCleary

Lew said:
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?

He's using Google Groups. I don't know, but suspect from seeing many
cases like this all of which were posted from Google Groups, that Google
Groups sometimes spits out an error message after posting successfully.

In fact it's almost certain that it does, and that any other Web based
news gateway does: these will be based on a web form interaction, where
the user types in a post, clicks some "submit" button, and the browser
loads a page from the site that either says it was posted successfully
or says it wasn't. But if it hiccups and times out loading the success
page, they'll get "the page cannot be displayed" yadda yadda yadda in
their browser, and the natural response will be to hit "back" and then
"submit" again until they see the success page. And then we get two or
three copies of the same message.

Just one more reason why everyone should be using real news programs and
not web sites to post to usenet.

Anyone somehow stuck using a web gateway to news should at least open
another tab and reload the group in it if they get such an error
message, and see if their post has actually appeared. (In theory, it
should be there nearly instantly if it did succeed, since they're
looking for it on the same server they posted through.) Only if, say,
after five minutes it hasn't shown up should they hit "back" and
"submit" again in the other tab to retry the post; in that case, it's
likely the form submit is what timed out, not the success response, or
the response that timed out was an error rather than success response.
(An error response might be because of a problem with their submission,
in which case an unedited retry will fail again, but the route to
success in this case is still such a retry -- until the error response
is successfully received and displayed by their browser, whereupon
they'll know how to alter their submission to make it work. Though one
might guess and trim either quoted material or crossposts, the two usual
reasons for post rejection by a server.)

The above doesn't work so well for moderated groups though: there's no
way to check whether a submission actually succeeded short of waiting
hours or even days before giving it up as lost. Even then it might have
been rejected by the moderator rather than failed to even reach the
moderator. The only advice that makes sense then, besides "don't use a
web gateway if at all possible", would be "just resubmit until apparent
success" on the part of posters and "if it gets annoying enough, set up
a filter in your email program that causes you to only see the first
copy of duplicate messages" on the part of moderators.

I wonder how this problem is handled elsewhere? It can happen any time
there's a gateway between two protocols that a submission is made using
protocol A, the gateway successfully submits it using protocol B with
the protocol B server indicating this success to the gateway, but then
the gateway fails to deliver the success message to the client using
protocol A, and the client gets a timeout or other error message. Many
users won't be able to distinguish protocol A errors from protocol B
errors, and those that can still won't know if a protocol A error
happened on the outbound or the inbound leg. It's probably a pretty
thorny problem in general. Thankfully, in the future the need for
gatewaying might go away. There are a few free newsservers, and
increasingly both unfiltered broadband and mobile devices that can
access it are becoming widespread, diminishing much of the need for web
gateways to other kinds of network services.
 
A

Arved Sandstrom

[ SNIP ]
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.

Fair enough, it wasn't clear to me what you were describing. I buy the
OP's argument insofar as it reduces maintenance and errors, but not that
the usefulness of this is that it cuts down on the tedium of writing code.

Although I have no objections to anything that reduces the amount of
boilerplate I have to write.

AHS
 
L

Lew

Arved said:
Although I have no objections to anything that reduces the amount of
boilerplate I have to write.

NetBeans, Eclipse, and presumably other IDEs, sport a useful "macro" or
template feature that lets you plug in arbitrary boilerplate with just a few
keystrokes. For example, at work we set up our Eclipse variant to insert a
private static final Logger instance by merely typing "psfl<CTRL-space>", and
the IDE fills in:

private static final CLASS_NAME = ${class}.getName();
private static final Logger LOGGER = Logger.getLogger( CLASS_NAME );

At home, I have the NetBeans "new class" template fill in the Logger import
and similar code to the above as part of the new class source.
 
B

Bill McCleary

Lew said:
It's actually painful watching trolls jump into the computer-language
newsgroups and try to start yet another Language War, particularly the
dweebs who desperately try to promote the unreadable execration known as
Lisp.

Switching to Lisp because you're too incompetent to use Java correctly
is like amputating your arm because your nose itches.

That tears it. You're joining Series Expansion and Seamus MacRae in my
killfile.

"Unreadable execration"? That would be perl. Java is eminently readable,
if you like the long flowery prose of classical authors. Lisp is
eminently readable and far less verbose. Well, except for complex LOOP
and FORMAT uses, which is why nobody sane uses LOOP, and why complex
FORMATs should be broken up into simpler ones with functional abstractions.

Actually, it reminds of the old joke: a Lisp hacker, a C++ programmer, a
writer of terrible perl, and a writer of good perl happen upon a
three-billion-dollar bearer bond lying in the street. Which one gets it?

The writer of bad perl and the C++ programmer think picking the bond up
will put them in debt to the tune of hundreds of millions, and the
writer of good perl doesn't exist, which leaves only the Lisp hacker.
 
L

Lew

Bill said:
That tears it. You're joining Series Expansion and Seamus MacRae in my
killfile.

That's funny. You start a Language War then get upset when someone joins?
That's dancing on the end of my string, pardner.
 
B

Bill McCleary

Joshua said:
And what does the advantages of another language have to do with a Java
program? Java, for better or worse, is not LISP, and the fact that LISP
has this functionality is completely nontopical to this newsgroup.

Wow, is everyone here like series expansion?
 
A

Arved Sandstrom

Bill said:
Wow, is everyone here like series expansion?

This is cljp, and SeriesExpansion is not a regular poster here. He shows
up in CLJP when people have posted to several groups, usually for bad
reasons. So it's not a question that makes any sense.

As for folks pointing out that CLJP is not the appropriate forum for
evangelizing LISP, what part of that is so difficult to understand? I
use about half a dozen other languages besides Java on a fairly regular
basis, and have dabbled with many more, including LISP. But I don't
frequent CLJP to hear about why I could solve a Java problem so much
better with Haskell or F# or J or LISP or C#. Neither does anyone else.

Here's a newsflash. I program for a living. There is zero chance of me
ever using LISP at any place I'll ever work. Even if I wanted to. There
is, OTOH, a very good chance of me using Java or C#, for example. So
zealous comments about how much better LISP is at doing something, maybe
true, maybe not, are completely useless to me. And probably to most of
the other people participating in this _Java_ newsgroup.

AHS
 
A

Arne Vajhøj

Lew said:
One suspects identical with.

Could be.

Distinct sounding name.

New around here at least with that name.

Post content that could be taken from the Java-Lisp-Paul thread.

Posted via a free anonymous usenet service.

Arne
 
A

Arne Vajhøj

Arved said:
This is cljp, and SeriesExpansion is not a regular poster here. He shows
up in CLJP when people have posted to several groups, usually for bad
reasons. So it's not a question that makes any sense.
Agree.

As for folks pointing out that CLJP is not the appropriate forum for
evangelizing LISP, what part of that is so difficult to understand? I
use about half a dozen other languages besides Java on a fairly regular
basis, and have dabbled with many more, including LISP. But I don't
frequent CLJP to hear about why I could solve a Java problem so much
better with Haskell or F# or J or LISP or C#. Neither does anyone else.

Here's a newsflash. I program for a living. There is zero chance of me
ever using LISP at any place I'll ever work. Even if I wanted to. There
is, OTOH, a very good chance of me using Java or C#, for example.

Very small but not zero.

A search at dice.com says:
Java - 9041 jobs
C# - 4156 jobs
Lisp - 15 jobs

15 jobs over all of US is not much. No even one job in each state. But
more than zero.

Arne
 
O

Oxide Scrubber

Arne said:
Could be.

Distinct sounding name.

New around here at least with that name.

Post content that could be taken from the Java-Lisp-Paul thread.

The pro-Lisp side though. Series and Seamus were on the pro-Java side.
It seems unlikely they're the same, given how vehemently the latter
believe Lisp macros to be evil incarnate. :)
 
D

Daniel Pitts

Bill said:
Wow, is everyone here like series expansion?
No, most of the people here are here to talk about Java, not LISP.
There are many good newsgroups for talking about LISP (comp.lang.lisp
would be my educated guess), and even some for comparing Java and LISP
(comp.object, comp.programming, and even comp.lang.java.advocacy)

Personally, I like the idea of LISP, but I couldn't see myself actually
writing anything in it. In any case, I know that Java and LISP are
fundamentally different, and wouldn't consider answering a question
about one with an answer about the other.

It might be appropriate in some contexts to point out that a different
tool is more suitable for the overall task at hand, but throwing one
feature of another language in for a solution of a small part of the
program is detrimental at worst, useless at best.
 
A

Arne Vajhøj

Oxide said:
The pro-Lisp side though. Series and Seamus were on the pro-Java side.
It seems unlikely they're the same, given how vehemently the latter
believe Lisp macros to be evil incarnate. :)

That assumes normal rational behavior.

And that assumption has already been proven false.

Arne
 
A

Adam Wozniak

Arved said:
Although I have no objections to anything that reduces the amount of
boilerplate I have to write.

NetBeans, Eclipse, and presumably other IDEs, sport a useful "macro" or
template feature that lets you plug in arbitrary boilerplate with just a few
keystrokes. [..]

But macros don't solve my primary problem / issue at all.

Adam
 
L

Lew

Adam said:
Arved said:
Although I have no objections to anything that reduces the amount of
boilerplate I have to write.
NetBeans, Eclipse, and presumably other IDEs, sport a useful "macro" or
template feature that lets you plug in arbitrary boilerplate with just a few
keystrokes. [..]

But macros don't solve my primary problem / issue at all.

Perhaps not, but they help that of which Arved spoke.
 

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

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top