Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using classOf with object type #9279

Merged
merged 1 commit into from Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/reflect/scala/reflect/internal/Types.scala
Expand Up @@ -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
Copy link
Contributor Author

@octonato octonato Oct 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end, this seems to be suspiciously simple fix. Not sure if that has other implications though.

case TypeRef(_, sym, _) => sym.isClass && !sym.isRefinementClass
case ErrorType => true
case _ => false
Expand Down
10 changes: 10 additions & 0 deletions 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();
}
7 changes: 7 additions & 0 deletions test/files/pos/classOfObjectType/Foo.scala
@@ -0,0 +1,7 @@

object Bar

trait Foo {
@AnnotationWithClassType(cls = classOf[Bar.type])
def function: Any = ???
}
Comment on lines +4 to +7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this compile test because the issue shows up when using a Java annotation that requires a class. But of course, the impl allows many other usages.

When I faced that issue for the first time, I tried to used it as follows:

trait Foo {
  @AnnotationWithClassType(cls = Bar.getClass)
  def function: Any = ???
}

which gives the following error:

Foo.scala:5: error: annotation argument needs to be a constant; found: Bar.getClass()
  @AnnotationWithClassType(cls = Bar.getClass)

7 changes: 7 additions & 0 deletions 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] )
}
}