1
25 package java.lang.invoke;
26
27 import jdk.internal.vm.annotation.ForceInline;
28 import jdk.internal.vm.annotation.Stable;
29
30 import java.lang.invoke.VarHandle.AccessMode;
31 import java.lang.reflect.Method;
32 import java.lang.reflect.Modifier;
33 import java.util.ArrayList;
34 import java.util.List;
35
36
40 final class VarForm {
41
42 final @Stable MethodType[] methodType_table;
43
44 final @Stable MemberName[] memberName_table;
45
46 VarForm(Class<?> implClass, Class<?> receiver, Class<?> value, Class<?>... intermediate) {
47 this.methodType_table = new MethodType[VarHandle.AccessType.values().length];
48
49
50 this.memberName_table = linkFromStatic(implClass);
51
52
53 List<Class<?>> l = new ArrayList<>();
54 if (receiver != null)
55 l.add(receiver);
56 for (Class<?> c : intermediate)
57 l.add(c);
58
59
60 methodType_table[VarHandle.AccessType.GET.ordinal()] =
61 MethodType.methodType(value, l).erase();
62
63
64 l.add(value);
65 methodType_table[VarHandle.AccessType.SET.ordinal()] =
66 MethodType.methodType(void.class, l).erase();
67
68
69 methodType_table[VarHandle.AccessType.GET_AND_UPDATE.ordinal()] =
70 MethodType.methodType(value, l).erase();
71
72
73 l.add(value);
74 methodType_table[VarHandle.AccessType.COMPARE_AND_SET.ordinal()] =
75 MethodType.methodType(boolean.class, l).erase();
76
77
78 methodType_table[VarHandle.AccessType.COMPARE_AND_EXCHANGE.ordinal()] =
79 MethodType.methodType(value, l).erase();
80 }
81
82 @ForceInline
83 final MethodType getMethodType(int type) {
84 return methodType_table[type];
85 }
86
87 @ForceInline
88 final MemberName getMemberName(int mode) {
89
90 MemberName mn = memberName_table[mode];
91 if (mn == null) {
92 throw new UnsupportedOperationException();
93 }
94 return mn;
95 }
96
97
98 @Stable
99 MethodType[] methodType_V_table;
100
101 @ForceInline
102 final MethodType[] getMethodType_V_init() {
103 MethodType[] table = new MethodType[VarHandle.AccessType.values().length];
104 for (int i = 0; i < methodType_table.length; i++) {
105 MethodType mt = methodType_table[i];
106
107 table[i] = mt.changeReturnType(void.class);
108 }
109 methodType_V_table = table;
110 return table;
111 }
112
113 @ForceInline
114 final MethodType getMethodType_V(int type) {
115 MethodType[] table = methodType_V_table;
116 if (table == null) {
117 table = getMethodType_V_init();
118 }
119 return table[type];
120 }
121
122
123
126 private static MemberName[] linkFromStatic(Class<?> implClass) {
127 MemberName[] table = new MemberName[AccessMode.values().length];
128
129 for (Class<?> c = implClass; c != VarHandle.class; c = c.getSuperclass()) {
130 for (Method m : c.getDeclaredMethods()) {
131 if (Modifier.isStatic(m.getModifiers())) {
132 AccessMode am = AccessMode.methodNameToAccessMode.get(m.getName());
133 if (am != null) {
134 assert table[am.ordinal()] == null;
135 table[am.ordinal()] = new MemberName(m);
136 }
137 }
138 }
139 }
140 return table;
141 }
142 }