dimanche 23 août 2015

example of getOwnerType method

Could I ask about any example which uses getOwnerType() method where this method would return any Type object, but not a value "null" ?

This is a certain example of use getOwnerType() method which I found in Google:

public class Main {

   public static void main(String args[]) throws Exception {

      Type type = StringList.class.getGenericSuperclass();
      System.out.println(type); 
      ParameterizedType pt = (ParameterizedType) type;
      Type ownerType = pt.getOwnerType();
      System.out.println(ownerType);
   }
}

class StringList extends ArrayList<String> {

}

This is a result:

java.util.ArrayList<java.lang.String>
null

Everything is fine, because a value of pt object is a top-level type and null is returned.

And now, arguably I don't understand these words of documentation:

Returns a Type object representing the type that this type is a member of. For example, if this type is O< T >.I< S >, return a representation of O< T >.

After reading this, I tried do something like this:

public class Main {

   public static void main(String args[]) throws Exception {

      ... // a body of the main method is unchanged
   }
}

class StringList extends ClassA<String>.ClassB<String> {   // line No. 17

}

public class ClassA<T> {
   public class ClassB<T> {

   }
}

But, it produces only such error:

No enclosing instance of type r61<T> is accessible to invoke the super constructor. Must define a constructor and explicitly qualify its super constructor invocation with an instance of r61<T> (e.g. x.super() where x is an instance of r61<T>).

Maybe I tried to do something what doesn't make a sense, but I have no more ideas..





Aucun commentaire:

Enregistrer un commentaire