java - AOP 或 APT 用于覆盖父类(super class)的方法

标签 java code-generation wicket aspectj apt

我有一个大型 wicket 组件库,这些组件使用自定义注释 @ReferencedResource 或另一个注释 @ReferencedResources 进行注释,具有 ReferencedResouce[] 值() 参数允许多个注释。

这是一个示例代码片段:

@ReferencedResources({
    @ReferencedResource(value = Libraries.MOO_TOOLS, type = ResourceType.JAVASCRIPT),
    @ReferencedResource(value = "behaviors/promoteSelectOptions", type = ResourceType.JAVASCRIPT) })
public class PromoteSelectOptionsBehavior extends AbstractBehavior{
 ...
}

到目前为止,我使用 apt检查引用的资源是否确实存在。例如。

@ReferencedResource(value = "behaviors/promoteSelectOptions",
                     type = ResourceType.JAVASCRIPT)

除非可以在类路径中找到文件 js/behaviors/promoteSelectOptions.js,否则将导致编译失败。这部分工作得很好。

现在我也是 DRY 的粉丝,我想使用相同的注释在创建对象时将资源实际注入(inject)到对象中。我使用 AspectJ 实现了其中的一部分。

带注释的对象总是 Component 的任一实例或 AbstractBehavior .

对于组件来说,事情很简单,只需要在构造函数之后匹配即可。这是执行此操作的建议:

pointcut singleAnnotation() : @within(ReferencedResource);

pointcut multiAnnotation() : @within(ReferencedResources);

after() : execution(Component+.new(..)) && (singleAnnotation() || multiAnnotation()){
    final Component component = (Component) thisJoinPoint.getTarget();
    final Collection<ReferencedResource> resourceAnnotations =
        // gather annotations from cache
        this.getResourceAnnotations(component.getClass());
    for(final ReferencedResource annotation : resourceAnnotations){
        // helper utility that handles the creation of statements like
        // component.add(JavascriptPackageResource.getHeaderContribution(path))
        this.resourceInjector.inject(component, annotation);
    }
}

但是,对于行为,我需要将资源附加到响应,而不是行为本身。以下是我使用的切入点:

pointcut renderHead(IHeaderResponse response) :
    execution(* org.apache.wicket.behavior.AbstractBehavior+.renderHead(*))
        && args(response);

这里是建议:

before(final IHeaderResponse response) : 
    renderHead(response) && (multiAnnotation() || singleAnnotation()) {
    final Collection<ReferencedResource> resourceAnnotations =
        this.getResourceAnnotations(thisJoinPoint.getTarget().getClass());
    for(final ReferencedResource resource : resourceAnnotations){
        this.resourceInjector.inject(response, resource);
    }
}

如果类重写 renderHead(response),这也能很好地工作方法,但在许多情况下,这并不是必需的,因为父类(super class)已经实现了基本功能,而子类仅添加了一些配置。因此,一种解决方案是让这些类定义如下方法:

@Override
public void renderHead(IHeaderResponse response){
    super.renderHead(response);
}

我讨厌这个,因为这是死代码,但目前这是我看到的唯一可行的选项,所以我正在寻找其他解决方案。

编辑:

我已经使用 APT 和 sun javac 调用创建了一个可行的解决方案。然而,这会导致下一个问题:Running APT and AspectJ in the same project using maven .

无论如何,只要有空闲时间,我就会发布这个问题(或部分问题)的答案。

最佳答案

回答我自己的问题:

下面是插入 super 调用的相关代码:

这些字段都是在init(env)中初始化的或 process(annotations, roundEnv) :

private static Filer filer;
private static JavacProcessingEnvironment environment;
private static Messager messager;
private static Types types;
private static JavacElements elementUtils;
private Trees trees;
private TreeMaker treeMaker;
private IdentityHashMap<JCCompilationUnit, Void> compilationUnits;
private Map<String, JCCompilationUnit> typeMap;

如果具有注解的 AbstractBehavior 的子类型没有覆盖 renderHead(response) 方法,下面是调用的逻辑:

private void addMissingSuperCall(final TypeElement element){
    final String className = element.getQualifiedName().toString();
    final JCClassDecl classDeclaration =
        // look up class declaration from a local map 
        this.findClassDeclarationForName(className);
    if(classDeclaration == null){
        this.error(element, "Can't find class declaration for " + className);
    } else{
        this.info(element, "Creating renderHead(response) method");
        final JCTree extending = classDeclaration.extending;
        if(extending != null){
            final String p = extending.toString();
            if(p.startsWith("com.myclient")){
                // leave it alone, we'll edit the super class instead, if
                // necessary
                return;
            } else{
                // @formatter:off (turns off eclipse formatter if configured)

                // define method parameter name
                final com.sun.tools.javac.util.Name paramName =
                    elementUtils.getName("response");
                // Create @Override annotation
                final JCAnnotation overrideAnnotation =
                    this.treeMaker.Annotation(
                        Processor.buildTypeExpressionForClass(
                            this.treeMaker,
                            elementUtils,
                            Override.class
                        ),
                        // with no annotation parameters
                        List.<JCExpression> nil()
                    );
                // public
                final JCModifiers mods =
                    this.treeMaker.Modifiers(Flags.PUBLIC,
                        List.of(overrideAnnotation));
                // parameters:(final IHeaderResponse response)
                final List<JCVariableDecl> params =
                    List.of(this.treeMaker.VarDef(this.treeMaker.Modifiers(Flags.FINAL),
                        paramName,
                        Processor.buildTypeExpressionForClass(this.treeMaker,
                            elementUtils,
                            IHeaderResponse.class),
                        null));

                //method return type: void
                final JCExpression returnType =
                    this.treeMaker.TypeIdent(TypeTags.VOID);


                // super.renderHead(response);
                final List<JCStatement> statements =
                    List.<JCStatement> of(
                        // Execute this:
                        this.treeMaker.Exec(
                            // Create a Method call:
                            this.treeMaker.Apply(
                                // (no generic type arguments)
                                List.<JCExpression> nil(),
                                // super.renderHead
                                this.treeMaker.Select(
                                    this.treeMaker.Ident(
                                        elementUtils.getName("super")
                                    ),
                                    elementUtils.getName("renderHead")
                                ),
                                // (response)
                                List.<JCExpression> of(this.treeMaker.Ident(paramName)))
                            )
                     );
                // build code block from statements
                final JCBlock body = this.treeMaker.Block(0, statements);
                // build method
                final JCMethodDecl methodDef =
                    this.treeMaker.MethodDef(
                        // public
                        mods,
                        // renderHead
                        elementUtils.getName("renderHead"),
                        // void
                        returnType,
                        // <no generic parameters>
                        List.<JCTypeParameter> nil(),
                        // (final IHeaderResponse response)
                        params,
                        // <no declared exceptions>
                        List.<JCExpression> nil(),
                        // super.renderHead(response);
                        body,
                        // <no default value>
                        null);

                // add this method to the class tree
                classDeclaration.defs =
                    classDeclaration.defs.append(methodDef);

                // @formatter:on turn eclipse formatter on again
                this.info(element,
                    "Created renderHead(response) method successfully");

            }
        }

    }
}

关于java - AOP 或 APT 用于覆盖父类(super class)的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3652463/

相关文章:

java - 如何删除 JLabel?

asp.net-core - 如何从 OpenAPI 3.0 架构生成 ASP.NET Core 2 Controller ?

css - 使用带有 css 的 apache wicket id 来操作文本

java - 如何在单击 AjaxLink 时显示标签组件

java - 在哪里可以找到 Java 构造函数中的技术规范重载抽象方法?

java - Protocol Buffer : Enum issue

java - 在 Android 中使用广播

java - bbcode 中十六进制颜色代码的正则表达式?

编译器设计 - 为具有多个操作数的表达式生成代码

php - 管理员自动生成