Re: [ceylon-users] Java Class.forName equivalent in ceylon

98 vues
Accéder directement au premier message non lu
Le message a été supprimé

John Vasileff

non lue,
31 août 2016, 17:02:0131/08/2016
à ceylon...@googlegroups.com
Ceylon support for this is in the ceylon.language.meta.* packages of the language module. See https://modules.ceylon-lang.org/repo/1/ceylon/language/1.2.2/module-doc/api/meta/index.html

For your example of constructing a String, you can try:

import ceylon.language.meta { modules }
import ceylon.language.meta.model { Class }

shared void run() {
    assert (exists languageModule
            = modules.list.find((mod) => mod.name == "ceylon.language"));
    assert (exists languagePackage
            = languageModule.findPackage("ceylon.language"));
    assert (exists stringClassDeclaration
            = languagePackage.getClassOrInterface("String"));
    assert (is Class<> stringClass
            = stringClassDeclaration.apply<>());
    
    value myString = stringClass.apply(["myStringValue"]);
    assert (is String myString);
    print(myString);
}

The api is quite flexible and advanced, and pretty much supports as much or as little type safety as you like. For example, since you know String takes a String as an argument, you can assert that fact:

    assert (is Class<Anything, [String]> stringClass);

and then, instead of calling stringClass.apply(), call stringClass() directly as a function that takes a String:

    value myString2 = stringClass("myStringValue2");

HTH,

John


On Aug 31, 2016, at 4:15 PM, Wojciech Potiopa <wojciech...@gmail.com> wrote:

Hi I'm having fun serializing/deserializing objects to/from xml, Thing is, serialization should be generic and produce XML like this
<Data>
    <Entity type="com.voitech.serializer.xmltest.data.TestData">
        <Property name="id">
            <Simple type="ceylon.language::Integer">123</Simple>
        </Property>
        <Property name="name">
            <Simple type="ceylon.language::String">test name</Simple>
        </Property>
    </Entity>
</Data>

Of course for classes with arguments, there will be other attributes or XML elements. Is there way in Ceylon to find ClassDeclaration by name and then initiallize class models with generic arguments provided (or not) ?
Something like

ClassDeclaration stringDeclaration=findClassDeclaration("ceylon.language::String");
ClassModel<Anything,Anything> classModel=initModel<Anything>(stringDeclaration);
Anything str=classModel.defaultConstructor.call(args)...

Thanks
Wojciech.




--
You received this message because you are subscribed to the Google Groups "ceylon-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users...@googlegroups.com.
To post to this group, send email to ceylon...@googlegroups.com.
Visit this group at https://groups.google.com/group/ceylon-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-users/87c3e457-2848-4267-bead-664e81058b80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Vasileff

non lue,
31 août 2016, 17:26:5431/08/2016
à ceylon...@googlegroups.com
Actually, I slightly better way to get the “ceylon.language” package is:

assert (exists languagePackage = `module`.findImportedPackage("ceylon.language"));

John

Tom Bentley

non lue,
2 sept. 2016, 06:04:0302/09/2016
à ceylon...@googlegroups.com

This might be what you're looking for

https://modules.ceylon-lang.org/repo/1/com/github/tombentley/typeparser/1.0.2/module-doc/api/index.html

Hth,

Tom


On 31 Aug 2016 21:15, "Wojciech Potiopa" <wojciech...@gmail.com> wrote:
Hi I'm having fun serializing/deserializing objects to/from xml, Thing is, serialization should be generic and produce XML like this
<Data>
    <Entity type="com.voitech.serializer.xmltest.data.TestData">
        <Property name="id">
            <Simple type="ceylon.language::Integer">123</Simple>
        </Property>
        <Property name="name">
            <Simple type="ceylon.language::String">test name</Simple>
        </Property>
    </Entity>
</Data>

Of course for classes with arguments, there will be other attributes or XML elements. Is there way in Ceylon to find ClassDeclaration by name and then initiallize class models with generic arguments provided (or not) ?
Something like

ClassDeclaration stringDeclaration=findClassDeclaration("ceylon.language::String");
ClassModel<Anything,Anything> classModel=initModel<Anything>(stringDeclaration);
Anything str=classModel.defaultConstructor.call(args)...

Thanks
Wojciech.



--
You received this message because you are subscribed to the Google Groups "ceylon-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users+unsubscribe@googlegroups.com.
Répondre à tous
Répondre à l'auteur
Transférer
Le message a été supprimé
0 nouveau message