Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 39492 invoked from network); 11 Oct 2006 20:06:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 11 Oct 2006 20:06:24 -0000 Received: (qmail 76987 invoked by uid 500); 11 Oct 2006 20:06:24 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 76962 invoked by uid 500); 11 Oct 2006 20:06:24 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 76951 invoked by uid 99); 11 Oct 2006 20:06:24 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Oct 2006 13:06:24 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Oct 2006 13:06:22 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id BB2201A981A; Wed, 11 Oct 2006 13:06:01 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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... Date: Wed, 11 Oct 2006 20:06:01 -0000 To: scm@geronimo.apache.org From: dain@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20061011200601.BB2201A981A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 @@ 1.0.1 + + junit + junit + 3.8.1 + test + 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 properties = new TreeMap(); + + 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 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 factories = new TreeMap(); - 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 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 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 @@ org.apache.maven.plugins maven-apt-plugin - false + true false debug, loglevel=3 1.5 - 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 newFactory = factory.read(new FileInputStream(file)); + return newFactory.getObject(); + } + +}