File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Reflection got at field editing exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflection got at field editing exception " Watch "Reflection got at field editing exception " New topic
Author

Reflection got at field editing exception

mandlar suurla
Ranch Hand

Joined: Jun 11, 2008
Posts: 67

Problem is that i cant access other class private member number, it gives me exception and i dont understand the exception meaning ?


Exception in thread "main" java.lang.IllegalArgumentException: Can not set int field DesignerSwingTest.number to java.lang.Class
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
at sun.reflect.UnsafeIntegerFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at Reflection.main(Reflection.java:89)number
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20


f is a field of class DesignerSwingTest. Therefore, it can only get the fields from objects that are instances of DesignerSwingTest or subclasses. The object you're trying to get the field from is a Class object.
Replace that c with an instance of DesignerSwingTest and it'll work.

This is just API related so I'm moving this to Java in general.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
mandlar suurla
Ranch Hand

Joined: Jun 11, 2008
Posts: 67

Yes that's right but is there possible to return the class instance some how or need i create it manual?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

If it is a static field, just use null as the object. This is specified in the JavaDoc for the get(Object) method:
mandlar suurla
Ranch Hand

Joined: Jun 11, 2008
Posts: 67

Ty your helping me lot, but if its not static, then i need create instance right, but how to do that?
I got swing app and there is private field number. When i am running that app i want get with other app the number field. I cant just make instance of that program SwingApp app = SwingApp();, i need to get that instance which is running in that movement. I hope you understand my text. If not ill try to explain more.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
Are you asking how to get the contents of a private member of an existing object? Will have to do some research to see if/how it can be done.
mandlar suurla
Ranch Hand

Joined: Jun 11, 2008
Posts: 67

yes that's right! Is it possible or need to use some bytecode library ?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
It shouldn't be possible. It would violate the private attribute!
If you need to see a private variable's content at runtime, you need to modify the class so it will give you the value you want to see.
There wouldn't be much security if you could bypass it using reflection.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Originally posted by mandlar suurla:
Ty

Please UseRealWords

then i need create instance right, but how to do that?

Using the "new" keyword would probably help a lot

Originally posted by Norm Radder:
It shouldn't be possible. It would violate the private attribute!
If you need to see a private variable's content at runtime, you need to modify the class so it will give you the value you want to see.
There wouldn't be much security if you could bypass it using reflection.

Both Field and Method have a method (inherited from Member) called "setAccessible". With that, you can try to make it accessible. I say try, because a security manager may prevent it. Never rely on such techniques.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 690
    
    1
thanks Rob.
I'm suprised that you can so easily see the value of a private member. I wrote a short test program and it worked!


The error message was from when I ran it without setAccessible()
[ June 26, 2008: Message edited by: Norm Radder ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by mandlar suurla:
Ty your helping me lot, but if its not static, then i need create instance right, but how to do that?
I got swing app and there is private field number. When i am running that app i want get with other app the number field. I cant just make instance of that program SwingApp app = SwingApp();, i need to get that instance which is running in that movement. I hope you understand my text. If not ill try to explain more.


You mean those two programs are running in separate JVMs? Then reflection won't help you a bit.

*Why* would you want to do such a thing?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
mandlar suurla
Ranch Hand

Joined: Jun 11, 2008
Posts: 67

No they are not running seperate JVM-s just are seperate programs.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Originally posted by Norm Radder:
thanks Rob.
I'm suprised that you can so easily see the value of a private member. I wrote a short test program and it worked!

I have even been able to change a String! It was quite funny to see one String change because I changed another one, simply because they can share their internal char[].

Originally posted by mandlar suurla:
No they are not running seperate JVM-s just are seperate programs.

Separate programs means separate JVMs. Each time you call "java" or "javaw", you start a new JVM.
mandlar suurla
Ranch Hand

Joined: Jun 11, 2008
Posts: 67

Yeah, that i didn't know, but when using some bytecode library is it possible to see the object values? I think ill use ObjectWeb ASM library.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reflection got at field editing exception