gpt4 book ai didi

spring - 使用 Java 8 流 API 时出现 Aspectj BootstrapMethodError

转载 作者:行者123 更新时间:2023-12-02 02:26:16 25 4
gpt4 key购买 nike

所以我在这里 - 运行一个带有 spring roo 的 Spring 应用程序。

我过去常常将 Controller 分成多个方面,所以我的主 Controller 将如下所示:

@Controller
@RequestMapping("/apples")
@SessionAttributes(types = {Apple.class})
public class AppleController {
}

以及其他方面扩展了其功能,例如:

privileged aspect AppleController_Basics {

@RequestMapping(value = "/allApples", produces = "text/html", method=RequestMethod.GET)
public String AppleController.allApples(Model model) {
...
return "apples/list";
}
}

现在,当我尝试在以下方面使用 Java 8 流 API 时:

apples.stream().filter(a -> a.isSweet()).collect(Collectors.toList());

我面临以下异常:

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.BootstrapMethodError: java.lang.NoSuchMethodError: com.apple.web.AppleController.lambda$0(Lcom/apple/model/Apple;)Z

当我对 Apple 本身以外的其他实体使用流 API 时,我得到了略有不同的异常:

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.BootstrapMethodError: java.lang.IllegalAccessError: tried to access method com.apple.web.AppleController.lambda$0(Lcom/apple/security/AppleEater;)Z from class com.apple.web.aspects.AppleController_Basics

当使用 forEach 时,我得到 OutOfMemoryError::

apples.forEach(System.out::println);

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.OutOfMemoryError: Java heap space

当我在主类中使用这些表达式时,一切正常。

插件看起来像这样:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.9</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.10</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<complianceLevel>1.8</complianceLevel>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.8</source>
<target>1.8</target>
<showWeaveInfo>true</showWeaveInfo>
<weaveWithAspectsInMainSourceFolder>false</weaveWithAspectsInMainSourceFolder>
</configuration>
</plugin>

我尝试了不同的方法来更改我的aspectj插件配置以使其工作 - 但没有成功。我感谢任何提示或帮助,因为我现在真的很困惑,请不要讨厌<3

javap -c -p AppleController.class

public java.lang.String allApples(org.springframework.ui.Model);
Code:
0: aload_0
1: aload_1
2: invokestatic #528 // Method com/apple/web/aspects/AppleController_Basics.ajc$interMethod$com_apple_web_aspects_AppleController_Basics$com_apple_web_AppleController$allApples:(Lcom/apple/web/AppleController;Lorg/springframework/ui/Model;)Ljava/lang/String;
5: areturn

最佳答案

这显然是 AspectJ 编译器的错误或缺点。我创建了一个bug ticket为了它。

这是我从您的代码中提取的(非 Spring)测试用例:

package de.scrum_master.app;

public class Apple {
private String type;
private boolean sweet;

public Apple(String type, boolean sweet) {
this.type = type;
this.sweet = sweet;
}

public String getType() {
return type;
}

public boolean isSweet() {
return sweet;
}
}
package de.scrum_master.app;

import java.util.Arrays;
import java.util.List;

public class AppleController {
private static final List<Apple> APPLES =
Arrays.asList(new Apple("Granny Smith", false), new Apple("Golden Delicious", true));

public static void main(String[] args) {
AppleController appleController = new AppleController();
System.out.println("Named: " + appleController.namedApples(APPLES, "Smith"));
System.out.println("Sweet: " + appleController.sweetApples(APPLES));
System.out.println("Sour: " + appleController.sourApples(APPLES));
}
}
package de.scrum_master.aspect;

import java.util.List;
import java.util.stream.Collectors;

import java.util.function.Predicate;
import de.scrum_master.app.Apple;
import de.scrum_master.app.AppleController;

public privileged aspect AppleControllerITDAspect {
public List<Apple> AppleController.namedApples(List<Apple> apples, String subString) {
// Anonymous subclass works
return apples.stream().filter(new Predicate<Apple>() {
@Override
public boolean test(Apple a) {
return a.getType().contains(subString);
}
}).collect(Collectors.toList());
}

public List<Apple> AppleController.sweetApples(List<Apple> apples) {
// Method reference works
return apples.stream().filter(Apple::isSweet).collect(Collectors.toList());
}

public List<Apple> AppleController.sourApples(List<Apple> apples) {
// Lambda causes IllegalAccessError
return apples.stream().filter(a -> !a.isSweet()).collect(Collectors.toList());
}
}

控制台日志如下所示:

Named: [de.scrum_master.app.Apple@6f496d9f]
Sweet: [de.scrum_master.app.Apple@4e50df2e]
Exception in thread "main" java.lang.BootstrapMethodError: java.lang.IllegalAccessError: tried to access method de.scrum_master.app.AppleController.lambda$0(Lde/scrum_master/app/Apple;)Z from class de.scrum_master.aspect.AppleControllerITDAspect
at de.scrum_master.aspect.AppleControllerITDAspect.ajc$interMethod$de_scrum_master_aspect_AppleControllerITDAspect$de_scrum_master_app_AppleController$sourApples(AppleControllerITDAspect.aj:28)
at de.scrum_master.app.AppleController.sourApples(AppleController.java:1)
at de.scrum_master.aspect.AppleControllerITDAspect.ajc$interMethodDispatch1$de_scrum_master_aspect_AppleControllerITDAspect$de_scrum_master_app_AppleController$sourApples(AppleControllerITDAspect.aj)
at de.scrum_master.app.AppleController.main(AppleController.java:14)
Caused by: java.lang.IllegalAccessError: tried to access method de.scrum_master.app.AppleController.lambda$0(Lde/scrum_master/app/Apple;)Z from class de.scrum_master.aspect.AppleControllerITDAspect
at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
at java.lang.invoke.MemberName$Factory.resolve(Unknown Source)
at java.lang.invoke.MemberName$Factory.resolveOrFail(Unknown Source)
at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(Unknown Source)
at java.lang.invoke.MethodHandles$Lookup.linkMethodHandleConstant(Unknown Source)
at java.lang.invoke.MethodHandleNatives.linkMethodHandleConstant(Unknown Source)
... 4 more

在上面的方面,您还可以看到一个临时解决方法:使用方法引用或经典的匿名子类而不是 lambda。

背景信息:AspectJ 编译器 AJC 是 Eclipse Java 编译器 ECJ 的定期更新分支(顺便说一句,AspectJ 也是一个官方 Eclipse 项目)。因此,bug 可能出现在 ECJ 中,但也可能出现在 AJC 中。

关于spring - 使用 Java 8 流 API 时出现 Aspectj BootstrapMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42732445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com