Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotation not found on object with Runtime retention

Ok, I'm a little bit confused here. I'm trying to select a "DAO" class by using an annotation on the model:

@Entity
@Table(name="dispatcher")
// use the Kamailio Base DAO for code that supports this annotation
@DAOSelector(dao = DAOBaseKamailio.class) 
public class DispatcherSet extends Model {
    [...]
}

Here is the annotation defenition:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DAOSelector {
       Class<?> dao();
}

I use the following code to return the proper "DAO" class:

public static DAOInterface getCorrectDAO(final Object object) throws Exception {
  final DAOSelector annotation = 
    object.getClass().getAnnotation(DAOSelector.class);

  if(annotation != null) {
    System.out.println("Annotation present: " + 
      annotation.dao().getName() + " for class " + object.getClass().getName());

    final Object dao = annotation.dao().newInstance();
    if(!(dao instanceof DAOInterface)) {
      throw new Exception("Invalid Base DAO in annotation for entity " + 
        object.getClass().getName());
    }
    return (DAOInterface) dao;
  }
  else {
    System.out.println("Annotation not present for class " + 
      object.getClass().getName());
    return new DAOBase();
  }
}

However, when I feed a DispatcherSet object annotation is always null:

10:33:38,498 [INFO] [STDOUT] Annotation not present for class model.DispatcherSet

Am I missing something here?

edit:

OK, found something interesting, I'm running this code inside a JBoss container and when I print out all the annotations:

{{{
$Proxy76
$Proxy708
$Proxy77
}}}

One of these should be a proxied instance of the DAOSelector annotation I'm guessing, so that's probably why getAnnotation(DAOSelector.class) won't work, checking it out.

edit2:

Nope, they are not an instance of DAOSelector

like image 757
Matthias van der Vlies Avatar asked Jan 19 '12 09:01

Matthias van der Vlies


1 Answers

I've fixed the problem. it was a classpath issue. I have an ear containing a jar and war. The model was in the jar and the annotation was present in both.

like image 93
Matthias van der Vlies Avatar answered Sep 24 '22 10:09

Matthias van der Vlies