|
1 | 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 | + |
2 | 16 | def main(args: Array[String]): Unit = {
|
3 |
| - val members = classOf[Foo].getDeclaredMethods.map(_.toGenericString) |
4 |
| - assert(members.forall(_ != "<java.lang.NullPointerException>"), members.mkString(", ")) |
| 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 | + |
5 | 31 | println("OK")
|
6 | 32 | }
|
7 | 33 | }
|
8 |
| -class Foo { |
9 |
| - def m[T](x: T): Int => T = { |
10 |
| - def bar(y: Int): T = x |
11 |
| - bar _ |
| 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 |
12 | 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 |
13 | 97 | }
|
14 | 98 |
|
0 commit comments