2 Replies Latest reply on Dec 29, 2017 8:42 PM by jasong RSS
    jasong Creator

    Casting Things as their implemented ThingShape

    I have developed a ThingShape in Java as a POJO.


    I place this shape on several templates, hence why it's a shape.


    Once I have an instantiated Thing, is there any way I can cast that Thing as this ThingShape it uses to get access to the public services, without having to use the reflective 'processServiceRequest(serviceName, params) method?


    It's not a huge deal, but seems like due to the reflective nature of how Things are loaded, this would be doable, by either extending the ThingShape in Java from Thing or by some other mechanism.

      • Re: Casting Things as their implemented ThingShape
        pchung Collaborator

        On the Thing level you will only be able to use the services that apply to an instance, so from a ThingShape perspective only the custom (My) services, properties and events you've added. If you wanted to do something that is executed against the ThingShape itself, you will need to specifically do that as ThingShapes[NameOfShape].Service/Property/Event I don't know any way around that.

        But if it is something you added as a MY item, then in the context of the Thing you can use me.Service/Property/Event

          • Re: Casting Things as their implemented ThingShape
            jasong Creator

            Yeah I was hoping there was something I could do like this:


            Thing t = EntityUtilities.findEntity("MyThingName", ThingworxRelationshipTypes.Thing);
            MyThingShape mts = (MyThingShape) t;




            The alternative is not bad but just inconsistent with my other things. For the services internal to the shape I do this:


            private Thing getMe() throws Exception { 
                Object meObj = ThreadLocalContext.getMeContext();
                if (meObj instanceof Thing) {
                    return (Thing)meObj;        
                } else {
                    throw new Exception("Cannot cast me to Thing");
            private void someServce() {
               Thing t = getMe();
            private void someOtherServce() {


            But then in any other code that needs to access this services defined on the thing shape, it goes something like this

            Thing t = EntityUtilities.findEntity("MyThingName", ThingworxRelationshipTypes.Thing);
            DeviceTools.runSomeService(t); //DeviceTools is just a wrapper that uses the reflective Thing.processServiceRequest(...)


            Excuse any typos!