【问题标题】:AspectJ Compile time weaving fails for streams流的 AspectJ 编译时间编织失败
【发布时间】:2018-08-14 23:10:50
【问题描述】:

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

【问题讨论】:

  • 看来与AspectJ无关,是你的代码。请您用一个较小的演示对其进行测试,然后发布错误?
  • 我已经用虚拟代码测试过它运行良好。通过 maven 运行 ApectJ 编译器时抛出错误。
  • 您可以尝试在map 调用中明确指定泛型类型,即.&lt;Tag&gt;map( ...
  • 抱歉,我无法重现您的问题...有什么办法重现它?
  • 是的,明确指定 .map(tag -> {... 有帮助!没有那个 AspectJ 编译器会导致问题。

标签: java generics java-stream aspectj


【解决方案1】:

明确指定 .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());

【讨论】:

    猜你喜欢
    • 2013-03-18
    • 2012-11-21
    • 2011-07-27
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    相关资源
    最近更新 更多