Go to implementation for autoboxed lambdas (JDK8) ?

given a single method lambda interface:

interface MyLambda {
  String myMethod(File file);
}


And a class:

class MyService {

private final MyLambda myLambda

public MyService(MyLambda lambda){
this.myLambda = lambda;
}

.. some stuff that uses the lambda...
}


Now this class would be used in two different ways:

File myFile = something
MyService myservice1 = new MyService(myFile::getName);

MyService myservice2 = new MyService( (file) -> {return file.getName()};)


In both cases I would expect "Go to Implementation" on the interface MyLambda would bring up the two usages above. It would appear to me that Idea should be able to infer this by static analyzis of the autoboxing that is happening in the constructor call. Is this possible ? Will it be implemented ?

Kristian

4 comments
Comment actions Permalink

If your code is not red, then IDEA infers lambda.

Your problem is very similar to (http://youtrack.jetbrains.com/issue/IDEA-95127 Lambda expression does not apear in hirachy). It's possible to to implement it but in many library cases it would give too much results: you would end up in browsing all lambdas based on Runnable, Computable, etc. We do not have a good workaround for it, so find usages, hierarchy and some other features are not yet available for lambda/method references.

Thanks,
Anna

0
Comment actions Permalink

It seems like you're thinking about inferring *every* method that matches a given lambda, which would be much like you're saying; too much data;
I mean there must be a million methods that match String(String bar).


What I am thinking about is technically not a "go to implementation" but more like a "find usages" that would track the autoboxing to a given interface type, which would show me everywhere something is autoboxed to MyLambda, which means I would be looking at only the REAL cases that implement the method.

It seems like IDEA-95127 expresses what I want, but it seems like you're answering a different question :) I'm thinking about actual usages in actual code, not theoretically possible matches. I want to see all the places where a given interface is being realized in this code base, regardless of where the implementation is being picked from. Hence it's mostly about detecting autoboxing of an implementation to a lambda.
 
Kristian

0
Comment actions Permalink

Sorry, I don't understand.

Would you like to see all places in the code which takes MyLambda interface as a method parameter, variable declaration, etc? What if MyLambda is Runnable?

0
Comment actions Permalink

Going back to the code:

interface MyLambda {
  String myMethod(File file);
}


And a class:

class MyService {

private final MyLambda myLambda

public MyService(MyLambda lambda){
this.myLambda = lambda;
}

.. some stuff that uses the lambda...
}

class MyLambdaImpl implements MyLambda {

}


With regular "find usages" on MyLambda I would see MyLambdaImpl. What I want is this (line numbers in code):

1. File myFile = new File()
2. MyService myservice1 = new MyService(myFile::getName);
3. MyService myservice2 = new MyService( (file) -> {return file.getName()};)
4. MyService myservice1 = new MyService(SomeClass::someStaticMethod);
5. MyService myservice1 = new MyService(nre MyLambdaImpl ());
6. MyLambda ml = myFile::getName


If I do "find usages" on MyLambda, I want to see:

Implementing classes:
     MyLambdaImpl
Autoboxed implementations:
    sampleCode line 2
    sampleCode line 3
    sampleCode line 4
    sampleCode line 6



I'm sorry if autoboxing is not the correct term; but it walks/talks/smells like autoboxing; the lambda is being cast to a specific interface type.  This cast should be possible to identify with static analysis, hence you can also identify the locations in the source code where this cast happens; hopefully :) As it is, there is really no way to find this information other than browsing the code.

Kristian

0

Please sign in to leave a comment.