reflection: can I hack the class to make it public?

S

szczepiq

Hi,

If a class is package protected, is there a way to hack it using
reflection and change the class to be public?

Cheers,
Szczepan
 
A

Arne Vajhøj

szczepiq said:
If a class is package protected, is there a way to hack it using
reflection and change the class to be public?

Not really.

But you can create a new class based on the byte code of the
old one just public.

Or you can create another class that delegates to the first
class.

Consider though that someone may have made it package protected
for a reason !

Arne
 
S

szczepiq

Consider though that someone may have made it package protected
for a reason !

Don't worry, I'm not that evil ;) The problem I have is related to
mocking, precisely to creating cglib proxies of classes from signed
packages. Basically, ClassLoader doesn't like it and the workaround is
to change the package of the generated class. But when I change the
package I have problems with package protected classes...

Anyway, thanks for your replay!
Szczepan
 
A

alexandre_paterson

Don't worry, I'm not that evil ;) The problem I have is related to
mocking

I know you're not the author of the 'protected' class, but seen
your comment I want say something: easier testing (and easier testing
using mocks if you use these kind of practices) is one of the many,
many benefits a 'clean' OO design brings (i.e., in Java, one based
on interface and considering implementation as a detail).

I dropped the 'abstract' and 'protected' keywords from my Java
vocabulary
several years ago and I never looked back.

Packages that make you rely on concrete implementation
are broken for a lot of reasons and are very often a pain
in the arse to use for a great many kind of testing.

People have been saying that all along and warning about that a long
time ago. Yet many didn't listen.

On the other hand there are wonderfull APIs and frameworks, very
successfully used in the Real World [TM], that are correctly designed.

Of course you'll find many people disagreeing with me, but then
they're
disagreeing too with Bjarne Stroustrup, James Gosling, Coplien,
etc. ;)

I won't argue with the nay-sayers, I'm glad to be called an 'interface
biggot' and I'll stick with my "appeal-to-authority" logical
fallacy :)

Talk to you soon,

Alex


--
"I have consistently pointed out that one of the major ways of writing
"classes in C++ is without any state, that is, just an interface.
-- Bjarne Stroustrup

"Rather than subclassing, just use pure interfaces. It's not so much
"that class inheritance is particularly bad. It just has problems.
-- James Gosling
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top