Giter VIP home page Giter VIP logo

annotation-literal-generator's Introduction

Build Annotation Literal Generator Coverage (Sonar) Status (Sonar) Maintainability (Sonar) Maven Central

Annotation Literal Generator

Introduction

This library should serve primarily education purposes. It should nevertheless prove useful and is developed and tested to current standards.

Problem Challenge

When developing CDI applications or extensions you often have to create instances of annotations e.g. for firing events. This is supported by the AnnotationLiteral superclass.

Unfortunately you have to write quite some boiler plate code. Typical usage looks like this:

    public void notify(final EventType testCaseLifecycle, Object description) {
        AnnotationLiteral<TestEvent> event = new TestEventLiteral() {
            @Override
            public EventType value() {
                return testCaseLifecycle;
            }
        };
        lifecycleEvent.select(event).fire(description);
    }

    private abstract static class TestEventLiteral extends AnnotationLiteral<TestEvent> implements TestEvent {
    }

That's quite some code where you would just like to create an instance of an annotation.

Solution

This library provides an annotation processor that generates the annotation literals for you! It's only necessary to include the library into your java project as follows (see also the integration-tests project):

        <dependency>
            <groupId>de.hilling.lang.annotations</groupId>
            <artifactId>processor</artifactId>
            <version>1.0</version>
            <scope>provided</scope>
        </dependency>

The processor is only needed for the code generation. To compile the generated code you also need the cdi-api in any version for the AnnotationLiteral class.

Your annotation will look like this:

@GenerateLiteral
@interface AnnotatedAnnotation {

    int value();
}

And the generated code becomes:

@Generated("de.hilling.lang.annotations.AnnotationLiteralGenerator")
public class AnnotatedAnnotation__Literal extends AnnotationLiteral<AnnotatedAnnotation> implements AnnotatedAnnotation {
    private final int value;

    public AnnotatedAnnotation__Literal(int value) {
        this.value = value;
    }

    @Override
    public int value() {
        return value;
    }
}

LICENSE

Copyright 2018 Gunnar Hilling

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

annotation-literal-generator's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar guhilling avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.