Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AspectJ编译问题,java8 lambda表达式,switch语句_Java_Lambda_Java 8_Aop_Aspectj - Fatal编程技术网

AspectJ编译问题,java8 lambda表达式,switch语句

AspectJ编译问题,java8 lambda表达式,switch语句,java,lambda,java-8,aop,aspectj,Java,Lambda,Java 8,Aop,Aspectj,我正在尝试使用aspectJ进行AOP编程。 但AJC编译器会在下面抛出错误,javac编译器工作得非常好 Error:(19, 0) ajc: The method getKey() is undefined for the type Object Error:(19, 0) ajc: The method getValue() is undefined for the type Object Error:(22, 0) ajc: Cannot refer to the static enum

我正在尝试使用aspectJ进行AOP编程。 但AJC编译器会在下面抛出错误,javac编译器工作得非常好

Error:(19, 0) ajc: The method getKey() is undefined for the type Object
Error:(19, 0) ajc: The method getValue() is undefined for the type Object
Error:(22, 0) ajc: Cannot refer to the static enum field ApplicationType.transport within an initializer
Error:(25, 0) ajc: Cannot refer to the static enum field ApplicationType.exposure within an initializer
Error:(28, 0) ajc: Cannot refer to the static enum field ApplicationType.manufacture within an initializer
Error:(31, 0) ajc: Cannot refer to the static enum field ApplicationType.factoryhub within an initializer
我的源文件是Maps.java ApplicationType.java--

公共类映射{
公共静态映射toMap(Map.Entry…entries){
return Collections.unmodifiableMap(
流动
。共(项)
//编译错误类型对象的方法getKey()未定义
.collect(Collectors.toMap(e->e.getKey(),e->e.getValue());
}
}
ApplicationType.java
公共枚举应用程序类型{
运输(0),,
暴露(1),
制造(2),
factoryhub(3);
私有整数码;
应用程序类型(int代码){
this.code=代码;
}
应用程序类型(字符串应用程序){
对于(ApplicationType值:values()){
if(value.name().equals(应用程序)){
开关(值){
//ajc:无法在初始值设定项中引用静态枚举字段ApplicationType.transport
箱子运输:
设置码(0);
打破
个案曝光:
设定码(1);
打破
外壳制造:
设定码(2);
打破
案例工厂中心:
设定码(3);
打破
违约:
打破
}
}
}
}
公共int getCode(){
返回码;
}
公共无效设置码(整数码){
this.code=代码;
}
}

修复

Error:(19, 0) ajc: The method getKey() is undefined for the type Object
Error:(19, 0) ajc: The method getValue() is undefined for the type Object
上述错误的原因是aspectj无法发现
.toMap
结果的类型。每当执行collect操作时,Lambda表达式都会被延迟计算。要修复错误,请参阅下面代码的变体。显式存储
.toMap
操作的结果,然后存储用户
集合。不可修改映射

public static <K, V> Map<K, V> toMap(final Map.Entry<K, V>... entries) {
    Map<K, V> map = Stream.of(entries).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    return Collections.unmodifiableMap(map);
}
枚举的重载构造函数
应用程序(字符串名称)
看起来不正确。您可以简化枚举代码,如下所示,以消除错误

public enum ApplicationType {
    transport(0),
    exposure(1),
    manufacture(2),
    factoryhub(3);

    private int code;

    ApplicationType(int code) {
        this.code = code;
    }

    public static ApplicationType parse(String name) {
        return ApplicationType.valueOf(name.toLowerCase());
    }

    public int getCode() {
        return code;
    }
}
我添加了一个
parse
方法,以防您希望基于
name
返回枚举

希望这有帮助

Error:(22, 0) ajc: Cannot refer to the static enum field ApplicationType.transport within an initializer
public enum ApplicationType {
    transport(0),
    exposure(1),
    manufacture(2),
    factoryhub(3);

    private int code;

    ApplicationType(int code) {
        this.code = code;
    }

    public static ApplicationType parse(String name) {
        return ApplicationType.valueOf(name.toLowerCase());
    }

    public int getCode() {
        return code;
    }
}