繁体   English   中英

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

[英]AspectJ compile issues, java8 lambda expression, switch statement

我正在尝试使用 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 --

public class Maps {
    public static <K, V> Map<K, V> toMap(Map.Entry<K, V>... entries) {
        return Collections.unmodifiableMap(
          Stream
            .of(entries)
             // compile error The method getKey() is undefined for the type Object
            .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())));
    }
}
ApplicationType.java 
public enum ApplicationType {

    transport(0),
    exposure(1),
    manufacture(2),
    factoryhub(3);

    private int code;

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

    ApplicationType(String application) {
        for (ApplicationType value : values()) {
            if (value.name().equals(application)) {
                switch (value) {
//ajc: Cannot refer to the static enum field ApplicationType.transport within an initializer
                    case transport:
                        setCode(0);
                        break;
                    case exposure:
                        setCode(1);
                        break;
                    case manufacture:
                        setCode(2);
                        break;
                    case factoryhub:
                        setCode(3);
                        break;
                    default:
                        break;
                }
            }
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = 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结果的类型。 每当执行收集操作时,都会延迟评估 Lambda 表达式。 要修复错误,请参阅下面的代码变体。 显式存储.toMap操作的结果,然后使用Collections.unmodifiableMap

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

修复:

Error:(22, 0) ajc: Cannot refer to the static enum field ApplicationType.transport within an initializer

枚举的重载构造函数Application(String name)看起来不正确。 您可以如下所示简化您的枚举代码以消除错误。

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返回枚举。

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM