/
APIBrowser.java
87 lines (72 loc) · 2.43 KB
/
APIBrowser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package net.virtualvoid.android.browser;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class APIBrowser extends ListActivity {
private Object current;
private LayoutInflater inflater;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setListAdapter(new Adapter());
setContentView(R.layout.main);
setObject(this);
}
private ArrayList<Method> methods = new ArrayList<Method>();
private final static String TAG = "APIBrowser";
private void setObject(Object o){
this.current = o;
Log.d(TAG,"setObject called with "+o.toString());
((TextView)findViewById(R.id.object)).setText(current.toString());
methods.clear();
for (Method m:current.getClass().getMethods()){
if (m.getParameterTypes().length == 0 && (m.getModifiers()&Modifier.STATIC)==0)
methods.add(m);
}
((Adapter)getListAdapter()).notifyDataSetInvalidated();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Method m = methods.get(position);
try {
setObject(m.invoke(current));
} catch (Exception e) {
e.printStackTrace();
}
}
class Adapter extends BaseAdapter{
@Override
public int getCount() {
return methods.size();
}
@Override
public Object getItem(int position) {
return methods.get(position);
}
@Override
public long getItemId(int position) {
return methods.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.method, null);
Method m = methods.get(position);
((TextView) convertView.findViewById(R.id.name)).setText(m.getName());
((TextView) convertView.findViewById(R.id.result_type)).setText(m.getReturnType().getSimpleName());
return convertView;
}
}
}