簡體   English   中英

枚舉是原始類型。 對泛型類型 Enum 的引用<E>應該參數化

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM