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 import org.objectweb.asm.Opcodes;
17 
18 import java.util.ArrayList;
19 import java.util.List;
20 
21 public class Utils {
22 
23     public static final int ASM_VERSION = Opcodes.ASM9;
24 
25     /**
26      * Reads a comma separated configuration similar to the Jack definition.
27      */
getTargetsFromLegacyJackConfig(String classList, String requestList, String resetList)28     public static List<LockTarget> getTargetsFromLegacyJackConfig(String classList,
29             String requestList, String resetList) {
30 
31         String[] classes = classList.split(",");
32         String[] requests = requestList.split(",");
33         String[] resets = resetList.split(",");
34 
35         int total = classes.length;
36         assert requests.length == total;
37         assert resets.length == total;
38 
39         List<LockTarget> config = new ArrayList<LockTarget>();
40 
41         for (int i = 0; i < total; i++) {
42             config.add(new LockTarget(classes[i], requests[i], resets[i]));
43         }
44 
45         return config;
46     }
47 
48     /**
49      * Returns a single {@link LockTarget} from a string.  The target is a comma-separated list of
50      * the target class, the request method, the release method, and a boolean which is true if this
51      * is a scoped target and false if this is a legacy target.  The boolean is optional and
52      * defaults to true.
53      */
getScopedTarget(String arg)54     public static LockTarget getScopedTarget(String arg) {
55         String[] c = arg.split(",");
56         if (c.length == 3) {
57           return new LockTarget(c[0], c[1], c[2], true);
58         } else if (c.length == 4) {
59             if (c[3].equals("true")) {
60                 return new LockTarget(c[0], c[1], c[2], true);
61             } else if (c[3].equals("false")) {
62                 return new LockTarget(c[0], c[1], c[2], false);
63             } else {
64                 System.err.println("illegal target parameter \"" + c[3] + "\"");
65             }
66         }
67         // Fall through
68         throw new RuntimeException("invalid scoped target format");
69     }
70 }
71