Scorb
Scorb

Reputation: 1870

Enum is a raw type. References to generic type Enum<E> should be parameterized

I am getting the following warning in my java code...

Enum is a raw type. References to generic type Enum<E> should be parameterized

I am not sure what the proper course of action is to rectify this warning...

My code that triggers the warning is the following class definition on the method process...

The implementing classes of this abstract class return varying types of enums, and the user of the Message abstract class simply calls ordinal() on the enum, without knowing anything about the underlying type of 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;
    }
}

Upvotes: 1

Views: 5372

Answers (2)

deduper
deduper

Reputation: 1964

Enum is a raw type. References to generic type Enum<E> should be parameterized…

Enum<E extends Enum<E>> is a generic class. That's what the warning message is referring to by telling you „Enum<E> should be parameterized“. But that warning is a red herring.

…I am not sure what the proper course of action is to rectify this warning…

The design of your Message.process() method should probably be refactored.

…the user of the Message abstract class simply calls oridinal() on the enum, without knowing anything about the underlying type of Enum…

If you are certain that Enum.ordinal( ) is all that will ever be called, and if you have control over what Enums will be exposed in your app's API, then one option you have is to introduce an interface (an Ordinalable, say) that specifies the ordinal() method. Then all your app's Enums could implement Ordinalable (or any name you want to give it).

Something like…

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;
    }
}

…Which can be called like…

Message msg = new AMessage( );
    
int ordinal = msg.process( ).ordinal( );
…
msg = new BMessage( );
    
ordinal = msg.process( ).ordinal( );
…
msg = new DuoMessage( );
    
ordinal = msg.process( ).ordinal( );
…

You can see an experimental implementation of that approach here.

Upvotes: 0

Andreas
Andreas

Reputation: 159114

If method can return various enums, use

public abstract Enum<?> process(...

If the caller knows which enum to expect, use

public abstract <T extends Enum<T>> T process(...

Upvotes: 5

Related Questions