2012-12-03 60 views
0

我想用javassist來檢查Android類的內容。爲什麼我這樣做的目的是無關緊要的,請向我解釋爲什麼會發生以下情況:Android中的Javassist缺少通過反射找到的類方法

當我使用反射來檢查com.android.Intent類時,我得到一個巨大的類列表。當我使用javassist時,我得到的很少。爲什麼這種差異存在?不應該有相同數量的方法嗎?這裏是我的了Javassist代碼:

final ClassPool pool = ClassPool.getDefault(getApplicationContext()); 
final CtClass cc = pool.get("android.content.Intent"); 
CtMethod[] mts = cc.getDeclaredMethods(); 

for (int i = 0; i < mts.length; i++) { 
    System.out.println("JAVASSIST: " + mts[i].toString()); 
} 

這裏是輸出:

12-03 14:42:23.819: I/System.out(2308): JAVASSIST: [email protected][protected clone()Ljava/lang/Object;] 
12-03 14:42:23.819: I/System.out(2308): JAVASSIST: [email protected][public equals (Ljava/lang/Object;)Z] 
12-03 14:42:23.819: I/System.out(2308): JAVASSIST: [email protected][protected finalize()V] 
12-03 14:42:23.828: I/System.out(2308): JAVASSIST: [email protected][public final native getClass()Ljava/lang/Class;] 
12-03 14:42:23.828: I/System.out(2308): JAVASSIST: [email protected][public native hashCode()I] 
12-03 14:42:23.838: I/System.out(2308): JAVASSIST: [email protected][public final native notify()V] 
12-03 14:42:23.838: I/System.out(2308): JAVASSIST: [email protected][public final native notifyAll()V] 
12-03 14:42:23.838: I/System.out(2308): JAVASSIST: [email protected][public toString()Ljava/lang/String;] 
12-03 14:42:23.838: I/System.out(2308): JAVASSIST: [email protected][public final wait()V] 
12-03 14:42:23.838: I/System.out(2308): JAVASSIST: [email protected][public final wait (J)V] 
12-03 14:42:23.849: I/System.out(2308): JAVASSIST: [email protected][public final native wait (JI)V] 
12-03 14:42:23.849: I/System.out(2308): JAVASSIST: [email protected][private native internalClone (Ljava/lang/Cloneable;)Ljava/lang/Object;] 

但是,顯然這是缺少了很多附加到這個類的50多個功能。下面是一個使用我的代碼反思:

Class<?> c = Class.forName("android.content.Intent"); 
Method[] allMethods = c.getDeclaredMethods(); 
for (int i = 0; i < allMethods.length; i++) { 
    System.out.println("REFLECTION: " + allMethods[i].toString()); 
} 

這裏是輸出:

12-03 14:42:23.869: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.addCategory(java.lang.String) 
12-03 14:42:23.869: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.addFlags(int) 
12-03 14:42:23.878: I/System.out(2308): REFLECTION: public java.lang.Object android.content.Intent.clone() 
12-03 14:42:23.878: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.cloneFilter() 
12-03 14:42:23.878: I/System.out(2308): REFLECTION: public int android.content.Intent.describeContents() 
12-03 14:42:23.889: I/System.out(2308): REFLECTION: public int android.content.Intent.fillIn(android.content.Intent,int) 
12-03 14:42:23.889: I/System.out(2308): REFLECTION: public boolean android.content.Intent.filterEquals(android.content.Intent) 
12-03 14:42:23.889: I/System.out(2308): REFLECTION: public int android.content.Intent.filterHashCode() 
12-03 14:42:23.889: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.getAction() 
12-03 14:42:23.899: I/System.out(2308): REFLECTION: public [Z android.content.Intent.getBooleanArrayExtra(java.lang.String) 
12-03 14:42:23.899: I/System.out(2308): REFLECTION: public boolean android.content.Intent.getBooleanExtra(java.lang.String,boolean) 
12-03 14:42:23.899: I/System.out(2308): REFLECTION: public android.os.Bundle android.content.Intent.getBundleExtra(java.lang.String) 
12-03 14:42:23.899: I/System.out(2308): REFLECTION: public [B android.content.Intent.getByteArrayExtra(java.lang.String) 
12-03 14:42:23.899: I/System.out(2308): REFLECTION: public byte android.content.Intent.getByteExtra(java.lang.String,byte) 
12-03 14:42:23.909: I/System.out(2308): REFLECTION: public java.util.Set android.content.Intent.getCategories() 
12-03 14:42:23.909: I/System.out(2308): REFLECTION: public [C android.content.Intent.getCharArrayExtra(java.lang.String) 
12-03 14:42:23.909: I/System.out(2308): REFLECTION: public char android.content.Intent.getCharExtra(java.lang.String,char) 
12-03 14:42:23.909: I/System.out(2308): REFLECTION: public [Ljava.lang.CharSequence; android.content.Intent.getCharSequenceArrayExtra(java.lang.String) 
12-03 14:42:23.909: I/System.out(2308): REFLECTION: public java.util.ArrayList android.content.Intent.getCharSequenceArrayListExtra(java.lang.String) 
12-03 14:42:23.918: I/System.out(2308): REFLECTION: public java.lang.CharSequence android.content.Intent.getCharSequenceExtra(java.lang.String) 
12-03 14:42:23.918: I/System.out(2308): REFLECTION: public android.content.ComponentName android.content.Intent.getComponent() 
12-03 14:42:23.918: I/System.out(2308): REFLECTION: public android.net.Uri android.content.Intent.getData() 
12-03 14:42:23.928: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.getDataString() 
12-03 14:42:23.928: I/System.out(2308): REFLECTION: public [D android.content.Intent.getDoubleArrayExtra(java.lang.String) 
12-03 14:42:23.928: I/System.out(2308): REFLECTION: public double android.content.Intent.getDoubleExtra(java.lang.String,double) 
12-03 14:42:23.939: I/System.out(2308): REFLECTION: public java.lang.Object android.content.Intent.getExtra(java.lang.String) 
12-03 14:42:23.939: I/System.out(2308): REFLECTION: public java.lang.Object android.content.Intent.getExtra(java.lang.String,java.lang.Object) 
12-03 14:42:23.949: I/System.out(2308): REFLECTION: public android.os.Bundle android.content.Intent.getExtras() 
12-03 14:42:23.949: I/System.out(2308): REFLECTION: public int android.content.Intent.getFlags() 
12-03 14:42:23.949: I/System.out(2308): REFLECTION: public [F android.content.Intent.getFloatArrayExtra(java.lang.String) 
12-03 14:42:23.959: I/System.out(2308): REFLECTION: public float android.content.Intent.getFloatExtra(java.lang.String,float) 
12-03 14:42:23.959: I/System.out(2308): REFLECTION: public android.os.IBinder android.content.Intent.getIBinderExtra(java.lang.String) 
12-03 14:42:23.959: I/System.out(2308): REFLECTION: public [I android.content.Intent.getIntArrayExtra(java.lang.String) 
12-03 14:42:23.959: I/System.out(2308): REFLECTION: public int android.content.Intent.getIntExtra(java.lang.String,int) 
12-03 14:42:23.959: I/System.out(2308): REFLECTION: public java.util.ArrayList android.content.Intent.getIntegerArrayListExtra(java.lang.String) 
12-03 14:42:23.959: I/System.out(2308): REFLECTION: public [J android.content.Intent.getLongArrayExtra(java.lang.String) 
12-03 14:42:23.959: I/System.out(2308): REFLECTION: public long android.content.Intent.getLongExtra(java.lang.String,long) 
12-03 14:42:23.968: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.getPackage() 
12-03 14:42:23.968: I/System.out(2308): REFLECTION: public [Landroid.os.Parcelable; android.content.Intent.getParcelableArrayExtra(java.lang.String) 
12-03 14:42:23.968: I/System.out(2308): REFLECTION: public java.util.ArrayList android.content.Intent.getParcelableArrayListExtra(java.lang.String) 
12-03 14:42:23.968: I/System.out(2308): REFLECTION: public android.os.Parcelable android.content.Intent.getParcelableExtra(java.lang.String) 
12-03 14:42:23.968: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.getScheme() 
12-03 14:42:23.968: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.getSelector() 
12-03 14:42:23.968: I/System.out(2308): REFLECTION: public java.io.Serializable android.content.Intent.getSerializableExtra(java.lang.String) 
12-03 14:42:23.968: I/System.out(2308): REFLECTION: public [S android.content.Intent.getShortArrayExtra(java.lang.String) 
12-03 14:42:23.978: I/System.out(2308): REFLECTION: public short android.content.Intent.getShortExtra(java.lang.String,short) 
12-03 14:42:23.978: I/System.out(2308): REFLECTION: public android.graphics.Rect android.content.Intent.getSourceBounds() 
12-03 14:42:23.978: I/System.out(2308): REFLECTION: public [Ljava.lang.String; android.content.Intent.getStringArrayExtra(java.lang.String) 
12-03 14:42:23.978: I/System.out(2308): REFLECTION: public java.util.ArrayList android.content.Intent.getStringArrayListExtra(java.lang.String) 
12-03 14:42:23.978: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.getStringExtra(java.lang.String) 
12-03 14:42:23.978: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.getType() 
12-03 14:42:23.989: I/System.out(2308): REFLECTION: public boolean android.content.Intent.hasCategory(java.lang.String) 
12-03 14:42:23.989: I/System.out(2308): REFLECTION: public boolean android.content.Intent.hasExtra(java.lang.String) 
12-03 14:42:23.989: I/System.out(2308): REFLECTION: public boolean android.content.Intent.hasFileDescriptors() 
12-03 14:42:23.989: I/System.out(2308): REFLECTION: public boolean android.content.Intent.isExcludingStopped() 
12-03 14:42:23.989: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putCharSequenceArrayListExtra(java.lang.String,java.util.ArrayList) 
12-03 14:42:23.989: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,byte) 
12-03 14:42:23.989: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,char) 
12-03 14:42:23.999: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,double) 
12-03 14:42:23.999: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,float) 
12-03 14:42:23.999: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,int) 
12-03 14:42:23.999: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,long) 
12-03 14:42:23.999: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,android.os.Bundle) 
12-03 14:42:24.008: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,android.os.IBinder) 
12-03 14:42:24.008: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,android.os.Parcelable) 
12-03 14:42:24.008: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,java.io.Serializable) 
12-03 14:42:24.019: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,java.lang.CharSequence) 
12-03 14:42:24.019: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,java.lang.String) 
12-03 14:42:24.028: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,short) 
12-03 14:42:24.028: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,boolean) 
12-03 14:42:24.028: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[B) 
12-03 14:42:24.028: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[C) 
12-03 14:42:24.028: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[D) 
12-03 14:42:24.039: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[F) 
12-03 14:42:24.039: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[I) 
12-03 14:42:24.039: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[J) 
12-03 14:42:24.039: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[Landroid.os.Parcelable;) 
12-03 14:42:24.039: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[Ljava.lang.CharSequence;) 
12-03 14:42:24.039: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[Ljava.lang.String;) 
12-03 14:42:24.048: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[S) 
12-03 14:42:24.048: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtra(java.lang.String,[Z) 
12-03 14:42:24.048: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtras(android.content.Intent) 
12-03 14:42:24.048: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putExtras(android.os.Bundle) 
12-03 14:42:24.048: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putIntegerArrayListExtra(java.lang.String,java.util.ArrayList) 
12-03 14:42:24.048: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putParcelableArrayListExtra(java.lang.String,java.util.ArrayList) 
12-03 14:42:24.048: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.putStringArrayListExtra(java.lang.String,java.util.ArrayList) 
12-03 14:42:24.058: I/System.out(2308): REFLECTION: public void android.content.Intent.readFromParcel(android.os.Parcel) 
12-03 14:42:24.058: I/System.out(2308): REFLECTION: public void android.content.Intent.removeCategory(java.lang.String) 
12-03 14:42:24.058: I/System.out(2308): REFLECTION: public void android.content.Intent.removeExtra(java.lang.String) 
12-03 14:42:24.058: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.replaceExtras(android.content.Intent) 
12-03 14:42:24.058: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.replaceExtras(android.os.Bundle) 
12-03 14:42:24.058: I/System.out(2308): REFLECTION: public android.content.ComponentName android.content.Intent.resolveActivity(android.content.pm.PackageManager) 
12-03 14:42:24.058: I/System.out(2308): REFLECTION: public android.content.pm.ActivityInfo android.content.Intent.resolveActivityInfo(android.content.pm.PackageManager,int) 
12-03 14:42:24.068: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.resolveType(android.content.ContentResolver) 
12-03 14:42:24.068: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.resolveType(android.content.Context) 
12-03 14:42:24.068: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.resolveTypeIfNeeded(android.content.ContentResolver) 
12-03 14:42:24.068: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setAction(java.lang.String) 
12-03 14:42:24.079: I/System.out(2308): REFLECTION: public void android.content.Intent.setAllowFds(boolean) 
12-03 14:42:24.079: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setClass(android.content.Context,java.lang.Class) 
12-03 14:42:24.079: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setClassName(android.content.Context,java.lang.String) 
12-03 14:42:24.079: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setClassName(java.lang.String,java.lang.String) 
12-03 14:42:24.079: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setComponent(android.content.ComponentName) 
12-03 14:42:24.079: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setData(android.net.Uri) 
12-03 14:42:24.079: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setDataAndType(android.net.Uri,java.lang.String) 
12-03 14:42:24.079: I/System.out(2308): REFLECTION: public void android.content.Intent.setExtrasClassLoader(java.lang.ClassLoader) 
12-03 14:42:24.089: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setFlags(int) 
12-03 14:42:24.089: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setPackage(java.lang.String) 
12-03 14:42:24.089: I/System.out(2308): REFLECTION: public void android.content.Intent.setSelector(android.content.Intent) 
12-03 14:42:24.089: I/System.out(2308): REFLECTION: public void android.content.Intent.setSourceBounds(android.graphics.Rect) 
12-03 14:42:24.098: I/System.out(2308): REFLECTION: public android.content.Intent android.content.Intent.setType(java.lang.String) 
12-03 14:42:24.108: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.toInsecureString() 
12-03 14:42:24.108: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.toShortString(boolean,boolean,boolean) 
12-03 14:42:24.108: I/System.out(2308): REFLECTION: public void android.content.Intent.toShortString(java.lang.StringBuilder,boolean,boolean,boolean) 
12-03 14:42:24.108: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.toString() 
12-03 14:42:24.108: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.toURI() 
12-03 14:42:24.108: I/System.out(2308): REFLECTION: public java.lang.String android.content.Intent.toUri(int) 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: public void android.content.Intent.writeToParcel(android.os.Parcel,int) 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: public static android.content.Intent android.content.Intent.createChooser(android.content.Intent,java.lang.CharSequence) 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: public static android.content.Intent android.content.Intent.getIntent(java.lang.String) throws java.net.URISyntaxException 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: public static android.content.Intent android.content.Intent.getIntentOld(java.lang.String) throws java.net.URISyntaxException 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: public static android.content.Intent android.content.Intent.makeMainActivity(android.content.ComponentName) 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: public static android.content.Intent android.content.Intent.makeMainSelectorActivity(java.lang.String,java.lang.String) 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: public static android.content.Intent android.content.Intent.makeRestartActivityTask(android.content.ComponentName) 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: public static android.content.Intent android.content.Intent.parseIntent(android.content.res.Resources,org.xmlpull.v1.XmlPullParser,android.util.AttributeSet) throws org.xmlpull.v1.XmlPullParserException,java.io.IOException 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: public static android.content.Intent android.content.Intent.parseUri(java.lang.String,int) throws java.net.URISyntaxException 
12-03 14:42:24.118: I/System.out(2308): REFLECTION: private void android.content.Intent.toUriInner(java.lang.StringBuilder,java.lang.String,int) 

爲什麼Javassist是錯過了那麼多的方法呢?我是否以某種方式引用了錯誤的ClassPool?有任何想法嗎?

回答

2

雖然我知道它適用於一個名爲Dalvik VM的修改後的JVM,但我並不完全熟悉android堆棧。 Dalvik VM類的格式完全不同,因爲它是基於寄存器的架構,與通常的基於堆棧的架構相反。

我認爲這個問題存在一個很大的可能性,那就是javassist不能完全/正確地解析類格式,只給出一些方法。

UPDATE

一些搜索我found another post in SO,這證實了我的懷疑之後。看來javassist不能用於android。儘管,看起來ASM確實如此。但是通過ASM,您可以在較低的抽象層次上工作。

引用用戶alokoko:

  • 我們可以使用了Javassist修改Java字節碼,但它並沒有在Android這樣的平臺上工作。
  • 可以使用ASM,但涉及處理Java程序集(例如:http://community.jboss.org/thread/150002)。
+0

感謝您的時間 – seibelj