3

I have an entity as following :

public class Test extends AbstractEntity<Long> implements IHasDate { ... }

And it's DAO as following :

public class TestDAO extends AbstractDAO<Test, Long> { ... }

In the AbstractDAO I want to test if the passed type parameter Test is an instance of IHasDate.

This is the defintion of the AbstractDAO :

public abstract class AbstractDAO<T extends AbstractEntity<ID>, ID extends Serializable> { 
    public void test(){ 
        // Here I want to test if the passed type parameter is an instance of IHasDate.
    }
}

How can I solve this ?

Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191
  • 1
    "*`Here I want to test if the passed type parameter is an instance of IHasDate.`*" Which one? `AbstractEntity` or `ID`? --- You can enforce it through the bound of the generic parameter by e.g. `T extends AbstractEntity & IHasDate` – Turing85 Jun 13 '18 at 16:05
  • Not a dupe - at best an XY problem. – Makoto Jun 13 '18 at 16:12
  • @Turing85 the class `Test` is the one that implements `IHasDate`, so in this case `T extends AbstractEntity` is the one I want to test. – Renaud is Not Bill Gates Jun 13 '18 at 16:13
  • For reference: [What is the XY-Problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Turing85 Jun 13 '18 at 16:13
  • @IchigoKurosaki As I said: there is no need to test it, you can enforce it. Maybe you want to talk a little bit more about what problem you are actually trying so solve. – Turing85 Jun 13 '18 at 16:14
  • @Turing85 I dont want to inforce it since there are some other classes that extends `AbstractEntity` but they are not implementing `IHasDate`. – Renaud is Not Bill Gates Jun 13 '18 at 16:18
  • 2
    @IchigoKurosaki Again: what is your actual problem? This all sounds very much like an XY-problem. – Turing85 Jun 13 '18 at 16:21
  • Possible duplicate of [Get generic type of class at runtime](https://stackoverflow.com/questions/3403909/get-generic-type-of-class-at-runtime) – Yosef Weiner Jun 13 '18 at 16:43

2 Answers2

0

This is fundamentally unnecessary if all you want to do is ensure that your generic is bound properly.

Instead of checking the type inside of your AbstractDAO, you require a bound on your AbstractDAO to IHasDate, although this is clunky. This means that any entity that doesn't extend IHasDate that attempts to use this will fail to compile.

abstract class AbstractDAO<T extends AbstractEntity<ID> & IHasDate, ID extends Serializable> {
    public void test(){

    }
}

Using the above you can accomplish a similar objective if you create a different DAO which binds to IHasDate.

abstract class IHasDateDao<T extends AbstractEntity<ID> & IHasDate, ID extends Serializable> extends AbstractDAO<T, ID> {
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
  • "*This is fundamentally unnecessary if all you want to do is ensure that your generic is bound properly.*" - But sadly, this is not what OP wants. – Turing85 Jun 13 '18 at 16:33
  • @Turing85: They don't want to enforce it on `AbstractDAO`, to which I provide an alternative. – Makoto Jun 13 '18 at 16:33
  • Life would be sooooo much easier without XY-Problems :) – Turing85 Jun 13 '18 at 16:34
-3

You can use method instanceof
if(yourObject instanceof yourClass){ ... }

Wassim Jied
  • 25
  • 1
  • 5
  • The method has no parameter, thus you do not necessarily have an instance of your class to work with. – Turing85 Jun 13 '18 at 16:09