开发者

Standalone version of class AnnotationLiteral?

开发者 https://www.devze.com 2023-03-29 01:02 出处:网络
The CDI (Context Dependency Injection) Framework contains the awesome class javax.enterprise.util.AnnotationLit开发者_Python百科eral that makes it very easy to create an instance of an Annotation (esp

The CDI (Context Dependency Injection) Framework contains the awesome class javax.enterprise.util.AnnotationLit开发者_Python百科eral that makes it very easy to create an instance of an Annotation (especaly useful for tests).

I want to use that util class in my own library. But because my library has noting to do with CDI, I do not want to have all the other CDI stuff. Fortunately AnnotationLiteral does not use any other classes than standard java classes, so this should be possible in general.

Up to now the smallest Library that contains AnnotationLiteral is the official CDI-API

<dependency>
   <groupId>javax.enterprise</groupId>
   <artifactId>cdi-api</artifactId>
   <version>1.0</version>
</dependency>

But even if I cut off all the dependencies (by maven exclude), the jar will contain a lot of other CDI-API and -SPI stuff.

So my question is: Is there already some (more or less official) (maven) library that contains only the util.AnnotationLiteral Class, or at least not so much other stuff?


As long as you comply with the license, you could copy the source into your project.

If that is possible, I would retain the package name incase CDI eventually does become a project dependency. The only reason I would change the package is if you decide to modify the source, which of course doesn't mean you can remove the license.


An Apache licensed version of AnnotationLiteral can be found in the geronimo api. It does not look like it has any dependencies to other parts of CDI. If you don't need the equals and hashCode implementation it provides in your tests you could even directly implement an annotation interface. This is not really good style but definitely possible. CDI needs the additional equals and hashCode implementation for its Qualifier logic. Here is an example using the jsf ManagedBean annotation:

public static ManagedBean test = new ManagedBean() {
    @Override
    public String name() {
        return "test";
    }

    @Override
    public boolean eager() {
        return false;
    }

    @Override
    public Class<? extends Annotation> annotationType() {
        return ManagedBean.class;
    }  
};
0

精彩评论

暂无评论...
验证码 换一张
取 消