Kotlin 1.3 & Java 11 & enum

Env is as below
JDK → OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
Kotlin-> 1.3

I’m having issue with below code. It works perfectly fine on JDK 8. Issue is observed on JDK 11.

enum class TaxReturnFrequencyEnum {
    MONTH {
        override fun calculatePeriod(dueDate: LocalDate) = calculateInternal(dueDate, 1, 0)
    },
    BI_MONTH {
        override fun calculatePeriod(dueDate: LocalDate) = calculateInternal(dueDate, 2, 1)

    },
    QUARTER {
        override fun calculatePeriod(dueDate: LocalDate) = calculateInternal(dueDate, 3, 2)

    },
    ANNUAL {
        override fun calculatePeriod(dueDate: LocalDate) = calculateInternal(dueDate, 3, 2)

    };


    fun calculateInternal(dueDate: LocalDate, step: Int, offset: Int): TaxReturnPeriod {
        //implementation
    }


    abstract fun calculatePeriod(dueDate: LocalDate): TaxReturnPeriod

}

That enum won’t compile on JDK 11. Compilation error is “error: invalid method declaration; return type required
TaxReturnFrequencyEnum() {
^”

it is complaining about constructor generated by kotlin compiler. Did anyone encountered same issue on JDK 11?

1 Like

Could you please use code highlight wrapper for your code?

@darksnake done!

Your code works for me, java is

C:\projects\tests2>%JAVA_HOME%\bin\java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

project is in kotlin 1.3.10, created from gradle 5.0, using

gradlew init --type kotlin-application --dsl kotlin

and works flawlessly (sans obvious warnings of parameters in calculateInternal never used)

@jakubgwozdz thanks. It turns out issue is down to kapt. My project uses querydsl and Q-classes are generated during the build. It is complaining about constructors on lines 16,29,42.

Was any progress made here? I just upgraded to Java 11 and am having the same issue.

1 Like

I have the same issue.

Apache Maven 3.5.4
Java version: 13.0.1-BellSoft, vendor: BellSoft, runtime: /usr/lib/jvm/bellsoft-java13-amd64
OS name: "linux", version: "4.15.0-66-generic", arch: "amd64", family: "unix"

enum class Interval(open val duration: Duration) {
    Min1(Duration.ofMinutes(1)),
    Min5(Duration.ofMinutes(5)),
    Min15(Duration.ofMinutes(15)),
    Min30(Duration.ofMinutes(30)),
    Hour(Duration.ofHours(1)),
    Day(Duration.ofDays(1)),
    Week(Duration.ofDays(7)),

    Month(Duration.ofDays(30)) {
        override fun idx(ts: Instant): Long {
            val dateTime = ts.atZone(ZoneOffset.UTC)
            return ((dateTime.year - 1970) * 12 + dateTime.monthValue - 1).toLong()
        }

        override val duration: Duration
            get() = throw UnsupportedOperationException("Duration of month interval is ambiguous")
    };

    private val durationNs by lazy { duration.toNanos() }

    open fun idx(ts: Instant): Long = ts.asTimestampNs() / durationNs

    companion object {
        fun all(): Set<Interval> = EnumSet.allOf(Interval::class.java)
    }
}

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.3.50:kapt ... on project ...: Compilation failure
[ERROR] .../target/kaptStubs/compile/.../Interval.java:28: error: invalid method declaration; return type required
[ERROR]         Interval() {
[ERROR]         ^

Same here, any workarounds? Though, I’m not using Java 11

$ kotlinc -version
info: kotlinc-jvm 1.3.50 (JRE 1.8.0_232-8u232-b09-0ubuntu1~18.04.1-b09)

$ java -version
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)