how to write a java method with generic class type?

Elad Benda

I want to write a generic method (either with generics or with param)

that will replace these two calls:

private ServerEvent filterEvents() {
    return argThat(new ArgumentMatcher<ServerEvent>() {
        @Override
        public boolean matches(Object argument) {
            return argument instanceof Type1;
        }
    });

private ServerEvent filterEvents() {
    return argThat(new ArgumentMatcher<ServerEvent>() {
        @Override
        public boolean matches(Object argument) {
            return argument instanceof Type2;
        }
    });

I have tried this, but got compilation errors:

private <T> ServerEvent filterEvents() {
    return argThat(new ArgumentMatcher<ServerEvent>() {
        @Override
        public boolean matches(Object argument) {
            return argument instanceof T;
        }
    });

update:

I have tried this also, but still got compiliation error:

    verify(loggerUtilsWrapper).writeEvent(filterEvents(MatchNotFoundEvent.class));


private ServerEvent filterEvents(final Class<MatcherEvent> eventType) {
    return argThat(new ArgumentMatcher<ServerEvent>() {
        @Override
        public boolean matches(Object argument) {
            return argument instanceof eventType;
        }
    });
}
Bohemian

Due to runtime type erasure, the "T" is not available. The standard way is to pass a type token by way of a parameter of type Class<T>.

However, in this case you don't need generic, because you aren't actually using the type. Instead, just pass a Class object and compare it:

private ServerEvent filterEvents(final Class<?> c) {
    return argThat(new ArgumentMatcher<ServerEvent>() {
        @Override
        public boolean matches(Object argument) {
            return c.isInstance(argument);
        }
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to write a java method with generic class type?

From Dev

How do I write generic Java method and compare two variables of the generic type inside the method?

From Dev

How to use Class type Generic in a method

From Dev

how to write this method in a generic way in java

From Dev

Java: How do I write a generic method?

From Dev

How to apply complex generic method to class with wildcard generic type?

From Dev

Passing a class with type parameter as type parameter for generic method in Java

From Dev

Define generic Type of parent class method depending on subclass Type in Java

From Dev

Class type reference not pass in java generic class or method

From Dev

How to use override method in generic class in JAVA

From Dev

Java: Generic class with generic method

From Dev

How to infer class from generic type in Java?

From Dev

How to Write Generic Extension Method to Convert Type in C#

From Dev

How to pass generic list class as an argument to generic method in Java?

From Dev

How to pass generic list class as an argument to generic method in Java?

From Dev

Execute a method of accepted generic Type, inside a partially abstract class - Java

From Dev

Solve Java Generic Type Error with Abstract Class Method

From Dev

pass generic type class to method

From Dev

Generic method without class type?

From Dev

Generic Type Method Without Generic Reference in Class

From Dev

Type inference in generic method & generic class

From Dev

Generic method with generic class type constraint

From Dev

How to define a generic method that returns an instance of the deriving class type?

From Java

How to get the type of T from a member of a generic class or method?

From Dev

How to trigger a Generic Class method recursively changing the type of T?

From Dev

How to run method that returns an object of "Type" from generic class

From Dev

How to write a generic method that takes two arguments of the same types in java?

From Dev

How to write an interface for a generic method

From Dev

How to write a generic method for lambda?

Related Related

  1. 1

    how to write a java method with generic class type?

  2. 2

    How do I write generic Java method and compare two variables of the generic type inside the method?

  3. 3

    How to use Class type Generic in a method

  4. 4

    how to write this method in a generic way in java

  5. 5

    Java: How do I write a generic method?

  6. 6

    How to apply complex generic method to class with wildcard generic type?

  7. 7

    Passing a class with type parameter as type parameter for generic method in Java

  8. 8

    Define generic Type of parent class method depending on subclass Type in Java

  9. 9

    Class type reference not pass in java generic class or method

  10. 10

    How to use override method in generic class in JAVA

  11. 11

    Java: Generic class with generic method

  12. 12

    How to infer class from generic type in Java?

  13. 13

    How to Write Generic Extension Method to Convert Type in C#

  14. 14

    How to pass generic list class as an argument to generic method in Java?

  15. 15

    How to pass generic list class as an argument to generic method in Java?

  16. 16

    Execute a method of accepted generic Type, inside a partially abstract class - Java

  17. 17

    Solve Java Generic Type Error with Abstract Class Method

  18. 18

    pass generic type class to method

  19. 19

    Generic method without class type?

  20. 20

    Generic Type Method Without Generic Reference in Class

  21. 21

    Type inference in generic method & generic class

  22. 22

    Generic method with generic class type constraint

  23. 23

    How to define a generic method that returns an instance of the deriving class type?

  24. 24

    How to get the type of T from a member of a generic class or method?

  25. 25

    How to trigger a Generic Class method recursively changing the type of T?

  26. 26

    How to run method that returns an object of "Type" from generic class

  27. 27

    How to write a generic method that takes two arguments of the same types in java?

  28. 28

    How to write an interface for a generic method

  29. 29

    How to write a generic method for lambda?

HotTag

Archive