Aspectj for anotations with classes not working

2019-08-05 04:59发布

问题:

Below code is not complied correctly with 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;
         }
    }
}

I am using Eclipse Aspectj Tool. and the error is shown for @Conditional annotation.

The is as @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();

}

The error is:

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;

Any Idea how can I make it work, Or at least is it a way which I can set Eclipse AspectJ Tool to simply ignore this file.

回答1:

I was just able to reproduce your problem. Your code is wrong, not AspectJ. Change your annotation value like this:

@Conditional(ConditionalConfiguration.Condition.class)

You just forgot to use the .class suffix. Fix your own code, then the compiler will not complain anymore. ;-)

Update: Because your bug should not kill the AspectJ compiler anyway, I created a bug ticket for it, but the fact remains that your own code was bogus.