程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 解讀java.lang.Class

解讀java.lang.Class

編輯:J2ME

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 isInte***ce();     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,所以並沒有復雜的依附關系了。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved