Accessing a privat method?

J

Jeroen Budts

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

Is it somehow possible to access a private method of one class from
inside another class?
by example: I have a class MyEntity with a private method setFoo(int n)
and i want to call it from inside the class MyBar. I know this is
against the rules of encapsulation but somehow it is possible i think,
since Hibernate seems to do this. For Hibernate it doesn't mather if the
accessors of a mapped attribute are private, protected or public, it
will set them anyway..
Do I need something like ASM for this or can it be done with reflection?

thx!
Jeroen

- --
<TeRanEX/>
--- e-mail: (e-mail address removed) - jid: (e-mail address removed)
--- blog: http://budts.be/weblog/ - cv: http://budts.be/jeroen/
--- projects: http://lightyear.be - pgp: 0x8B7B774A


_____________________________________
NO SoftwarePatents in Europe!
See an example: http://webshop.ffii.org/


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCgNFmH04wF4t7d0oRArVWAKCCVpPD9fxxSm0DiahNAFL4qNieHwCffEe9
t0cSRRwCYmVZmaThBsIyIFk=
=RhfE
-----END PGP SIGNATURE-----
 
B

Bryce

Is it somehow possible to access a private method of one class from
inside another class?
by example: I have a class MyEntity with a private method setFoo(int n)
and i want to call it from inside the class MyBar. I know this is
against the rules of encapsulation but somehow it is possible i think,
since Hibernate seems to do this. For Hibernate it doesn't mather if the
accessors of a mapped attribute are private, protected or public, it
will set them anyway..
Do I need something like ASM for this or can it be done with reflection?

It can be done with reflection, but I would question the design if
this was required...
 
Z

zebulon

I can tell you that with reflection, you can modify a private field

(field.setAccessible(true) ...)
 
R

Ross Bamford

It can be done with reflection, but I would question the design if
this was required...

Agreed. Don't make it private if you don't want it to be private :>. IMO
Reflection should only be used for this in three cases:

1) You are writing persistence / serialization / etc and *really* know
what you're doing, and

2) You're forced to use someone else's broken design (and only then if
you really really can't work around it some other way).

At the end of the day, when you make a member private you're saying you
don't even trust subclass implementors to access it, so you'd better
have a damn good reason to force the issue.

For some reason this is currently my favourite sig =]

Ross
 
K

Kristof Rennen

Hi,

I'm working together on the project and we use reflection for a own
persistency framework. All entities got the game an id, the programmer
shouldn't be able to set the id, but our loading method does. The only
way we could find out was reflection but even with setAccessible(true)
didn't work out :(.
 
A

Andrew McDonagh

Kristof said:
Hi,

I'm working together on the project and we use reflection for a own
persistency framework. All entities got the game an id, the programmer
shouldn't be able to set the id, but our loading method does. The only
way we could find out was reflection but even with setAccessible(true)
didn't work out :(.

You can get lots of ideas from taking a look at
http://sourceforge.net/projects/skaringa/

its a Java <--> XML framework, which can be used together with
persistent frameworks like Prevayler - (in fact, I modified Prevayler to
use Skaringa)

HTH

Andrew
 
R

Ross Bamford

Hi,

I'm working together on the project and we use reflection for a own
persistency framework. All entities got the game an id, the programmer
shouldn't be able to set the id, but our loading method does. The only
way we could find out was reflection but even with setAccessible(true)
didn't work out :(.

Private members are different to other methods, in that they're not
'virtual' (They simply don't exist for other classes, because they could
never call them anyway).

Something that occurs off the top of my head is why not serialise your
object, which takes care of all this for you. You can serialize to a
byte stream if you want to handle the data specially, and also get
control over the reading and writing of the object.

If your needs are more complex, you could start with a 'base' object
read from a stream to setup private members and stuff, and then setup
your own stuff on top.

Alternatively, look at Externalizable if your needs are more
complicated.

Just an idea ...

Ross
 

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,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top