Code Monkey home page Code Monkey logo

annotationtest's Introduction

Contract Annotation POC

What is Contract Annotation POC

Sample project demonstrating replacing the Contract interfaces which are tedious to write and their implementations abuse the language feature (@Override annotation) for unrelated reason, this turns the logic there, defines a dedicated annotation (@Contract), defines AnnotationProcessor which at the build time analyzes all the usage of this Contract annotation and creates the contract interface files from this information. This way we have cleaner marker annotation, and there is no need to create/maintain the interface file by hand. As extra feature, it is easy to extend the AnnotationProcessor to perform some additional check(s).

How to use

MainActivity has some dummy methods that get annotated with the Contract annotation. This annotation should have a value, which is a String constant, that defines the contract. It is possible for a method to be part of multiple contracts, using ContractArray annotation, see doubleAgent() method for example. The generated contract interfaces are located at app/build/generated/source/apt/debug/

How it works

AnnotationProcessor does most of the work. SupportedAnnotationTypes annotation ties it to Contract annotation and resources/META-INF.services/javax.annotation.processing.Processor points build system to the AnnotationProcessor class. Everything is updated during the build process (see the gradle files for additional tasks) and generated files are first class citizens, so they can even be used within the application. The processing of annotated elements in the AnnotationProcessor is simplified (cheating to assume all methods are returning void and have no parameters) but this would be easy to change and is sufficient for the illustration purposed for this POC. Real implementation should use robust solution such as JavaPoet

annotationtest's People

Contributors

lahoda avatar

Watchers

Actiwerks avatar James Cloos 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.