我实现了以下访问方法来获取所用方法的名称及其相应的完全限定类名。
public boolean visit(MethodInvocation node)
{
SimpleName name = node.getName();
try
{
bw.write(node.getName() + "\t\t\t");
Expression expression = node.getExpression();
if (expression != null)
{
ITypeBinding binding = expression.resolveTypeBinding();
IType type = (IType)binding.getJavaElement();
bw.write(type.getFullyQualifiedName() + "\n");
}
else
{
bw.write("\n");
}
return true;
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
因此,在运行时它会解析以下两个java文件:
第一个Java文件
package com.example.androidsample;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Context helloworld;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
第二个Java文件
package com.example.androidsample;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.telephony.SmsManager;
public class MockLocationProvider {
String providerName;
android.content.Context ctx;
public MockLocationProvider(String name, Context ctx) {
this.providerName = name;
this.ctx = ctx;
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
lm.addTestProvider(providerName, false, false, false, false, false, true, true, 0, 5); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage("Phone Number", null, "Message", null, null);
lm.setTestProviderEnabled(providerName, true);
}
public void pushLocation(double lat, double lon) {
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
Location mockLocation = new Location(providerName);
mockLocation.setLatitude(lat);
mockLocation.setLongitude(lon);
mockLocation.setAltitude(0);
mockLocation.setTime(System.currentTimeMillis());
lm.setTestProviderLocation(providerName, mockLocation);
}
public void shutdown() {
LocationManager lm = (LocationManager) ctx.getSystemService(
Context.LOCATION_SERVICE);
lm.removeTestProvider(providerName);
}
}
和输出:
setContentView
inflate android.view.MenuInflater
getMenuInflater
getSystemService android.content.Context
addTestProvider android.location.LocationManager
getDefault android.telephony.SmsManager
sendTextMessage android.telephony.SmsManager
setTestProviderEnabled android.location.LocationManager
getSystemService android.content.Context
setLatitude android.location.Location
setLongitude android.location.Location
setAltitude android.location.Location
setTime android.location.Location
currentTimeMillis java.lang.System
setTestProviderLocation android.location.LocationManager
getSystemService android.content.Context
removeTestProvider android.location.LocationManager
但是,我无法获取方法 setContentView 和 getMenuInflater 的类名。它们都是扩展的 Acitivity 类的方法。我想要输出像 android.app.Activity 这两个输出。我该怎么做到这一点?
答案 0 :(得分:1)
也许use可以使用IType.getSuperclassName()来查看它是否为null,你可以递归扫描超类的方法等等。另外,为了获得更多信息,您可以切换到ITypeBinding以获取有关超类的更多信息。