AOP或APT用于覆盖超类的方法

AOP或APT用于覆盖超类的方法,第1张

AOP或APT用于覆盖超类的方法

回答我自己的问题:

这是插入超级调用的相关代码位:

这些字段都在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 pre 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"); }        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5490249.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存