Giter VIP home page Giter VIP logo

simpleannotation's Introduction

Simple Annotation

Requirements

  • Java 8 or newer installed and configured (JAVA_HOME environment variable should be available globally)
  • Maven installed and configured (M2_HOME environment variable should be available globally)

Installation

To install this as a dependency please download the source and run the following command:

  mvn clean install

Afterwards, you can add it as a dependency to your Maven project.

    <dependencies>
        ...
        <dependency>
            <groupId>com.camacho</groupId>
            <artifactId>simpleannotation</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

Adding a new annotation

Preferably, you would need a different maven project to the one you are going to annotate. However, it is not entirely necessary. On the project which will hold the annotations and the processors you can add your annotations classes. Simply follow the template below

package your.ownpackage;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface YourAnnotation {}

Notice that the annotation you are creating is also annotated with @Retention and with @Target. The first annotation indicates if the annotation you add to your code will be deleted at which stage or if it is going to be preserved at runtime. This is useful if you want to check if a class is annotated; however, remember that Java Reflection API is performance consuming and should be avoided in performance sensitive applications. The second annotation tells the compiler what type of elements can be annotated, in this example YourAnnotation indicates only classes can be annotated with it.

Once you created your new annotation, all that is left is to create your annotation processor. You are welcome to extend from com.camacho.AbstractAnnotationProcessor which already implements from javax.annotation.processing.AbstractProcessor and provides some useful lifecycle annotation processing methods to implement, or you can extend from javax.annotation.processing.AbstractProcessor yourself.

Whichever you end up choosing, you will need to annotate your processor with @SupportedAnnotationTypes to indicate the annotations which the processor will processor, @SupportedSourceVersion to indicate the Java Versions supported by the annotation and the processor, @AutoService to generate the necessary registrations for your processor. Note that @AutoService is an annotation from an external dependency from Google. Check for the latest documentation and licenses at this page

All that is left to do is to process the annotation you just created. The SimpleAnnotation project offers some utility classes to generate new Java files. Note that at this time it is not compiler checked. Therefore, you may need check for errors in your generated files. It also does not automatically format your code, so you are responsible for it.

simpleannotation's People

Contributors

oscarcamacho 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.