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
17public class Main {
18  public static void expectEquals(int expected, int actual) {
19    if (expected != actual) {
20      throw new Error("Expected " + expected + ", got " + actual);
21    }
22  }
23
24  public static void main(String[] unused) throws Exception {
25    Class<?> cls = Class.forName("TestCase");
26
27    cls.getMethod("setByteStaticField").invoke(null);
28    expectEquals(1, cls.getField("staticByteField").getByte(null));
29
30    cls.getMethod("setShortStaticField").invoke(null);
31    expectEquals(0x101, cls.getField("staticShortField").getShort(null));
32
33    cls.getMethod("setCharStaticField").invoke(null);
34    expectEquals(0x101, cls.getField("staticCharField").getChar(null));
35
36    {
37      Object[] args = { new byte[2] };
38      cls.getMethod("setByteArray", byte[].class).invoke(null, args);
39      expectEquals(1, ((byte[])args[0])[0]);
40    }
41    {
42      Object[] args = { new short[2] };
43      cls.getMethod("setShortArray", short[].class).invoke(null, args);
44      expectEquals(0x101, ((short[])args[0])[0]);
45    }
46    {
47      Object[] args = { new char[2] };
48      cls.getMethod("setCharArray", char[].class).invoke(null, args);
49      expectEquals(0x101, ((char[])args[0])[0]);
50    }
51    {
52      Object[] args = { cls.newInstance() };
53
54      cls.getMethod("setByteInstanceField", cls).invoke(null, args);
55      expectEquals(1, cls.getField("staticByteField").getByte(args[0]));
56
57      cls.getMethod("setShortInstanceField", cls).invoke(null, args);
58      expectEquals(0x101, cls.getField("staticShortField").getShort(args[0]));
59
60      cls.getMethod("setCharInstanceField", cls).invoke(null, args);
61      expectEquals(0x101, cls.getField("staticCharField").getChar(args[0]));
62    }
63  }
64}
65