You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/05/06 22:13:30 UTC

svn commit: r772413 - in /incubator/pivot/trunk: build.xml wtk/src/pivot/wtkx/BindProcessor.java

Author: tvolkert
Date: Wed May  6 20:13:30 2009
New Revision: 772413

URL: http://svn.apache.org/viewvc?rev=772413&view=rev
Log:
Changed BindProcessor so that it creates a bind() method override instead of an instance initializer, giving developers control over when the binding takes place; Re-enabled bind processing in the Ant build script

Modified:
    incubator/pivot/trunk/build.xml
    incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java

Modified: incubator/pivot/trunk/build.xml
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/build.xml?rev=772413&r1=772412&r2=772413&view=diff
==============================================================================
--- incubator/pivot/trunk/build.xml (original)
+++ incubator/pivot/trunk/build.xml Wed May  6 20:13:30 2009
@@ -480,7 +480,7 @@
             target="${compiler.target}"
             encoding="${compiler.encoding}"
             failonerror="true">
-            <!-- <compilerarg line="-Xlint -processor pivot.wtkx.BindProcessor"/> -->
+            <compilerarg line="-Xlint -processor pivot.wtkx.BindProcessor"/>
             <classpath>
                 <pathelement location="core/${folder.bin}"/>
                 <pathelement location="web/${folder.bin}"/>
@@ -540,7 +540,7 @@
             target="${compiler.target}"
             encoding="${compiler.encoding}"
             failonerror="true">
-            <!-- <compilerarg line="-Xlint -processor pivot.wtkx.BindProcessor"/> -->
+            <compilerarg line="-Xlint -processor pivot.wtkx.BindProcessor"/>
             <classpath>
                 <pathelement location="core/${folder.bin}"/>
                 <pathelement location="web/${folder.bin}"/>

Modified: incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java?rev=772413&r1=772412&r2=772413&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java Wed May  6 20:13:30 2009
@@ -33,19 +33,28 @@
 import pivot.collections.ArrayStack;
 import pivot.collections.HashMap;
 
+import com.sun.tools.javac.code.Flags;
+import com.sun.tools.javac.code.Symbol;
+import com.sun.tools.javac.code.Symtab;
+import com.sun.tools.javac.code.Type;
 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
 import com.sun.tools.javac.parser.Parser;
 import com.sun.tools.javac.parser.Scanner;
 import com.sun.tools.javac.tree.JCTree;
+import com.sun.tools.javac.tree.TreeMaker;
 import com.sun.tools.javac.tree.TreeTranslator;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.Name;
 import com.sun.source.util.Trees;
 
 /**
- * Annotation processor that injects instance initializers into classes that
- * use the <tt>@Load</tt> and <tt>@Bind</tt> annotations to perform the loading
- * and binding.
+ * Annotation processor that injects <tt>bind()</tt> overrides into classes
+ * that use the <tt>@Load</tt> and <tt>@Bind</tt> annotations to perform WTKX
+ * loading and binding.
+ * <p>
+ * TODO Make base bind() final, and strip out that flag at compilation time
+ * to allow us to extend it.
  *
  * @author tvolkert
  */
@@ -89,8 +98,9 @@
             if (bindScope.loadGroups != null) {
                 StringBuilder sourceCode = new StringBuilder("{");
 
+                // TODO Call super.bind() once impl stripping is being done
+                // on the base class' implementation
                 sourceCode.append("pivot.wtkx.WTKXSerializer wtkxSerializer;");
-                sourceCode.append("Object resource;");
                 sourceCode.append("Object value;");
 
                 for (String loadFieldName : bindScope.loadGroups) {
@@ -109,13 +119,13 @@
                     sourceCode.append("wtkxSerializer = new pivot.wtkx.WTKXSerializer();");
                     sourceCode.append(String.format("java.net.URL location = getClass().getResource(\"%s\");", loadAnnotation.value()));
                     sourceCode.append("try {");
-                    sourceCode.append("resource = wtkxSerializer.readObject(location);");
+                    sourceCode.append("value = wtkxSerializer.readObject(location);");
                     sourceCode.append("} catch (Exception ex) {");
                     sourceCode.append("throw new pivot.wtkx.BindException(ex);");
                     sourceCode.append("}");
 
                     // Bind the resource to the field
-                    sourceCode.append(String.format("%s = (%s)resource;", loadFieldName, loadField.vartype.toString()));
+                    sourceCode.append(String.format("%s = (%s)value;", loadFieldName, loadField.vartype.toString()));
 
                     // Bind the resource lookups to their corresponding fields
                     if (loadGroup.bindFields != null) {
@@ -147,16 +157,34 @@
 
                 sourceCode.append("}");
 
-                // Parse our source code into a AST block
+                // Parse our source code into a AST block (bind() method body)
                 Scanner.Factory scannerFactory = Scanner.Factory.instance(context);
                 Parser.Factory parserFactory = Parser.Factory.instance(context);
 
                 Scanner scanner = scannerFactory.newScanner(sourceCode.toString());
                 Parser parser = parserFactory.newParser(scanner, false, false);
-                JCTree.JCBlock block = parser.block();
+                JCTree.JCBlock methodBody = parser.block();
 
-                // Add the AST block (instance initializer) to the class
-                tree.defs = tree.defs.prepend(block);
+                // Create the bind() override AST method declaration
+                Type.MethodType methodType = new Type.MethodType(
+                    List.<Type>nil(),             // Argument types
+                    symbolTable.voidType,         // Return type
+                    List.<Type>nil(),             // Throws types
+                    symbolTable.methodClass       // Type symbol
+                );
+                Symbol.MethodSymbol methodSymbol = new Symbol.MethodSymbol(
+                    Flags.PROTECTED,              // Flags
+                    nameTable.fromString("bind"), // Name
+                    methodType,                   // Type
+                    tree.sym                      // Owner
+                );
+                JCTree.JCMethodDecl methodDeclaration = make.MethodDef(
+                    methodSymbol,                 // Symbol
+                    methodBody                    // Body
+                );
+
+                // Add the AST method declaration to the class
+                tree.defs = tree.defs.prepend(methodDeclaration);
             }
         }
 
@@ -173,8 +201,8 @@
                 bindAnnotation = element.getAnnotation(Bindable.Bind.class);
             } else if (tree.mods != null
                 && tree.mods.annotations != null) {
-                List<JCTree.JCAnnotation> annotations = tree.mods.annotations;
                 // TODO
+                //List<JCTree.JCAnnotation> annotations = tree.mods.annotations;
             }
 
             if (loadAnnotation != null
@@ -218,6 +246,10 @@
 
     private Trees trees;
     private Context context;
+    private TreeMaker make;
+    private Name.Table nameTable;
+    private Symtab symbolTable;
+    private BindInjector bindInjector = new BindInjector();
 
     private static final boolean DEBUG = false;
 
@@ -227,17 +259,14 @@
 
         trees = Trees.instance(processingEnvironment);
         context = ((JavacProcessingEnvironment)processingEnvironment).getContext();
+        make = TreeMaker.instance(context);
+        symbolTable = Symtab.instance(context);
+        nameTable = Name.Table.instance(context);
     }
 
     @Override
     public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
-        boolean claimAnnotations = false;
-
         if (!roundEnvironment.processingOver()) {
-            claimAnnotations = true;
-
-            BindInjector bindInjector = new BindInjector();
-
             for (Element rootElement : roundEnvironment.getRootElements()) {
                 if (rootElement.getKind() == ElementKind.CLASS) {
                     // Visit each Class tree with our bindInjector visitor
@@ -248,10 +277,10 @@
         } else {
             processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
                 String.format("%d WTKX %s processed (bound to %d %s).",
-                loadTally, loadTally == 1 ? "load" : "loads", bindTally,
-                bindTally == 1 ? "variable" : "variables"));
+                loadTally, loadTally == 1 ? "load" : "loads",
+                bindTally, bindTally == 1 ? "variable" : "variables"));
         }
 
-        return claimAnnotations;
+        return true;
     }
 }