* Using CNI on inner classes
@ 2009-08-13 7:18 Vaijayanthi Mala Suresh
2009-08-13 9:10 ` Andrew Haley
0 siblings, 1 reply; 5+ messages in thread
From: Vaijayanthi Mala Suresh @ 2009-08-13 7:18 UTC (permalink / raw)
To: java
Hi,
I have an Test.java as shown below
public class Test {
private native void natRegister(Inner obj);
Inner myInnerClassObj;
class Inner
{
private void printline()
{
System.out.printline("Prints from printline");
}
Inner()
{
}
}
public Test()
{
myInnerClassObj = new Inner();
}
public void register()
{
natRegister(myInnerClassObj);
}
public static void main(String [] args) {
System.out.println("Hello from main");
Test ht = new Test();
ht.register();
}
}
I have the Test.cc defined as shown below
// This file is intended to give you a head start on implementing native
// methods using CNI.
// Be aware: running 'gcjh -stubs' once more for this class may
// overwrite any edits you have made to this file.
#include <gcj/cni.h>
#include <java/lang/UnsupportedOperationException.h>
#include "Test.h"
#include "Test$Inner.h"
void
Test::natRegister(::Test$Inner *innerObj)
{
innerObj->printline();
}
I need to access the private method from the native code. This was
possible in JNI but it is not possible in CNI.
Can you please suggest me a solution for this using CNI.
Thanks
Mala
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Using CNI on inner classes
2009-08-13 7:18 Using CNI on inner classes Vaijayanthi Mala Suresh
@ 2009-08-13 9:10 ` Andrew Haley
2009-08-13 10:07 ` Bryce McKinlay
0 siblings, 1 reply; 5+ messages in thread
From: Andrew Haley @ 2009-08-13 9:10 UTC (permalink / raw)
To: Vaijayanthi Mala Suresh; +Cc: java
Vaijayanthi Mala Suresh wrote:
> Hi,
>
> I have an Test.java as shown below
>
>
> public class Test {
>
> private native void natRegister(Inner obj);
> Inner myInnerClassObj;
>
> class Inner
> {
> private void printline()
> {
> System.out.printline("Prints from printline");
> }
> Inner()
> {
> }
> }
>
> public Test()
> {
> myInnerClassObj = new Inner();
> }
>
> public void register()
> {
> natRegister(myInnerClassObj);
> }
>
>
> public static void main(String [] args) {
> System.out.println("Hello from main");
> Test ht = new Test();
> ht.register();
> }
> }
>
> I have the Test.cc defined as shown below
> // This file is intended to give you a head start on implementing native
> // methods using CNI.
> // Be aware: running 'gcjh -stubs' once more for this class may
> // overwrite any edits you have made to this file.
>
> #include <gcj/cni.h>
> #include <java/lang/UnsupportedOperationException.h>
>
> #include "Test.h"
> #include "Test$Inner.h"
> void
> Test::natRegister(::Test$Inner *innerObj)
> {
> innerObj->printline();
> }
>
> I need to access the private method from the native code. This was
> possible in JNI but it is not possible in CNI.
>
> Can you please suggest me a solution for this using CNI.
If the method is declared private, it really will be private, even
to cni.
If you really need to breach privacy, you can do this in Test$Inner.h:
class Test$Inner : public ::java::lang::Object
{
friend class ::Test;
but I can't really see the point of private methods in an inner class
that you actually intend to be called from the enclosing class.
Andrew.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Using CNI on inner classes
2009-08-13 9:10 ` Andrew Haley
@ 2009-08-13 10:07 ` Bryce McKinlay
2009-08-13 10:44 ` Andrew Haley
0 siblings, 1 reply; 5+ messages in thread
From: Bryce McKinlay @ 2009-08-13 10:07 UTC (permalink / raw)
To: Andrew Haley; +Cc: Vaijayanthi Mala Suresh, java
On Thu, Aug 13, 2009 at 10:10 AM, Andrew Haley<aph@redhat.com> wrote:
> If the method is declared private, it really will be private, even
> to cni.
>
> If you really need to breach privacy, you can do this in Test$Inner.h:
>
> class Test$Inner : public ::java::lang::Object
> {
> friend class ::Test;
>
> but I can't really see the point of private methods in an inner class
> that you actually intend to be called from the enclosing class.
In Java the private methods are accessible to the enclosing class. So,
ideally gcjh would add friend declarations, like the above,
automatically to inner classes to approximate the Java semantics.
But, the easiest fix here is just to make the private method
package-private instead.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Using CNI on inner classes
2009-08-13 10:07 ` Bryce McKinlay
@ 2009-08-13 10:44 ` Andrew Haley
2009-08-13 10:49 ` Andrew Haley
0 siblings, 1 reply; 5+ messages in thread
From: Andrew Haley @ 2009-08-13 10:44 UTC (permalink / raw)
To: Bryce McKinlay; +Cc: Vaijayanthi Mala Suresh, java
Bryce McKinlay wrote:
> On Thu, Aug 13, 2009 at 10:10 AM, Andrew Haley<aph@redhat.com> wrote:
>
>> If the method is declared private, it really will be private, even
>> to cni.
>>
>> If you really need to breach privacy, you can do this in Test$Inner.h:
>>
>> class Test$Inner : public ::java::lang::Object
>> {
>> friend class ::Test;
>>
>> but I can't really see the point of private methods in an inner class
>> that you actually intend to be called from the enclosing class.
>
> In Java the private methods are accessible to the enclosing class.
Ah yes, that rule, the one that requires all the "synthetic access$0"
business. I just did a web search to find out the reason for this
rather odd rule, and I found "... inner classes can access all members
of the declaring class, even private members. In fact, the inner class
itself is said to be a member of the class; therefore, following the
rules of object-oriented engineering, it should have access to all
members of the class."
http://onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index.html
> So, ideally gcjh would add friend declarations, like the above,
> automatically to inner classes to approximate the Java semantics.
That's a nice idea. There's no reason why not; I just think no-one
ever thought of it.
> But, the easiest fix here is just to make the private method
> package-private instead.
I think so.
Andrew.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Using CNI on inner classes
2009-08-13 10:44 ` Andrew Haley
@ 2009-08-13 10:49 ` Andrew Haley
0 siblings, 0 replies; 5+ messages in thread
From: Andrew Haley @ 2009-08-13 10:49 UTC (permalink / raw)
To: Bryce McKinlay; +Cc: Vaijayanthi Mala Suresh, java
Andrew Haley wrote:
> Bryce McKinlay wrote:
>> On Thu, Aug 13, 2009 at 10:10 AM, Andrew Haley<aph@redhat.com> wrote:
>>
>>> If the method is declared private, it really will be private, even
>>> to cni.
>>>
>>> If you really need to breach privacy, you can do this in Test$Inner.h:
>>>
>>> class Test$Inner : public ::java::lang::Object
>>> {
>>> friend class ::Test;
>>>
>>> but I can't really see the point of private methods in an inner class
>>> that you actually intend to be called from the enclosing class.
>> In Java the private methods are accessible to the enclosing class.
>
> Ah yes, that rule, the one that requires all the "synthetic access$0"
> business. I just did a web search to find out the reason for this
> rather odd rule, and I found "... inner classes can access all members
> of the declaring class, even private members. In fact, the inner class
> itself is said to be a member of the class; therefore, following the
> rules of object-oriented engineering, it should have access to all
> members of the class."
> http://onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/index.html
Actually, that doesn't explain it: it only explains why inner classes
can access the private methods of enclosing classes, not vice versa.
Andrew.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2009-08-13 10:49 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-13 7:18 Using CNI on inner classes Vaijayanthi Mala Suresh
2009-08-13 9:10 ` Andrew Haley
2009-08-13 10:07 ` Bryce McKinlay
2009-08-13 10:44 ` Andrew Haley
2009-08-13 10:49 ` Andrew Haley
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).