流的AspectJ编译时编织失败

时间:2018-08-14 23:10:50

标签: java generics java-stream aspectj

Aspectj编译器运行时出现错误提示。

[ERROR] Type mismatch: cannot convert from List<Object> to List<Tag>

我的代码是

        final List<Tag> customTags = 
             pathVariables.entrySet().stream().filter(entry -> {
                return tagList.contains(entry.getKey());
            }).map(tag -> {
                return new Tag() {

                    @Override
                    public String getValue() {
                        logger.info("Key for the attached tag is: {}", 
                        tag.getKey());
                        return tag.getKey();
                    }

                    @Override
                    public String getKey() {
                        logger.info("Value for the attached tag is: {}", (String)tag.getValue());
                        return (String) tag.getValue();
                    }
                };
            }).collect(Collectors.toList());

pom.xml

<dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.7</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjtools</artifactId>
        <version>1.8.7</version>
    </dependency>
<plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.8</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <complianceLevel>1.8</complianceLevel>
                <encoding>UTF-8</encoding>
                <verbose>true</verbose>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
                        <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
                    </goals>
                </execution>
            </executions>
</plugin>

我尝试过的事情, 1.添加属性,以告知Maven编译器插件符合Java 8

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>
  1. 将AspectJ版本更改为1.8.13

在两种情况下,我都遇到相同的错误。如果我使用

final List customTags = ... 

使用相同的代码,我不会收到此错误。我想念什么吗?

  1. 尝试运行类似结构的伪代码,

    Map<String, Object> HOSTING1 = new HashMap<>();
    HOSTING1.put("1", "linode.com");
    HOSTING1.put("2", "heroku.com");
    HOSTING1.put("3", "digitalocean.com");
    HOSTING1.put("4", "aws.amazon.com");
    
    List<String> tagList = Arrays.asList("1", "2", "3");
    
    final List<Tag> customTags = HOSTING1.entrySet().stream().filter(entry - 
    > {
        return tagList.contains(entry.getKey());
    }).map(tag -> {
        return new Tag() {
    
            @Override
            public String getValue() {
    
                System.out.println("Value for the attached tag is: {}" + tag.getValue());
                return (String) tag.getValue();
            }
    
            @Override
            public String getKey() {
                System.out.println("Key for the attached tag is: {}" + tag.getKey());
                return (String) tag.getKey();
            }
        };
    }).collect(Collectors.toList());
    

1 个答案:

答案 0 :(得分:0)

明确指定.map(tag-> {...帮助!没有该AspectJ编译器会导致此问题。代码是

final List<Tag> customTags = 
         pathVariables.entrySet().stream().filter(entry -> {
            return tagList.contains(entry.getKey());
        }).<Tag>map(tag -> {
            return new Tag() {

                @Override
                public String getValue() {
                    logger.info("Key for the attached tag is: {}", 
                    tag.getKey());
                    return tag.getKey();
                }

                @Override
                public String getKey() {
                    logger.info("Value for the attached tag is: {}", (String)tag.getValue());
                    return (String) tag.getValue();
                }
            };
        }).collect(Collectors.toList());