我是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)时,classloader在构建(...)方法时抛出ClassFormat错误,同时在应用程序上下文中初始化该bean ...
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)
请帮助我解决这个问题,我已经在我的项目上的任何其他类上尝试了这个(注释可以应用于Type或方法)并且它可以工作,但正是这种bean构建方法我很好面临问题,无法找到任何解决方法。也许facej当前对java 8的支持是错误的,它实际上破坏了字节码,或者我在那里做错了什么?
感谢您的时间。
答案 0 :(得分:1)
有些问题:
AJ 1.8.0是全新的,其中一些问题实际上是由ECJ(Eclipse Java编译器)引起的。有些已修复,因此您可以尝试使用当前的developer build(选择&#34;最后一次正确的开发人员构建&#34;。
<强>更新强>
我能够使用一个小代码示例重现该问题,独立于任何其他类。问题不是注释或简单的forEach
lambdas,而是明显嵌套的forEach
lambdas。
我在http://bugs.eclipse.org/bugs/show_bug.cgi?id=435446上提交了一个AspectJ错误。
更新2:
错误提示还描述了如何通过从切入点中排除lambda调用来解决问题。
我刚发现的另一种解决方法是使用参数-noverify
运行JVM。
更新3:
错误修复已完成,可作为开发版本使用。它将成为即将到来的AspectJ 1.8.1的一部分。
答案 1 :(得分:0)
我发现问题来自lambda表达式用法:(
用常规替换所有lambdas来修复问题
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)) {
associateCurrency(currency, cryptsyCurrencyCode);
} else {
LOGGER.debug("associated currency [" + currencySymbol + "] does not exist in database, removing from configuration");
markedForRemoval.add(cryptsyCurrencyCode);
}
});
结果
for(Map.Entry<String, String> entry : exchangeCurrencyToCurrency.entrySet()){
String cryptsyCurrencyCode = entry.getKey();
String currencySymbol = entry.getValue();
com.jarvis.data.entity.Currency currency = dataService.getCurrency(currencySymbol);
if (notNull(currency)) {
associateCurrency(currency, cryptsyCurrencyCode);
} else {
LOGGER.debug("associated currency [" + currencySymbol + "] does not exist in database, removing from configuration");
markedForRemoval.add(cryptsyCurrencyCode);
}
}