|
21 | 21 | */
|
22 | 22 | package saarland.cispa.artist.codelib;
|
23 | 23 |
|
| 24 | +import android.content.Context; |
24 | 25 | import android.util.Log;
|
25 | 26 |
|
| 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 | + */ |
26 | 34 | public class CodeLib {
|
27 | 35 |
|
| 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 | + */ |
28 | 40 | @interface Inject {}
|
29 | 41 |
|
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 | + */ |
31 | 48 | public static CodeLib INSTANCE = new CodeLib();
|
32 | 49 |
|
33 |
| - // <Constants> ///////////////////////////////////////////////////////////////////////////////// |
34 |
| - private static final String TAG = "ArtistCodeLib"; |
| 50 | + // Constants |
| 51 | + private static final String TAG = CodeLib.class.toString(); |
35 | 52 | private static final String VERSION = TAG + " # 1.0.0";
|
36 | 53 |
|
37 |
| - final String MSG_NOT_FOUND = "<Not Found>"; |
38 |
| - // </Constants> //////////////////////////////////////////////////////////////////////////////// |
| 54 | + @SuppressWarnings("WeakerAccess") |
| 55 | + public final static String MSG_NOT_FOUND = "<Not Found>"; |
39 | 56 |
|
40 |
| - /** |
41 |
| - * Static Class Constructor |
42 |
| - */ |
43 |
| - static { |
44 |
| - // <Code> |
45 |
| - } |
46 | 57 |
|
47 | 58 | /**
|
48 |
| - * Private Class Constructor |
49 |
| - * => Forbidden Class Initialisation (Singleton) |
50 |
| - */ |
| 59 | + * Private class constructor to forbid further instance creation. |
| 60 | + */ |
51 | 61 | private CodeLib() {
|
52 |
| - Log.v(TAG, TAG + " CodeLib() " + VERSION); |
| 62 | + Log.v(TAG, TAG + " CodeLib v" + VERSION + " initialized."); |
53 | 63 | }
|
54 | 64 |
|
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. |
58 | 69 | *
|
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. |
60 | 72 | */
|
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"); |
74 | 80 | }
|
75 |
| - return callingMethodName; |
76 | 81 | }
|
77 | 82 |
|
78 | 83 | /**
|
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. |
80 | 90 | */
|
| 91 | + @SuppressWarnings("unused") |
81 | 92 | @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 | + // ... |
85 | 104 | }
|
86 | 105 | }
|
0 commit comments