Giter Site home page Giter Site logo

koaben / bulletproof Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tvinke/bulletproof

0.0 2.0 0.0 205 KB

Groovy AST transformations, such as @Value and @NonNull, which makes constructing an instance more bullet-proof.

License: Apache License 2.0

Java 5.18% Groovy 94.82%

bulletproof's Introduction

Travis CI

Bulletproof

Groovy AST transformations, such as @Value and @NonNull, which makes constructing an instance more bullet-proof.

bulletproof logo

Groovy has the Immutable annotation which allows to create immutable classes, which is a prerequisite for creating value objects. Unfortunately, when a class has been annotated with Immutable it's no longer possible to add your own constructor to verify if provided parameters are not null, making our value objects really bullet-proof.

Bulletproof helps to fill this gap by adding a few AST transformations.

  • The NonNull annotation which modifies every constructor to perform null-checks. Add this to an Immutable class and no null slips past your constructor.
  • The Value meta-annotation which puts both NonNull and Immutable on your class as a convenience to do above step with one annotation.

Contents

Annotations

Consult the bulletproof 0.1 Groovydocs for complete API information.

NonNull

The NonNull annotation on the class-level triggers an AST transformation which modifies every constructor to perform a null-check.

@groovy.transform.Immutable
@tvinke.bulletproof.transform.NonNull
class Person {
    String name
}
new Person() // throws IllegalArgumentException: "Name can not be null"

Value

The Value meta-annotation combines the Immutable and NonNull annotations, which is used to assist in the creation of value objects.

@tvinke.bulletproof.transform.Value
class Money {
    BigDecimal amount
}

new Money(amount: null) // throws IllegalArgumentException because of NonNull

def money = new Money(2.95)
money.amount = 3.0 // throws ReadOnlyPropertyException because of Immutable

Future Changes

  • Consider allowing a mutable object (e.g. default Map constructor, or with TupleConstructor) also to have null-checks performed. In this case probably also through the setters.
  • In above case, then probably you need you want to specify which properties need to be null-checked instead of just everything.

bulletproof's People

Contributors

tvinke avatar

Watchers

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