Search code examples
ceylon

In ceylon, how do I get Class object from a class?


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.


Solution

  • 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.)