From 0f9911ca87f78680759277e73dff33c66e017b58 Mon Sep 17 00:00:00 2001 From: Renato Cavalcanti Date: Mon, 26 Oct 2020 22:02:35 +0100 Subject: [PATCH] allow using classOf with object type --- src/reflect/scala/reflect/internal/Types.scala | 2 +- .../pos/classOfObjectType/AnnotationWithClassType.java | 10 ++++++++++ test/files/pos/classOfObjectType/Foo.scala | 7 +++++++ test/files/run/classOfObjectType.scala | 7 +++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/files/pos/classOfObjectType/AnnotationWithClassType.java create mode 100644 test/files/pos/classOfObjectType/Foo.scala create mode 100644 test/files/run/classOfObjectType.scala diff --git a/src/reflect/scala/reflect/internal/Types.scala b/src/reflect/scala/reflect/internal/Types.scala index afe923e49380..a7399e805b09 100644 --- a/src/reflect/scala/reflect/internal/Types.scala +++ b/src/reflect/scala/reflect/internal/Types.scala @@ -4752,7 +4752,7 @@ trait Types /** def isNonValueType(tp: Type) = !isValueElseNonValue(tp) */ def isNonRefinementClassType(tpe: Type) = tpe match { - case SingleType(_, sym) => sym.isModuleClass + case SingleType(_, sym) => sym.isModuleOrModuleClass case TypeRef(_, sym, _) => sym.isClass && !sym.isRefinementClass case ErrorType => true case _ => false diff --git a/test/files/pos/classOfObjectType/AnnotationWithClassType.java b/test/files/pos/classOfObjectType/AnnotationWithClassType.java new file mode 100644 index 000000000000..476cfdeffe1b --- /dev/null +++ b/test/files/pos/classOfObjectType/AnnotationWithClassType.java @@ -0,0 +1,10 @@ +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface AnnotationWithClassType { + Class cls(); +} diff --git a/test/files/pos/classOfObjectType/Foo.scala b/test/files/pos/classOfObjectType/Foo.scala new file mode 100644 index 000000000000..b7f91bd877be --- /dev/null +++ b/test/files/pos/classOfObjectType/Foo.scala @@ -0,0 +1,7 @@ + +object Bar + +trait Foo { + @AnnotationWithClassType(cls = classOf[Bar.type]) + def function: Any = ??? +} diff --git a/test/files/run/classOfObjectType.scala b/test/files/run/classOfObjectType.scala new file mode 100644 index 000000000000..2df7d614f709 --- /dev/null +++ b/test/files/run/classOfObjectType.scala @@ -0,0 +1,7 @@ + +object Test { + object Bar + def main(args: Array[String]): Unit = { + assert(Bar.getClass == classOf[Bar.type] ) + } +} \ No newline at end of file