Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > reflection: can I hack the class to make it public?

Reply
Thread Tools

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

 
 
szczepiq
Guest
Posts: n/a
 
      04-27-2008
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
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      04-27-2008
szczepiq wrote:
> 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
 
Reply With Quote
 
 
 
 
szczepiq
Guest
Posts: n/a
 
      04-27-2008
> 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

On Apr 27, 4:42 pm, Arne Vajh°j <(E-Mail Removed)> wrote:
> szczepiq wrote:
> > 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


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-28-2008
On Sun, 27 Apr 2008 08:37:26 -0700 (PDT), szczepiq
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
said :

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


If you understand the class format, you could tweak the attribute
bites..

See http://mindprod.com/jgloss/javaclassformat.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
alexandre_paterson@yahoo.fr
Guest
Posts: n/a
 
      04-30-2008
On Apr 27, 5:09 pm, szczepiq <(E-Mail Removed)> wrote:
> > 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


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hack to make a member function final Rajib C++ 1 08-03-2008 01:29 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Hack can upgrade XP Home to XP Pro Lite??? =?Utf-8?B?0KHQsNGI0LA=?= Windows 64bit 16 06-15-2005 11:31 PM
Can you hack Salvia divinorum? Many can't. dusenostachys@yahoo.com Computer Security 0 03-02-2005 12:27 PM
HACK YOUR DVD PLAYER (Make it MULTIREGIONAL) in SECONDS! Aareth DVD Video 5 10-08-2004 05:40 AM



Advertisments