gpt4 book ai didi

java - 方面破坏特定类上的字节码

转载 作者:太空宇宙 更新时间:2023-11-04 06:47:29 25 4
gpt4 key购买 nike

我是 AOP 新手,我创建了一个方面来跟踪所有用 @Trace 注释标记的方法或类。我正在使用编译时编织。 (Java 8、Aspectj 1.8、Spring 4)

TraceAspect.java

@Aspect
public class TraceAspect {
private static Map<String, Integer> threadMap = new HashMap<>();

@Pointcut("@within(Trace) || @annotation(Trace)")
void annotated(){}

@Around("annotated() && execution(* *(..))")
public Object trace(final ProceedingJoinPoint joinPoint) throws Throwable {
String threadName = Thread.currentThread().getName();

String indent = indent(inThread(threadName));

System.out.println(threadName + " : " + indent + "-> " + joinPoint.getSignature().toString());

long start = System.nanoTime();
Object ret = joinPoint.proceed();
long end = System.nanoTime();

System.out.println(threadName + " : " + indent + "<- " + joinPoint.getSignature().toString() + " ended (took " + (end - start) + " nanoseconds)");

outThread(threadName);
return ret;
}

private String indent(int depth) {
String result = "";
for (int index = 0; index < depth; index++) {
result += " ";
}
return result;
}

private int inThread(String threadName) {
if (threadMap.get(threadName) == null) {
threadMap.put(threadName, 0);
}

int stackDepth = threadMap.get(threadName) + 1;
threadMap.put(threadName, stackDepth);
return stackDepth;
}

private void outThread(String threadName) {
int stackDepth = threadMap.get(threadName) - 1;
threadMap.put(threadName, stackDepth);
}
}

当使用 @Trace 标记 CryptsyExchange.java(这是一个 Spring Bean)时,类加载器在应用程序上下文中初始化该 Bean 时会在 build(..) 方法上抛出 ClassFormat 错误...

CryptsyExchange.java

@Trace
public class CryptsyExchange {
private static final Logger LOGGER = LoggerFactory.getLogger(CryptsyExchange.class);

private DataService dataService;
private Configuration config;

private Converter converter;
private Exchange exchange;
private List<CryptsyAccount> accounts = Collections.synchronizedList(new LinkedList<>());
private CryptsyAccount defaultAccount;

public static CryptsyExchange build(String name, DataService dataService, ConfigPathBuilder pathBuilder) {
condition(notNullOrEmpty(name) && notNull(dataService, pathBuilder));

CryptsyExchange cryptsyExchange = new CryptsyExchange();
cryptsyExchange.dataService = dataService;


// Loading configuration
final Configuration configuration = Configuration.load(pathBuilder.getExchangeConfigPath(name));
cryptsyExchange.config = configuration;

// Retrieve corresponding exchange from datastore
cryptsyExchange.exchange = dataService.registerExchange(cryptsyExchange.config.getString("exchange"));

// Get accounts from configuration

Map<String, Map<String, String>> accounts = configuration.getMap("accounts");

// Initialize accounts
accounts.entrySet().stream().forEach((entry) -> {
String key = entry.getKey();
Map<String, String> accountMap = entry.getValue();

// Retrieve corresponding datastore account
Account account = dataService.registerAccount(cryptsyExchange.exchange, key);

// Initialize cryptsy specific account
CryptsyAccount cryptsyAccount = new CryptsyAccount(account, accountMap.get("key"), accountMap.get("secret"));

cryptsyExchange.accounts.add(cryptsyAccount);

if (notNull(accountMap.get("isDefault")) && Boolean.valueOf(accountMap.get("isDefault"))) {
cryptsyExchange.defaultAccount = cryptsyAccount;
}
});

// Initializing Converter
cryptsyExchange.converter = cryptsyExchange.new Converter();

// Recover associations from configuration
Map<String, String> exchangeCurrencyToCurrency = configuration.getMap("exchangeCurrencyToCurrency");
Set<String> markedForRemoval = new HashSet<>();

exchangeCurrencyToCurrency.entrySet().stream().forEach((entry) -> {
String cryptsyCurrencyCode = entry.getKey();
String currencySymbol = entry.getValue();

com.jarvis.data.entity.Currency currency = dataService.getCurrency(currencySymbol);
if (notNull(currency)) {
cryptsyExchange.converter.associateCurrency(currency, cryptsyCurrencyCode);
} else {
LOGGER.debug("associated currency [" + currencySymbol + "] does not exist in database, removing from configuration");
markedForRemoval.add(cryptsyCurrencyCode);
}
});

// Removing currency associations missing from database
if (!markedForRemoval.isEmpty()) {
markedForRemoval.forEach((currency) -> configuration.remove("exchangeCurrencyToCurrency", currency));
}

Map<String, String> exchangeMarketToMarket = configuration.getMap("exchangeMarketToMarket");
markedForRemoval.clear();

exchangeMarketToMarket.entrySet().stream().forEach((entry) -> {
String cryptsyMarketId = entry.getKey();
String marketName = entry.getValue();

Market market = dataService.getMarket(marketName);
if (notNull(market)) {
cryptsyExchange.converter.associateMarket(market, Integer.valueOf(cryptsyMarketId));
} else {
LOGGER.debug("associated market [+" + marketName + "] does not exist, removing from configuration");
markedForRemoval.add(cryptsyMarketId);
}
});

// Removing market associations missing from database
if (!markedForRemoval.isEmpty()) {
markedForRemoval.forEach((market) -> configuration.remove("exchangeMarketToMarket", market));
}

// Update configuration
configuration.save();

return cryptsyExchange;
}

// Lot of other code there
}

当然还有 stackTrace:

Exception in thread "main" java.lang.ClassFormatError: Illegal local variable table length 288 in method com.jarvis.exchange.cryptsy.CryptsyExchange.build_aroundBody0(Ljava/lang/String;Lcom/jarvis/data/service/DataService;Lcom/jarvis/util/ConfigPathBuilder;Lorg/aspectj/lang/JoinPoint;)Lcom/jarvis/exchange/cryptsy/CryptsyExchange;
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getDeclaredMethods(Class.java:1962)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:467)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:451)
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:512)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:663)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1396)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:382)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:353)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:82)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.jarvis.Jarvis.<clinit>(Jarvis.java:10)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)

我已经在我的项目中的任何其他类上尝试过这个(注释可以应用于类型或方法)并且它有效,但正是使用这个 bean 构建方法我遇到了问题并且找不到任何解决方法。也许 Aspectj 目前对 Java 8 的支持存在缺陷,并且实际上会损坏字节码。或者也许我在那里做错了什么?

最佳答案

一些问题:

  • 您使用成熟的 AspectJ 还是仅使用带有 @AspectJ 语法的 Spring AOP?
  • 您使用源/目标级别 1.8 还是仍然使用 1.7 进行编译?
  • 完全相同的代码是否适用于 Java 7 和 AspectJ 1.8.0?
  • 如果不能,它是否适用于 Java 7 和 AspectJ 1.7.4?
  • 您能否提供重现该问题的独立最小代码示例?甚至可能在 GitHub 上使用 Maven 构建?)您引用的许多类对我来说是不可见的,因此我无法重现它。

AJ 1.8.0是全新的,它的一些问题实际上是由ECJ(Eclipse Java编译器)引起的。有些已经修复,所以你可以尝试当前的 developer build (选择“最后一次已知的良好开发人员版本”。

更新:

我能够使用一个小的代码示例重现该问题,独立于任何其他类。问题不是注释或简单的 forEach lambda,而是明显嵌套的 forEach lambda。

我在 http://bugs.eclipse.org/bugs/show_bug.cgi?id=435446 上提交了 AspectJ 错误.

更新2:

错误单还描述了如何通过从切入点排除 lambda 调用来解决该问题。

我刚刚发现的另一个解决方法是使用参数 -noverify 运行 JVM。

更新3:

错误修复已完成并可作为开发版本使用。它将成为即将推出的 AspectJ 1.8.1 的一部分。

关于java - 方面破坏特定类上的字节码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23787068/

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