gpt4 book ai didi

java - Eclipse插件: get extended class name using ASTParser

转载 作者:行者123 更新时间:2023-12-01 13:10:54 25 4
gpt4 key购买 nike

我实现了以下访问方法来获取所使用的方法的名称及其相应的完全限定类名。

                    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

但是,我无法获取方法 setContentViewgetMenuInflater 的类名称。它们都是扩展的Acitivity类的方法。我想要它们的输出像 android.app.Activity 。我应该如何实现这个目标?

最佳答案

也许可以使用 IType.getSuperclassName() 来查看它是否不为空,您可以递归扫描父类(super class)的方法等等。另外,要获取更多信息,也许您可​​以切换到 ITypeBinding 以获取有关父类(super class)的更多信息。

关于java - Eclipse插件: get extended class name using ASTParser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22879124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com