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/12 01:35:21 UTC

svn commit: r773723 - in /incubator/pivot/trunk/wtk/src/pivot/wtkx: BindProcessor.java Bindable.java WTKXSerializer.java

Author: tvolkert
Date: Mon May 11 23:35:20 2009
New Revision: 773723

URL: http://svn.apache.org/viewvc?rev=773723&view=rev
Log:
Added BindProcessor handling of the @Load(compile=true) attribute, removed superfluous interpretObject signatures from WTKXSerializer, fixed bug in WTKXSerializer#readObject(URL) (it wasn't closing the input stream)

Modified:
    incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java
    incubator/pivot/trunk/wtk/src/pivot/wtkx/Bindable.java
    incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java

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=773723&r1=773722&r2=773723&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/BindProcessor.java Mon May 11 23:35:20 2009
@@ -16,6 +16,11 @@
  */
 package pivot.wtkx;
 
+import java.io.BufferedInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.net.URL;
+
 import javax.annotation.processing.AbstractProcessor;
 import javax.annotation.processing.ProcessingEnvironment;
 import javax.annotation.processing.RoundEnvironment;
@@ -26,6 +31,9 @@
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.TypeElement;
 import javax.tools.Diagnostic;
+import javax.tools.FileObject;
+import javax.tools.JavaFileManager;
+import javax.tools.StandardLocation;
 
 import pivot.collections.ArrayList;
 import pivot.collections.ArrayStack;
@@ -257,20 +265,20 @@
             if (loadGroups != null || strandedBindFields != null) {
                 // There is some bind work to be done in this class; start by
                 // creating the source code buffer
-                StringBuilder sourceCode = new StringBuilder("class _A {");
-                sourceCode.append("@Override ");
-                sourceCode.append("protected void bind(pivot.collections.Dictionary<String,");
-                sourceCode.append("pivot.collections.Dictionary<String, Object>> namedObjectDictionaries) {");
-                sourceCode.append("super.bind(namedObjectDictionaries);");
+                StringBuilder buf = new StringBuilder("class _A {");
+                buf.append("@Override ");
+                buf.append("protected void bind(pivot.collections.Dictionary<String,");
+                buf.append("pivot.collections.Dictionary<String, Object>> namedObjectDictionaries) {");
+                buf.append("super.bind(namedObjectDictionaries);");
 
-                sourceCode.append("pivot.wtkx.WTKXSerializer wtkxSerializer;");
-                sourceCode.append("Object object;");
+                buf.append("pivot.wtkx.WTKXSerializer wtkxSerializer;");
+                buf.append("Object object;");
 
                 if (loadGroups != null) {
                     // Process WTKX loads in this class
-                    sourceCode.append("java.net.URL location;");
-                    sourceCode.append("java.util.Locale locale;");
-                    sourceCode.append("pivot.util.Resources resources;");
+                    buf.append("java.net.URL location;");
+                    buf.append("java.util.Locale locale;");
+                    buf.append("pivot.util.Resources resources;");
 
                     for (String loadFieldName : loadGroups) {
                         AnnotationDossier.LoadGroup loadGroup = loadGroups.get(loadFieldName);
@@ -280,7 +288,7 @@
                         String resourceName = getAnnotationProperty(loadAnnotation, "name");
                         String baseName = getAnnotationProperty(loadAnnotation, "resources");
                         String language = getAnnotationProperty(loadAnnotation, "locale");
-                        //Boolean compile = getAnnotationProperty(loadAnnotation, "compile");
+                        Boolean compile = getAnnotationProperty(loadAnnotation, "compile");
                         boolean defaultResources = (baseName == null);
 
                         if (DEBUG) {
@@ -296,77 +304,137 @@
                             }
                         }
 
-                        // Attempt to load the resources
-                        sourceCode.append("resources = null;");
-                        if (baseName != null) {
-                            if (language == null) {
-                                sourceCode.append("locale = java.util.Locale.getDefault();");
-                            } else {
-                                sourceCode.append(String.format("locale = new java.util.Locale(\"%s\");", language));
+                        if (compile != null && compile.booleanValue()) {
+                            FileObject sourceFile = null;
+                            if (classDeclaration.sym != null) {
+                                sourceFile = classDeclaration.sym.sourcefile;
                             }
-                            sourceCode.append("try {");
-                            sourceCode.append(String.format
-                                ("resources = new pivot.util.Resources(%s, locale, \"UTF8\");",
-                                defaultResources ? (baseName + ".class.getName()") : ("\"" + baseName + "\"")));
-                            sourceCode.append("} catch(java.io.IOException ex) {");
-                            sourceCode.append("throw new pivot.wtkx.BindException(ex);");
-                            sourceCode.append("} catch (pivot.serialization.SerializationException ex) {");
-                            sourceCode.append("throw new pivot.wtkx.BindException(ex);");
-                            sourceCode.append("} catch (java.util.MissingResourceException ex) {");
-                            if (!defaultResources) {
-                                sourceCode.append("throw new pivot.wtkx.BindException(ex);");
-                            }
-                            sourceCode.append("}");
-                        }
 
-                        // Load the WTKX resource
-                        sourceCode.append("wtkxSerializer = new pivot.wtkx.WTKXSerializer(resources);");
-                        sourceCode.append(String.format("location = getClass().getResource(\"%s\");", resourceName));
-                        sourceCode.append("try {");
-                        sourceCode.append("object = 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)object;", loadFieldName,
-                            loadField.vartype.toString()));
-
-                        // Public and protected fields get kept for subclasses
-                        if ((loadField.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0) {
-                            sourceCode.append(String.format
-                                ("namedObjectDictionaries.put(\"%s\", wtkxSerializer.getNamedObjects());", loadFieldName));
-                        }
+                            if (sourceFile != null) {
+                                JavaFileManager fileManager = context.get(JavaFileManager.class);
 
-                        // Bind the resource lookups to their corresponding fields
-                        if (loadGroup.bindFields != null) {
-                            for (JCVariableDecl bindField : loadGroup.bindFields) {
-                                String bindFieldName = bindField.name.toString();
-                                JCAnnotation bindAnnotation = getBindAnnotation(bindField);
-
-                                String bindName = getAnnotationProperty(bindAnnotation, "name");
-                                if (bindName == null) {
-                                    // The bind name defaults to the field name
-                                    bindName = bindFieldName;
+                                InputStream inputStream;
+                                try {
+                                    if (resourceName.startsWith("/")) {
+                                        resourceName = resourceName.substring(1);
+                                        ClassLoader classLoader = fileManager.getClassLoader
+                                            (StandardLocation.SOURCE_PATH);
+                                        URL resourceLocation = classLoader.getResource(resourceName);
+                                        inputStream = new BufferedInputStream(resourceLocation.openStream());
+                                    } else {
+                                        String packageName = classDeclaration.sym.packge().toString();
+                                        FileObject resourceFile = fileManager.getFileForInput
+                                            (StandardLocation.SOURCE_PATH, packageName, resourceName);
+                                        inputStream = resourceFile.openInputStream();
+                                    }
+
+                                    try {
+                                        // TODO Handle resources
+                                        WTKXSerializer wtkxSerializer = new WTKXSerializer();
+                                        String blockCode = wtkxSerializer.interpretObject(inputStream);
+
+                                        // Open local scope for variable name protection
+                                        buf.append("{");
+
+                                        // Add interpreted code
+                                        buf.append(blockCode);
+
+                                        // Record interpreted values back into our main scope
+                                        buf.append(String.format
+                                            ("namedObjectDictionaries.put(\"%s\", __namedObjects);", loadFieldName));
+                                        buf.append(String.format
+                                            ("%s = (%s)__result;", loadFieldName, loadField.vartype.toString()));
+
+                                        // Close local scope
+                                        buf.append("}");
+                                    } finally {
+                                        inputStream.close();
+                                    }
+                                } catch (Exception ex) {
+                                    processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+                                    "Error trying to load field " + classDeclaration.name.toString() + "." +
+                                    loadFieldName + ": " + ex.getMessage());
                                 }
-
-                                if (DEBUG) {
-                                    String property = getAnnotationProperty(bindAnnotation, "property");
-                                    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
-                                        String.format("Processing bind(%s.%s, %s#%s)", property,
-                                        bindName, classDeclaration.name.toString(), bindFieldName));
+                            } else {
+                                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
+                                    "Unable to determine source file location for " +
+                                    classDeclaration.name.toString());
+                            }
+                        } else {
+                            // Attempt to load the resources
+                            buf.append("resources = null;");
+                            if (baseName != null) {
+                                if (language == null) {
+                                    buf.append("locale = java.util.Locale.getDefault();");
+                                } else {
+                                    buf.append(String.format
+                                        ("locale = new java.util.Locale(\"%s\");", language));
+                                }
+                                buf.append("try {");
+                                buf.append(String.format
+                                    ("resources = new pivot.util.Resources(%s, locale, \"UTF8\");",
+                                    defaultResources ? (baseName + ".class.getName()") : ("\"" + baseName + "\"")));
+                                buf.append("} catch(java.io.IOException ex) {");
+                                buf.append("throw new pivot.wtkx.BindException(ex);");
+                                buf.append("} catch (pivot.serialization.SerializationException ex) {");
+                                buf.append("throw new pivot.wtkx.BindException(ex);");
+                                buf.append("} catch (java.util.MissingResourceException ex) {");
+                                if (!defaultResources) {
+                                    buf.append("throw new pivot.wtkx.BindException(ex);");
                                 }
+                                buf.append("}");
+                            }
 
-                                sourceCode.append(String.format
-                                    ("object = wtkxSerializer.getObjectByName(\"%s\");", bindName));
-                                sourceCode.append
-                                    ("if (object == null) {");
-                                sourceCode.append(String.format
-                                    ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", bindName));
-                                sourceCode.append
-                                    ("}");
-                                sourceCode.append(String.format
-                                    ("%s = (%s)object;", bindFieldName, bindField.vartype.toString()));
+                            // Load the WTKX resource
+                            buf.append("wtkxSerializer = new pivot.wtkx.WTKXSerializer(resources);");
+                            buf.append(String.format("location = getClass().getResource(\"%s\");", resourceName));
+                            buf.append("try {");
+                            buf.append("object = wtkxSerializer.readObject(location);");
+                            buf.append("} catch (Exception ex) {");
+                            buf.append("throw new pivot.wtkx.BindException(ex);");
+                            buf.append("}");
+
+                            // Bind the resource to the field
+                            buf.append(String.format
+                                ("%s = (%s)object;", loadFieldName, loadField.vartype.toString()));
+
+                            // Public and protected fields get kept for subclasses
+                            if ((loadField.mods.flags & (Flags.PUBLIC | Flags.PROTECTED)) != 0) {
+                                buf.append(String.format
+                                    ("namedObjectDictionaries.put(\"%s\", wtkxSerializer.getNamedObjects());",
+                                    loadFieldName));
+                            }
+
+                            // Bind the resource lookups to their corresponding fields
+                            if (loadGroup.bindFields != null) {
+                                for (JCVariableDecl bindField : loadGroup.bindFields) {
+                                    String bindFieldName = bindField.name.toString();
+                                    JCAnnotation bindAnnotation = getBindAnnotation(bindField);
+
+                                    String bindName = getAnnotationProperty(bindAnnotation, "name");
+                                    if (bindName == null) {
+                                        // The bind name defaults to the field name
+                                        bindName = bindFieldName;
+                                    }
+
+                                    if (DEBUG) {
+                                        String property = getAnnotationProperty(bindAnnotation, "property");
+                                        processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
+                                            String.format("Processing bind(%s.%s, %s#%s)", property,
+                                            bindName, classDeclaration.name.toString(), bindFieldName));
+                                    }
+
+                                    buf.append(String.format
+                                        ("object = wtkxSerializer.getObjectByName(\"%s\");", bindName));
+                                    buf.append
+                                        ("if (object == null) {");
+                                    buf.append(String.format
+                                        ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", bindName));
+                                    buf.append
+                                        ("}");
+                                    buf.append(String.format
+                                        ("%s = (%s)object;", bindFieldName, bindField.vartype.toString()));
+                                }
                             }
                         }
                     }
@@ -374,7 +442,7 @@
 
                 if (strandedBindFields != null) {
                     // Process binds to superclass-loaded fields
-                    sourceCode.append("pivot.collections.Dictionary<String, Object> namedObjects;");
+                    buf.append("pivot.collections.Dictionary<String, Object> namedObjects;");
 
                     for (JCVariableDecl bindField : strandedBindFields) {
                         String bindFieldName = bindField.name.toString();
@@ -387,34 +455,34 @@
                             bindName = bindFieldName;
                         }
 
-                        sourceCode.append(String.format
+                        buf.append(String.format
                             ("namedObjects = namedObjectDictionaries.get(\"%s\");", loadFieldName));
 
-                        sourceCode.append
+                        buf.append
                             ("if (namedObjects == null) {");
-                        sourceCode.append(String.format
+                        buf.append(String.format
                             ("throw new pivot.wtkx.BindException(\"Property not found: %s.\");", loadFieldName));
-                        sourceCode.append
+                        buf.append
                             ("}");
 
-                        sourceCode.append(String.format
+                        buf.append(String.format
                             ("object = namedObjects.get(\"%s\");", bindName));
-                        sourceCode.append
+                        buf.append
                             ("if (object == null) {");
-                        sourceCode.append(String.format
+                        buf.append(String.format
                             ("throw new pivot.wtkx.BindException(\"Element not found: %s.\");", bindName));
-                        sourceCode.append
+                        buf.append
                             ("}");
-                        sourceCode.append(String.format
+                        buf.append(String.format
                             ("%s = (%s)object;", bindFieldName, bindField.vartype.toString()));
                     }
                 }
 
-                sourceCode.append("}");
-                sourceCode.append("}");
+                buf.append("}");
+                buf.append("}");
 
                 // Parse the source code and extract the method declaration
-                Scanner scanner = scannerFactory.newScanner(sourceCode.toString());
+                Scanner scanner = scannerFactory.newScanner(buf.toString());
                 Parser parser = parserFactory.newParser(scanner, false, false);
                 JCCompilationUnit parsedCompilationUnit = parser.compilationUnit();
                 JCClassDecl parsedClassDeclaration = (JCClassDecl)parsedCompilationUnit.defs.head;

Modified: incubator/pivot/trunk/wtk/src/pivot/wtkx/Bindable.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/Bindable.java?rev=773723&r1=773722&r2=773723&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/Bindable.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/Bindable.java Mon May 11 23:35:20 2009
@@ -323,9 +323,9 @@
             }
         } else {
             // Invoke the bind overload
-            HashMap<String, Dictionary<String, Object>> namedObjectsDictionaries =
+            HashMap<String, Dictionary<String, Object>> namedObjectDictionaries =
                 new HashMap<String, Dictionary<String, Object>>();
-            bind(namedObjectsDictionaries);
+            bind(namedObjectDictionaries);
         }
     }
 
@@ -334,7 +334,7 @@
      * override. It exists to support {@link BindProcessor}. Dealing directly
      * with this method in any way may yield unpredictable behavior.
      */
-    protected void bind(Dictionary<String, Dictionary<String, Object>> namedObjectsDictionaries) {
+    protected void bind(Dictionary<String, Dictionary<String, Object>> namedObjectDictionaries) {
         // No-op
     }
 }

Modified: incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java?rev=773723&r1=773722&r2=773723&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtkx/WTKXSerializer.java Mon May 11 23:35:20 2009
@@ -176,7 +176,6 @@
             return result;
         }
 
-        @SuppressWarnings("unchecked")
         public boolean isEmpty() {
             boolean empty = namedObjects.isEmpty();
 
@@ -283,7 +282,12 @@
         }
 
         this.location = location;
-        return readObject(new BufferedInputStream(location.openStream()));
+        InputStream inputStream = new BufferedInputStream(location.openStream());
+        try {
+            return readObject(inputStream);
+        } finally {
+            inputStream.close();
+        }
     }
 
     @SuppressWarnings("unchecked")
@@ -706,54 +710,6 @@
     }
 
     /**
-     * Interprets an object from the specified WTKX resource.
-     * <p>
-     * The name of a resource is a '/'-separated path name that identifies the
-     * resource. See {@link ClassLoader#getResource(String)} for more details.
-     *
-     * @param resourceName
-     * The resource name
-     *
-     * @return
-     * The interpreted Java source code
-     */
-    public String interpretObject(String resourceName) throws IOException,
-        SerializationException {
-        if (resourceName == null) {
-            throw new IllegalArgumentException("resourceName is null.");
-        }
-
-        ClassLoader classLoader = ThreadUtilities.getClassLoader();
-        URL location = classLoader.getResource(resourceName);
-
-        if (location == null) {
-            throw new SerializationException("Could not find resource named \""
-                + resourceName + "\".");
-        }
-
-        return interpretObject(location);
-    }
-
-    /**
-     * Interprets an object from a URL that points to a WTKX resource.
-     *
-     * @param location
-     * The location of the resource to be interpreted
-     *
-     * @return
-     * The interpreted Java source code
-     */
-    public String interpretObject(URL location) throws IOException,
-        SerializationException {
-        if (location == null) {
-            throw new IllegalArgumentException("location is null.");
-        }
-
-        this.location = location;
-        return interpretObject(new BufferedInputStream(location.openStream()));
-    }
-
-    /**
      * Interprets an object from a WTKX input stream.
      *
      * @param inputStream
@@ -769,7 +725,8 @@
         }
 
         StringBuilder buf = new StringBuilder();
-        buf.append("pivot.collections.Dictionary<String, Object> namedObjects = " +
+        buf.append("Object __result = null;");
+        buf.append("pivot.collections.Dictionary<String, Object> __namedObjects = " +
             "new pivot.collections.HashMap<String, Object>();\n");
 
         // Parse the XML stream