< prev index next > src/java.base/share/classes/java/lang/invoke/AbstractValidatingLambdaMetafactory.java
Print this page
samStart = capturedArity;
receiverClass = factoryType.parameterType(0);
}
// check receiver type
- if (!implClass.isAssignableFrom(receiverClass)) {
+ if (!implClass.asPrimaryType().isAssignableFrom(receiverClass.asPrimaryType())) {
throw new LambdaConversionException(
String.format("Invalid receiver type %s; not a subtype of implementation type %s",
- receiverClass, implClass));
+ receiverClass.descriptorString(), implClass.descriptorString()));
}
} else {
// no receiver
capturedStart = 0;
samStart = capturedArity;
/** Validate that the given descriptor's types are compatible with {@code dynamicMethodType} **/
private void checkDescriptor(MethodType descriptor) throws LambdaConversionException {
for (int i = 0; i < dynamicMethodType.parameterCount(); i++) {
Class<?> dynamicParamType = dynamicMethodType.parameterType(i);
Class<?> descriptorParamType = descriptor.parameterType(i);
- if (!descriptorParamType.isAssignableFrom(dynamicParamType)) {
+ if (!descriptorParamType.asPrimaryType().isAssignableFrom(dynamicParamType.asPrimaryType())) {
String msg = String.format("Type mismatch for dynamic parameter %d: %s is not a subtype of %s",
i, dynamicParamType, descriptorParamType);
throw new LambdaConversionException(msg);
}
}
} else {
// must be convertible to primitive
return !strict;
}
} else {
- // both are reference types: fromType should be a superclass of toType.
- return !strict || toType.isAssignableFrom(fromType);
+ // primitive types: fromType and toType are types of the same primitive class
+ // identity types: fromType should be a superclass of toType.
+ return !strict || canConvert(fromType, toType);
}
}
}
+ /**
+ * Tests if {@code fromType} can be converted to {@code toType}
+ * via an identity conversion, via a widening reference conversion or
+ * via primitive class narrowing and widening conversions.
+ * <p>
+ * If {@code fromType} represents a class or interface, this method
+ * returns {@code true} if {@code toType} is the same as,
+ * or is a superclass or superinterface of, {@code fromType}.
+ * <p>
+ * If {@code fromType} and {@code toType} is of the same primitive class,
+ * this method returns {@code true}.
+ * <p>
+ * Otherwise, this method returns {@code false}.
+ *
+ * @param fromType the {@code Class} object to be converted from
+ * @param toType the {@code Class} object to be converted to
+ * @return {@code true} if {@code fromType} can be converted to {@code toType}
+ */
+ private boolean canConvert(Class<?> fromType, Class<?> toType) {
+ if (toType.isAssignableFrom(fromType)) {
+ return true;
+ }
+
+ if (fromType.isValue() && toType.isValue()) {
+ // val projection can be converted to ref projection; or vice verse
+ return fromType.asPrimaryType() == toType.asPrimaryType();
+ }
+
+ return false;
+ }
+
/**
* Check type adaptability for return types --
* special handling of void type) and parameterized fromType
* @return True if 'fromType' can be converted to 'toType'
*/
< prev index next >