Kiril Kirilov
Kiril Kirilov

Reputation: 11257

Is there wildcard for annotatted classes

Persistent object:

@Entity
public class PersistentModelObject{
  ...
}

I need something like:

interface GenericDao<T annotated_with Entity>{
  //crud
}

Or can it be simulated in some way (with extension, implementation, etc).

EDIT: Please someone who understands my question to edit it to some understandable level.

Upvotes: 1

Views: 111

Answers (2)

vbezhenar
vbezhenar

Reputation: 12336

This restriction can only be checked at runtime using reflection.

Upvotes: 1

activist
activist

Reputation: 121

I dont think you can use annotations like that via generics, but you can use java.lang.Class java.lang.reflect.Field isAnnotationPresent(YourAnnotation.class) to check if a class is annotated with a certain annotation.

A better aproach might be using marker interfaces? Something like:

public class PersistentModelObject implements MyPersistableType{
     ...
}

and

interface MyPersistableType {} //marker interface

Then you may use it like this:

interface GenericDao<T extends MyPersistableType>{
  //crud
}

But then again, it depends on what you are trying to solve.

Upvotes: 1

Related Questions