|
| 1 | +object Test { |
| 2 | + |
| 3 | + def hasGenericSignature(cls: Class[_], methName: String): Boolean = { |
| 4 | + cls.getDeclaredMethods().find(_.getName.contains(methName)) match { |
| 5 | + case None => throw new NoSuchMethodError(s"No $methName in ${cls.getName}") |
| 6 | + case Some(meth) => meth.getTypeParameters.nonEmpty |
| 7 | + } |
| 8 | + } |
| 9 | + |
| 10 | + def checkHasGenericSignature(cls: Class[_], methName: String): Unit = |
| 11 | + assert(hasGenericSignature(cls, methName)) |
| 12 | + |
| 13 | + def checkDoesntHaveGenericSignature(cls: Class[_], methName: String): Unit = |
| 14 | + assert(!hasGenericSignature(cls, methName)) |
| 15 | + |
| 16 | + def main(args: Array[String]): Unit = { |
| 17 | + |
| 18 | + checkHasGenericSignature(classOf[TopLevelClass], "meth") |
| 19 | + checkHasGenericSignature(classOf[AbstractTopLevelClass], "meth") |
| 20 | + checkHasGenericSignature(classOf[TopLevelClass#InsideClass], "meth") |
| 21 | + checkHasGenericSignature(classOf[TopLevelClass#AbstractInsideClass], "meth") |
| 22 | + checkDoesntHaveGenericSignature(new TopLevelClass().localClass, "meth") |
| 23 | + checkDoesntHaveGenericSignature(new TopLevelClass().otherLocalClass, "meth") |
| 24 | + |
| 25 | + checkHasGenericSignature(TopLevelObject.getClass, "meth") |
| 26 | + checkHasGenericSignature(classOf[TopLevelObject.InsideObject], "meth") |
| 27 | + checkHasGenericSignature(classOf[TopLevelObject.AbstractInsideObject], "meth") |
| 28 | + checkDoesntHaveGenericSignature(TopLevelObject.localClass, "meth") |
| 29 | + checkDoesntHaveGenericSignature(TopLevelObject.otherLocalClass, "meth") |
| 30 | + |
| 31 | + println("OK") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +object TopLevelObject { |
| 36 | + def meth[T](x: T): T = x |
| 37 | + |
| 38 | + def localObject: Class[_] = { |
| 39 | + object LocalObject { |
| 40 | + def meth[T](x: T): T = x |
| 41 | + } |
| 42 | + LocalObject.getClass |
| 43 | + } |
| 44 | + |
| 45 | + def localClass: Class[_] = { |
| 46 | + class LocalClass { |
| 47 | + def meth[T](x: T): T = x |
| 48 | + } |
| 49 | + classOf[LocalClass] |
| 50 | + } |
| 51 | + |
| 52 | + val otherLocalClass: Class[_] = { |
| 53 | + class LocalClass { |
| 54 | + def meth[T](x: T): T = x |
| 55 | + } |
| 56 | + classOf[LocalClass] |
| 57 | + } |
| 58 | + |
| 59 | + class InsideObject { |
| 60 | + def meth[T](x: T): T = x |
| 61 | + } |
| 62 | + |
| 63 | + abstract class AbstractInsideObject { |
| 64 | + def meth[T](x: T): T = x |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class TopLevelClass { |
| 69 | + |
| 70 | + def meth[T](x: T): T = x |
| 71 | + |
| 72 | + def localClass: Class[_] = { |
| 73 | + class LocalClass { |
| 74 | + def meth[T](x: T): T = x |
| 75 | + } |
| 76 | + classOf[LocalClass] |
| 77 | + } |
| 78 | + |
| 79 | + val otherLocalClass: Class[_] = { |
| 80 | + class LocalClass { |
| 81 | + def meth[T](x: T): T = x |
| 82 | + } |
| 83 | + classOf[LocalClass] |
| 84 | + } |
| 85 | + |
| 86 | + class InsideClass { |
| 87 | + def meth[T](x: T): T = x |
| 88 | + } |
| 89 | + |
| 90 | + abstract class AbstractInsideClass { |
| 91 | + def meth[T](x: T): T = x |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +abstract class AbstractTopLevelClass { |
| 96 | + def meth[T](x: T): T = x |
| 97 | +} |
| 98 | + |
0 commit comments