Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to let the methods of a class only can be invoked by a specialpackage(the class not in the package)?

Reply
Thread Tools

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

 
 
Jebel.Star@gmail.com
Guest
Posts: n/a
 
      01-22-2008
There is a class A in package XX.ZZ.GG , and A has a static method
A.GetInstance(). There are 3 classes B, C, D in package XX.ZZ .
How can I make the A.GetInstance() only can be invoked by the B, C and
D.
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      01-22-2008
On Mon, 21 Jan 2008 20:01:50 -0800 (PST), wrote,
quoted or indirectly quoted someone who said :

>How can I make the A.GetInstance() only can be invoked by the B, C and
>D.


There are three levels: public -- everyone, package, just in same
package, and private -- just current class.

To pull off what you want means putting the classes that can talk in
the same package.

see http://mindprod.com/jgloss/package.html
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      01-22-2008
wrote:
> There is a class A in package XX.ZZ.GG , and A has a static method
> A.GetInstance(). There are 3 classes B, C, D in package XX.ZZ .
> How can I make the A.GetInstance() only can be invoked by the B, C and
> D.


There are really untidy things you could do at run time to get a stack
trace and check that the caller is one of the specified method.

However, it would be much better to reorganize the packaging.

Patricia
 
Reply With Quote
 
Jebel.Star@gmail.com
Guest
Posts: n/a
 
      01-22-2008
On Jan 22, 12:51 pm, Patricia Shanahan <p...@acm.org> wrote:
> Jebel.S...@gmail.com wrote:
> > There is a class A in package XX.ZZ.GG , and A has a static method
> > A.GetInstance(). There are 3 classes B, C, D in package XX.ZZ .
> > How can I make the A.GetInstance() only can be invoked by the B, C and
> > D.

>
> There are really untidy things you could do at run time to get a stack
> trace and check that the caller is one of the specified method.
>
> However, it would be much better to reorganize the packaging.
>
> Patricia


Unfortunately I have no right to reorganize the packaging, and I don't
known how to get a stack in the JVM with Ver 1.3 .
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-22-2008
Roedy Green wrote:
> On Mon, 21 Jan 2008 20:01:50 -0800 (PST), wrote,
> quoted or indirectly quoted someone who said :
>
>> How can I make the A.GetInstance() only can be invoked by the B, C and
>> D.

>
> There are three levels: public -- everyone, package, just in same
> package, and private -- just current class.


And protected - package-private plus inheriting classes. Don't forget.

--
Lew
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      01-22-2008
wrote:
> On Jan 22, 12:51 pm, Patricia Shanahan <p...@acm.org> wrote:
>> Jebel.S...@gmail.com wrote:
>>> There is a class A in package XX.ZZ.GG , and A has a static method
>>> A.GetInstance(). There are 3 classes B, C, D in package XX.ZZ .
>>> How can I make the A.GetInstance() only can be invoked by the B, C and
>>> D.


Can you make the A.staticMethod() protected?

If so, protected access might give you partial protection - any other package
can have an extending class, but they must do so in order to access the method.

<http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6>
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.2.1>

--
Lew
 
Reply With Quote
 
Andreas Leitgeb
Guest
Posts: n/a
 
      01-22-2008
<> wrote:
> On Jan 22, 12:51 pm, Patricia Shanahan <p...@acm.org> wrote:
>> Jebel.S...@gmail.com wrote:
>> > There is a class A in package XX.ZZ.GG , and A has a static method
>> > A.GetInstance(). There are 3 classes B, C, D in package XX.ZZ .
>> > How can I make the A.GetInstance() only can be invoked by the B, C and
>> > D.

>>
>> There are really untidy things you could do at run time to get a stack
>> trace and check that the caller is one of the specified method.
>>
>> However, it would be much better to reorganize the packaging.
>>
>> Patricia

> Unfortunately I have no right to reorganize the packaging, and I don't
> known how to get a stack in the JVM with Ver 1.3 .


You could throw an exception, catch it immediately, and see what you
can read from the exception's printStackTrace(), by passing it your
own PrintWriter (or PrintStream), wrapped around some ByteArrayOutputStream.

I just tried it with an (admittedly newer-than-1.3) java:
class X {
public static void main(String[] args) { foo(); }
public static int foo() {
try {
throw new Exception("foo");
} catch (Exception e) {
e.printStackTrace(System.out);
}
return 0;
}
}
Try, if that also works with java-1.3, in that it also
writes the "outer"-levels. To implement this in to-be-
protected code, You'd of course not print to stdout, but
instead to a bytearray.
Happy parsing that automatically.

.... But then, again, it's quite an unusual wish, what you made.
 
Reply With Quote
 
Jebel.Star@gmail.com
Guest
Posts: n/a
 
      01-24-2008
On Jan 23, 12:03 am, Andreas Leitgeb <a...@gamma.logic.tuwien.ac.at>
wrote:
> Jebel.S...@gmail.com <Jebel.S...@gmail.com> wrote:
> > On Jan 22, 12:51 pm, Patricia Shanahan <p...@acm.org> wrote:
> >> Jebel.S...@gmail.com wrote:
> >> > There is a class A in package XX.ZZ.GG , and A has a static method
> >> > A.GetInstance(). There are 3 classes B, C, D in package XX.ZZ .
> >> > How can I make the A.GetInstance() only can be invoked by the B, C and
> >> > D.

>
> >> There are really untidy things you could do at run time to get a stack
> >> trace and check that the caller is one of the specified method.

>
> >> However, it would be much better to reorganize the packaging.

>
> >> Patricia

> > Unfortunately I have no right to reorganize the packaging, and I don't
> > known how to get a stack in the JVM with Ver 1.3 .

>
> You could throw an exception, catch it immediately, and see what you
> can read from the exception's printStackTrace(), by passing it your
> own PrintWriter (or PrintStream), wrapped around some ByteArrayOutputStream.
>
> I just tried it with an (admittedly newer-than-1.3) java:
> class X {
> public static void main(String[] args) { foo(); }
> public static int foo() {
> try {
> throw new Exception("foo");
> } catch (Exception e) {
> e.printStackTrace(System.out);
> }
> return 0;
> }}
>
> Try, if that also works with java-1.3, in that it also
> writes the "outer"-levels. To implement this in to-be-
> protected code, You'd of course not print to stdout, but
> instead to a bytearray.
> Happy parsing that automatically.
>
> ... But then, again, it's quite an unusual wish, what you made.


Thanks for all of you. You let me to find the limitation of my design.
So I change it.

I changed my design:

1 Add a protect method GetInstance() in Class B;
2 Add a class E which is extends from B in package XX.ZZ.GG. Create a
protected method GetInstance() which has the same function of
A.GetInstance() in class E. Remove the Method A.GetInstance().
3 Add a static member 'B e ' withe the default access right in Class
B, construct 'e'in the static block of B as 'E':
static{ e = new E; }

In this design B,C and D can invoke B.e.GetInstance() which is
E.GetInstance(), any other classes that is not in package XX.ZZ (and
also not in package XX.ZZ.GG) can't invoke the E.GetInstance().

Is my design OK ?



 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      01-24-2008
On Mon, 21 Jan 2008 20:01:50 -0800 (PST), wrote,
quoted or indirectly quoted someone who said :

>There is a class A in package XX.ZZ.GG , and A has a static method
>A.GetInstance(). There are 3 classes B, C, D in package XX.ZZ .
>How can I make the A.GetInstance() only can be invoked by the B, C and
>D.


Please follow the caps conventions. See
http://mindprod.com/jgloss/codingconventions.html

Your question translated then becomes:

>There is a class A in package xx.zz.gg, and A has a static method
>A.getInstance(). There are 3 classes B, C, D in package xx.zz .
>How can I make A.getInstance()so that it can only be invoked from
>classes B, C and D.


You can't. To do that classes A, B, C and D would have to be in the
same package, and getInstance would need default package scope. You
could still invoke it from class A.

Note that, as far as Java is concerned, package xx.zz.gg and package
xx.zz are TOTALLY unrelated.

see http://mindprod.com/jgloss/package.html
--
Roedy Green, Canadian Mind Products
The Java Glossary, http://mindprod.com
 
Reply With Quote
 
Jebel
Guest
Posts: n/a
 
      01-24-2008
On Jan 24, 4:57 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Mon, 21 Jan 2008 20:01:50 -0800 (PST), Jebel.S...@gmail.com wrote,
> quoted or indirectly quoted someone who said :
>
> >There is a class A in package XX.ZZ.GG , and A has a static method
> >A.GetInstance(). There are 3 classes B, C, D in package XX.ZZ .
> >How can I make the A.GetInstance() only can be invoked by the B, C and
> >D.

>
> Please follow the caps conventions. Seehttp://mindprod.com/jgloss/codingconventions.html
>
> Your question translated then becomes:
>
> >There is a class A in package xx.zz.gg, and A has a static method
> >A.getInstance(). There are 3 classes B, C, D in package xx.zz .
> >How can I make A.getInstance()so that it can only be invoked from
> >classes B, C and D.

>
> You can't. To do that classes A, B, C and D would have to be in the
> same package, and getInstance would need default package scope. You
> could still invoke it from class A.
>
> Note that, as far as Java is concerned, package xx.zz.gg and package
> xx.zz are TOTALLY unrelated.
>
> seehttp://mindprod.com/jgloss/package.html
> --
> Roedy Green, Canadian Mind Products
> The Java Glossary,http://mindprod.com


I know that "package xx.zz.gg and package xx.zz are TOTALLY unrelated
".The names of the two packages are no meaning for my problem.

The classes A and E are in the package XX.ZZ.GG; and the classes B,
C and D are in the package XX.ZZ.
The class E extends from class B.

When the class C wants to invoke the E.GetInstance()(has the same
function of the previous A.GetInstance) it invokes B.e.GetInstance().

package XX.ZZ;
import XX.ZZ.GG.A;
public class C
{
public void Func()
{
... ...
A a = B.e.GetInstanc();
... ...

}

}


The 'e' is a static member of class B, and 'e' is declared as B but
construct as E:

package XX.ZZ;
import XX.ZZ.GG.E;
import XX.ZZ.GG.A;
public class B
{
static B e;

static{
e = new E;
}
protect A GetInstance(){ return null;}

... ...
}

And the class E and class A are in the same package, E can do some
thing in its method GetInstance with A.
package XX.ZZ.GG;
import XX.ZZ.B;
final public class C extends B
{
private static A onlyOne;
protect A GetInstance()
{
if(onlyOne == NULL){ onlyOne = new A;}
return onlyOne;
}
}

Class A :
package XX.ZZ.GG;
public class A
{
// the access right of constructer of A is
default( the class in the same package can access)
A(){... ...}

... ...
}

That is all.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Can static methods be virtual i.e. invoked polymorphically? lirrar@yahoo.com Java 7 04-16-2010 06:19 AM
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
winxp IE will not let you exit, and my comptuer / other folders will not let you open them. Daniel NZ Computing 2 05-11-2004 02:02 AM
Let or not let the text float Luigi Donatello Asero HTML 6 01-15-2004 04:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57