CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Can you discover at runtime what class <T> is?

    I think I already know the answer to this is "it can't be done" but I need to ask first before giving up and trying something different.

    I've got some code which I'm writing with generics. However I have got one of the VERY rare cases where casting is still reqired. What makes this worse is that the one time I must cast in the eintire program is within a generic function. I'd hoped to be able to catch and handle the possible ClassCastException from within the same function.

    Now I tried this:
    Code:
    class Foo {
        static <T> T bar() {
            try {
                Object x;
                //... some code that puts an object in x
                T y = (T) x;
                return y;
            }
            catch (ClassCastException e) {
                // Handle what happens if x is not a T.
            }
        }
    }
    Annoyingly this code did not generate a ClassCastException at all and only generated it when the returned object was placed into a non-generic variable.

    If I can get a Class object for T from within my function I can call isAssignableFrom to check this first and prevent the exception entirely. But I can't find a way to get this class object without being actually passed one.

    All suggestions welcome.

    Thanks
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Can you discover at runtime what class <T> is?

    Annoyingly this code did not generate a ClassCastException at all and only generated it when the returned object was placed into a non-generic variable.
    The generics information is only available at compile time and not at runtime so your cast is effectively a cast to type Object and hence will never fail.

    Without knowing more about what you're doing to hard to give a definitive answer but I suspect you will have to pass the class type in to do the check.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: Can you discover at runtime what class <T> is?

    Quote Originally Posted by keang View Post
    The generics information is only available at compile time and not at runtime so your cast is effectively a cast to type Object and hence will never fail.
    Yes that's pretty much confirmed what I thought.

    I'm building a program with a plug-in architecture. Based on a configuration it will load objects of various classes named in a config file. Obviously the classes it loads must implement an interface specific to type of plug-in. The user could configure the program incorrectly, so it's important to catch the resulting class cast exception and handle it appropriately. I'm trying to wrap up the entire code for loading a plug-in into a generic function so that it does not have to be re-written for each type of plug-in.

    The flaw to all of this is that the calling code (not the plug-in loader) could generate a ClassCastException. Yes that's not impossible to fix but it's messy and ideally it should be caught or predicated and avoided by the plug-in loader.

    I think the plug-in loader function will have to take an extra java.lang.Class argument.
    Last edited by couling; November 15th, 2010 at 10:28 AM.
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Can you discover at runtime what class <T> is?

    I'm building a program with a plug-in architecture. Based on a configuration it will load objects of various classes named in a config file. Obviously the classes it loads must implement an interface specific to type of plug-in. The user could configure the program incorrectly, so it's important to catch the resulting class cast exception and handle it appropriately. I'm trying to wrap up the entire code for loading a plug-in into a generic function so that it does not have to be re-written for each type of plug-in.
    Ok, I can see your problem, unfortunately I can't suggest a solution other than the Class parameter.

    If there was another way around this then classes like EnumMap probably wouldn't require you to pass in the class type.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured