【问题标题】:Aspectj for anotations with classes not workingAspectj 用于类不工作的注释
【发布时间】:2015-02-14 09:52:54
【问题描述】:

下面的代码没有与 aspectJ 正确兼容

import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.type.AnnotatedTypeMetadata;

@Configuration
@Conditional(ConditionalConfiguration.Condition)
@ImportResource("/com/example/context-fragment.xml")
public class ConditionalConfiguration {
    static class Condition implements ConfigurationCondition {
         @Override  

         public ConfigurationPhase getConfigurationPhase() {
             return ConfigurationPhase.PARSE_CONFIGURATION;
         }          
         @Override
         public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
             // only load context-fragment.xml if the system property is defined
             return System.getProperty("com.example.context-fragment") != null;
         }
    }
}

我正在使用 Eclipse Aspectj 工具。并为@Conditional 注释显示错误。

@Conditional:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Conditional {

    /**
     * All {@link Condition}s that must {@linkplain Condition#matches match}
     * in order for the component to be registered.
     */
    Class<? extends Condition>[] value();

}

错误是:

org.aspectj.weaver.BCException
at org.aspectj.ajdt.internal.core.builder.AjState.recordClassFile(AjState.java:1519)
at org.aspectj.ajdt.internal.core.builder.AjState.noteResult(AjState.java:1325)
at org.aspectj.ajdt.internal.core.builder.AjBuildManager$3.acceptResult(AjBuildManager.java:1061)
at org.aspectj.ajdt.internal.compiler.AjPipeliningCompilerAdapter.afterProcessing(AjPipeliningCompilerAdapter.java:426)
at org.aspectj.ajdt.intern ... .0_21-64\jre\lib\ext\sunmscapi.jar;E:\jdk1.7.0_21-64\jre\lib\ext\zipfs.jar;D:\eclipse\\plugins/org.eclipse.equinox.launcher_1.3.0.v20140415-2008.jar;

任何想法我怎样才能让它工作,或者至少它是一种我可以设置 Eclipse AspectJ Tool 来简单地忽略这个文件的方法。

【问题讨论】:

  • 如果您有 AspectJ 问题,请同时发布您的 aspect(s)。如果是 AspectJ 编译器问题,请告诉我们您用于编译的 AspectJ 版本(AJDT 插件版本,如果您在 Eclipse 中使用 AJDT)。最好提供一个可复制的SSCCE

标签: eclipse aspectj


【解决方案1】:

我只是能够重现您的问题。您的代码是错误的,而不是 AspectJ。像这样更改您的注释值:

@Conditional(ConditionalConfiguration.Condition.class)

您只是忘记使用.class 后缀。修复你自己的代码,编译器就不会再抱怨了。 ;-)

更新:因为您的错误无论如何都不应该杀死 AspectJ 编译器,所以我为它创建了一个 bug ticket,但事实仍然是您自己的代码是伪造的。

【讨论】:

  • org.aspectj.weaver.BCException 误导了我!感谢您的回答,我在 stackovrflow (stackoverflow.com/questions/3035630/…) 上找到了上面的代码,因为我的 eclipse 项目启用了方面,所以我从来没有机会看到实际的编译错误而不是 aspectJ 问题。我要修复那里的代码!
  • 这里实际上并不是 AspectJ 有问题,而是 AspectJ 委托给它的 Eclipse 编译器生成了错误的字节码。因为,在编译后,AspectJ 立即想要深入研究它立即遇到问题的字节码。我提出了一个 eclipse 错误 (bugs.eclipse.org/bugs/show_bug.cgi?id=456960) 来修复它,然后 AspectJ 可以拾取它。使用常规 Eclipse 编译该 Java 示例不会以同样的方式失败,因为 Eclipse 不会立即检查字节码是否实际有效。无论如何,仅供参考。
  • 感谢 Andy 跟进此问题。从用户的角度来看,这是一个 Ajc 问题,因为普通的 AspectJ 用户不关心甚至不知道 Ajc 内部使用了哪些组件。我作为一个经常使用的用户确实知道这一点,并且理解你依赖于其他人来解决这个问题的根本原因。无论如何,这不是一个障碍,我很高兴 Eclipse 编译器的人知道它并会在适当的时候修复它。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多