New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Builder.Default: error: non-static type variable T cannot be referenced from a static context #1527

Closed
hosswald opened this Issue Nov 27, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@hosswald

hosswald commented Nov 27, 2017

Short description

Using @Builder on a generic class with a @Default T-typed variable, I get the above compile error.

Expected behavior

It should compile.

Version information

  • IDEA Version: IntelliJ IDEA 2017.3 EAP (Ultimate Edition)
    Build #IU-173.3727.22, built on November 15, 2017
    IntelliJ IDEA EAP User
    Expiration date: December 15, 2017
    JRE: 1.8.0_152-release-1024-b6 amd64
    JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
    Windows 10 10.0
  • JDK Version: I believe it's the same as the JRE version
  • OS Type & Version: Windows 10 Version 1703
  • Lombok Plugin Version: 0.15.17.2
  • Lombok Dependency Version: 1.16.18

Steps to reproduce

What steps do we need to take to reproduce this issue?

  1. Try to compile the following minimal example:
import lombok.Builder;
import lombok.Builder.Default;

@Builder
public class GenericsBuilderExample<T> {
    @Default private T variable = null; // it compiles if I remove the redundant initializer
}
  1. Get the following compile error:
C:\Users\...\GenericsBuilderExample.java:4: error: non-static type variable T cannot be referenced from a static context
@Builder
^

I tried compiling with --stacktrace, --info and --debug, but got no useful information.

Removing the initializer is not an option (and silly when using @default), since my concrete use case looks more like this:

@Default private Function<T, Boolean> function = (x) -> true; // leads to the same compile error

Apart from being incorrect (no handling of @Default resulting in incorrect semantics), the Delombok'd version compiles:

import lombok.Builder.Default;

public class GenericsBuilderExample<T> {
    @Default private T variable = null;

    @java.beans.ConstructorProperties({"variable"})
    GenericsBuilderExample(T variable) {
        this.variable = variable;
    }

    public static <T> GenericsBuilderExampleBuilder<T> builder() {return new GenericsBuilderExampleBuilder<>();}

    public static class GenericsBuilderExampleBuilder<T> {
        private T variable;

        GenericsBuilderExampleBuilder() {}

        public GenericsBuilderExampleBuilder<T> variable(T variable) {
            this.variable = variable;
            return this;
        }

        public GenericsBuilderExample<T> build() {
            return new GenericsBuilderExample<>(variable);
        }

        public String toString() {return "GenericsBuilderExample.GenericsBuilderExampleBuilder(variable=" + this.variable + ")";}
    }
}
@rspilker

This comment has been minimized.

Collaborator

rspilker commented Dec 11, 2017

I can reproduce the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment