2015-06-20 155 views
2

我想用aspectJ來做AOP編程。 但AJC編譯器在下面拋出錯誤,javac編譯器完美工作。AspectJ編譯問題,java8 lambda表達式,switch語句

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

回答

1

修正了

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

我已經加入的情況下,你想回的枚舉基於name一個parse方法。

希望這會有所幫助。