Named and Optional parameters for Java 17

Overview

Named Parameters

Named and Optional parameters for Java 17

Abstract

While working on another project, I found a way to make the Java compiler attribute a compilation unit without reporting issues if the AST is not correct. So I thought about some cool use cases and decided to create this project. Both classes and methods are supported.

How does it work

So let's say that you have a method or class declaration:

void sayHello(String name, String surname){
    System.out.printf("Hello, %s %s%n", name, surname);
}

Traditionally, you would call the method like this:

sayHello("Alessandro", "Autiero");

Thanks to this plugin, you can use the name of the parameter followed by an assignment operator to specify the parameter:

sayHello(name="Alessandro",surname="Autiero");

Considering that, according to the JLS, assignments can be legally used as arguments if an identifier matching the left side of the assignment exists, an argument is considered named only if said identifier cannot be resolved in the invocation's scope. This is done to preserve backwards compatibility and, for these reason, such calls are considered simply as positional arguments that contain an assignment. In short the parameter "name" in the snippet below is not considered a named parameter in any of the example scenarios.

var name = "Mario";
sayHello(name="Alessandro",surname="Autiero"); // name is not a named parameter as variable name exists
        
void hello(String name){
  sayHello(name="Alessandro", surname="Autiero"); // name is not a named parameter as method parameter name exists
}

class Whatever {
    private String name;
    
    void hello(){
       sayHello(name="Alessandro", surname="Autiero"); // name is not a named parameter as local variable name exists
    }
}

If you want to mark a parameter as Optional, you can do so by applying the @Optional annotation:

void sayHello(String name, @Optional String surname){
    System.out.printf("Hello, %s %s%n", name, surname);
}

sayHello(name="Alessandro");

As Java doesn't support non-constant values as annotation parameters(or generic constant values for that matter), null or 0 is passed as a parameter depending on whether the type is a primitive or not.

How to install

Installing the plugin is pretty easy, all you need to do is add a dependency to your project.

Maven

<dependencies>
    <dependency>
        <groupId>com.github.auties00</groupId>
        <artifactId>named</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

Gradle

implementation 'com.github.auties00:named:1.0'

Gradle Kotlin DSL

implementation("com.github.auties00:named:1.0")
You might also like...

Advanced Java library for integration testing, mocking, faking, and code coverage

Codebase for JMockit 1.x releases - Documentation - Release notes How to build the project: use JDK 1.8 or newer use Maven 3.6.0 or newer; the followi

Dec 9, 2022

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Rub

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Rub

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).

Jan 4, 2023

Apache JMeter - An Open Source Java application designed to measure performance and load test applications

Apache JMeter - An Open Source Java application designed to measure performance and load test applications

An Open Source Java application designed to measure performance and load test applications. By The Apache Software Foundation What Is It? Apache JMete

Jan 1, 2023

ScalaTest is a free, open-source testing toolkit for Scala and Java programmers

ScalaTest is a free, open-source testing toolkit for Scala and Java programmers.

Dec 26, 2022

Brings the popular ruby faker gem to Java and Kotlin

Data Faker This library is a modern port of java-faker, built on Java 8, with up to date libraries and several newly added Fake Generators. This libra

Jan 7, 2023

Ready-to-use UI Test Automation Architecture using Java and Selenium WebDriver.

Selenium Test Automation Boilerplate Ready-to-use UI Test Automation Architecture using Java and Selenium WebDriver. Languages and Frameworks The proj

Dec 26, 2022

make async-await code style available in java just like csharp and es6

make async-await code style available in java just like csharp and es6

JAsync - the async-await pattern of Java 中文版 JAsync implements Async-Await pattern just like es in Java. It allows developers to write asynchronous co

Dec 26, 2022

Restful-booker API test automation project using Java and REST Assured.

Restful-booker API Test Automation Restful-booker API is an API playground created by Mark Winteringham for those wanting to learn more about API test

Aug 14, 2022

HATEOAS with HAL for Java. Create hypermedia APIs by easily serializing your Java models into HAL JSON.

hate HATEOAS with HAL for Java. Create hypermedia APIs by easily serializing your Java models into HAL JSON. More info in the wiki. Install with Maven

Oct 5, 2022
Owner
Alessandro Autiero
Alessandro Autiero
Roman Beskrovnyi 248 Dec 21, 2022
A Java architecture test library, to specify and assert architecture rules in plain Java

ArchUnit is a free, simple and extensible library for checking the architecture of your Java code. That is, ArchUnit can check dependencies between pa

TNG Technology Consulting GmbH 2.5k Jan 2, 2023
Never debug a test again: Detailed failure reports and hassle free assertions for Java tests - Power Asserts for Java

Scott Test Reporter for Maven and Gradle Get extremely detailed failure messages for your tests without assertion libraries, additional configuration

Dávid Csákvári 133 Nov 17, 2022
IntelliJ IDEA and JUnit: Writing, Finding, and Running Tests

IntelliJ IDEA and JUnit: Writing, Finding, and Running Tests ?? Webinar https://blog.jetbrains.com/idea/2021/11/live-stream-recording-intellij-idea-an

Christian Stein 11 Jul 23, 2022
Library that allows tests written in Java to follow the BDD style introduced by RSpec and Jasmine.

J8Spec J8Spec is a library that allows tests written in Java to follow the BDD style introduced by RSpec and Jasmine. More details here: j8spec.github

J8Spec 45 Feb 17, 2022
A modern testing and behavioural specification framework for Java 8

Introduction If you're a Java developer and you've seen the fluent, modern specification frameworks available in other programming languages such as s

Richard Warburton 250 Sep 12, 2022
Fluent assertions for Java and Android

What is Truth? Truth makes your test assertions and failure messages more readable. Similar to AssertJ, it natively supports many JDK and Guava types,

Google 2.6k Jan 5, 2023
Java (and original) version of Hamcrest

Java Hamcrest Licensed under BSD License. What is Hamcrest? Hamcrest is a library of matchers, which can be combined in to create flexible expressions

Hamcrest 2k Jan 5, 2023
Playwright is a Java library to automate Chromium, Firefox and WebKit with a single API.

Playwright is a Java library to automate Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.

Microsoft 634 Jan 8, 2023
A BDD-style test runner for Java 8. Inspired by Jasmine, RSpec, and Cucumber.

Spectrum A colorful BDD-style test runner for Java Spectrum is inspired by the behavior-driven testing frameworks Jasmine and RSpec, bringing their ex

Greg Haskins 143 Nov 22, 2022