Java8 interoperability. Accesing interface default


#1

Not 100% sure this is a failure in Kotlin but I’m having a hard time figuring out what is wrong. Using Kotlin 1.1 RC_91.

I have a JAVA interface defined as follows (this is simplified):

public interface MyInterface {

  default Integer readAsInteger(String key, Integer defaultValue) {
        String val = readValue(key);
        if (val == null) return defaultValue;

        try {
            Integer result = Integer.valueOf(val);
            return result;
        }
        catch (NumberFormatException ex) {
            return defaultValue;
        }
    }
}

I seem to be able to invoke standard interface methods but when I try to invoke the method with the default I get:

Unresolved reference: readAsInteger

Could someone confirm the problem?

Notes: IDEA doesn’t seem to complain about invoking the method, it fails when I do a maven build but I don’t see how that could make any difference.


#2

Assuming that your error message is a typo (function name doesn’t match) are you generating the Java 8 class file format? Default methods were only introduced with that and if you want to depend on a default method you implicitly are aware of java 8 (It would be possible to generate the invocation bytecode targeting Java 6, but it could very well be that the JVM complains - in any case looking at it from a pure Java perspective it looks dodgy).


#3

Error message was, indeed a typo on the post, not on the code.

Thanks for the answer but I’m afraid you lost me on there…

This is my configuration for compiling that particular file on Maven:

    <properties>
        <jdk.version>1.8</jdk.version>
        <kotlin.version>1.1.0-rc-91</kotlin.version>
    </properties>

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>

I don’t think I understand what you mean by “generating the Java 8 class file format”. I would expect that maven configuration to produce bytecode for java 8 and thus, the interface bytecode to have the default implementation. Is there anything that needs to be configured in the kotlin counterpart to be aware of it, maybe to be able to say “when using that clase, note it is Java 8 and treat it as such”?