1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package lockedregioncodeinjection;
15 
16 /**
17  * Represent a specific class that is used for synchronization. A pre and post method can be
18  * specified to by the user to be called right after monitor_enter and after monitor_exit
19  * respectively.
20  */
21 public class LockTarget {
22     public static final LockTarget NO_TARGET = new LockTarget("", null, null);
23 
24     // The lock which must be instrumented, in Java internal form (L<path>;).
25     private final String targetDesc;
26     // The methods to be called when the lock is taken (released).  For non-scoped locks,
27     // these are fully qualified static methods.  For scoped locks, these are the
28     // unqualified names of a member method of the target lock.
29     private final String pre;
30     private final String post;
31     // If true, the pre and post methods are virtual on the target class.  The pre and post methods
32     // are both called while the lock is held.  If this field is false then the pre and post methods
33     // take no parameters and the post method is called after the lock is released.  This is legacy
34     // behavior.
35     private final boolean scoped;
36 
LockTarget(String targetDesc, String pre, String post, boolean scoped)37     public LockTarget(String targetDesc, String pre, String post, boolean scoped) {
38         this.targetDesc = targetDesc;
39         this.pre = pre;
40         this.post = post;
41         this.scoped = scoped;
42     }
43 
LockTarget(String targetDesc, String pre, String post)44     public LockTarget(String targetDesc, String pre, String post) {
45         this(targetDesc, pre, post, false);
46     }
47 
getTargetDesc()48     public String getTargetDesc() {
49         return targetDesc;
50     }
51 
getPre()52     public String getPre() {
53         return pre;
54     }
55 
getPreOwner()56     public String getPreOwner() {
57         if (scoped) {
58             return targetDesc.substring(1, targetDesc.length() - 1);
59         } else {
60             return pre.substring(0, pre.lastIndexOf('.'));
61         }
62     }
63 
getPreMethod()64     public String getPreMethod() {
65         return pre.substring(pre.lastIndexOf('.') + 1);
66     }
67 
getPost()68     public String getPost() {
69         return post;
70     }
71 
getPostOwner()72     public String getPostOwner() {
73         if (scoped) {
74             return targetDesc.substring(1, targetDesc.length() - 1);
75         } else {
76             return post.substring(0, post.lastIndexOf('.'));
77         }
78     }
79 
getPostMethod()80     public String getPostMethod() {
81         return post.substring(post.lastIndexOf('.') + 1);
82     }
83 
getScoped()84     public boolean getScoped() {
85         return scoped;
86     }
87 
88     @Override
toString()89     public String toString() {
90         return targetDesc + ":" + pre + ":" + post;
91     }
92 }
93