为什么编译器状态没有唯一的最大实例存在? [英] Why does the compiler state no unique maximal instance exists?

查看:201
本文介绍了为什么编译器状态没有唯一的最大实例存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

  public class Obj< T> extends BaseModel {

public static final String OBJECT =object;

public Obj(T object){
setObject(object);
}

public T getObject(){
return get(OBJECT);
}

public void setObject(T object){
set(OBJECT,object);
}
}

b

  / **这是第三方库类** / 
public class BaseModel实现ModelData,Serializable {
// .. .members和stuff ...

@SuppressWarnings({unchecked,rawtypes})
public< X> X get(String property){
X obj = null;
if(start> -1&& end> -1){
Object o = map.get(property.substring(0,start));
String p = property.substring(start + 1,end);
if(o instanceof Object []){
obj =(X)((Object [])o)[Integer.valueOf(p)];
} else if(o instanceof List){
obj =(X)((List)o).get(Integer.valueOf(p));
} else if(o instanceof Map){
obj =(X)((Map)o).get(p);
}
} else {
obj =(X)map.get(property);
}
return obj;
}
}



当编译时,得到以下错误。 / p>

不能确定< X>的类型参数;对于具有上限T的类型变量X,不存在唯一的最大实例,java.lang.Object - > getObject()



这不会发生在Eclipse中,据我所知,建立。我看到关于Sun编译器问题的 SO线程,但这似乎是静态方法声明类型的飞。



为什么会收到这个错误,更重要的是,我该如何解决?



到目前为止,我发现的唯一原因是在我的方法中,像这样:

  @SuppressWarnings({unchecked})
public T getObject(){
return(T)get(OBJECT); // yuck
}

告诉我我的破解,这是正确的方式是可以接受的。

解决方案

它不编译,因为你的代码期望从泛型太多 - > ie, X> X part in:

  public< X> X get(String property){...} 

在以下代码中:

  public T getObject(){
return get(OBJECT);
}

您必须记住,泛型总是展开之前,编译器实际开始编译Java代码。这是一个预处理步骤。



在你的情况下,编译器不知道在编译时如何替换X 编译器需要确定X的类型,因为它需要检查T以验证代码。因此,错误...



您的问题的解决方案是替换< X> X with Object:

  public Object get(String property){...} 

并在中添加投射:

  public T getObject(){
return(T)get(OBJECT);
}

您的代码将在编译时获得unchecked-cast警告,编译(所以是你的解决方法是有效的)。


I have the following classes:

public class Obj<T> extends BaseModel {

    public static final String OBJECT = "object";

    public Obj(T object) {
        setObject(object);
    }

    public T getObject() {
        return get(OBJECT);
    }

    public void setObject(T object) {
        set(OBJECT, object);
    }
}

And...

/** This is a 3rd party library class **/
public class BaseModel implements ModelData, Serializable {
  //...members and stuff...

  @SuppressWarnings({"unchecked", "rawtypes"})
  public <X> X get(String property) {
    X obj = null;
    if (start > -1 && end > -1) {
      Object o = map.get(property.substring(0, start));
      String p = property.substring(start + 1, end);
      if (o instanceof Object[]) {
        obj = (X) ((Object[]) o)[Integer.valueOf(p)];
      } else if (o instanceof List) {
        obj = (X) ((List) o).get(Integer.valueOf(p));
      } else if (o instanceof Map) {
        obj = (X) ((Map) o).get(p);
      }
    } else {
      obj = (X) map.get(property);
    }
    return obj;
  }
}

When I compile, I get the following error.

type parameters of <X>X cannot be determined; no unique maximal instance exists for type variable X with upper bounds T,java.lang.Object -> getObject()

It doesn't happen in Eclipse, which, as far as I can tell, is using the same JDK as my Ant build. I've seen the SO thread about the Sun compiler issue, but that seemed to be for static methods declaring types on the fly.

Why am I getting this error, and more importantly, how do I get around it?

So far the only why I've found is to cast in my method like this:

@SuppressWarnings({"unchecked"})
public T getObject() {
    return (T) get(OBJECT); //yuck
}

Telling my I'm on crack and this is the proper way is acceptable.

解决方案

It does not compile because your code expects too much from generics -> i.e., the < X > X part in:

public <X> X get(String property) { ... }

In the following code:

public T getObject() {
  return get(OBJECT);
}

you have to keep in mind that generics are always "unfolded" before the compiler actually starts to compile the Java code. It is a pre-processing step.

In your case, the compiler does not know what to use to replace X at compile time. The compiler needs to be sure about the type of X, because it needs to check it against T to validate the code. Hence the error...

A solution to your issue is to replace < X > X with Object:

public Object get(String property) { ... }

and add a cast in:

public T getObject() {
  return (T) get(OBJECT);
}

Your will get an unchecked-cast warning at compile time, but your code will compile (so yes your workaround is valid).

这篇关于为什么编译器状态没有唯一的最大实例存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆