![](/img/trans.png)
[英]ArrayList<Enum> returns "References to generic type Enum<e> should be parameterized
[英]Enum is a raw type. References to generic type Enum<E> should be parameterized
我在我的 Java 代碼中收到以下警告...
Enum is a raw type. References to generic type Enum<E> should be parameterized
我不確定糾正此警告的正確行動方案是什么......
我觸發警告的代碼是方法過程中的以下類定義......
這個抽象類的實現類返回不同類型的枚舉,Message 抽象類的用戶只是在枚舉上調用ordinal()
,而對 Enum 的底層類型一無所知。
public abstract class Message {
private User user;
// return string name of message that will be use for factory construction
public abstract String getName();
// if set to true, the framework will automatically check for a firebase idToken in the root json node
// it will then authenticate and call setUser with the internal user.
// If implementing class set requiresUserAuth to return true, then is it safe to call getUser in the
// process method.
public abstract boolean requiresUserAuth();
// process the json contents of the message and return the int result code specific for the message
// populate ObjectNode response with the json response data.
public abstract Enum process(JsonNodeThrows contents , ObjectNode response) throws JsonException;
// omit access modifier to use default package protected
void setUser(User user)
{
this.user = user;
}
protected User getUser()
{
if (!requiresUserAuth())
throw new RuntimeException("Message.getUser() - called getUser when implementing message class requireUserAuth method returns a value of false.");
if (user == null)
throw new RuntimeException("Message.getUser() - called getUser when user is null.");
return user;
}
}
如果方法可以返回各種枚舉,請使用
public abstract Enum<?> process(...
如果調用者知道期望使用哪個枚舉,請使用
public abstract <T extends Enum<T>> T process(...
Enum
是原始類型。 對泛型類型Enum<E>
References
應該被參數化......
Enum<E extends Enum<E>>
是一個泛型類。 這就是警告消息所指的內容,它告訴您“應該對Enum<E>
進行參數化”。 但這個警告是一個紅鯡魚。
„ ……我不確定糾正此警告的正確行動方案是…… ”
您的Message.process()
方法的設計可能應該重構。
„ ... Message 抽象類的用戶只是在 enum 上調用 oridinal() ,而對 Enum 的底層類型一無所知...... “
如果您確定Enum.ordinal( )
將被調用,並且如果您可以控制將在應用程序的 API 中公開的Enums
,那么您可以選擇引入一個接口(例如Ordinalable
)指定ordinal()
方法。 然后,所有應用的Enums
可以實現Ordinalable
(或者你想給它的任何名稱)。
就像是…
public interface Ordinalable {
int ordinal( );
}
…
enum Foo implements Ordinalable {
A, B, C … ;
}
…
enum Bar implements Ordinalable {
UNOS, DOS, TRES;
}
…
public abstract class Message {
…
public abstract Ordinalable process( … ){ … }
…
}
…
public class AMessage extends Message {
@Override
public Ordinalable process( ){
return Foo.A;
}
}
…
public class BMessage extends Message {
@Override
public Ordinalable process( ){
return Foo.B;
}
}
…
public class DuoMessage extends Message {
@Override
public Ordinalable process( ){
return Bar.DOS;
}
}
……可以這樣稱呼……
Message msg = new AMessage( );
int ordinal = msg.process( ).ordinal( );
…
msg = new BMessage( );
ordinal = msg.process( ).ordinal( );
…
msg = new DuoMessage( );
ordinal = msg.process( ).ordinal( );
…
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.