Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make hasProperty(), hasPropertyAtPath(), samePropertyValuesAs() work for Java Records #426

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Commits on Nov 10, 2024

  1. Add MethodUtil to make hasProperty() work for Java Records

    The current `hasProperty()` matcher can't deal with
    Java Records, as they are not compatible with JavaBeans
    specification.
    
    In this change, `HasProperty` first tries to do
    the original thing and then tries to find a method
    of which name is equivalent to the given property name,
    if there's no matching property.
    
    So for example,
    if the class has a getter method named `property()`,
    `hasProperty()` would regard it as the class has
    a property named `property`.
    
    I hope this change might be properly and easily removed
    when the time comes and Hamcrest starts to support Java 17.
    djkeh committed Nov 10, 2024
    Configuration menu
    Copy the full SHA
    8201577 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    f71c10b View commit details
    Browse the repository at this point in the history
  3. Fix MethodUtil to see if a method is a getter

    There is a flaw in 8201577, which is that
    the logic can't distinguish if the found method is a getter.
    Let's put a simple additional rule:
    The property must be a getter,
    so it should return something.
    djkeh committed Nov 10, 2024
    Configuration menu
    Copy the full SHA
    b1809a2 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    587ca12 View commit details
    Browse the repository at this point in the history

Commits on Nov 18, 2024

  1. Move functionalities of MethodUtil to PropertyUtil

    After the debate of pr hamcrest#426,
    We decided to move all the methods of `MethodUtil`
    to `PropertyUtil`.
    There was a confusion of understanding
    the name of the class `PropertyUtil`.
    First I thought it was kind of a technical name
    indicating that `PropertyUtil` deals with
    java beans property.
    But actually it wasn't just a technical name.
    `PropertyUtil` means a tool to find out some
    properties in the target class.
    So to say, it is like a `PropertyFindingUtil`.
    We don't have to change the utility name
    even if it finds some methods of the target class,
    not properties of it,
    as it does what it is meant to do.
    djkeh committed Nov 18, 2024
    Configuration menu
    Copy the full SHA
    958682e View commit details
    Browse the repository at this point in the history

Commits on Nov 19, 2024

  1. Refactor HasPropertyWithValue using lambda expression

    and polished its javadoc.
    djkeh committed Nov 19, 2024
    Configuration menu
    Copy the full SHA
    dbe26b4 View commit details
    Browse the repository at this point in the history

Commits on Nov 20, 2024

  1. Implement matching conditions of HasPropertyWithValue for Java Records

    The basic approach is as same as the `HasProperty`
    in 8201577.
    `hasProperty(propertyName, valueMatcher)` will
    recognize properties in java record classes if:
    
    1. There's a method of which name is same to the given property name
    2. The found method returns something
    
    If it doesn't meet condition 1, the property does not exist.
    If it doesn't meet condition 2, the property is write-only.
    djkeh committed Nov 20, 2024
    Configuration menu
    Copy the full SHA
    84c238b View commit details
    Browse the repository at this point in the history

Commits on Nov 24, 2024

  1. Configuration menu
    Copy the full SHA
    0fb657c View commit details
    Browse the repository at this point in the history
  2. Implement matching conditions of SamePropertyValuesAs for Java Records

    To achieve this goal I needed a different approach
    from 8201577.
    `samePropertyValuesAs(expectedBean, ignoredProperties)`
    will recognize properties in java record classes if:
    
    * There's a read accessor method of which name is same to the given property name
    
    A read accessor method is a method
    which acts like a getter for a field.
    To check if the method is a read accessor method,
    the algorithm filters the following:
    
    1. The name of it should be identical to the name of the field.
    2. It should return something.
    3. It should not have any method parameters.
    
    In this manner, we can now assure that
    the collected methods are getters.
    
    ## Reference
    
    * https://docs.oracle.com/en/java/javase/14/language/records.html
    djkeh committed Nov 24, 2024
    Configuration menu
    Copy the full SHA
    5068e67 View commit details
    Browse the repository at this point in the history
  3. Remove unused method in PropertyUtil

    The former implementation
    `methodDescriptorsFor()` can be
    replaced to the new method
    `recordReadAccessorMethodDescriptorsFor()`.
    This doesn't change current behavior
    except one, the case for the non-readable property.
    Actually, there can't be any non-readable property in
    Java Records by its specification.
    djkeh committed Nov 24, 2024
    Configuration menu
    Copy the full SHA
    c397cbf View commit details
    Browse the repository at this point in the history