Skip to content

Commit 575fc84

Browse files
Implement getClass method
1 parent b3ca8e1 commit 575fc84

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Compiler {
6262
new AugmentScala2Traits,
6363
new ResolveSuper),
6464
List(new Erasure),
65+
List(new GetClass), // getClass transformation should be applied to specialized methods
6566
List(new ElimErasedValueType,
6667
new VCElideAllocations,
6768
new Mixin,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package dotty.tools.dotc
2+
package transform
3+
4+
import ast.tpd
5+
import core.Constants.Constant
6+
import core.Contexts.Context
7+
import core.StdNames.nme
8+
import core.Symbols.TermSymbol
9+
import core.TypeErasure
10+
import TreeTransforms.{MiniPhaseTransform, TransformerInfo}
11+
12+
/**
13+
* FILL ME
14+
*/
15+
class GetClass extends MiniPhaseTransform {
16+
import tpd.{Apply, Tree, Literal, ref, TreeOps}
17+
18+
override def phaseName: String = "getClass"
19+
20+
override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo): Tree = {
21+
import ast.Trees.{Apply, Select}
22+
23+
tree match {
24+
case Apply(Select(typ, nm), List()) if nm.toString == "getClass" =>
25+
val tp = typ.tpe
26+
val defn = ctx.definitions
27+
val claz = tp.classSymbol
28+
29+
def TYPE(module: TermSymbol) = ref(module).select(nme.TYPE_).ensureConforms(tree.tpe)
30+
claz match {
31+
case defn.BooleanClass => TYPE(defn.BoxedBooleanModule)
32+
case defn.ByteClass => TYPE(defn.BoxedByteModule)
33+
case defn.ShortClass => TYPE(defn.BoxedShortModule)
34+
case defn.CharClass => TYPE(defn.BoxedCharModule)
35+
case defn.IntClass => TYPE(defn.BoxedIntModule)
36+
case defn.LongClass => TYPE(defn.BoxedLongModule)
37+
case defn.FloatClass => TYPE(defn.BoxedFloatModule)
38+
case defn.DoubleClass => TYPE(defn.BoxedDoubleModule)
39+
case defn.UnitClass => TYPE(defn.BoxedVoidModule)
40+
case _ =>
41+
Literal(Constant(TypeErasure.erasure(tp)))
42+
}
43+
case _ => tree
44+
}
45+
}
46+
}

tests/run/getclass.check

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Value types:
2+
void
3+
boolean
4+
byte
5+
short
6+
char
7+
int
8+
long
9+
float
10+
double
11+
12+
Class types:
13+
class SomeClass
14+
class ValueClass
15+
class scala.collection.immutable.List
16+
class scala.Tuple2
17+
18+
Arrays:
19+
class [Lscala.runtime.BoxedUnit;
20+
class [I
21+
class [D
22+
class [Lscala.collection.immutable.List;
23+
Functions:
24+
class Test$$anonfun$1
25+
class Test$$anonfun$2

tests/run/getclass.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class ValueClass(val i: Integer) extends AnyVal
2+
class SomeClass
3+
4+
object Test {
5+
def main(args: Array[String]): Unit = {
6+
val cls: Predef.Class[_] = new SomeClass().getClass
7+
val valCls: Predef.Class[_] = new ValueClass(1).getClass
8+
val iCls: Class[Int] = 1.getClass
9+
val f1: Function2[Int, Int, Unit] = (a: Int, b: Int) => println(a + b)
10+
val f2: Function1[Int, Boolean] = (a: Int) => a % 2 == 0
11+
12+
println("Value types:")
13+
println(().getClass)
14+
println(true.getClass)
15+
println(1.asInstanceOf[Byte].getClass)
16+
println(1.asInstanceOf[Short].getClass)
17+
println('a'.getClass)
18+
println(1.getClass)
19+
println(1L.getClass)
20+
println(1f.getClass)
21+
println(1d.getClass)
22+
23+
println("\nClass types:")
24+
println(new SomeClass().getClass)
25+
println(new ValueClass(1).getClass)
26+
println(List(Array(1f)).getClass)
27+
println(("a", Map(1 -> "b")).getClass)
28+
29+
println("\nArrays:")
30+
println(Array(()).getClass)
31+
println(Array(1).getClass)
32+
println(Array(1d).getClass)
33+
println(Array(List("1")).getClass)
34+
35+
println("\nFunctions:")
36+
println(f1.getClass)
37+
println(f2.getClass)
38+
}
39+
}

0 commit comments

Comments
 (0)