Skip to content

Commit

Permalink
Fix sbt#389: Ignore null in generic lambdas parameters
Browse files Browse the repository at this point in the history
Because of the way lambdas are synthesized by the JVM, at the call-site,
it is not possible to know which generic types do lambdas have via the
current reflection API.

Related but not strictly reasons why this happens can be found in JDK's
issue tracker:

https://bugs.openjdk.java.net/browse/JDK-8178523?jql=text%20%7E%20%22lambda%20generic%20type%22

As a result, we ignore nulls that are returned by
`getGenericParameterTypes`.
  • Loading branch information
jvican committed Sep 28, 2017
1 parent 81f76c3 commit bbd3b2f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,19 @@ object ClassToAPI {
}.toMap
def primitive(name: String): api.Type = PrimitiveRefs(name)

// sbt/zinc#389: Ignore nulls coming from generic parameter types of lambdas
private[this] def ignoreNulls(genericTypes: Array[Type]): Array[Type] =
genericTypes.filterNot(_ == null)

private[this] def returnType(f: Field): Type = f.getGenericType
private[this] def returnType(m: Method): Type = m.getGenericReturnType
private[this] def exceptionTypes(c: Constructor[_]): Array[Type] = c.getGenericExceptionTypes

private[this] def exceptionTypes(m: Method): Array[Type] = m.getGenericExceptionTypes
private[this] def parameterTypes(m: Method): Array[Type] = m.getGenericParameterTypes
private[this] def parameterTypes(c: Constructor[_]): Array[Type] = c.getGenericParameterTypes
private[this] def parameterTypes(m: Method): Array[Type] =
ignoreNulls(m.getGenericParameterTypes)
private[this] def parameterTypes(c: Constructor[_]): Array[Type] =
ignoreNulls(c.getGenericParameterTypes)

private[this] def typeParameterTypes[T](m: Constructor[T]): Array[TypeVariable[Constructor[T]]] =
m.getTypeParameters
Expand Down
1 change: 1 addition & 0 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ object Dependencies {
val scalaCheck = "org.scalacheck" %% "scalacheck" % "1.13.4"
val scalatest = "org.scalatest" %% "scalatest" % "3.0.1"
val junit = "junit" % "junit" % "4.11"
val typetools = "net.jodah" % "typetools" % "0.5.0"
val sjsonnew = Def.setting {
"com.eed3si9n" %% "sjson-new-core" % contrabandSjsonNewVersion.value
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package typeparameters;

import java.util.function.Supplier;

public class Example {

static <I, O> void call() {
Supplier<BaseBlah<I, O>> blah = () ->
new BaseBlah<I, O>() {
@Override
protected O getResponseInternal(I i) {
return null;
}
};
}

public static void main(String[] args) {
Example.<String, String>call();
}
}

abstract class BaseBlah<I, O> {
protected O getResponseInternal(I i) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> compile

0 comments on commit bbd3b2f

Please sign in to comment.