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

Discussion in 'Java' started by szczepiq, Apr 27, 2008.

  1. szczepiq

    szczepiq Guest

    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
     
    szczepiq, Apr 27, 2008
    #1
    1. Advertising

  2. szczepiq

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Apr 27, 2008
    #2
    1. Advertising

  3. szczepiq

    szczepiq Guest

    > 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 <> 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
     
    szczepiq, Apr 27, 2008
    #3
  4. szczepiq

    Roedy Green Guest

    On Sun, 27 Apr 2008 08:37:26 -0700 (PDT), szczepiq
    <> 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
     
    Roedy Green, Apr 28, 2008
    #4
  5. szczepiq

    Guest

    On Apr 27, 5:09 pm, szczepiq <> 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
     
    , Apr 30, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    1
    Views:
    1,420
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    618
    SpamProof
    Oct 21, 2003
  3. DaveLessnau
    Replies:
    3
    Views:
    443
    Howard
    May 16, 2005
  4. Chip Gore
    Replies:
    6
    Views:
    2,330
    Ben Voigt [C++ MVP]
    Apr 10, 2008
  5. Stefan Ram
    Replies:
    1
    Views:
    352
    Owen Jacobson
    May 21, 2008
Loading...

Share This Page