Skip to main content

Solution to reduce boilerplate code

You can use lombok to reduce the boilerplate code.

Please note that you'll also need to download lombok plugin  for your IDE to avoid incorrect compile error messages.

Here's sample usage with lombok.

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
    <scope>provided</scope>
</dependency>

package com.javahowdoi.challengeq;

import lombok.Data;
import lombok.NonNull;

import static junit.framework.TestCase.assertEquals;

@Data
public class Lombok {
    @NonNull
    private Integer i;
    @NonNull
    private String s;

    public static void main(String[] args ) {
        Lombok l = new Lombok(0, "test");
        assertEquals("test get int method", (Integer) 0, l.getI());
        assertEquals("test get string method", "test", l.getS());
    }
}

Comments