将对象转换为genericstypes以返回

有没有办法将一个对象返回一个方法的返回值? 我试过这种方式,但它在“instanceof”部分给了一个编译时exception:

public static <T> T convertInstanceOfObject(Object o) { if (o instanceof T) { return (T) o; } else { return null; } } 

我也试过这个,但它给了一个运行时exception,ClassCastException:

 public static <T> T convertInstanceOfObject(Object o) { try { T rv = (T)o; return rv; } catch(java.lang.ClassCastException e) { return null; } } 

有没有一个可能的方式做到这一点很容易:

 String s = convertInstanceOfObject("string"); System.out.println(s); // should print "string" Integer i = convertInstanceOfObject(4); System.out.println(i); // should print "4" String k = convertInstanceOfObject(345435.34); System.out.println(k); // should print "null" 

编辑:我写了一个正确答案的工作副本:

 public static <T> T convertInstanceOfObject(Object o, Class<T> clazz) { try { return clazz.cast(o); } catch(ClassCastException e) { return null; } } public static void main(String args[]) { String s = convertInstanceOfObject("string", String.class); System.out.println(s); Integer i = convertInstanceOfObject(4, Integer.class); System.out.println(i); String k = convertInstanceOfObject(345435.34, String.class); System.out.println(k); } 

由于编译期间的genericstypes擦除,您必须使用Class实例。

 public static <T> T convertInstanceOfObject(Object o, Class<T> clazz) { try { return clazz.cast(o); } catch(ClassCastException e) { return null; } } 

该方法的声明是:

 public T cast(Object o) 

这也可以用于数组types。 它看起来像这样:

 @SuppressWarnings("unchecked") final Class<int[]> intArrayType = (Class<int[]>) Array.newInstance(Integer.TYPE, 0).getClass(); final Object someObject = new int[]{1,2,3}; final int[] instance = convertInstanceOfObject(someObject, intArrayType); 

请注意,当someObject传递给convertToInstanceOfObject它具有编译时间typesObject

我偶然发现了这个问题,并且抓住了我的兴趣。 接受的答案是完全正确的,但我想我提供我的发现在JVM字节代码级别解释为什么OP遇到ClassCastException

我的代码与OP代码几乎相同:

 public static <T> T convertInstanceOfObject(Object o) { try { return (T) o; } catch (ClassCastException e) { return null; } } public static void main(String[] args) { String k = convertInstanceOfObject(345435.34); System.out.println(k); } 

而相应的字节码是:

 public static <T> T convertInstanceOfObject(java.lang.Object); Code: 0: aload_0 1: areturn 2: astore_1 3: aconst_null 4: areturn Exception table: from to target type 0 1 2 Class java/lang/ClassCastException public static void main(java.lang.String[]); Code: 0: ldc2_w #3 // double 345435.34d 3: invokestatic #5 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double; 6: invokestatic #6 // Method convertInstanceOfObject:(Ljava/lang/Object;)Ljava/lang/Object; 9: checkcast #7 // class java/lang/String 12: astore_1 13: getstatic #8 // Field java/lang/System.out:Ljava/io/PrintStream; 16: aload_1 17: invokevirtual #9 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 20: return 

注意, checkcast字节码指令在main方法中不会发生, convertInstanceOfObjectconvertInstanceOfObject方法没有任何可以抛出ClassCastException指令。 因为main方法不会捕获ClassCastException因此当您执行main方法时,您将得到一个ClassCastException而不是打印null的期望。

现在我将代码修改为接受的答案:

 public static <T> T convertInstanceOfObject(Object o, Class<T> clazz) { try { return clazz.cast(o); } catch (ClassCastException e) { return null; } } public static void main(String[] args) { String k = convertInstanceOfObject(345435.34, String.class); System.out.println(k); } 

相应的字节码是:

 public static <T> T convertInstanceOfObject(java.lang.Object, java.lang.Class<T>); Code: 0: aload_1 1: aload_0 2: invokevirtual #2 // Method java/lang/Class.cast:(Ljava/lang/Object;)Ljava/lang/Object; 5: areturn 6: astore_2 7: aconst_null 8: areturn Exception table: from to target type 0 5 6 Class java/lang/ClassCastException public static void main(java.lang.String[]); Code: 0: ldc2_w #4 // double 345435.34d 3: invokestatic #6 // Method java/lang/Double.valueOf:(D)Ljava/lang/Double; 6: ldc #7 // class java/lang/String 8: invokestatic #8 // Method convertInstanceOfObject:(Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object; 11: checkcast #7 // class java/lang/String 14: astore_1 15: getstatic #9 // Field java/lang/System.out:Ljava/io/PrintStream; 18: aload_1 19: invokevirtual #10 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 22: return 

注意,在convertInstanceOfObject方法中有一个convertInstanceOfObject指令,它调用抛出ClassCastException Class.cast()方法,该方法将被catch(ClassCastException e) bock catch(ClassCastException e)并返回null ; 因此,“null”打印到控制台没有任何例外。

 Object obj; return obj.getClass().cast( obj ); 

你可以直接使用它,而不需要指定对象的类名!