07-05-2010 01:20 PM
How does class loader system run in blackberry ?
I have some questions
* can I load a class with spesific file name. for example, I have a file "myclass.txt" in cod. and it is a class file. can I load it ?
can I load class like this: Class.forName("/myclass.txt");
* I can only load class that's names end .class. is it a must that file names end with ".class" ?
* I use, Class.forName to load classes. is there any other way to load them ?
* If a class has some function that is not in device OS, I cannot load it. But, in BlackBerry, OS doesnt allow run application. however, OS should throw exception when I try to load that class. isnt it ?
Solved! Go to Solution.
07-05-2010 01:56 PM
You don't have the questions numbered but I'll go in order:
The only way to load classes is with Class.forName.
Even RIM does this* (just debug through RIM's OS functions), they have some device/API specific class, call Class.forName, the class has "loading functions" and the function that calls Class.forName will call one of those loading functions to get the specific class.
*Found from debugging a I/O connection.
But there is no ClassLoader on BlackBerry.
07-06-2010 08:49 AM
last question about trying load class that has newer functions.
for example:
interface SpellCheckable {
public void checkSpell(Field f,boolean check);
}
class SpellCheck implements SpellCheckable {
public void checkSpell(Field f,boolean check) {
f.setNonSpellCheckable(check);
}
}
try {
Class c = Class.forName("SpellCheck");
SpellCheckable sp = (SpellCheckable)c.newInstance();
} catch (Exception e) {
}
can I trying to load SpellCheck class in 4.2.0 version like that ?
I think, If OS that has not setNonSpellCheckable function, I cannot load class. But, I cannot start program because, OS throws "Field has not setNonSpellCheckable", even I dont run that function or SpellCheck class.
07-06-2010 08:41 PM
The way J2SE loads classes/functions is "as it is needed" so if you tried something like that or used ClassLoader it would only "see" setNonSpellCheckable if it was called.
On BlackBerry it is loaded when the whole class is loaded, so when SpellCheck is loaded the native (no developer access) ClassLoader reads everything and says "wait, I don't have this call in here, even though it might not get called I don't want to take the risk."
Does that make sense?
A workaround would be something like making a separate COD file with device specific function calls and then calling them, this way if it is not supported on that API version it doesn't get coded into the device specific COD file.
07-07-2010 03:54 AM
OK, I see.
I can do it in some j2me device. but, blackberry doesnt allow. I think also like you. seperate them in other cod file. managing this is hard but it is only way for solution.
thank you
07-07-2010 09:23 AM
Yea its not the simplest but it is an unintended side-effect of how RIM built there VM. On a positive note it does allow for increased flexibility, write the app and have all device specific libraries separate. Now if a new app comes out all you change is some GUI elements in the separate library and you now support a new device.