我如何摆脱这些关于枚举的警告?

时间:2022-08-16 23:49:43

i would like to get rid of these warnings about unchecked conversion and parameterization without surpressing them.

我想摆脱关于未经检查的转换和参数化的这些警告,而不是压制它们。

interface Switch {
    void toggle();
}
enum A implements Switch {
    a1,a2;
    @Override public void toggle() {
        state=!state;
    }
    boolean state;
}
enum B implements Switch {
    b1,b2;
    @Override public void toggle() {
        state=!state;
    }
    boolean state;
}
public class Warnings {
    public static void main(String[] args) {
        Class<? extends Enum>[] enums=new Class[]{A.class,B.class};
        for(Class<? extends Enum> clazz:enums)
            try {
                Enum s=Enum.valueOf(clazz,args[0]);
                ((Switch)s).toggle();
            } catch(IllegalArgumentException eee) {}
    }
}

2 个解决方案

#1


0  

You can't without writing your own valueOf. Enum is defined as:

你不能不写自己的价值。枚举定义为:

class Enum<E extends Enum<E>>

and Enum.valueOf is defined as:

和Enum.valueOf定义为:

public static <T extends Enum<T>> T valueOf(Class<T> enumType,
                                            String name) 

Note the recursive type parameterization which implies that you can only call valueOf with a specific enum class (e.g. A.class), but not with a generic one, as a Class<? extends Enum<?>> is not a match because the two question marks aren't assumed to represent the same (unknown) type by the compiler.

请注意递归类型参数化,这意味着您只能使用特定的枚举类(例如A.class)调用valueOf,而不能使用通用类调用valueOf作为Class >不匹配,因为编译器不会假定两个问号代表相同(未知)类型。

So apart from using generic collections instead of arrays, you have to write your own valueOf method that accepts any enum class.

因此,除了使用泛型集合而不是数组之外,您还必须编写自己的valueOf方法来接受任何枚举类。

public class Warnings {
    public static void main(final String[] args) {
        List<Class<? extends Enum<?>>> enums = new ArrayList<Class<? extends Enum<?>>>();
        enums.add(A.class);
        enums.add(B.class);
        for (Class<? extends Enum<?>> clazz : enums) {
            try {
                Switch s = valueOf(clazz, args[0]);
                s.toggle();
            } catch (IllegalArgumentException eee) {
            }
        }
    }

    private static Switch valueOf(final Class<? extends Enum<?>> enumClass, final String name) {
        Enum<?>[] enumConstants = enumClass.getEnumConstants();
        for (Enum<?> constant : enumConstants) {
            if (constant.name().equals(name)) {
                return (Switch) constant;
            }
        }
        throw new IllegalArgumentException(name + " is not a constant of enum class " + enumClass.getName());
    }
}

#2


0  

Don't mix arrays and generics. They do not work well together because generics in java is implemented using type erasure.

不要混合数组和泛型。它们不能很好地协同工作,因为java中的泛型是使用类型擦除实现的。

This should work.

这应该工作。

interface Switch {
void toggle();
}

enum A implements Switch {

a1, a2;
@Override
public void toggle() {
    state = !state;
}

boolean state;
}
enum B implements Switch {

b1, b2;
@Override
public void toggle() {
    state = !state;
}

boolean state;
}

public class Test {

public static void main(String[] args) {
    List<Class<? extends Switch>> enums = new ArrayList<Class<? extends Switch>>();
    enums.add(A.class);
    enums.add(B.class);

    for (Class<? extends Switch> clazz : enums)
        try {
            Switch s = clazz.getEnumConstants()[0];
            ((Switch) s).toggle();
        } catch (IllegalArgumentException eee) {
    }
} 
}

#1


0  

You can't without writing your own valueOf. Enum is defined as:

你不能不写自己的价值。枚举定义为:

class Enum<E extends Enum<E>>

and Enum.valueOf is defined as:

和Enum.valueOf定义为:

public static <T extends Enum<T>> T valueOf(Class<T> enumType,
                                            String name) 

Note the recursive type parameterization which implies that you can only call valueOf with a specific enum class (e.g. A.class), but not with a generic one, as a Class<? extends Enum<?>> is not a match because the two question marks aren't assumed to represent the same (unknown) type by the compiler.

请注意递归类型参数化,这意味着您只能使用特定的枚举类(例如A.class)调用valueOf,而不能使用通用类调用valueOf作为Class >不匹配,因为编译器不会假定两个问号代表相同(未知)类型。

So apart from using generic collections instead of arrays, you have to write your own valueOf method that accepts any enum class.

因此,除了使用泛型集合而不是数组之外,您还必须编写自己的valueOf方法来接受任何枚举类。

public class Warnings {
    public static void main(final String[] args) {
        List<Class<? extends Enum<?>>> enums = new ArrayList<Class<? extends Enum<?>>>();
        enums.add(A.class);
        enums.add(B.class);
        for (Class<? extends Enum<?>> clazz : enums) {
            try {
                Switch s = valueOf(clazz, args[0]);
                s.toggle();
            } catch (IllegalArgumentException eee) {
            }
        }
    }

    private static Switch valueOf(final Class<? extends Enum<?>> enumClass, final String name) {
        Enum<?>[] enumConstants = enumClass.getEnumConstants();
        for (Enum<?> constant : enumConstants) {
            if (constant.name().equals(name)) {
                return (Switch) constant;
            }
        }
        throw new IllegalArgumentException(name + " is not a constant of enum class " + enumClass.getName());
    }
}

#2


0  

Don't mix arrays and generics. They do not work well together because generics in java is implemented using type erasure.

不要混合数组和泛型。它们不能很好地协同工作,因为java中的泛型是使用类型擦除实现的。

This should work.

这应该工作。

interface Switch {
void toggle();
}

enum A implements Switch {

a1, a2;
@Override
public void toggle() {
    state = !state;
}

boolean state;
}
enum B implements Switch {

b1, b2;
@Override
public void toggle() {
    state = !state;
}

boolean state;
}

public class Test {

public static void main(String[] args) {
    List<Class<? extends Switch>> enums = new ArrayList<Class<? extends Switch>>();
    enums.add(A.class);
    enums.add(B.class);

    for (Class<? extends Switch> clazz : enums)
        try {
            Switch s = clazz.getEnumConstants()[0];
            ((Switch) s).toggle();
        } catch (IllegalArgumentException eee) {
    }
} 
}