question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Backporting a method reference to interface enum methods fails

See original GitHub issue

Hi,

Please consider the following code. A simple interface that associates a parameterized key with an object:

public interface IIdentifiable<K> {

    @Nonnull
    K getId();

}

A special test enum whose members are associated with a key:

public enum BinaryDigit implements IIdentifiable<Integer> {

    ZERO(0),
    ONE(1);

    private final Integer id;

    BinaryDigit(final Integer id) {
        this.id = id;
    }

    @Override public final Integer getId() {
        return id;
    }

}

An utility method that builds an IIdentifiable-enum index using Google Guava:

public static <K, E extends Enum<E> & IIdentifiable<K>> Map<K, E> enumIndexOf(final Class<E> type) {
    return uniqueIndex(asList(type.getEnumConstants()), e -> {
        return e.getId();
    });
}

Please note that the last method lambda can be simplified to a method reference like this:

public static <K, E extends Enum<E> & IIdentifiable<K>> Map<K, E> enumIndexOf(final Class<E> type) {
    return uniqueIndex(asList(type.getEnumConstants()), E::getId);
}

Once this refactoring is performed, the Retrolambda (I’m using the Maven plugin) is not able to backport the method reference producing the following error during the build:

Caused by: java.lang.invoke.LambdaConversionException: Invalid receiver type class java.lang.Enum; not a subtype of implementation type interface halo.base.IIdentifiable
    at java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:233)
    at java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:303)
    at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:625)
    at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:647)
    at net.orfjackal.retrolambda.lambdas.LambdaReifier.callBootstrapMethod(LambdaReifier.java:110)
    at net.orfjackal.retrolambda.lambdas.LambdaReifier.reifyLambdaClass(LambdaReifier.java:37)
    ... 37 more

My JDK version is 1.8.0.40 (64). Could you please check? Thanks!


(Also it was difficult to find out which class fails. Is it possible to configure the Retrolambda Maven plugin to report which classes and methods fail to be backported? Currently the plugin only reports the previously saved lambdas like Saving lambda class: foo/bar/Baz$$Lambda$1, and I couldn’t find the failed classes even with the Maven option -X set on.)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
luontolacommented, Dec 19, 2015

The improved error messages is included in Retrolambda 2.1.0. The fix for this issue is still under work.

1reaction
luontolacommented, Dec 18, 2015

And now it reports also the lambda/method reference which caused the problem. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Enum, interfaces and (Java 8) lambdas: code compiles but ...
Currently, javac generates a method reference on J::foo with an invokedynamic that takes an I as parameter, hence it fails at runtime. javac ......
Read more >
How to Use Enum With Java 8 Lambda & Method References
Now let's create FileReaderService class that has methods to read various files, such as readCSV, read excel, etc that takes fileName as the...
Read more >
[JDK-8049512] Should ignore nested lambda bodies during ...
Backport. JDK-8039199javac crash with method references plus lambda plus var args ... JDK-8038082private static method in enum not visible from enum values.
Read more >
Spring Data for Apache Cassandra - Reference Documentation
This chapter explains the core concepts and interfaces of Spring Data repositories. The information in this chapter is pulled from the ...
Read more >
31 SWIG and Python
Double-check the interface to make sure the module name and the shared object filename match. Another possible cause of this error is forgetting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found