dimanche 24 mai 2015

How can I count the number of each variable type declared in a particular Android class?

I've created a small Android application which I'm intending to use, to read in a particular Android class (.java).

But I'd like to:

  • Count the number of each variable type globally declared e.g.

5 regular ints, 3 regular strings, 1 regular drawables, *2 ArrayList of ints

  • Count the number of each variable type locally declared in each function e.g.

Function A: 2 regular ints, 1 regular strings

Function B: 3 regular drawables, 4 ArrayList of ints

  • Count the number subroutines and functions

So far I've figured the below, but due to the programmatic nature it doesn't work correctly:

        int numberofArrayLists = Collections.frequency(androidClass, "ArrayList<String>");
        int numberofIntLists = Collections.frequency(androidClass, "ArrayList<Int>");
        int numberofBooleanLists = Collections.frequency(androidClass, "ArrayList<Boolean>");

My code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        ArrayList<String> androidClass =  readAndroidClass("/sdcard/MainActivity.java");
    } catch (IOException e) {
        e.printStackTrace();
    }
}


public ArrayList<String> readAndroidClass(String classLocationString) throws IOException {
    ArrayList<String> androidClass = new ArrayList<String>();
    FileInputStream is;
    BufferedReader reader;
    final File classLocation = new File(classLocationString);

    if (classLocation.exists()) {
        is = new FileInputStream(classLocation);
        reader = new BufferedReader(new InputStreamReader(is));
        String line = reader.readLine();
        while(line != null){
            androidClass.add(reader.readLine());
        }
    }

    return androidClass;
}

Example of an Android Java class to attempt the above:

public class MainActivity extends ListActivity {
    private PackageManager packageManager = null;
    private List<ApplicationInfo> applist = null;
    private ApplicationAdapter listadaptor = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        packageManager = getPackageManager();

        new LoadApplications().execute();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        return false;
    }

    private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
        ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
        for (ApplicationInfo info : list) {
            try {
                if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                    applist.add(info);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return applist;
    }

    private class LoadApplications extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progress = null;

        @Override
        protected Void doInBackground(Void... params) {
            applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
            listadaptor = new ApplicationAdapter(MainActivity.this,
                    R.layout.snippet_list_row, applist);

            return null;
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }

        @Override
        protected void onPostExecute(Void result) {
            setListAdapter(listadaptor);
            progress.dismiss();
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(MainActivity.this, null,
                    "Loading application info...");
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire