9 Replies Latest reply: Jul 22, 2011 2:52 AM by Mohan RSS

    instance of Set<T>

    Mohan
      Is there a way to use a instanceOf to check that object A is an intance of Set<T> ?

      The problem is that the method is like this.

      public boolean matches( Object o ){

      //instanceof required
      }

      Thanks.
        • 1. Re: instance of Set<T>
          BIJ001
          http://download.oracle.com/javase/tutorial/java/generics/erasure.html

          Type Erasure

          When a generic type is instantiated, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

          For instance, Box<String> is translated to type Box, which is called the raw type — a raw type is a generic class or interface name without any type arguments. This means that you can't find out what type of Object a generic class is using at runtime. The following operations are not possible:
              public class MyClass<E> {
                  public static void myMethod(Object item) {
                      if (item instanceof E) {  //Compiler error
                          ...
                      }
                      E item2 = new E();   //Compiler error
                      E[] iArray = new E[10]; //Compiler error
                      E obj = (E)new Object(); //Unchecked cast warning
                  }
              }
          • 2. Re: instance of Set<T>
            Mohan
            I have managed to elaborate this question with some code from Neal Gafter's blog. Looks like there is a way.

            So I think if I manage to compare the type like this and ensure that the Object passed is actually a java.util.Set then it should work ??
            public class SetComparison<T>{
            
                private Type objectTypeExpected;
            
                private Type type;
            
                public MySet( Type objectTypeExpected) {  /* I have to pass this though */
            
                    ParameterizedType pt =
                                (ParameterizedType) getClass().getGenericSuperclass();
                    type = pt.getActualTypeArguments()[ 0 ];
               }
            }
            
            The equals method in 'SetComparison' can have this logic. 
            
                    /** Unchecked is a potential problem*/
                    @SuppressWarnings("unchecked")
                    Set elements = (Set) actual;
            and then

            /*Compare type passed in constructor with actual generic type*/


            Thanks.
            • 3. Re: instance of Set<T>
              852060
              If you flesh out your sketch then with it you can check if an object is an instance of MySet<T>, not, however, Set<T>. (More precisely, there exist instances of Set<T> for which you can not decide that they are instances of Set<T>. These happen to be all instances of Set<T> which are not also instances of MySet<T>.)

              With kind regards
              Ben
              • 4. Re: instance of Set<T>
                Mohan
                I understand your point :-)

                I have edited the code and the text. It is just an implementation of 'equals' to compare the generic type - passed through the parameterized SetComparison<T> - and another object which is a java.util.Set passed to the 'equals' method. There is no direct relation between SetComparison<T> and java.util.Set except the <T> used for comparison using reflection.

                Thanks.

                Edited by: Mohan on Jul 6, 2011 8:56 PM
                • 5. Re: instance of Set<T>
                  Mohan
                  If the class is declared like this
                  public class SetComparison<T> implemented simpleinterface{
                  
                  }
                  then this code throws an exception
                          ParameterizedType pt =
                                      (ParameterizedType) getClass().getGenericSuperclass();
                  java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
                  • 6. Re: instance of Set<T>
                    EJP
                    ParameterizedType pt =
                    (ParameterizedType) getClass().getGenericSuperclass();
                    This is just a precedence problem:
                    ParameterizedType pt = (ParameterizedType) (getClass().getGenericSuperclass());
                    • 7. Re: instance of Set<T>
                      Mohan
                      That doesn't seem to be the case unless I am overlooking something.
                      • 8. Re: instance of Set<T>
                        796440
                        Mohan wrote:
                        If the class is declared like this
                        public class SetComparison<T> implemented simpleinterface{
                        
                        }
                        then this code throws an exception
                        ParameterizedType pt =
                        (ParameterizedType) getClass().getGenericSuperclass();
                        java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
                        So, quite obviously, from the error message, getGenericSuperClass() is returning a Class, not a ParameterizedType. (You do understand Java's casting rules, right? In particular, do you understand that you can't just magically cast any old thing to any old other thing? You can only cast a reference to something that it actually is.)

                        So:

                        1) Read the docs for getGenericSuperclass() carefully.

                        2) Print out the results of getClass().

                        3) Print out the results of getGenericSuperclass().

                        If that doesn't shed some light on it for you, post again, including those results.

                        Edited by: jverd on Jul 20, 2011 7:30 AM
                        • 9. Re: instance of Set<T>
                          Mohan
                          public class ReturnTest<T>  implements IArgumentMatcher {
                          }
                          
                          public class SetReturnTest<T> extends ReturnTest<T>{
                          }
                          I overlooked the fact that this API specifically refers to a super class. Now it doesn't throw the exception.
                                  ParameterizedType pt =
                                              (ParameterizedType) (getClass().getGenericSuperclass());