• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import com.android.jack.annotations.CalledByInvokeCustom;
18 import com.android.jack.annotations.Constant;
19 import com.android.jack.annotations.LinkerMethodHandle;
20 import com.android.jack.annotations.MethodHandleKind;
21 
22 import java.lang.invoke.CallSite;
23 import java.lang.invoke.ConstantCallSite;
24 import java.lang.invoke.MethodHandle;
25 import java.lang.invoke.MethodHandles;
26 import java.lang.invoke.MethodType;
27 
28 public class TestLinkerMethodMinimalArguments {
29 
30   private static int forceFailureType = 0;
31 
32   private static int FAILURE_TYPE_NONE = 0;
33   private static int FAILURE_TYPE_LINKER_METHOD_RETURNS_NULL = 1;
34   private static int FAILURE_TYPE_LINKER_METHOD_THROWS = 2;
35   private static int FAILURE_TYPE_TARGET_METHOD_THROWS = 3;
36 
37   @CalledByInvokeCustom(
38       invokeMethodHandle = @LinkerMethodHandle(
39           kind = MethodHandleKind.INVOKE_STATIC,
40           enclosingType = TestLinkerMethodMinimalArguments.class,
41           argumentTypes = {MethodHandles.Lookup.class, String.class, MethodType.class},
42           name = "linkerMethod"),
43       name = "add",
44       returnType = int.class,
45       argumentTypes = {int.class, int.class})
add(int a, int b)46   private static int add(int a, int b) {
47     if (forceFailureType == FAILURE_TYPE_TARGET_METHOD_THROWS) {
48       System.out.println("Throwing ArithmeticException in add()");
49       throw new ArithmeticException("add");
50     }
51     return a + b;
52   }
53 
54   @SuppressWarnings("unused")
linkerMethod(MethodHandles.Lookup caller, String name, MethodType methodType)55   private static CallSite linkerMethod(MethodHandles.Lookup caller, String name,
56                                        MethodType methodType) throws Throwable {
57     System.out.println("linkerMethod failure type " + forceFailureType);
58     MethodHandle mh_add =
59         caller.findStatic(TestLinkerMethodMinimalArguments.class, name, methodType);
60     if (forceFailureType == FAILURE_TYPE_LINKER_METHOD_RETURNS_NULL) {
61       System.out.println("Returning null instead of CallSite for " + name + " " + methodType);
62       return null;
63     } else if (forceFailureType == FAILURE_TYPE_LINKER_METHOD_THROWS) {
64       System.out.println("Throwing InstantiationException in linkerMethod()");
65       throw new InstantiationException("linkerMethod");
66     } else {
67       return new ConstantCallSite(mh_add);
68     }
69   }
70 
test(int failureType, int x, int y)71   public static void test(int failureType, int x, int y) throws Throwable {
72     assertTrue(failureType >= FAILURE_TYPE_NONE);
73     assertTrue(failureType <= FAILURE_TYPE_TARGET_METHOD_THROWS);
74     forceFailureType = failureType;
75     assertEquals(x + y, add(x, y));
76     System.out.println("Failure Type + " + failureType + " (" + x + y+ ")");
77   }
78 
assertTrue(boolean value)79   public static void assertTrue(boolean value) {
80     if (!value) {
81       throw new AssertionError("assertTrue value: " + value);
82     }
83   }
84 
assertEquals(byte b1, byte b2)85   public static void assertEquals(byte b1, byte b2) {
86     if (b1 == b2) { return; }
87     throw new AssertionError("assertEquals b1: " + b1 + ", b2: " + b2);
88   }
89 
assertEquals(char c1, char c2)90   public static void assertEquals(char c1, char c2) {
91     if (c1 == c2) { return; }
92     throw new AssertionError("assertEquals c1: " + c1 + ", c2: " + c2);
93   }
94 
assertEquals(short s1, short s2)95   public static void assertEquals(short s1, short s2) {
96     if (s1 == s2) { return; }
97     throw new AssertionError("assertEquals s1: " + s1 + ", s2: " + s2);
98   }
99 
assertEquals(int i1, int i2)100   public static void assertEquals(int i1, int i2) {
101     if (i1 == i2) { return; }
102     throw new AssertionError("assertEquals i1: " + i1 + ", i2: " + i2);
103   }
104 
assertEquals(long l1, long l2)105   public static void assertEquals(long l1, long l2) {
106     if (l1 == l2) { return; }
107     throw new AssertionError("assertEquals l1: " + l1 + ", l2: " + l2);
108   }
109 
assertEquals(float f1, float f2)110   public static void assertEquals(float f1, float f2) {
111     if (f1 == f2) { return; }
112     throw new AssertionError("assertEquals f1: " + f1 + ", f2: " + f2);
113   }
114 
assertEquals(double d1, double d2)115   public static void assertEquals(double d1, double d2) {
116     if (d1 == d2) { return; }
117     throw new AssertionError("assertEquals d1: " + d1 + ", d2: " + d2);
118   }
119 
assertEquals(Object o, Object p)120   public static void assertEquals(Object o, Object p) {
121     if (o == p) { return; }
122     if (o != null && p != null && o.equals(p)) { return; }
123     throw new AssertionError("assertEquals: o1: " + o + ", o2: " + p);
124   }
125 
assertEquals(String s1, String s2)126   public static void assertEquals(String s1, String s2) {
127     if (s1 == s2) {
128       return;
129     }
130 
131     if (s1 != null && s2 != null && s1.equals(s2)) {
132       return;
133     }
134 
135     throw new AssertionError("assertEquals s1: " + s1 + ", s2: " + s2);
136   }
137 }
138