Page 2 of 2 FirstFirst 12
Results 21 to 31 of 31

Thread: Dynamic Casting

  1. #21
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by JosAH View Post
    You're welcome of course but please realize that reflection is just a dirty cheat, it's like surgery and bypassing everything that is sane in Java.

    kind regards,

    Jos
    Indeed. Which is why we have been vainly asking the reason for this.
    How is the target program going to use this? I guess the OP will never tell us.

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    Indeed. Which is why we have been vainly asking the reason for this.
    How is the target program going to use this? I guess the OP will never tell us.
    Who knows? Maybe the OP has a fascinating story telling us the exquisite uses of that dirty code fragment; uses we never thought of in our wildest dreams ;-)

    kind regards,

    Jos

  3. #23
    Thorton is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    I don't see the need to pass a typed array at all. There will be no difference in behavior whether you passed a typed array or an Object array. In fact if you did have a typed array the type would get erased before the code reaches the run time anyway.
    What do you hope to achieve by casting to a special type?
    It is the third party application that is doing the casting. I merely have to make sure the cast doesn't fail.

    I assume the cast is to ensure type consistency.

  4. #24
    Thorton is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    You're welcome of course but please realize that reflection is just a dirty cheat, it's like surgery and bypassing everything that is sane in Java.

    kind regards,

    Jos
    I tend to agree that reflection is a bit of a hack or a lazy way to get around what may be a poor design.

    However, I do tend to keep running into it on various projects. It is an official part of the language and quite frequently used. So I ask, when is it appropriate to use reflection?

  5. #25
    Thorton is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Who knows? Maybe the OP has a fascinating story telling us the exquisite uses of that dirty code fragment; uses we never thought of in our wildest dreams ;-)

    kind regards,

    Jos
    Thanks for the help and many responses. Me, the OP (Original Poster?), was avoiding explaining the details, because I wasn't sure I could do it justice, and didn't want to get distracted from the original question.

    However, you've definitely paid to see my cards so, I'll do my best to describe the situation.

    I'm working on an application that takes WSDLs as input and creates client classes for those web services. These services have a wrapper class that exposes each service operation and its inputs and outputs. This wrapper class can then be used with third party plug-ins and can be used to validate that certain services have compatible types.

    Since this is all done at run time, reflection needs to be used. For example, the validation process needs to check input types against output types.

    Also the use cases for the third party plug-ins, allows the third parties control what they will will be passed as their inputs. Plug-ins are loaded at run time, so the main application can't know types at compile time (even if it did, there would be hundreds). Because of this, the main application must pass and object with the declared defined type of "Object". The objects are vastly different, a common interface can not be used either.

    I was attempting to add the capability to concatenate multiple operation outputs into a single typed array. That's where you all came in.

    I hope that's somewhat clear. I'm open to suggestions on how to remove reflection or do it better.


    Java Code:
                          ---> Operation Input Objects   -----
    WSDL -> WrapperClass -|                                   |--->  Validation and Plugins
                          ---> Operation Output Objects -----

  6. #26
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Thorton View Post
    I tend to agree that reflection is a bit of a hack or a lazy way to get around what may be a poor design.

    However, I do tend to keep running into it on various projects. It is an official part of the language and quite frequently used. So I ask, when is it appropriate to use reflection?
    Reflection (or 'Introspection') surpasses everything that makes Java 'safe'. It is used in a form of 'meta-programming', a Java program that wants to 'know' something about another piece of Java; it treats code as data and data as code, a very object oriented uncommon way of thinking about objects. You almost never need to use it otherwise.

    kind regards,

    Jos

  7. #27
    Thorton is offline Member
    Join Date
    Jan 2010
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Reflection (or 'Introspection') surpasses everything that makes Java 'safe'. It is used in a form of 'meta-programming', a Java program that wants to 'know' something about another piece of Java; it treats code as data and data as code, a very object oriented uncommon way of thinking about objects. You almost never need to use it otherwise.

    kind regards,

    Jos
    Can you give a concrete example of a "good" use of reflection.

  8. #28
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    EJB containers, EL resolvers

  9. #29
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    EJB containers, EL resolvers
    Also BeanShell, a great Java (like) interpreter heavily relies on introspection. (b.t.w. it had closures before any JCP dared to think about it ;-)

    kind regards,

    Jos

  10. #30
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by JosAH View Post
    Also BeanShell, a great Java (like) interpreter heavily relies on introspection. (b.t.w. it had closures before any JCP dared to think about it ;-)

    kind regards,

    Jos
    Full grown-up men closures?

  11. #31
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by r035198x View Post
    Full grown-up men closures?
    Yup, not those baby-closures they're struggling with now: every variable visible at the time of creation of the closure is part of the closure, either readable and/or writable; no strings attatched. If it's done well it's not so difficult to implement (unlike those toddler-closures ;-)

    kind regards,

    Jos

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Casting
    By zzpprk in forum Advanced Java
    Replies: 13
    Last Post: 08-13-2009, 07:59 PM
  2. What does casting mean?
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 04:31 PM
  3. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-13-2009, 11:07 PM
  4. Casting an int value into a char
    By kurtulas in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 08:03 PM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 12:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •