Skip to content

Commit e26e726

Browse files
committed
(Re)introduced tracing method API.
This is now the corresponding codelib for the trace-module repository
1 parent 1bd781c commit e26e726

File tree

1 file changed

+23
-33
lines changed

1 file changed

+23
-33
lines changed

app/src/main/java/saarland/cispa/artist/codelib/CodeLib.java

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626

2727

2828
/**
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.
3330
*/
3431
public class CodeLib {
3532

@@ -45,14 +42,15 @@ public class CodeLib {
4542
*
4643
* It HAS to be a static field with exactly this name b/c ARTist expects this field to be present.
4744
*/
45+
@SuppressWarnings("unused")
4846
public static CodeLib INSTANCE = new CodeLib();
4947

5048
// Constants
5149
private static final String TAG = CodeLib.class.toString();
5250
private static final String VERSION = TAG + " # 1.0.0";
5351

5452
@SuppressWarnings("WeakerAccess")
55-
public final static String MSG_NOT_FOUND = "<Not Found>";
53+
public final static String MSG_NOT_FOUND = "<Not Found>";
5654

5755

5856
/**
@@ -64,42 +62,34 @@ private CodeLib() {
6462

6563

6664

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.
6968
*
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
7270
*/
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;
8082
}
83+
return callingMethodName;
8184
}
8285

8386
/**
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.
9088
*/
9189
@SuppressWarnings("unused")
9290
@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);
10494
}
10595
}

0 commit comments

Comments
 (0)