How to let the methods of a class only can be invoked by a specialpackage(the class not in the packa

A

Andreas Leitgeb

Jebel said:
I not understand how to do this. How can I "wrap" B's methods ?
Would you please provide some code as a example?

It was based on a wrong understanding on my side. Sorry, but
I think it doesn't apply, anyway.

The point was about your class in xx.zz.gg which you could
give a such limited interface, that noone could do anything
harmful with it, even if the wrong "user" tries...

The wrapping was just meant as a way to restrict the interface:
e.g.:
package xx.zz.gg;
class B {
// lots of public methods, some of which are potentially dangerous
public void foo() { ... }
public void bar() { ... }
public void snafu() { ... }
...
}
and:
package xx.zz.gg;
class BWrapper {
B wrappedB;
BWrapper(B b) { wrappedB=b; }
private B getB() { return wrappedB; }
// just the really important methods:
public void foo() { do some checks, and then: getB().foo(); }
}
After that, you can give all the methods of B default-access,
and no one else can use anything than foo() on B, and only through
your class BWrapper (unless they find a way to sneak code into
xx.zz or xx.zz.gg bypassing your classloader, or patching their
JVM).

Also, I don't yet understand what big evil would be unleashed by
letting people just use your xx.zz.gg.B as they feel like.
 
J

Jebel

It seems to me you would have to make getClassContext public. Is it
accessible just as protected? The original was already protected.

// Determining the calling class.
// ca[0] is the anonymous security manager
// ca[1] is this class
// ca[2] is the calling class
Class[] ca = new SecurityManager()
{
// convert from protected to public to make the method accessible.
public Class[] getClassContext()
{
return super.getClassContext();
}
}.getClassContext();

No, it's needn't. A class can invoke the methods with protected-access
of the inner classes.
 
J

Jebel

It was based on a wrong understanding on my side. Sorry, but
I think it doesn't apply, anyway.

The point was about your class in xx.zz.gg which you could
give a such limited interface, that noone could do anything
harmful with it, even if the wrong "user" tries...

The wrapping was just meant as a way to restrict the interface:
e.g.:
package xx.zz.gg;
class B {
// lots of public methods, some of which are potentially dangerous
public void foo() { ... }
public void bar() { ... }
public void snafu() { ... }
...}

and:
package xx.zz.gg;
class BWrapper {
B wrappedB;
BWrapper(B b) { wrappedB=b; }
private B getB() { return wrappedB; }
// just the really important methods:
public void foo() { do some checks, and then: getB().foo(); }}

After that, you can give all the methods of B default-access,
and no one else can use anything than foo() on B, and only through
your class BWrapper (unless they find a way to sneak code into
xx.zz or xx.zz.gg bypassing your classloader, or patching their
JVM).

Also, I don't yet understand what big evil would be unleashed by
letting people just use your xx.zz.gg.B as they feel like.

Thank you Andreas.You have teach me much, and now I understand what is
the ' wrap ' . :)
Thank you very much.
 

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,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top