2

I have a method that takes a java.lang.Class object as a parameter. How do I get that from a Ceylon class?

That is, the equivalent of SomeClass.class in Java.

Lii
  • 11,553
  • 8
  • 64
  • 88
Tomas Hofman
  • 138
  • 1
  • 4

1 Answers1

4

For SomeClass.class, use a meta literal: `SomeClass` for a closed model, `class SomeClass` for the open declaration.

For someInstance.class, you can use the type function from ceylon.language.meta.

import ceylon.language.meta { type }

class C() {}
class D() extends C() {}

shared void run() {
    C c = D();
    print(type(c));
}

Try it!

(type returns a closed model, i. e. with type arguments applied; you can get the open declaration with .declaration.)

Lucas Werkmeister
  • 2,584
  • 1
  • 17
  • 31
  • 4
    This will not return a `java.lang.Class`, though. You'll need to use `javaClass`, `javaClassFromDeclaration`, `javaClassFromModel` or `javaClassFromInstance` from [ceylon.interop.java](https://modules.ceylon-lang.org/repo/1/ceylon/interop/java/1.3.2/module-doc/api/index.html) for that. – Paŭlo Ebermann Apr 06 '17 at 18:08
  • Yes, but the OP asked specifically for _"equivalent of `SomeClass.class` in java"_. I take it to mean that the OP was interested in Ceylon metaprogramming facilities – Roland Tepp Apr 07 '17 at 07:44
  • 1
    He also says "a method that takes a java.lang.Class object as a parameter" :) – Quintesse Apr 07 '17 at 11:37
  • Perfect, thanks for elaborate answer and tips in comments! – Tomas Hofman Apr 08 '17 at 05:59