简体   繁体   English

Java泛型使用反射从方法(创建对象)获取Type

[英]Java Generics get the Type from the method (that creates object) using reflection

I wanted to create a GenericBuilder that could be used to retrieve the Builder classes that are defined inside my classes. 我想创建一个GenericBuilder,该类可用于检索在类内部定义的Builder类。 I created the following interfaces and classes 我创建了以下接口和类

public interface BuilderType<T> {
}

public class MyObject implements BuilderType<MyObject.MyObjectBuilder> {
private int i;
private MyObject() {}
public int getI() {
    return i;
}
public static MyObjectBuilder buildMyObject() {
    MyObjectBuilder builder = new MyObjectBuilder();
    return builder;
}
public static class MyObjectBuilder {
    private final MyObject obj;
    MyObjectBuilder() {
        obj = new MyObject();
    }
    public MyObjectBuilder withI(int i) {
        obj.i = i;
        return this;
    }
    public MyObject build() {
        return obj;
    }
}
public class GenericBuilder {
 public static <T extends BuilderType<S>, S> S getBuilder(Class<T> t) {
 S s = null;
 try {
     s = (S) t.getDeclaredMethod("build" + t.getSimpleName(), null)
                .invoke(null, null);
 } catch (Exception e) {
     e.printStackTrace();
 }
 return s;
 }
}

the statement s=(S)t.get... in my GenericBuilder gives a cast warning: Unchecked Cast from Object to S, is there a way to eliminate it?? 我的GenericBuilder中的语句s =(S)t.get ...给出了强制转换警告:取消选中从Object到S的强制转换,有没有办法消除它?

There is no way to eliminate the warning because S is not a real type. 由于S不是实数,因此无法消除该警告。 By runtime it will get erased to its upper bound, which is Object in your case. 在运行时,它将被擦除到上限,在您的情况下为Object So the "downcast" isn't actually doing anything. 因此,“垂头丧气”实际上没有做任何事情。 You can check the Method 's return type by calling the appropriate method, and then do a reflective downcast (not very helpful for your scenario). 您可以通过调用适当的方法来检查Method的返回类型,然后进行反射式向下转换(对您的情况不是很有用)。 Type safety cannot possibly be achieved when you dynamically fetch a method at runtime. 在运行时动态获取方法时,可能无法实现类型安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用具有 generics 参数和返回类型方法的 java 中的反射来获取 Class 的实例? - How to get Instance of a Class using reflection in java having generics parameter and return type method? 使用泛型时,Java反射获取运行时类型 - Java reflection get runtime type when using generics 使用Java反射和泛型:方法是否可以将实际类型指定为其返回值? - Using Java reflection and generics: can a method specify an actual type as its return value? Java泛型:如何从方法中获取泛型类型? - Java generics: how to get a generic type from a method? 反射:在运行时从方法的返回类型获取对象状态 - Reflection:get Object state from return type of method in runtime 如何从使用Java Reflection API调用的方法中检索对象? - How to retrieve an object from a method that is invoked using Java Reflection API? 使用反射从动态方法中获取参数类型 - Get parameter Type from dynamic method using reflection Java - 获取Method的泛型类型的反射 - Java - Reflection to get Method's Generic Type 在 Java 中使用泛型通过反射获取类方法 - Using generics to get class methods via reflection in Java 如何使用Java Reflection从ParameterizedType获取GenericDeclaration的类型? - How to get the Type of GenericDeclaration from ParameterizedType using Java Reflection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM