Instantiating an abstract class

311 views
Skip to first unread message

Igor R

unread,
Jul 3, 2011, 1:25:19 PM7/3/11
to andro...@googlegroups.com
Hello,

In jni code I attempt the subj. This works well on the simulator and
on some devices, but fails with InstantiationException on some other
devices.
What's the reason of such a behavior? Is it configurable?

Thanks.

Tim Mensch

unread,
Jul 3, 2011, 8:57:14 PM7/3/11
to andro...@googlegroups.com

That sounds like a Java question, since if you tried to instantiate an
abstract class in C++, it simply wouldn't compile.

This being the NDK list, you might have better luck asking on another
list, where the topic is Java.

Tim

Doug Schaefer

unread,
Jul 3, 2011, 10:58:13 PM7/3/11
to andro...@googlegroups.com

Even in Java, you shouldn't have been able to instantiate an abstract
class. Sounds like you found a bug in your first attempt and should
not count on that happening again.

alan

unread,
Jul 4, 2011, 4:10:46 AM7/4/11
to andro...@googlegroups.com
I imagine that unless check jni is tunred on you are probably able to instantiate an abstract class. I imagine as soon as you try to use it though bad/random things will happen

Igor R

unread,
Jul 4, 2011, 4:53:12 AM7/4/11
to android-ndk
> That sounds like a Java question, since if you tried to instantiate an
> abstract class in C++, it simply wouldn't compile.
>
> This being the NDK list, you might have better luck asking on another
> list, where the topic is Java.


I probably didn't explain myself well... I know that both Java c++
forbid to instantiate abstract classes :).
I mean the following: knowing that jni allows to ignore some java
rules (like member protection), I attempted to instantiate abstract
java classes this way:
jclass cls = env->FindClass("path/to/myclass");
jmethodID method = env->GetMethodID(cls, "<init>", "()V");
jobject result = env->NewObject(cls, method);

In most cases this *does* work.
But on some device it thows InstantiationException.

The question is: why? What it depends on?

Igor R

unread,
Jul 4, 2011, 4:56:21 AM7/4/11
to android-ndk
> I imagine that unless check jni is tunred on you are probably able to instantiate an abstract class.

Oh I see.
But I didn't change my project settings, when switched from one device
to another, and I also didn't tweak device settings.



> I imagine as soon as you try to use it though bad/random things will happen

Why? In case the class is fully defined, "abstract" is just syntactic
restriction (just like "private", which jni ignores).

alan

unread,
Jul 4, 2011, 6:13:08 AM7/4/11
to andro...@googlegroups.com
probably whether checkjni is turned on, I beleive that it is normally on by default on emulators and off by default on devices but some manufacturers may have it turned on in a device. If your class is fully defined then instantiating it will probably do no harm but if checkjni is on then it will probably crash. The only solution is to not try to create abstract classes, jni is for making faster or more portable code it is not for bypassing jvm checks

Linux Box Solutions

unread,
Jul 4, 2011, 6:30:26 PM7/4/11
to andro...@googlegroups.com, andro...@googlegroups.com
Everyone trying start a developer community at tabletmodz.com so us developers can have a resource to refer to. These google groups are becoming hard to navigate get a response from people or even getting your question out to people this site is fr developers only not the end user. Check it out

www.Krzyview.com


On Jul 4, 2011, at 6:13 AM, alan <al...@birtles.org.uk> wrote:

probably whether checkjni is turned on, I beleive that it is normally on by default on emulators and off by default on devices but some manufacturers may have it turned on in a device. If your class is fully defined then instantiating it will probably do no harm but if checkjni is on then it will probably crash. The only solution is to not try to create abstract classes, jni is for making faster or more portable code it is not for bypassing jvm checks

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/android-ndk/-/7ouyuLqsf_EJ.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

David Turner

unread,
Jul 4, 2011, 6:48:18 PM7/4/11
to andro...@googlegroups.com
On Tue, Jul 5, 2011 at 12:30 AM, Linux Box Solutions <linuxbox...@gmail.com> wrote:
Everyone trying start a developer community at tabletmodz.com so us developers can have a resource to refer to. These google groups are becoming hard to navigate get a response from people or even getting your question out to people this site is fr developers only not the end user. Check it out

www.Krzyview.com


This message has been sent as a reply to several unrelated threads on android-ndk.
Spamming the forum like is not acceptable. This account has been banned.

Sorry for the annoyance.

Dianne Hackborn

unread,
Jul 4, 2011, 9:55:48 PM7/4/11
to andro...@googlegroups.com
From what I can tell, you are saying "I am trying to do this thing that I know I am not supposed to do and isn't supported, but when I do it I am getting different kinds of errors in different situations and why is that?"

If so...  well, I think the problem mostly lies in the first part of that not the second. :}  If it is not something that should work, the failure behavior may vary.  But since this is not supposed to work, isn't the solution to just not do it?

As far as the differences in behavior...  you haven't really given much information for people to speculate.  Maybe this undefined different devices you are trying it on are running different versions of the platform?  Maybe some manufacturers have some different behavior?

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

fadden

unread,
Jul 6, 2011, 4:41:42 PM7/6/11
to android-ndk
Back in Cupcake-era Android it was possible to instantiate abstract
classes through reflection (Constructor.newInstance()). This was
fixed some time ago.

AllocObject and NewObject still allowed this up until about a year
ago. Here's the change that fixed it:

http://android.git.kernel.org/?p=platform/dalvik.git;a=commit;h=b76594d71daec00e5947a5f6f14282f8f9edebe4

I'm not sure what release that first appeared in.

Bottom line: it's not allowed, it should never have been allowed, you
will get nothing but sadness if you try to use it.

Christopher Van Kirk

unread,
Jul 4, 2011, 10:33:37 PM7/4/11
to andro...@googlegroups.com

I love your responses sometimes, Dianne.


No virus found in this message.
Checked by AVG - www.avg.com
Version: 10.0.1388 / Virus Database: 1516/3734 - Release Date: 06/29/11

Reply all
Reply to author
Forward
0 new messages