You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by da...@apache.org on 2006/10/11 22:06:01 UTC

svn commit: r462924 - in /geronimo/xbean/sandbox: xbean-apt/ xbean-apt/src/main/java/org/apache/xbean/apt/ xbean-apt/src/main/resources/ xbean-factory/ xbean-factory/src/main/java/org/apache/xbean/factory/model/ xbean-factory/src/test/java/org/apache/x...

Author: dain
Date: Wed Oct 11 13:06:00 2006
New Revision: 462924

URL: http://svn.apache.org/viewvc?view=rev&rev=462924
Log:
First working generator

Added:
    geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Factory.java
    geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Property.java
    geronimo/xbean/sandbox/xbean-factory/src/test/java/org/apache/xbean/factory/XBeanJaxbTest.java
Modified:
    geronimo/xbean/sandbox/xbean-apt/pom.xml
    geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/VelocityClassVisitor.java
    geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/XBeanAnnotationProcessor.java
    geronimo/xbean/sandbox/xbean-apt/src/main/resources/FactoryBean.vm
    geronimo/xbean/sandbox/xbean-factory/pom.xml
    geronimo/xbean/sandbox/xbean-factory/src/main/java/org/apache/xbean/factory/model/Database.java

Modified: geronimo/xbean/sandbox/xbean-apt/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/xbean/sandbox/xbean-apt/pom.xml?view=diff&rev=462924&r1=462923&r2=462924
==============================================================================
--- geronimo/xbean/sandbox/xbean-apt/pom.xml (original)
+++ geronimo/xbean/sandbox/xbean-apt/pom.xml Wed Oct 11 13:06:00 2006
@@ -85,6 +85,12 @@
       <version>1.0.1</version>
     </dependency>
 
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <profiles>
     <profile>

Added: geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Factory.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Factory.java?view=auto&rev=462924
==============================================================================
--- geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Factory.java (added)
+++ geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Factory.java Wed Oct 11 13:06:00 2006
@@ -0,0 +1,202 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.apt;
+
+import com.sun.mirror.declaration.ClassDeclaration;
+import com.sun.mirror.declaration.MethodDeclaration;
+import com.sun.mirror.declaration.ParameterDeclaration;
+import com.sun.mirror.type.VoidType;
+
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.Collection;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class Factory {
+    private String packagePostfix = ".factory";
+    private String classPostfix = "FactoryBean";
+
+    private String className;
+    private String classPackage;
+    private String classFqn;
+    private String factoryPackage;
+    private String factoryName;
+    private String factoryFqn;
+
+    private final Map<String, Property> properties = new TreeMap<String, Property>();
+
+    public Factory() {
+    }
+
+    public Factory(ClassDeclaration declaration) {
+        className = declaration.getSimpleName();
+        classPackage = declaration.getPackage().getQualifiedName();
+        classFqn = classPackage + "." + className;
+
+        factoryName = className + classPostfix;
+        factoryPackage = classPackage + packagePostfix;
+        factoryFqn = factoryPackage + "." + factoryName;
+
+        for (MethodDeclaration method : declaration.getMethods()) {
+            String methodName = method.getSimpleName();
+
+            boolean isGetter = isGetter(method);
+            boolean isSetter = isSetter(method);
+
+            if (isGetter || isSetter) {
+                String propertyName = getPropertyName(methodName);
+
+                Property property = getProperty(propertyName);
+                if (property == null) {
+                    property = new Property();
+                    property.setName(propertyName);
+                    properties.put(propertyName, property);
+
+                    if (isGetter) {
+                        property.setType(method.getReturnType());
+                    } else {
+                        ParameterDeclaration parameter = method.getParameters().iterator().next();
+                        property.setType(parameter.getType());
+                    }
+                }
+
+                if (isGetter) {
+                    property.setGetter(method);
+                } else {
+                    property.setSetter(method);
+                }
+            }
+        }
+    }
+
+    private static String getPropertyName(String methodName) {
+        if (methodName.startsWith("get")) {
+            if (methodName.length() < 4) {
+                return null;
+            }
+            return Character.toUpperCase(methodName.charAt(3)) + methodName.substring(4);
+        } else if (methodName.startsWith("is")) {
+            if (methodName.length() < 3) {
+                return null;
+            }
+            return Character.toUpperCase(methodName.charAt(2)) + methodName.substring(3);
+        } else if (methodName.startsWith("set")) {
+            if (methodName.length() < 4) {
+                return null;
+            }
+            return Character.toUpperCase(methodName.charAt(3)) + methodName.substring(4);
+        }
+        return null;
+    }
+
+    private static boolean isGetter(MethodDeclaration method) {
+        if (!method.getParameters().isEmpty()) {
+            return false;
+        }
+        if (method.getReturnType() instanceof VoidType) {
+            return false;
+        }
+
+        String methodName = method.getSimpleName();
+        if (methodName.startsWith("get")) {
+            return methodName.length() >= 4;
+        } else if (methodName.startsWith("is")) {
+            return methodName.length() >= 3;
+        }
+        return false;
+    }
+
+    private static boolean isSetter(MethodDeclaration method) {
+        if (method.getParameters().size() == 1) {
+            return false;
+        }
+        if (!(method.getReturnType() instanceof VoidType)){
+            return false;
+        }
+
+        String methodName = method.getSimpleName();
+        if (methodName.startsWith("set")) {
+            return methodName.length() >= 4;
+        }
+        return false;
+    }
+
+    public String getClassName() {
+        return className;
+    }
+
+    public void setClassName(String className) {
+        this.className = className;
+    }
+
+    public String getClassPackage() {
+        return classPackage;
+    }
+
+    public void setClassPackage(String classPackage) {
+        this.classPackage = classPackage;
+    }
+
+    public String getClassFqn() {
+        return classFqn;
+    }
+
+    public void setClassFqn(String classFqn) {
+        this.classFqn = classFqn;
+    }
+
+    public String getFactoryPackage() {
+        return factoryPackage;
+    }
+
+    public void setFactoryPackage(String factoryPackage) {
+        this.factoryPackage = factoryPackage;
+    }
+
+    public String getFactoryName() {
+        return factoryName;
+    }
+
+    public void setFactoryName(String factoryName) {
+        this.factoryName = factoryName;
+    }
+
+    public String getFactoryFqn() {
+        return factoryFqn;
+    }
+
+    public void setFactoryFqn(String factoryFqn) {
+        this.factoryFqn = factoryFqn;
+    }
+
+    public Property addProperty(String name) {
+        Property property = new Property();
+        property.setName(name);
+        properties.put(name, property);
+        return property;
+    }
+
+    public Property getProperty(String name) {
+        return properties.get(name);
+    }
+
+    public Collection<Property> getProperties() {
+        return properties.values();
+    }
+}

Added: geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Property.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Property.java?view=auto&rev=462924
==============================================================================
--- geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Property.java (added)
+++ geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/Property.java Wed Oct 11 13:06:00 2006
@@ -0,0 +1,62 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.apt;
+
+import com.sun.mirror.type.TypeMirror;
+import com.sun.mirror.declaration.MethodDeclaration;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class Property {
+    private String name;
+    private TypeMirror type;
+    private MethodDeclaration getter;
+    private MethodDeclaration setter;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public TypeMirror getType() {
+        return type;
+    }
+
+    public void setType(TypeMirror type) {
+        this.type = type;
+    }
+
+    public MethodDeclaration getGetter() {
+        return getter;
+    }
+
+    public void setGetter(MethodDeclaration getter) {
+        this.getter = getter;
+    }
+
+    public MethodDeclaration getSetter() {
+        return setter;
+    }
+
+    public void setSetter(MethodDeclaration setter) {
+        this.setter = setter;
+    }
+}

Modified: geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/VelocityClassVisitor.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/VelocityClassVisitor.java?view=diff&rev=462924&r1=462923&r2=462924
==============================================================================
--- geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/VelocityClassVisitor.java (original)
+++ geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/VelocityClassVisitor.java Wed Oct 11 13:06:00 2006
@@ -16,33 +16,25 @@
  */
 package org.apache.xbean.apt;
 
-import com.sun.mirror.apt.AnnotationProcessorEnvironment;
 import com.sun.mirror.declaration.ClassDeclaration;
 import com.sun.mirror.util.SimpleDeclarationVisitor;
-import org.apache.velocity.Template;
-import org.apache.velocity.VelocityContext;
-import org.apache.velocity.app.Velocity;
 import org.apache.xbean.annotations.XmlRootElement;
 
-import java.io.PrintWriter;
-import java.util.Properties;
+import java.util.Map;
+import java.util.TreeMap;
 
 /**
  * @version $Revision: 69 $
  */
 public class VelocityClassVisitor extends SimpleDeclarationVisitor {
-    private final AnnotationProcessorEnvironment env;
+    private final Map<String, Factory> factories = new TreeMap<String, Factory>();
 
-    private String templateName = "FactoryBean.vm";
-    private String packagePostfix = ".factory";
-    private String classPostfix = "FactoryBean";
-
-    private String packageName;
-    private String className;
-    private String qualifiedName;
+    public Factory getFactory(String name) {
+        return factories.get(name);
+    }
 
-    public VelocityClassVisitor(final AnnotationProcessorEnvironment env) {
-        this.env = env;
+    public Map<String, Factory> getFactories() {
+        return factories;
     }
 
     public void visitClassDeclaration(ClassDeclaration declaration) {
@@ -50,51 +42,9 @@
         if (!matchesDeclaration(declaration)) {
             return;
         }
-        createClassProperties(declaration);
-        System.out.println("Creating file: " + qualifiedName);
-        PrintWriter file = null;
-        try {
-            Properties p = new Properties();
-            p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
-
-            Velocity.init(p);
-
-            VelocityContext context = createVelocityContext(declaration);
-            file = env.getFiler().createSourceFile(qualifiedName);
-            // engine.evaluate(context, arg1, className, arg3)
-            Template template = Velocity.getTemplate(templateName);
-
-            template.merge(context, file);
-            file.close();
-            file = null;
-        }
-        catch (Throwable e) {
-            System.out.println(e);
-            e.printStackTrace();
-        }
-    }
-
-    protected void createClassProperties(ClassDeclaration declaration) {
-        packageName = declaration.getPackage().getQualifiedName();
-        packageName += packagePostfix;
-        className = declaration.getSimpleName() + classPostfix;
-        qualifiedName = packageName + "." + className;
-    }
-
-    protected VelocityContext createVelocityContext(ClassDeclaration declaration) {
-        VelocityContext answer = new VelocityContext();
-        answer.put("declaration", declaration);
-        answer.put("packageName", packageName);
-        answer.put("license", "/** TODO license goes here */");
-        answer.put("className", className);
-        answer.put("qualifiedName", qualifiedName);
-        /*
-          Class type = loadClass(declaration);
-          answer.put("info", new EntityInfo(type));
-          */
-        return answer;
+        Factory factory = new Factory(declaration);
+        factories.put(factory.getClassFqn(), factory);
     }
-
 
     protected boolean matchesDeclaration(ClassDeclaration declaration) {
         XmlRootElement annotation = declaration.getAnnotation(XmlRootElement.class);

Modified: geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/XBeanAnnotationProcessor.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/XBeanAnnotationProcessor.java?view=diff&rev=462924&r1=462923&r2=462924
==============================================================================
--- geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/XBeanAnnotationProcessor.java (original)
+++ geronimo/xbean/sandbox/xbean-apt/src/main/java/org/apache/xbean/apt/XBeanAnnotationProcessor.java Wed Oct 11 13:06:00 2006
@@ -23,16 +23,56 @@
 import com.sun.mirror.apt.AnnotationProcessorEnvironment;
 import com.sun.mirror.declaration.TypeDeclaration;
 
+import java.util.Map;
+import java.util.Properties;
+import java.io.PrintWriter;
+
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.Template;
+import org.apache.velocity.app.Velocity;
+
 public class XBeanAnnotationProcessor implements AnnotationProcessor {
     private final AnnotationProcessorEnvironment env;
+    private String templateName = "FactoryBean.vm";
 
     public XBeanAnnotationProcessor(AnnotationProcessorEnvironment env) {
         this.env = env;
     }
 
     public void process() {
+        VelocityClassVisitor velocityClassVisitor = new VelocityClassVisitor();
         for (TypeDeclaration typeDecl : env.getSpecifiedTypeDeclarations()) {
-            typeDecl.accept(getDeclarationScanner(new VelocityClassVisitor(env), NO_OP));
+            typeDecl.accept(getDeclarationScanner(velocityClassVisitor, NO_OP));
+        }
+        Map<String, Factory> factories = velocityClassVisitor.getFactories();
+        for (Factory factory : factories.values()) {
+            generate(factory);
+        }
+    }
+
+    public void generate(Factory factory) {
+        System.out.println("Creating file: " + factory.getFactoryFqn());
+        PrintWriter file = null;
+        try {
+            Properties p = new Properties();
+            p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
+
+            Velocity.init(p);
+
+            VelocityContext context = new VelocityContext();
+            context.put("f", factory);
+            context.put("license", "/** TODO license goes here */");
+            file = env.getFiler().createSourceFile(factory.getFactoryFqn());
+            // engine.evaluate(context, arg1, className, arg3)
+            Template template = Velocity.getTemplate(templateName);
+
+            template.merge(context, file);
+            file.close();
+            file = null;
+        }
+        catch (Throwable e) {
+            System.out.println(e);
+            e.printStackTrace();
         }
     }
 }

Modified: geronimo/xbean/sandbox/xbean-apt/src/main/resources/FactoryBean.vm
URL: http://svn.apache.org/viewvc/geronimo/xbean/sandbox/xbean-apt/src/main/resources/FactoryBean.vm?view=diff&rev=462924&r1=462923&r2=462924
==============================================================================
--- geronimo/xbean/sandbox/xbean-apt/src/main/resources/FactoryBean.vm (original)
+++ geronimo/xbean/sandbox/xbean-apt/src/main/resources/FactoryBean.vm Wed Oct 11 13:06:00 2006
@@ -1,15 +1,42 @@
 $license
 
-package $packageName;
-
-import $declaration.qualifiedName;
+package $f.factoryPackage;
 
+import org.apache.xbean.factory.FactoryBeanSupport;
 import javax.xml.bind.annotation.*;
 
+import $f.classFqn;
 
 /**
  * JAXB2 Factory Bean
  */
 @XmlRootElement
-public class $className {
+public class $f.factoryName extends FactoryBeanSupport<$f.className> {
+
+    public $f.className getObject() {
+        ${f.className} _object = new ${f.className}();
+#foreach ( $prop in $f.properties)
+        _object.set${prop.name}(${prop.name});
+#end
+        return _object;
+    }
+
+
+    public void configure($f.className _object) {
+#foreach ( $prop in $f.properties)
+        ${prop.name} = _object.get${prop.name}();
+#end
+    }
+
+
+#foreach ( $prop in $f.properties)
+    private $prop.type $prop.name;
+    public ${prop.type} get${prop.name}() {
+        return $prop.name;
+    }
+    public void set${prop.name}(${prop.type} ${prop.name}) {
+        this.${prop.name} = ${prop.name};
+    }
+
+#end
 }

Modified: geronimo/xbean/sandbox/xbean-factory/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/xbean/sandbox/xbean-factory/pom.xml?view=diff&rev=462924&r1=462923&r2=462924
==============================================================================
--- geronimo/xbean/sandbox/xbean-factory/pom.xml (original)
+++ geronimo/xbean/sandbox/xbean-factory/pom.xml Wed Oct 11 13:06:00 2006
@@ -113,20 +113,23 @@
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-apt-plugin</artifactId>
         <configuration>
-          <nocompile>false</nocompile>
+          <nocompile>true</nocompile>
           <verbose>false</verbose>
           <A>debug, loglevel=3</A>
           <target>1.5</target>
         </configuration>
-        <!--
         <dependencies>
           <dependency>
             <groupId>velocity</groupId>
             <artifactId>velocity</artifactId>
             <version>1.4</version>
           </dependency>
+          <dependency>
+            <groupId>org.apache.xbean</groupId>
+            <artifactId>xbean-apt</artifactId>
+            <version>1.0-SNAPSHOT</version>
+          </dependency>
         </dependencies>
-        -->
       </plugin>
     </plugins>
   </build>

Modified: geronimo/xbean/sandbox/xbean-factory/src/main/java/org/apache/xbean/factory/model/Database.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/sandbox/xbean-factory/src/main/java/org/apache/xbean/factory/model/Database.java?view=diff&rev=462924&r1=462923&r2=462924
==============================================================================
--- geronimo/xbean/sandbox/xbean-factory/src/main/java/org/apache/xbean/factory/model/Database.java (original)
+++ geronimo/xbean/sandbox/xbean-factory/src/main/java/org/apache/xbean/factory/model/Database.java Wed Oct 11 13:06:00 2006
@@ -16,6 +16,7 @@
  */
 package org.apache.xbean.factory.model;
 
+import javax.xml.bind.annotation.XmlRootElement;
 import java.net.URI;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -25,6 +26,7 @@
 /**
  * @version $Rev$ $Date$
  */
+@XmlRootElement 
 public class Database {
     private String name;
     private Vendor vendor;

Added: geronimo/xbean/sandbox/xbean-factory/src/test/java/org/apache/xbean/factory/XBeanJaxbTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/sandbox/xbean-factory/src/test/java/org/apache/xbean/factory/XBeanJaxbTest.java?view=auto&rev=462924
==============================================================================
--- geronimo/xbean/sandbox/xbean-factory/src/test/java/org/apache/xbean/factory/XBeanJaxbTest.java (added)
+++ geronimo/xbean/sandbox/xbean-factory/src/test/java/org/apache/xbean/factory/XBeanJaxbTest.java Wed Oct 11 13:06:00 2006
@@ -0,0 +1,48 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.xbean.factory;
+
+import org.apache.xbean.factory.model.User;
+import org.apache.xbean.factory.model.factory.CustomUserFactory;
+import org.apache.xbean.factory.model.factory.UserFactoryBean;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileInputStream;
+
+/**
+ * @version $Revision: $
+ */
+public class XBeanJaxbTest extends UserTest {
+
+    protected String dir = "target/data";
+    protected String file = dir + "/user.xml";
+
+    public User loadUser() throws Exception {
+        new File(dir).mkdirs();
+
+        User user = super.loadUser();
+        UserFactoryBean factory = new UserFactoryBean();
+        factory.write(new FileOutputStream(file), user);
+
+        // in case there's caching, lets make another
+        factory = new UserFactoryBean();
+        FactoryBeanSupport<User> newFactory = factory.read(new FileInputStream(file));
+        return newFactory.getObject();
+    }
+
+}