< prev index next >

src/java.base/share/classes/java/lang/invoke/AbstractValidatingLambdaMetafactory.java

Print this page

352                 // both are primitive: widening
353                 Wrapper wto = forPrimitiveType(toType);
354                 return wto.isConvertibleFrom(wfrom);
355             } else {
356                 // from primitive to reference: boxing
357                 return toType.isAssignableFrom(wfrom.wrapperType());
358             }
359         } else {
360             if (toType.isPrimitive()) {
361                 // from reference to primitive: unboxing
362                 Wrapper wfrom;
363                 if (isWrapperType(fromType) && (wfrom = forWrapperType(fromType)).primitiveType().isPrimitive()) {
364                     // fromType is a primitive wrapper; unbox+widen
365                     Wrapper wto = forPrimitiveType(toType);
366                     return wto.isConvertibleFrom(wfrom);
367                 } else {
368                     // must be convertible to primitive
369                     return !strict;
370                 }
371             } else {
372                 // both are reference types: fromType should be a superclass of toType.
373                 return !strict || toType.isAssignableFrom(fromType);

374             }
375         }
376     }
377 































378     /**
379      * Check type adaptability for return types --
380      * special handling of void type) and parameterized fromType
381      * @return True if 'fromType' can be converted to 'toType'
382      */
383     private boolean isAdaptableToAsReturn(Class<?> fromType, Class<?> toType) {
384         return toType.equals(void.class)
385                || !fromType.equals(void.class) && isAdaptableTo(fromType, toType, false);
386     }
387     private boolean isAdaptableToAsReturnStrict(Class<?> fromType, Class<?> toType) {
388         if (fromType.equals(void.class) || toType.equals(void.class)) return fromType.equals(toType);
389         else return isAdaptableTo(fromType, toType, true);
390     }
391 
392 
393     /*********** Logging support -- for debugging only, uncomment as needed
394     static final Executor logPool = Executors.newSingleThreadExecutor();
395     protected static void log(final String s) {
396         MethodHandleProxyLambdaMetafactory.logPool.execute(new Runnable() {
397             @Override

352                 // both are primitive: widening
353                 Wrapper wto = forPrimitiveType(toType);
354                 return wto.isConvertibleFrom(wfrom);
355             } else {
356                 // from primitive to reference: boxing
357                 return toType.isAssignableFrom(wfrom.wrapperType());
358             }
359         } else {
360             if (toType.isPrimitive()) {
361                 // from reference to primitive: unboxing
362                 Wrapper wfrom;
363                 if (isWrapperType(fromType) && (wfrom = forWrapperType(fromType)).primitiveType().isPrimitive()) {
364                     // fromType is a primitive wrapper; unbox+widen
365                     Wrapper wto = forPrimitiveType(toType);
366                     return wto.isConvertibleFrom(wfrom);
367                 } else {
368                     // must be convertible to primitive
369                     return !strict;
370                 }
371             } else {
372                 // primitive types: fromType and toType are types of the same primitive class
373                 // identity types: fromType should be a superclass of toType.
374                 return !strict || canConvert(fromType, toType);
375             }
376         }
377     }
378 
379     /**
380      * Tests if {@code fromType} can be converted to {@code toType}
381      * via an identity conversion, via a widening reference conversion or
382      * via primitive class narrowing and widening conversions.
383      * <p>
384      * If {@code fromType} represents a class or interface, this method
385      * returns {@code true} if {@code toType} is the same as,
386      * or is a superclass or superinterface of, {@code fromType}.
387      * <p>
388      * If {@code fromType} and {@code toType} is of the same primitive class,
389      * this method returns {@code true}.
390      * <p>
391      * Otherwise, this method returns {@code false}.
392      *
393      * @param     fromType the {@code Class} object to be converted from
394      * @param     toType the {@code Class} object to be converted to
395      * @return    {@code true} if {@code fromType} can be converted to {@code toType}
396      */
397     private boolean canConvert(Class<?> fromType, Class<?> toType) {
398         if (toType.isAssignableFrom(fromType)) {
399             return true;
400         }
401 
402         if (fromType.isPrimitiveClass() && toType.isPrimitiveClass()) {
403             // val projection can be converted to ref projection; or vice verse
404             return fromType.asPrimaryType() == toType.asPrimaryType();
405         }
406 
407         return false;
408     }
409 
410     /**
411      * Check type adaptability for return types --
412      * special handling of void type) and parameterized fromType
413      * @return True if 'fromType' can be converted to 'toType'
414      */
415     private boolean isAdaptableToAsReturn(Class<?> fromType, Class<?> toType) {
416         return toType.equals(void.class)
417                || !fromType.equals(void.class) && isAdaptableTo(fromType, toType, false);
418     }
419     private boolean isAdaptableToAsReturnStrict(Class<?> fromType, Class<?> toType) {
420         if (fromType.equals(void.class) || toType.equals(void.class)) return fromType.equals(toType);
421         else return isAdaptableTo(fromType, toType, true);
422     }
423 
424 
425     /*********** Logging support -- for debugging only, uncomment as needed
426     static final Executor logPool = Executors.newSingleThreadExecutor();
427     protected static void log(final String s) {
428         MethodHandleProxyLambdaMetafactory.logPool.execute(new Runnable() {
429             @Override
< prev index next >