2016-03-24 11:21:21 +01:00
|
|
|
/*
|
2019-09-20 11:07:52 +01:00
|
|
|
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
|
2016-03-24 11:21:21 +01:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
package java.lang.invoke;
|
|
|
|
|
2016-05-05 11:39:08 -07:00
|
|
|
import jdk.internal.vm.annotation.ForceInline;
|
2016-04-11 18:21:25 +02:00
|
|
|
import jdk.internal.vm.annotation.Stable;
|
|
|
|
|
2016-03-24 11:21:21 +01:00
|
|
|
import java.lang.invoke.VarHandle.AccessMode;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.lang.reflect.Modifier;
|
2016-05-05 11:39:08 -07:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-03-24 11:21:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A var handle form containing a set of member name, one for each operation.
|
|
|
|
* Each member characterizes a static method.
|
|
|
|
*/
|
2016-05-05 11:39:08 -07:00
|
|
|
final class VarForm {
|
|
|
|
|
|
|
|
final @Stable MethodType[] methodType_table;
|
|
|
|
|
|
|
|
final @Stable MemberName[] memberName_table;
|
|
|
|
|
|
|
|
VarForm(Class<?> implClass, Class<?> receiver, Class<?> value, Class<?>... intermediate) {
|
|
|
|
this.methodType_table = new MethodType[VarHandle.AccessType.values().length];
|
2020-05-25 10:54:39 +01:00
|
|
|
if (receiver == null) {
|
|
|
|
initMethodTypes(value, intermediate);
|
|
|
|
} else {
|
|
|
|
Class<?>[] coordinates = new Class<?>[intermediate.length + 1];
|
|
|
|
coordinates[0] = receiver;
|
|
|
|
System.arraycopy(intermediate, 0, coordinates, 1, intermediate.length);
|
|
|
|
initMethodTypes(value, coordinates);
|
|
|
|
}
|
2016-05-05 11:39:08 -07:00
|
|
|
|
|
|
|
// TODO lazily calculate
|
|
|
|
this.memberName_table = linkFromStatic(implClass);
|
2020-05-25 10:54:39 +01:00
|
|
|
}
|
2016-05-05 11:39:08 -07:00
|
|
|
|
2020-05-25 10:54:39 +01:00
|
|
|
VarForm(Class<?> value, Class<?>[] coordinates) {
|
|
|
|
this.methodType_table = new MethodType[VarHandle.AccessType.values().length];
|
|
|
|
this.memberName_table = null;
|
|
|
|
initMethodTypes(value, coordinates);
|
|
|
|
}
|
2016-05-05 11:39:08 -07:00
|
|
|
|
2020-05-25 10:54:39 +01:00
|
|
|
void initMethodTypes(Class<?> value, Class<?>... coordinates) {
|
2016-05-05 11:39:08 -07:00
|
|
|
// (Receiver, <Intermediates>)Value
|
|
|
|
methodType_table[VarHandle.AccessType.GET.ordinal()] =
|
2020-05-25 10:54:39 +01:00
|
|
|
MethodType.methodType(value, coordinates).erase();
|
2016-05-05 11:39:08 -07:00
|
|
|
|
|
|
|
// (Receiver, <Intermediates>, Value)void
|
|
|
|
methodType_table[VarHandle.AccessType.SET.ordinal()] =
|
2020-05-25 10:54:39 +01:00
|
|
|
MethodType.methodType(void.class, coordinates).appendParameterTypes(value).erase();
|
2016-05-05 11:39:08 -07:00
|
|
|
|
|
|
|
// (Receiver, <Intermediates>, Value)Value
|
|
|
|
methodType_table[VarHandle.AccessType.GET_AND_UPDATE.ordinal()] =
|
2020-05-25 10:54:39 +01:00
|
|
|
MethodType.methodType(value, coordinates).appendParameterTypes(value).erase();
|
2016-05-05 11:39:08 -07:00
|
|
|
|
|
|
|
// (Receiver, <Intermediates>, Value, Value)boolean
|
2017-11-17 12:09:56 -08:00
|
|
|
methodType_table[VarHandle.AccessType.COMPARE_AND_SET.ordinal()] =
|
2020-05-25 10:54:39 +01:00
|
|
|
MethodType.methodType(boolean.class, coordinates).appendParameterTypes(value, value).erase();
|
2016-05-05 11:39:08 -07:00
|
|
|
|
|
|
|
// (Receiver, <Intermediates>, Value, Value)Value
|
|
|
|
methodType_table[VarHandle.AccessType.COMPARE_AND_EXCHANGE.ordinal()] =
|
2020-05-25 10:54:39 +01:00
|
|
|
MethodType.methodType(value, coordinates).appendParameterTypes(value, value).erase();
|
2016-05-05 11:39:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@ForceInline
|
|
|
|
final MethodType getMethodType(int type) {
|
|
|
|
return methodType_table[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
@ForceInline
|
|
|
|
final MemberName getMemberName(int mode) {
|
|
|
|
// TODO calculate lazily
|
|
|
|
MemberName mn = memberName_table[mode];
|
|
|
|
if (mn == null) {
|
|
|
|
throw new UnsupportedOperationException();
|
2016-03-24 11:21:21 +01:00
|
|
|
}
|
2016-05-05 11:39:08 -07:00
|
|
|
return mn;
|
|
|
|
}
|
|
|
|
|
2016-03-24 11:21:21 +01:00
|
|
|
|
2016-05-05 11:39:08 -07:00
|
|
|
@Stable
|
|
|
|
MethodType[] methodType_V_table;
|
2016-03-24 11:21:21 +01:00
|
|
|
|
2016-05-05 11:39:08 -07:00
|
|
|
@ForceInline
|
|
|
|
final MethodType[] getMethodType_V_init() {
|
|
|
|
MethodType[] table = new MethodType[VarHandle.AccessType.values().length];
|
|
|
|
for (int i = 0; i < methodType_table.length; i++) {
|
|
|
|
MethodType mt = methodType_table[i];
|
|
|
|
// TODO only adjust for sig-poly methods returning Object
|
|
|
|
table[i] = mt.changeReturnType(void.class);
|
|
|
|
}
|
|
|
|
methodType_V_table = table;
|
|
|
|
return table;
|
2016-03-24 11:21:21 +01:00
|
|
|
}
|
|
|
|
|
2016-05-05 11:39:08 -07:00
|
|
|
@ForceInline
|
|
|
|
final MethodType getMethodType_V(int type) {
|
|
|
|
MethodType[] table = methodType_V_table;
|
|
|
|
if (table == null) {
|
|
|
|
table = getMethodType_V_init();
|
|
|
|
}
|
|
|
|
return table[type];
|
2016-03-24 11:21:21 +01:00
|
|
|
}
|
|
|
|
|
2016-05-05 11:39:08 -07:00
|
|
|
|
2016-03-24 11:21:21 +01:00
|
|
|
/**
|
|
|
|
* Link all signature polymorphic methods.
|
|
|
|
*/
|
2016-04-11 18:21:25 +02:00
|
|
|
private static MemberName[] linkFromStatic(Class<?> implClass) {
|
|
|
|
MemberName[] table = new MemberName[AccessMode.values().length];
|
2016-03-24 11:21:21 +01:00
|
|
|
|
|
|
|
for (Class<?> c = implClass; c != VarHandle.class; c = c.getSuperclass()) {
|
|
|
|
for (Method m : c.getDeclaredMethods()) {
|
|
|
|
if (Modifier.isStatic(m.getModifiers())) {
|
2016-04-13 15:05:48 +02:00
|
|
|
AccessMode am = AccessMode.methodNameToAccessMode.get(m.getName());
|
|
|
|
if (am != null) {
|
2016-04-11 18:21:25 +02:00
|
|
|
assert table[am.ordinal()] == null;
|
|
|
|
table[am.ordinal()] = new MemberName(m);
|
2016-03-24 11:21:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 18:21:25 +02:00
|
|
|
}
|
|
|
|
return table;
|
2016-03-24 11:21:21 +01:00
|
|
|
}
|
2019-09-20 11:07:52 +01:00
|
|
|
}
|