jdk/src/java.base/share/classes/java/lang/invoke/BoundMethodHandle.java
changeset 26469 e6bc14fae1cf
parent 26467 d69abed3a07d
child 26472 71b6a6f208c0
equal deleted inserted replaced
26468:2d57604f9299 26469:e6bc14fae1cf
    58 
    58 
    59     //
    59     //
    60     // BMH API and internals
    60     // BMH API and internals
    61     //
    61     //
    62 
    62 
    63     static MethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    63     static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    64         // for some type signatures, there exist pre-defined concrete BMH classes
    64         // for some type signatures, there exist pre-defined concrete BMH classes
    65         try {
    65         try {
    66             switch (xtype) {
    66             switch (xtype) {
    67             case L_TYPE:
    67             case L_TYPE:
    68                 if (true)  return bindSingle(type, form, x);  // Use known fast path.
    68                 return bindSingle(type, form, x);  // Use known fast path.
    69                 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(L_TYPE).constructor().invokeBasic(type, form, x);
       
    70             case I_TYPE:
    69             case I_TYPE:
    71                 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
    70                 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(I_TYPE).constructor().invokeBasic(type, form, ValueConversions.widenSubword(x));
    72             case J_TYPE:
    71             case J_TYPE:
    73                 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
    72                 return (BoundMethodHandle) SpeciesData.EMPTY.extendWith(J_TYPE).constructor().invokeBasic(type, form, (long) x);
    74             case F_TYPE:
    73             case F_TYPE:
    80         } catch (Throwable t) {
    79         } catch (Throwable t) {
    81             throw newInternalError(t);
    80             throw newInternalError(t);
    82         }
    81         }
    83     }
    82     }
    84 
    83 
    85     static MethodHandle bindSingle(MethodType type, LambdaForm form, Object x) {
    84     static BoundMethodHandle bindSingle(MethodType type, LambdaForm form, Object x) {
    86             return new Species_L(type, form, x);
    85         return Species_L.make(type, form, x);
    87     }
    86     }
    88 
    87 
    89     MethodHandle cloneExtend(MethodType type, LambdaForm form, BasicType xtype, Object x) {
    88     @Override // there is a default binder in the super class, for 'L' types only
    90         try {
    89     /*non-public*/
    91             switch (xtype) {
    90     BoundMethodHandle bindArgumentL(int pos, Object value) {
    92             case L_TYPE: return copyWithExtendL(type, form, x);
       
    93             case I_TYPE: return copyWithExtendI(type, form, ValueConversions.widenSubword(x));
       
    94             case J_TYPE: return copyWithExtendJ(type, form, (long) x);
       
    95             case F_TYPE: return copyWithExtendF(type, form, (float) x);
       
    96             case D_TYPE: return copyWithExtendD(type, form, (double) x);
       
    97             }
       
    98         } catch (Throwable t) {
       
    99             throw newInternalError(t);
       
   100         }
       
   101         throw newInternalError("unexpected type: " + xtype);
       
   102     }
       
   103 
       
   104     @Override
       
   105     MethodHandle bindArgument(int pos, BasicType basicType, Object value) {
       
   106         MethodType type = type().dropParameterTypes(pos, pos+1);
    91         MethodType type = type().dropParameterTypes(pos, pos+1);
   107         LambdaForm form = internalForm().bind(1+pos, speciesData());
    92         LambdaForm form = internalForm().bind(1+pos, speciesData());
   108         return cloneExtend(type, form, basicType, value);
    93         return copyWithExtendL(type, form, value);
   109     }
    94     }
   110 
    95     /*non-public*/
   111     @Override
    96     BoundMethodHandle bindArgumentI(int pos, int value) {
   112     MethodHandle dropArguments(MethodType srcType, int pos, int drops) {
    97         MethodType type = type().dropParameterTypes(pos, pos+1);
   113         LambdaForm form = internalForm().addArguments(pos, srcType.parameterList().subList(pos, pos + drops));
    98         LambdaForm form = internalForm().bind(1+pos, speciesData());
   114         try {
    99         return copyWithExtendI(type, form, value);
   115              return copyWith(srcType, form);
   100     }
   116          } catch (Throwable t) {
   101     /*non-public*/
   117              throw newInternalError(t);
   102     BoundMethodHandle bindArgumentJ(int pos, long value) {
   118          }
   103         MethodType type = type().dropParameterTypes(pos, pos+1);
   119     }
   104         LambdaForm form = internalForm().bind(1+pos, speciesData());
   120 
   105         return copyWithExtendJ(type, form, value);
   121     @Override
   106     }
   122     MethodHandle permuteArguments(MethodType newType, int[] reorder) {
   107     /*non-public*/
   123         try {
   108     BoundMethodHandle bindArgumentF(int pos, float value) {
   124              return copyWith(newType, form.permuteArguments(1, reorder, basicTypes(newType.parameterList())));
   109         MethodType type = type().dropParameterTypes(pos, pos+1);
   125          } catch (Throwable t) {
   110         LambdaForm form = internalForm().bind(1+pos, speciesData());
   126              throw newInternalError(t);
   111         return copyWithExtendF(type, form, value);
   127          }
   112     }
       
   113     /*non-public*/
       
   114     BoundMethodHandle bindArgumentD(int pos, double value) {
       
   115         MethodType type = type().dropParameterTypes(pos, pos + 1);
       
   116         LambdaForm form = internalForm().bind(1+pos, speciesData());
       
   117         return copyWithExtendD(type, form, value);
   128     }
   118     }
   129 
   119 
   130     /**
   120     /**
   131      * Return the {@link SpeciesData} instance representing this BMH species. All subclasses must provide a
   121      * Return the {@link SpeciesData} instance representing this BMH species. All subclasses must provide a
   132      * static field containing this value, and they must accordingly implement this method.
   122      * static field containing this value, and they must accordingly implement this method.