procyon icon indicating copy to clipboard operation
procyon copied to clipboard

Validation of method-based coercion expression is not correct

Open BladeWise opened this issue 3 years ago • 0 comments

The check to ensure the method singature of a method-based UnaryExpression expression is probably not correct.

The current method is https://github.com/mstrobel/procyon/blob/fec0d7ae031403b4a56fc2f2fd801fc6d416305a/Procyon.Expressions/src/main/java/com/strobel/expressions/Expression.java#L3200-L3230 which means that the coercion method must be:

  • A valid operator (see below)
  • Parameterless
  • A method whose return type is allowed to be assigned to the expected coercion type

To be a valid operator, as per the following function https://github.com/mstrobel/procyon/blob/fec0d7ae031403b4a56fc2f2fd801fc6d416305a/Procyon.Expressions/src/main/java/com/strobel/expressions/Expression.java#L3145-L3169 it must be:

  • Non-Static
  • Non-Void returning
  • Parameterless
  • Declared by a class compatible with its return type

Above criteria do not meet what I would expect from a coercion operator, because there is no way for a non-static parameterless method declared by the expected return type, to convert a value from another type (that cannot be supplied). Eventually, it should be provided by a type assignable from the operand type.

Looking at the 'default' coercion methods https://github.com/mstrobel/procyon/blob/fec0d7ae031403b4a56fc2f2fd801fc6d416305a/Procyon.Reflection/src/main/java/com/strobel/util/TypeUtils.java#L265-L285 we can see that the actual criteria could be either:

  • Static
  • Provided by a type assignable from the coercion type
  • Accepting a parameter compatible with the provided operand type
  • Returning a type compatible with the coercion method like String::valueOf or
  • Non-Static
  • Provided by a type assignable from the operand type
  • Parameterless
  • Returning a type compatible with the coercion method like Long.intValue().

At least, the default coercion methods seem to fall in the above categories (and to be honest, I would avoid the strict check over the declaring type: why a static method provided by another class to perform the conversion is not allowed?).

For reference the current .NET implementation requires the method to be:

  • Static
  • Accepting a single parameter assignable from the operand type
  • Able to return a value assignable from the expected coercion type

BladeWise avatar Feb 17 '22 17:02 BladeWise