Search code examples
javaswingassertj

Error: The type T cannot be used in GenericTypeMatcher - Expression expected


I tried to implement a generic method that will return a GenericTypeMatcher (provided by AssertJ Swing) of type parameter T. You can see the method here.

The problem is that I get an Expression expected error for the supported type of the GenericTypeMatcher with the Class<T>.

Do you have any idea how to solve it?


Solution

  • The problem here is that GenericTypeMatcher accepts a Class<T> argument.
    You're passing the type definition instead, which is invalid.

    private static <T extends Component> GenericTypeMatcher<T> getMatcher(final Class<T> clazz) {
       return new GenericTypeMatcher<T>(clazz) {
          @Override
          protected boolean isMatching(final T object) {
             return ...
          }
       }
    }
    

    Usage

    YourClass.getMatcher(YourComponent.class);