introspection - getting the value of a hashmap

Hello is it possible to get the value of a hashmap by introspection :

Here is my code that generate NoSuchFieldException :

Class clazz = Class.forName("java.util.HashMap");

Object uneMap = clazz.newInstance();

Field champ = clazz.getField("value");

String val = (String)champ.get(uneMap);

[376 byte] By [JusteUneQuestiona] at [2007-11-27 8:01:33]
# 1
What are you trying to do, and why?
Hippolytea at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 2
The value? What manner of HashMap has a value?
georgemca at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 3
Doesn't getField only work on public members? http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getField(java.lang.String)
hunter9000a at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 4
I need to get that value corresponding to a key, is it possible to do it by introspection ?
JusteUneQuestiona at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 5
> I need to get that value corresponding to a key, is> it possible to do it by introspection ?Probably, but what's the point? The Map interface allows you to do this the old-fashioned way. That's what Maps are for!
georgemca at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 6
> I need to get that value corresponding to a key, is> it possible to do it by introspection ?What does this have to do with the code you posted?
cotton.ma at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 7
Even if you played games with access, I see fields table, size, threshhold, loadFactor and modCount, but I don't see value. It's a mystery.
Hippolytea at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 8
> I need to get that value corresponding to a key, is it possible to do it by introspection ?Ah, the other shoe drops. Yes it is possible, use reflection on the method "get".Now perhaps you can tell us why you are doing this.
Hippolytea at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 9

> Even if you played games with access, I see fields

> table, size, threshhold, loadFactor and modCount, but

> I don't see value. It's a mystery.

Even more mysterious is this line

String val = (String)champ.get(uneMap);

JustUneQuestione,

I think you should eitehr speak to what problem you are trying to solve and/or just give this up. Your code mainly demonstrates a whole of voodoo accomplishing little.

The question was already put to you. Why?

cotton.ma at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 10

> Ah, the other shoe drops. Yes it is possible, use

> reflection on the method "get".

how ?

> Now perhaps you can tell us why you are doing this.

It has to do with swixml, during the parsing of the xml file. In the xml i have iterations that build panels and each of them have an id like this componentName + index. Hope its clear for you.

JusteUneQuestiona at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 11

It's early in the morning for me, so I don't understand your motivation, but

here's an example of making simple map code much harder ;-)

import java.lang.reflect.*;

import java.util.*;

public class ReflectExample {

public static void main(String[] args) throws Exception {

Class cls = Class.forName("java.util.HashMap");

Method put = cls.getMethod("put", Object.class, Object.class);

Method get = cls.getMethod("get", Object.class);

Object map = cls.newInstance();

for(int i = 0; i < 10 ; ++i)

put.invoke(map, i, i * i);

for(int i = 0; i < 10 ; ++i) {

Object value = get.invoke(map, i);

System.out.format("map[%d] = %s%n", i, value);

}

}

}

Hippolytea at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 12

> It has to do with swixml, during the parsing of the

> xml file. In the xml i have iterations that build

> panels and each of them have an id like this

> componentName + index. Hope its clear for you.

Can you expand on that? That sounds like a situation that maps (used the traditional way) are perfect for. If you have a map with string keys and panel values, then append the name with the index and get the corresponding panel from the map. I don't know anything about swixml though, so I'm sort of just guessing about how it uses the map.

hunter9000a at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 13
> > Ah, the other shoe drops. Yes it is possible, use> > reflection on the method "get".> > how ?It's just a method, dude, like any other. If you don't know how to invoke it by reflection, forget this
georgemca at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 14

> Can you expand on that? That sounds like a situation

> that maps (used the traditional way) are perfect for.

> If you have a map with string keys and panel values,

> then append the name with the index and get the

> corresponding panel from the map. I don't know

> anything about swixml though, so I'm sort of just

> guessing about how it uses the map.

That is what i m doing already, but then swixml get the fields from a class that contains my component(buttons, panel etc) that have an id and instanciate them when it founds this id in the xml file, but it doesnt know how to do it when component are created and given an id inside an iteration.

Message was edited by:

JusteUneQuestion

Message was edited by:

JusteUneQuestion

JusteUneQuestiona at 2007-7-12 19:43:39 > top of Java-index,Java Essentials,Java Programming...
# 15

>

> It has to do with swixml, during the parsing of the

> xml file.

I am glad to see that you are using this framework to make your code easier to maintain. I would treasure the delightful opportunity to maintain your code with it's abundant use of reflection. It would be a true joy because every day you would find a new nugget of pleasure hiding in the reeds waiting to be discovered at an inopportune time.

I can't help but feel that this thread http://forum.java.sun.com/thread.jspa?threadID=5185644&tstart=0 is very related to this one in many ways.

cotton.ma at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 16

> That is what i m doing already, but then swixml get

> the fields from a class that contains my

> component(buttons, panel etc) that have an id and

> instanciate them when it founds this id in the xml

> file, but it doesnt know how to do it when component

> are created and given an id inside an iteration.

I don't understand any of that sentence. Can you maybe post a small amount of code where you're trying to retrieve the components from the map?

hunter9000a at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 17
Wow u r such a cool poet, well its not my choice, i m just doing with it, and i m asking a simple question. If u dont know the answer just forget it, but you dont need to comment on what i m using or not, its not ur business.
JusteUneQuestiona at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 18

> Wow u r such a cool poet, well its not my choice, i m

> just doing with it, and i m asking a simple question.

> If u dont know the answer just forget it, but you

> dont need to comment on what i m using or not, its

> not ur business.

Developers in general (of which group I am a member) are well served by not condoning, promoting or supporting shitty code the likes of which you are writing.

So it is my business.

cotton.ma at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 19
...Edit: nevermind, I'm done helping you.Good luck.
prometheuzza at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 20
So be constructive then if u r such a great developper and keep ur shitty comment for urself. What u r saying is not helping me at all.
JusteUneQuestiona at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 21

> Wow u r such a cool poet, well its not my choice, i m

> just doing with it, and i m asking a simple question.

> If u dont know the answer just forget it, but you

> dont need to comment on what i m using or not, its

> not ur business.

It became everyone's business the second you posted a question on a public forum and asked for help. The purpose of the forum is to help you solve a problem. That includes rooting around and finding out why you're doing something, in the name of finding a possible alternative. So often, what appears to be a question is in fact a proposed - but fallible - solution to an unknown problem

georgemca at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 22

> So be constructive then if u r such a great

> developper and keep ur shitty comment for urself.

> What u r saying is not helping me at all.

We are all trying to help you and ask why you feel the need to do what you are doing. I asked why. Hippolyte asked why. Promtheuzz asked why. georgemc asked why.

But you won't say. Because you seem to think you know what's best.

Here's a clue. You don't.

So would you like to answer the question of why now?

cotton.ma at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 23

> So be constructive then if u r such a great

> developper and keep ur shitty comment for urself.

> What u r saying is not helping me at all.

It would do, if you co-operated. Trust us, we've done this a billion times. Nobody's trying to be sh!tty, we're trying to help. Since that help is free, you'll just have to put up with our choice of attitude. My mother always said "good manners cost nothing", but that's not true here. If you want help and you want to dictate the form and politeness of that help, it'll cost ya

georgemca at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 24

how to get the value of a hasmap by introspection, that is my question, i dont need to comment more on that, it is purely technical, it s straightforward. And if u dont understand four lines of code then dont say it is shitty. If u say it is shitty then say why and how to make it better. Now if u dont want u dont have to answer for nothing. I m just asking for a simple answer not the hole code.

By the way there is 2 pages of comment on nothing. Thanks a lot

Message was edited by:

JusteUneQuestion

JusteUneQuestiona at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 25

> how to get the value of a hasmap by introspection,

> that is my question, i dont need to comment more on

> that, it is purely technical, it s straightforward.

> And if u dont understand four lines of code then

> dont say it is shitty. If u say it is shitty then

> say why and how to make it better. Now if u dont

> want u dont have to answer for nothing. I m just

> asking for a simple answer not the hole code.

I've never yet encountered a need to reflectively access a Map. Nor it seems has anyone else. This leads us all to suspect you're barking up the wrong tree. Hence the questions. If you don't like it, then p1ss off somewhere else. Would you expect to get a straight answer from your doctor if you asked him "how would I fit a monkey wrench into an eye socket?"

georgemca at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 26

> how to get the value of a hasmap by introspection,

> that is my question, i dont need to comment more on

> that, it is purely technical, it s straightforward.

> And if u dont understand four lines of code then

> dont say it is shitty.

Not saying it doesn't make it less so.

So good luck to you then. Especially good luck to you when the maintainence programmers come after you with the pitchforks and torches.

cotton.ma at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 27

> how to get the value of a hasmap by introspection,

> that is my question, i dont need to comment more on

> that, it is purely technical, it s straightforward.

> And if u dont understand four lines of code then

> dont say it is shitty. If u say it is shitty then

> say why and how to make it better. Now if u dont

> want u dont have to answer for nothing. I m just

> asking for a simple answer not the hole code.

>

> By the way there is 2 pages of comment on nothing.

> Thanks a lot

Take a hike, bozo!

prometheuzza at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 28

My reply #11 demonstrated how to call "get" and "put" using reflection.

If I had to guess, I think you are populating a map from stuff read from a config file.

That's cool, but does the map itself need to be handled via reflection, or

is it just the contents that get created using reflection? That's what I'm wondering.

Hippolytea at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 29

> how to get the value of a hasmap by introspection,

> that is my question, i dont need to comment more on

> that, it is purely technical, it s straightforward.

> And if u dont understand four lines of code then

> dont say it is shitty. If u say it is shitty then

> say why and how to make it better. Now if u dont

> want u dont have to answer for nothing. I m just

> asking for a simple answer not the hole code.

>

> By the way there is 2 pages of comment on nothing.

> Thanks a lot

> Message was edited by:

> JusteUneQuestion

Hippolyte gave sample code to do that in reply 11. Does that work for you?

The reason we're asking why you need to use reflection is that it seems to us that you're doing things the hard way. It's much easier to just use the map as intended. If you're having problems doing it that way, we can help you figure out why.

hunter9000a at 2007-7-21 22:26:56 > top of Java-index,Java Essentials,Java Programming...
# 30
> > By the way there is 2 pages of comment on nothing.> Thanks a lotNo problem. Glad to help.You have a nice day then.
cotton.ma at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 31
> So be constructive then if u r such a great> developper and keep ur shitty comment for urself.> What u r saying is not helping me at all.Oh goody. Another one for the killfile.
jverda at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 32

Before we pound the OP into the ground like a tent stake, consider any number

of frameworks, like JSF. It sports a config file syntax where you specify

parameters in a very generic way. If a class you are configuring has a Map-valued property,

tyou can define that map and it's contents in the config file.

JSF uses reflection to reify that.

Hippolytea at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 33

Thanks for answer on 11, that is all i need, and not the rest of comment. If i post on how to get the value of hashmap by introspection is because i need it. If there was other way of doing it, it would be via some swixml tag or something like this but i dont know it so i m doing via introspection, what 's wrong with this, everything is done via introspection already, i just add small piece of code where it goes and that is it.

JusteUneQuestiona at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 34

> Before we pound the OP into the ground like a tent

> stake, consider any number

> of frameworks, like JSF. It sports a config file

> syntax where you specify

> parameters in a very generic way. If a class you are

> configuring has a Map-valued property,

> tyou can define that map and it's contents in the

> config file.

> JSF uses reflection to reify that.

I don't care if his approach is valid. When questioned on that validity, he got snitty, so I'm just adding his name to the list of people who aren't worth my time to help.

jverda at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 35

> I don't care if his approach is valid. When

> questioned on that validity, he got snitty, so I'm

> just adding his name to the list of people who aren't

> worth my time to help.

True. I was separating what he asked from how he asked it.

Since it's Monday morning, I'm giving all posters extra slack.

Hippolytea at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 36
> Oh goody. Another one for the killfile.That is what u said, so i dont care what u say as well, u already didnt help me. U think u know better than anyone, but i m the one with the code and i know what i have to ask. Dont need to make people feel stupid by the way u answer.
JusteUneQuestiona at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 37

> > I don't care if his approach is valid. When

> > questioned on that validity, he got snitty, so I'm

> > just adding his name to the list of people who

> aren't

> > worth my time to help.

>

> True. I was separating what he asked from how he

> asked it.

> Since it's Monday morning, I'm giving all posters

> extra slack.

Fair enough. I haven't had any caffeine yet. No slack for anybody!

jverda at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 38

> > Oh goody. Another one for the killfile.

>

> That is what u said, so i dont care what u say as

> well, u already didnt help me. U think u know better

> than anyone, but i m the one with the code and i know

> what i have to ask. Dont need to make people feel

> stupid by the way u answer.

How true. You are stupid all on your own without trying and no help is needed from anyone else for you to display this.

cotton.ma at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 39
If i didnt explain more about the problem is that i dont know really how the hole thing works and it would take too much time and i dont want to spend time learning swixml. My question at the base was simple, and you just turn around.
JusteUneQuestiona at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 40

> If i didnt explain more about the problem is that i

> dont know really how the hole thing works and it

> would take too much time and i dont want to spend

> time learning swixml. My question at the base was

> simple, and you just turn around.

cuz you're not welcome anymore

weren't you the one who tried to hurt me with goodbye

you think I'd crumble

you think I'd lay down and die

Oh no, not I

I will survive

cotton.ma at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 41

JusteUneQuestion:

Often there are posts with questions like; "how do I pound nails with a banana?"

One could reply with suggestions to freeze the banana in liquid nitrogen, but

perhaps the better approach is to find out why the OP thinks he should use a

banana in the first place.

In fact, it's surprising how often questions of this ilk do come up.

Hippolytea at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 42
You are arrogant in your answer. I dont know why u r here answering the stupid person.
JusteUneQuestiona at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 43
> You are arrogant in your answer. I dont know why u r> here answering the stupid person.This is good. Admitting the problem is the first step to recovery.
cotton.ma at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 44

> JusteUneQuestion:

>

> Often there are posts with questions like; "how do I

> pound nails with a banana?"

> One could reply with suggestions to freeze the banana

> in liquid nitrogen, but

> perhaps the better approach is to find out why the OP

> thinks he should use a

> banana in the first place.

>

> In fact, it's surprising how often questions of this

> ilk do come up.

I know why you asked but it was useless to explain more i thought...

JusteUneQuestiona at 2007-7-21 22:27:02 > top of Java-index,Java Essentials,Java Programming...
# 45

@OP:

Just to be clear:

The stuff in reply #11 is aequivalent to the following non-introspective version:import java.util.*;

public class NoReflectExample {

public static void main(String[] args) throws Exception {

Map map = new HashMap();

for(int i = 0; i < 10 ; ++i)

map.put(i, i * i);

for(int i = 0; i < 10 ; ++i) {

Object value = map.get(i);

System.out.format("map[%d] = %s%n", i, value);

}

}

}

This accomplishes almost certainly what you are trying to do. Try it!

Side note:

This sample uses autoboxing and will only work for JDK 1.5 and up.

thomas.behra at 2007-7-21 22:27:07 > top of Java-index,Java Essentials,Java Programming...
# 46

> > JusteUneQuestion:

> >

> > Often there are posts with questions like; "how do

> I

> > pound nails with a banana?"

> > One could reply with suggestions to freeze the

> banana

> > in liquid nitrogen, but

> > perhaps the better approach is to find out why the

> OP

> > thinks he should use a

> > banana in the first place.

> >

> > In fact, it's surprising how often questions of

> this

> > ilk do come up.

>

> I know why you asked but it was useless to explain

> more i thought...

You were wrong. A huge number of questions end up being resolved not by the OPs actual question being answered, but by it being disregarded altogether and the underlying problem extracted and solved instead.

georgemca at 2007-7-21 22:27:07 > top of Java-index,Java Essentials,Java Programming...
# 47

Yes i know that, sorry to know something... but it would have too long to explain everything, that is all, i was just searching for bit of code to get me started and some idea, maybe i will change and not use reflection, but i know what i m doing and what i m asking for. Its like this i can do nothing more about it.

JusteUneQuestiona at 2007-7-21 22:27:07 > top of Java-index,Java Essentials,Java Programming...
# 48

> Often there are posts with questions like; "how do I

> pound nails with a banana?"

> One could reply with suggestions to freeze the banana

> in liquid nitrogen, but

> perhaps the better approach is to find out why the OP

> thinks he should use a

> banana in the first place.

I'm gonna steal that one! :-)

jverda at 2007-7-21 22:27:07 > top of Java-index,Java Essentials,Java Programming...
# 49

> Yes i know that, sorry to know something... but it

> would have too long to explain everything, that is

> all, i was just searching for bit of code to get me

> started and some idea, maybe i will change and not

> use reflection, but i know what i m doing and what i

> m asking for. Its like this i can do nothing more

> about it.

I'm curious about what kind of situation would have you knowing that you have to call a get(key) method to get a value, but not know that you're operating on a Map--as that's the only situation in which reflection is needed, rather than just directly calling Map's get method.

jverda at 2007-7-21 22:27:07 > top of Java-index,Java Essentials,Java Programming...