• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reflection got at field editing exception

 
Ranch Hand
Posts: 67
PHP Debian Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
mandlar suurla
Ranch Hand
Posts: 67
PHP Debian Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that's right but is there possible to return the class instance some how or need i create it manual?
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
PHP Debian Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 67
PHP Debian Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes that's right! Is it possible or need to use some bytecode library ?
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
mandlar suurla
Ranch Hand
Posts: 67
PHP Debian Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No they are not running seperate JVM-s just are seperate programs.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 67
PHP Debian Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic