Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Answering my own question:</p> <p>Here is the relevant bit of code to insert the super call:</p> <p>these fields are all initialized in <a href="http://download.oracle.com/javase/6/docs/api/javax/annotation/processing/AbstractProcessor.html#init%28javax.annotation.processing.ProcessingEnvironment%29" rel="nofollow noreferrer">init(env)</a> or <a href="http://download.oracle.com/javase/6/docs/api/javax/annotation/processing/AbstractProcessor.html#process%28java.util.Set,%20javax.annotation.processing.RoundEnvironment%29" rel="nofollow noreferrer">process(annotations, roundEnv)</a>:</p> <pre><code>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&lt;JCCompilationUnit, Void&gt; compilationUnits; private Map&lt;String, JCCompilationUnit&gt; typeMap; </code></pre> <p>And here is the logic that is called if a subtype of <code>AbstractBehavior</code> that has the annotation does not override the <code>renderHead(response)</code> method:</p> <pre><code>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.&lt;JCExpression&gt; nil() ); // public final JCModifiers mods = this.treeMaker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation)); // parameters:(final IHeaderResponse response) final List&lt;JCVariableDecl&gt; 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&lt;JCStatement&gt; statements = List.&lt;JCStatement&gt; of( // Execute this: this.treeMaker.Exec( // Create a Method call: this.treeMaker.Apply( // (no generic type arguments) List.&lt;JCExpression&gt; nil(), // super.renderHead this.treeMaker.Select( this.treeMaker.Ident( elementUtils.getName("super") ), elementUtils.getName("renderHead") ), // (response) List.&lt;JCExpression&gt; 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, // &lt;no generic parameters&gt; List.&lt;JCTypeParameter&gt; nil(), // (final IHeaderResponse response) params, // &lt;no declared exceptions&gt; List.&lt;JCExpression&gt; nil(), // super.renderHead(response); body, // &lt;no default value&gt; 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"); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload