Skip to content

Commit 1bd781c

Browse files
committed
Generalization (close #1), cleanup & adaption for template module
This is now the template codelib corresponding to the template module repository. It also comes with descriptions and explanations to get a better feeling for what components are needed and what they do exactly.
1 parent b47c8d8 commit 1bd781c

File tree

1 file changed

+56
-37
lines changed

1 file changed

+56
-37
lines changed

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

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,66 +21,85 @@
2121
*/
2222
package saarland.cispa.artist.codelib;
2323

24+
import android.content.Context;
2425
import android.util.Log;
2526

27+
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.
33+
*/
2634
public class CodeLib {
2735

36+
/**
37+
* This annotation lets you define those APIs that will be made available in target apps,
38+
* hence in ARTist you can only inject calls to those CodeLib methods that are annotated properly.
39+
*/
2840
@interface Inject {}
2941

30-
// Instance variable for singleton usage ///////////////////////////////////////////////////////
42+
/**
43+
* Instance variable for singleton usage. Using the singleton pattern ensures we can have
44+
* shared state when calling multiple CodeLib methods from different parts of a target app.
45+
*
46+
* It HAS to be a static field with exactly this name b/c ARTist expects this field to be present.
47+
*/
3148
public static CodeLib INSTANCE = new CodeLib();
3249

33-
// <Constants> /////////////////////////////////////////////////////////////////////////////////
34-
private static final String TAG = "ArtistCodeLib";
50+
// Constants
51+
private static final String TAG = CodeLib.class.toString();
3552
private static final String VERSION = TAG + " # 1.0.0";
3653

37-
final String MSG_NOT_FOUND = "<Not Found>";
38-
// </Constants> ////////////////////////////////////////////////////////////////////////////////
54+
@SuppressWarnings("WeakerAccess")
55+
public final static String MSG_NOT_FOUND = "<Not Found>";
3956

40-
/**
41-
* Static Class Constructor
42-
*/
43-
static {
44-
// <Code>
45-
}
4657

4758
/**
48-
* Private Class Constructor
49-
* => Forbidden Class Initialisation (Singleton)
50-
*/
59+
* Private class constructor to forbid further instance creation.
60+
*/
5161
private CodeLib() {
52-
Log.v(TAG, TAG + " CodeLib() " + VERSION);
62+
Log.v(TAG, TAG + " CodeLib v" + VERSION + " initialized.");
5363
}
5464

55-
/** Get the name of the calling method
56-
*
57-
* The name is probed from the current Thread's stacktrace.
65+
66+
67+
/**
68+
* Injection target for an injection artist instrumentation pass.
5869
*
59-
* @return the name of the calling method
70+
* Invocations of this method will be added to the target by ARTist.
71+
* @param fortytwo expected to be the constant 42.
6072
*/
61-
private String getCallingMethodName() {
62-
// CallStack depth of calling function.
63-
final int CALLING_METHOD_STACK_LEVEL = 4;
64-
65-
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
66-
String callingMethodName;
67-
try {
68-
final StackTraceElement callingMethod = stackTrace[CALLING_METHOD_STACK_LEVEL];
69-
callingMethodName = callingMethod.toString();
70-
} catch (final NullPointerException e) {
71-
callingMethodName = MSG_NOT_FOUND;
72-
} catch (final ArrayIndexOutOfBoundsException e) {
73-
callingMethodName = MSG_NOT_FOUND;
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");
7480
}
75-
return callingMethodName;
7681
}
7782

7883
/**
79-
* Tracelog method, prints the method name of the calling method.
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.
8090
*/
91+
@SuppressWarnings("unused")
8192
@Inject
82-
public void traceLog() {
83-
final String callingMethodName = getCallingMethodName();
84-
Log.d(TAG, "Caller -> " + callingMethodName);
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+
// ...
85104
}
86105
}

0 commit comments

Comments
 (0)