Skip to content

Commit

Permalink
Merge pull request #9478 from retronym/ticket/12038-cyclic-found-refl…
Browse files Browse the repository at this point in the history
…ection

Fix cylic error in runtime reflection (protobuf)
  • Loading branch information
lrytz committed Feb 2, 2021
2 parents 5f6e5a1 + 845cda3 commit adc9a22
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/reflect/scala/reflect/runtime/JavaMirrors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,10 @@ private[scala] trait JavaMirrors extends internal.SymbolTable with api.JavaUnive
}
case japplied: ParameterizedType =>
// http://stackoverflow.com/questions/5767122/parameterizedtype-getrawtype-returns-j-l-r-type-not-class
val sym = classToScala(japplied.getRawType.asInstanceOf[jClass[_]])
val pre = if (japplied.getOwnerType ne null) typeToScala(japplied.getOwnerType) else sym.owner.thisType
val jcls = japplied.getRawType.asInstanceOf[jClass[_]]
val sym = classToScala(jcls)
val isStatic = java.lang.reflect.Modifier.isStatic(jcls.getModifiers)
val pre = if (!isStatic && (japplied.getOwnerType ne null)) typeToScala(japplied.getOwnerType) else sym.owner.thisType
val args0 = japplied.getActualTypeArguments
val (args, bounds) = targsToScala(pre.typeSymbol, args0.toList)
newExistentialType(bounds, typeRef(pre, sym, args))
Expand Down
3 changes: 3 additions & 0 deletions test/files/run/t12038a.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[BuilderType <: p1.J_1.AbstractMessageLite.Builder[BuilderType]]Object {
def <init>(): p1.J_1.AbstractMessageLite[BuilderType]
}
11 changes: 11 additions & 0 deletions test/files/run/t12038a/J_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package p1;

public class J_1 {
public abstract static class AbstractMessageLite<
BuilderType extends AbstractMessageLite.Builder<BuilderType>> {

public abstract static class Builder<BuilderType extends Builder<BuilderType>>{

}
}
}
7 changes: 7 additions & 0 deletions test/files/run/t12038a/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Test {
def main(args: Array[String]): Unit = {
import reflect.runtime.universe._
val m = scala.reflect.runtime.currentMirror
println(m.staticClass("p1.J_1.AbstractMessageLite").info.toString) // was cyclic error
}
}
1 change: 1 addition & 0 deletions test/files/run/t12038b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(x$1: p1.J_1.O[Object]#$I[String])Unit
21 changes: 21 additions & 0 deletions test/files/run/t12038b/J_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package p1;

public class J_1 {
public static abstract class O<A> {
// non static, runtime reflection should use the generic owner type
// in the type signature below.
//
// also doing for static inner classes runs into cyclic errors (see t12038a.scala)
// in the current implementation of runtime reflection.
//
// This is fine as Java rejects the type selections in `notValidJava` below with:
//
// "cannot select a static class from a parameterized type"
public abstract class I<B extends A> {}

public abstract static class J<B> {}
}
static void test(O<Object>.I<String> oi) {}

// static void notValidJava(O<Object>.J<String> oj) {}
}
7 changes: 7 additions & 0 deletions test/files/run/t12038b/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object Test {
def main(args: Array[String]): Unit = {
import reflect.runtime.universe._
val m = scala.reflect.runtime.currentMirror
println(m.staticClass("p1.J_1").companion.info.decl(TermName("test")).asMethod.info.toString)
}
}

0 comments on commit adc9a22

Please sign in to comment.