public final class Class { private Class() {} public static native Class forName(String className) throws ClassNotFoundException; public native Object newInstance() throws InstantiationException, IllegalAccessException; public native boolean isInstance(Object obj); public native boolean isAssignableFrom(Class cls); public native boolean isInterface(); public native boolean isArray(); public native String getName(); public java.io.InputStream getResourceAsStream(String name) { try { if (name.length() > 0 && name.charAt(0) == '/') { name = name.substring(1); } else { String className = this.getName(); int dotIndex = className.lastIndexOf('.'); if (dotIndex >= 0) { name = className.substring(0, dotIndex + 1).replace('.', '/') + name; } } return new ResourceInputStream(name); } catch (java.io.IOException x) { return null; } } private static void runCustomCode() {} private native Class getSuperclass(); private transient Object vmClass; private int status; private Thread thread; private static final int VERIFIED = 1; private static final int IN_PROGRESS = 2; private static final int INITIALIZED = 3; private static final int ERROR = 4; private native void invoke_clinit(); private native void invoke_verifier(); void initialize() throws Throwable { verify(); synchronized (this) { while (status == IN_PROGRESS && thread != Thread.currentThread()) { try { wait(); } catch (InterruptedException e) {} } if (status == IN_PROGRESS && thread == Thread.currentThread()) { return; } if (status == INITIALIZED) { return; } if (status == ERROR) { throw new Error("NoClassDefFoundError:" + getName()); } status = IN_PROGRESS; thread = Thread.currentThread(); } try { Class s = getSuperclass(); if (s != null && s.status != INITIALIZED) { s.initialize(); } invoke_clinit(); synchronized (this) { status = INITIALIZED; thread = null; notifyAll(); } } catch (Throwable e) { synchronized (this) { status = ERROR; thread = null; notifyAll(); throwError(e); } } } synchronized void verify() throws Throwable { if (status < VERIFIED) { getSuperclass().verify(); invoke_verifier(); status = VERIFIED; } } private Error throwError(Throwable e) throws Error { throw (e instanceof Error) ? (Error) e : new Error("Static initializer: " + e.getClass().getName() + ", " + e.getMessage()); } }
-------------------------------------------
- 为什么private Class()呢?因为class是需要编译好的class文件。当然,现在有技术将代码编译成class文件。
- forName():从.class文件装载并创建Class。
- Class是object的结构信息呀。但J2ME的能力有限,你不能获取该class有多少个字段,多少个方法。
- 注
意:j2me里面没有ClassLoader。但是,通过另外方式来处理ClassLoader了,从上面可以看出,每次Class装载需要两
部:init和verify。class装载的顺序呢,当然首先是系统,然后就是用户的了。由于JVM每次只跑一个MIDlet,所以并没有复杂的依赖关
系了。