Coding man
Coding man

Reputation: 967

Eclipse Plugin : get extended class name using ASTParser

I implemented the following visit method to get the names of the methods used and their corresponding fully-qualified class names.

                    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;
                    }

So, at runtime it parses the following two java files:

First Java File

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;
}

}

Second Java File

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);
  }
}

and outputs:

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

But, I am not able to get the class names for the methods setContentView and getMenuInflater. Both of them are the methods of the Acitivity class which is extended. I want the output like android.app.Activity for both of them. How should I achieve this?

Upvotes: 0

Views: 186

Answers (1)

blackuprise
blackuprise

Reputation: 450

Maybe use can use IType.getSuperclassName() to see if it's not null you can recursively scan methods for super class and so on. Also to get more info maybe you can switch to ITypeBinding to get more info on super class.

Upvotes: 1

Related Questions