2

I have few classes that I need to annotate with a name so I defined my annotation as

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface JsonUnmarshallable {
    public String value();
}

Now the class that needs this annotation is defined as

@JsonUnmarshallable("myClass")
public class MyClassInfo {
<few properties>
}

I used below code to scan the annotations

private <T> Map<String, T> scanForAnnotation(Class<JsonUnmarshallable> annotationType) {
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(applicationContext, false);
    scanner.addIncludeFilter(new AnnotationTypeFilter(annotationType));
    scanner.scan("my");
    applicationContext.refresh();
    return (Map<String, T>) applicationContext.getBeansWithAnnotation(annotationType);
}

The problem is that the map returned contains ["myClassInfo" -> object of MyClassInfo] but I need the map to contain "myClass" as key, which is the value of the Annotation not the bean name.

Is there a way of doing this?

Manoj
  • 5,542
  • 9
  • 54
  • 80

4 Answers4

5

Just get the annotation object and pull out the value

Map<String,T> tmpMap = new HashMap<String,T>();
JsonUnmarshallable ann;
for (T o : applicationContext.getBeansWithAnnotation(annotationType).values()) {
    ann = o.getClass().getAnnotation(JsonUnmarshallable.class);
    tmpMap.put(ann.value(),o);
}
return o;

Let me know if that's not clear.

MaddHacker
  • 1,118
  • 10
  • 17
5

In my case I wrote like below:

ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
scanner.addIncludeFilter(new AnnotationTypeFilter(JsonUnmarshallable.class));
Set<BeanDefinition> definitions = scanner.findCandidateComponents("base.package.for.scanning");

for(BeanDefinition d : definitions) {
    String className = d.getBeanClassName();
    String packageName = className.substring(0,className.lastIndexOf('.'));
    System.out.println("packageName:" + packageName + " , className:" + className);
}
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
user6690034
  • 51
  • 1
  • 1
0

Maybe you can use http://scannotation.sourceforge.net/ framework to achive that.

Hope it helps.

Alexey Ogarkov
  • 2,916
  • 23
  • 28
  • I tried to use the framework, it is more flexible but I couldn't find that is specific to my use case. Can you tell me how can I get the annotationDb to return a Map keyed by the value of annotation defined in the class – Manoj May 11 '12 at 10:48
0

You can provide a custom BeanNameGenerator to the ClassPathBeanDefinitionScanner, which can look for the value of your annotation and return that as the bean name.

I think an implementation along these lines should work for you.

package org.bk.lmt.services;

import java.util.Map;
import java.util.Set;

import org.springframework.context.annotation.AnnotationBeanNameGenerator;
public class CustomBeanNameGenerator extends AnnotationBeanNameGenerator{
    @Override
    protected boolean isStereotypeWithNameValue(String annotationType,
            Set<String> metaAnnotationTypes, Map<String, Object> attributes) {

        return annotationType.equals("services.JsonUnmarshallable");
    }
}

Add this to your previous scanner code: scanner.setBeanNameGenerator(new CustomBeanNameGenerator());

Community
  • 1
  • 1
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125