26
26
27
27
28
28
/**
29
- * While the CodeLib project can be considered a companion Android library for ARTist modules,
30
- * the `CodeLib` class in particular represents the facade/api to this library. It defines
31
- * methods that are available to ARTist instrumentation passes and provides access through a
32
- * singleton instance stored in a public field.
29
+ * Represents the codelib for the ARTist trace-module with a simple, one-method API.
33
30
*/
34
31
public class CodeLib {
35
32
@@ -45,14 +42,15 @@ public class CodeLib {
45
42
*
46
43
* It HAS to be a static field with exactly this name b/c ARTist expects this field to be present.
47
44
*/
45
+ @ SuppressWarnings ("unused" )
48
46
public static CodeLib INSTANCE = new CodeLib ();
49
47
50
48
// Constants
51
49
private static final String TAG = CodeLib .class .toString ();
52
50
private static final String VERSION = TAG + " # 1.0.0" ;
53
51
54
52
@ SuppressWarnings ("WeakerAccess" )
55
- public final static String MSG_NOT_FOUND = "<Not Found>" ;
53
+ public final static String MSG_NOT_FOUND = "<Not Found>" ;
56
54
57
55
58
56
/**
@@ -64,42 +62,34 @@ private CodeLib() {
64
62
65
63
66
64
67
- /**
68
- * Injection target for an injection artist instrumentation pass.
65
+ /** Get the name of the calling method
66
+ *
67
+ * The name is probed from the current Thread's stacktrace.
69
68
*
70
- * Invocations of this method will be added to the target by ARTist.
71
- * @param fortytwo expected to be the constant 42.
69
+ * @return the name of the calling method
72
70
*/
73
- @ SuppressWarnings ("unused" )
74
- @ Inject
75
- public void injectionArtistTarget (int fortytwo ) {
76
- if (fortytwo != 42 ) {
77
- Log .e (TAG , "Error! Our artist pass provided " + fortytwo + " instead of 42" );
78
- } else {
79
- Log .d (TAG , "Injection successfull" );
71
+ private String getCallingMethodName () {
72
+ // CallStack depth of calling function.
73
+ final int CALLING_METHOD_STACK_LEVEL = 4 ;
74
+
75
+ final StackTraceElement [] stackTrace = Thread .currentThread ().getStackTrace ();
76
+ String callingMethodName ;
77
+ try {
78
+ final StackTraceElement callingMethod = stackTrace [CALLING_METHOD_STACK_LEVEL ];
79
+ callingMethodName = callingMethod .toString ();
80
+ } catch (final NullPointerException | ArrayIndexOutOfBoundsException e ) {
81
+ callingMethodName = MSG_NOT_FOUND ;
80
82
}
83
+ return callingMethodName ;
81
84
}
82
85
83
86
/**
84
- * Injection target for an artist instrumentation pass.
85
- *
86
- * Invocations of this method will be added to the target by ARTist.
87
- *
88
- * @param leet expected to be 1337.
89
- * @param thiz the object from which this method was called.
87
+ * Tracelog method, prints the method name of the calling method.
90
88
*/
91
89
@ SuppressWarnings ("unused" )
92
90
@ Inject
93
- public void basicArtistTarget (int leet , Object thiz ) {
94
- if (leet != 1337 ) {
95
- Log .e (TAG , "Error! Our artist pass provided " + leet + " instead of 1337" );
96
- return ;
97
- }
98
- // now you can do sth meaningful with the `this` pointer of the object from which we are called
99
- if (thiz instanceof Context ) {
100
- Log .i (TAG , "Found a context object, maybe store it for later?" );
101
- // ...
102
- }
103
- // ...
91
+ public void traceLog () {
92
+ final String callingMethodName = getCallingMethodName ();
93
+ Log .d (TAG , "Caller -> " + callingMethodName );
104
94
}
105
95
}
0 commit comments