jdk/src/java.base/share/classes/java/lang/invoke/VarHandle.java
changeset 40256 c5e03eaf7ba2
parent 39474 2dbd21fdb7a0
child 40732 2fd9cf42bb3c
equal deleted inserted replaced
40255:0222e4232e7c 40256:c5e03eaf7ba2
  1055     @MethodHandle.PolymorphicSignature
  1055     @MethodHandle.PolymorphicSignature
  1056     @HotSpotIntrinsicCandidate
  1056     @HotSpotIntrinsicCandidate
  1057     Object addAndGet(Object... args);
  1057     Object addAndGet(Object... args);
  1058 
  1058 
  1059     enum AccessType {
  1059     enum AccessType {
  1060         GET(Object.class) {
  1060         GET(Object.class),
  1061             @Override
  1061         SET(void.class),
  1062             MethodType accessModeType(Class<?> receiver, Class<?> value,
  1062         COMPARE_AND_SWAP(boolean.class),
  1063                                       Class<?>... intermediate) {
  1063         COMPARE_AND_EXCHANGE(Object.class),
  1064                 Class<?>[] ps =  allocateParameters(0, receiver, intermediate);
  1064         GET_AND_UPDATE(Object.class);
  1065                 fillParameters(ps, receiver, intermediate);
       
  1066                 return MethodType.methodType(value, ps);
       
  1067             }
       
  1068         },
       
  1069         SET(void.class) {
       
  1070             @Override
       
  1071             MethodType accessModeType(Class<?> receiver, Class<?> value,
       
  1072                                       Class<?>... intermediate) {
       
  1073                 Class<?>[] ps =  allocateParameters(1, receiver, intermediate);
       
  1074                 int i = fillParameters(ps, receiver, intermediate);
       
  1075                 ps[i] = value;
       
  1076                 return MethodType.methodType(void.class, ps);
       
  1077             }
       
  1078         },
       
  1079         COMPARE_AND_SWAP(boolean.class) {
       
  1080             @Override
       
  1081             MethodType accessModeType(Class<?> receiver, Class<?> value,
       
  1082                                       Class<?>... intermediate) {
       
  1083                 Class<?>[] ps =  allocateParameters(2, receiver, intermediate);
       
  1084                 int i = fillParameters(ps, receiver, intermediate);
       
  1085                 ps[i++] = value;
       
  1086                 ps[i] = value;
       
  1087                 return MethodType.methodType(boolean.class, ps);
       
  1088             }
       
  1089         },
       
  1090         COMPARE_AND_EXCHANGE(Object.class) {
       
  1091             @Override
       
  1092             MethodType accessModeType(Class<?> receiver, Class<?> value,
       
  1093                                       Class<?>... intermediate) {
       
  1094                 Class<?>[] ps =  allocateParameters(2, receiver, intermediate);
       
  1095                 int i = fillParameters(ps, receiver, intermediate);
       
  1096                 ps[i++] = value;
       
  1097                 ps[i] = value;
       
  1098                 return MethodType.methodType(value, ps);
       
  1099             }
       
  1100         },
       
  1101         GET_AND_UPDATE(Object.class) {
       
  1102             @Override
       
  1103             MethodType accessModeType(Class<?> receiver, Class<?> value,
       
  1104                                       Class<?>... intermediate) {
       
  1105                 Class<?>[] ps =  allocateParameters(1, receiver, intermediate);
       
  1106                 int i = fillParameters(ps, receiver, intermediate);
       
  1107                 ps[i] = value;
       
  1108                 return MethodType.methodType(value, ps);
       
  1109             }
       
  1110         };
       
  1111 
  1065 
  1112         final Class<?> returnType;
  1066         final Class<?> returnType;
  1113         final boolean isMonomorphicInReturnType;
  1067         final boolean isMonomorphicInReturnType;
  1114 
  1068 
  1115         AccessType(Class<?> returnType) {
  1069         AccessType(Class<?> returnType) {
  1116             this.returnType = returnType;
  1070             this.returnType = returnType;
  1117             isMonomorphicInReturnType = returnType != Object.class;
  1071             isMonomorphicInReturnType = returnType != Object.class;
  1118         }
  1072         }
  1119 
  1073 
  1120         abstract MethodType accessModeType(Class<?> receiver, Class<?> value,
  1074         MethodType accessModeType(Class<?> receiver, Class<?> value,
  1121                                            Class<?>... intermediate);
  1075                                   Class<?>... intermediate) {
       
  1076             Class<?>[] ps;
       
  1077             int i;
       
  1078             switch (this) {
       
  1079                 case GET:
       
  1080                     ps = allocateParameters(0, receiver, intermediate);
       
  1081                     fillParameters(ps, receiver, intermediate);
       
  1082                     return MethodType.methodType(value, ps);
       
  1083                 case SET:
       
  1084                     ps = allocateParameters(1, receiver, intermediate);
       
  1085                     i = fillParameters(ps, receiver, intermediate);
       
  1086                     ps[i] = value;
       
  1087                     return MethodType.methodType(void.class, ps);
       
  1088                 case COMPARE_AND_SWAP:
       
  1089                     ps = allocateParameters(2, receiver, intermediate);
       
  1090                     i = fillParameters(ps, receiver, intermediate);
       
  1091                     ps[i++] = value;
       
  1092                     ps[i] = value;
       
  1093                     return MethodType.methodType(boolean.class, ps);
       
  1094                 case COMPARE_AND_EXCHANGE:
       
  1095                     ps = allocateParameters(2, receiver, intermediate);
       
  1096                     i = fillParameters(ps, receiver, intermediate);
       
  1097                     ps[i++] = value;
       
  1098                     ps[i] = value;
       
  1099                     return MethodType.methodType(value, ps);
       
  1100                 case GET_AND_UPDATE:
       
  1101                     ps = allocateParameters(1, receiver, intermediate);
       
  1102                     i = fillParameters(ps, receiver, intermediate);
       
  1103                     ps[i] = value;
       
  1104                     return MethodType.methodType(value, ps);
       
  1105                 default:
       
  1106                     throw new InternalError("Unknown AccessType");
       
  1107             }
       
  1108         }
  1122 
  1109 
  1123         private static Class<?>[] allocateParameters(int values,
  1110         private static Class<?>[] allocateParameters(int values,
  1124                                                      Class<?> receiver, Class<?>... intermediate) {
  1111                                                      Class<?> receiver, Class<?>... intermediate) {
  1125             int size = ((receiver != null) ? 1 : 0) + intermediate.length + values;
  1112             int size = ((receiver != null) ? 1 : 0) + intermediate.length + values;
  1126             return new Class<?>[size];
  1113             return new Class<?>[size];