Giter Site home page Giter Site logo

reified's Introduction

Reified

Reified in Java 11 and upwards

What is Reified

Reified is an Annotation for Java 11 and upwards inspired by the reified keyword in Kotlin. Kotlin's approach requires the instruction to also be inlined, excluding as a result classes by design. This library supports also classes instead as inlining is not needed. Reified creates a local variable, or a parameter of type Class for each type parameter annotated. The correct type is deduced and passed to the tree that owns the type variable based on the enclosing statements(ex. return statements and variable declarations) and the type parameters of the invocation. If a non annotated type parameter is needed to determine the type of annotated one, Reified will take care of automatically applying the same process to said parameters to determine the first.

How to install

Maven

Add this dependency to your dependencies in the pom:

<dependencies>
    <dependency>
        <groupId>com.github.auties00</groupId>
        <artifactId>reified</artifactId>
        <version>1.15</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>17</source>
                <target>17</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>com.github.auties00</groupId>
                        <artifactId>reified</artifactId>
                        <version>1.15</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

Gradle

Add this dependency to your build.gradle:

implementation 'com.github.auties00:reified:1.15'
annotationProcessor 'com.github.auties00:reified:1.15'

Plugins

In order to make linting work in your favourite IDE, a plugin is needed.

Most common IDEs:

  1. IntelliJ - IntelliJ Marketplace, Source Code
  2. Eclipse - Not yet
  3. NetBeans - Not yet

Example

With reified:

class JsonUtils {
    private static final ObjectMapper JACKSON = new ObjectMapper();

    public static <@Reified T> T fromJson(String json){
        return JACKSON.readValue(json, T);
    }
}

record ExampleObject(String name) {
    public static ExampleObject fromJson(String json){
        return JsonUtils.fromJson(json);
    }
}

Without reified:

class JsonUtils {
    private static final ObjectMapper JACKSON = new ObjectMapper();

    public static <T> T fromJson(String json, Class<T> clazz){
        return JACKSON.readValue(json, clazz);
    }
}

record ExampleObject(String name) {
    public static ExampleObject fromJson(String json){
        return JsonUtils.fromJson(json, ExampleObject.class);
    }
}

reified's People

Contributors

auties00 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

reified's Issues

Cannot compile example with record on Java 17 and IntelliJ

I just tried to run the example with Java 17 (Temurin-17+35) and IntelliJ (2021.2.3) with the reified plugin but
I cannot get the project to compile with the record example:

This record:

package demo;

record ExampleObject (String name){

    public static ExampleObject fromJson(String json) {
        return JsonUtils.fromJson(json);
    }
}

yields the following compiler error:

/home/tom/dev/repos/gh/thomasdarimont/java-training/reified-demo/src/main/java/demo/ExampleObject.java:6:25
java: method fromJson in class demo.JsonUtils cannot be applied to given types;
  required: java.lang.Class<T>,java.lang.String
  found:    java.lang.String
  reason: cannot infer type-variable(s) T
    (actual and formal argument lists differ in length)

However the following java bean style pojo works fine:

package demo;

public class ExampleObject {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static ExampleObject fromJson(String json) {
        return JsonUtils.fromJson(json);
    }

    @Override
    public String toString() {
        return "ExampleObject{" +
                "name='" + name + '\'' +
                '}';
    }
}

Here is an example project to reproduce the issue: https://github.com/thomasdarimont/reified-java-demo

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.