Return-Path: Delivered-To: apmail-incubator-cxf-commits-archive@locus.apache.org Received: (qmail 66093 invoked from network); 8 Sep 2006 18:33:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 8 Sep 2006 18:33:52 -0000 Received: (qmail 9533 invoked by uid 500); 8 Sep 2006 18:33:51 -0000 Delivered-To: apmail-incubator-cxf-commits-archive@incubator.apache.org Received: (qmail 9493 invoked by uid 500); 8 Sep 2006 18:33:51 -0000 Mailing-List: contact cxf-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: cxf-dev@incubator.apache.org Delivered-To: mailing list cxf-commits@incubator.apache.org Received: (qmail 9484 invoked by uid 99); 8 Sep 2006 18:33:51 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 08 Sep 2006 11:33:51 -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; Fri, 08 Sep 2006 11:33:49 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id B7D491A981D; Fri, 8 Sep 2006 11:33:29 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r441603 - in /incubator/cxf/trunk: common/src/main/java/org/apache/cxf/configuration/ tools/xjc/cfg-test/ tools/xjc/cfg-test/src/test/java/org/apache/cxf/xjc/cfg/ tools/xjc/cfg-test/src/test/resources/schemas/configuration/ tools/xjc/cfg/sr... Date: Fri, 08 Sep 2006 18:33:29 -0000 To: cxf-commits@incubator.apache.org From: andreasmyth@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20060908183329.B7D491A981D@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: andreasmyth Date: Fri Sep 8 11:33:28 2006 New Revision: 441603 URL: http://svn.apache.org/viewvc?view=rev&rev=441603 Log: {JIRA CXF-24] Modified abstract base class and xjc plugin to: * notify a property change in the setters * getters to always be of the wrapped type (regardless if mapped from a an attribute with or without a default value, and regardless the minOccurs restriction for an element). * getters to have the option to overwrite an default/injected value or use a fallback if default is null and no injection took place. Added: incubator/cxf/trunk/tools/xjc/cfg-test/src/test/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPluginTest.java (with props) Modified: incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/AbstractConfigurableBeanBase.java incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/ConfigurationProvider.java incubator/cxf/trunk/tools/xjc/cfg-test/pom.xml incubator/cxf/trunk/tools/xjc/cfg-test/src/test/resources/schemas/configuration/foo.xsd incubator/cxf/trunk/tools/xjc/cfg/src/main/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPlugin.java Modified: incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/AbstractConfigurableBeanBase.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/AbstractConfigurableBeanBase.java?view=diff&rev=441603&r1=441602&r2=441603 ============================================================================== --- incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/AbstractConfigurableBeanBase.java (original) +++ incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/AbstractConfigurableBeanBase.java Fri Sep 8 11:33:28 2006 @@ -23,24 +23,44 @@ public abstract class AbstractConfigurableBeanBase implements Configurable { - private List providers; + private List fallbackProviders; + private List overwriteProviders; public String getBeanName() { return this.getClass().getName(); } - public List getProviders() { - return providers; + public List getFallbackProviders() { + return fallbackProviders; + } + + public void setFallbackProviders(List fallbackProviders) { + this.fallbackProviders = fallbackProviders; } - public void setProviders(List providers) { - this.providers = providers; + public List getOverwriteProviders() { + return overwriteProviders; + } + + public void setOverwriteProviders(List overwriteProviders) { + this.overwriteProviders = overwriteProviders; + } + + protected T tryOverwrite(Class cls, String name) { + if (null != overwriteProviders) { + for (ConfigurationProvider p : overwriteProviders) { + Object value = p.getObject(name); + if (null != value) { + return cls.cast(value); + } + } + } + return null; } - - protected T tryProviders(Class cls, String name) { - if (null != providers) { - for (ConfigurationProvider p : providers) { + protected T tryFallback(Class cls, String name) { + if (null != fallbackProviders) { + for (ConfigurationProvider p : fallbackProviders) { Object value = p.getObject(name); if (null != value) { return cls.cast(value); @@ -48,6 +68,10 @@ } } return null; + } + + protected void notifyPropertyChange(String propertyName) { + // do nothing here for now. } Modified: incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/ConfigurationProvider.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/ConfigurationProvider.java?view=diff&rev=441603&r1=441602&r2=441603 ============================================================================== --- incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/ConfigurationProvider.java (original) +++ incubator/cxf/trunk/common/src/main/java/org/apache/cxf/configuration/ConfigurationProvider.java Fri Sep 8 11:33:28 2006 @@ -32,22 +32,4 @@ */ Object getObject(String name); - /** - * Change the value of the configuration item with the given name. - * Return true if the change was accepted and the value changed. - * It is the providers responsibility to persiste the change in its underlying store - * if it accepts the change. - * - * @param name the name of the configuration item. - * @param value the new value for the configuration item. - * @return true if the change was accepted. - */ - boolean setObject(String name, Object value); - - /** - * Save the changes - * - * @return true if the save was successful. - */ - boolean save(); } Modified: incubator/cxf/trunk/tools/xjc/cfg-test/pom.xml URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/cfg-test/pom.xml?view=diff&rev=441603&r1=441602&r2=441603 ============================================================================== --- incubator/cxf/trunk/tools/xjc/cfg-test/pom.xml (original) +++ incubator/cxf/trunk/tools/xjc/cfg-test/pom.xml Fri Sep 8 11:33:28 2006 @@ -46,6 +46,12 @@ junit test + + jaxme + jaxme2 + 0.5.1 + test + Added: incubator/cxf/trunk/tools/xjc/cfg-test/src/test/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPluginTest.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/cfg-test/src/test/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPluginTest.java?view=auto&rev=441603 ============================================================================== --- incubator/cxf/trunk/tools/xjc/cfg-test/src/test/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPluginTest.java (added) +++ incubator/cxf/trunk/tools/xjc/cfg-test/src/test/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPluginTest.java Fri Sep 8 11:33:28 2006 @@ -0,0 +1,278 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.cxf.xjc.cfg; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.DatatypeConverter; +import javax.xml.bind.annotation.adapters.HexBinaryAdapter; +import javax.xml.namespace.QName; + +import junit.framework.TestCase; + +import org.apache.cxf.configuration.AbstractConfigurableBeanBase; +import org.apache.cxf.configuration.ConfigurationProvider; +import org.apache.cxf.configuration.foo.Foo; +import org.apache.ws.jaxme.impl.DatatypeConverterImpl; + + + +public class ConfigurableBeansPluginTest extends TestCase { + + public void testFooDefaultValues() throws Exception { + + DatatypeConverter.setDatatypeConverter(new DatatypeConverterImpl()); + + Foo foo = new org.apache.cxf.configuration.foo.ObjectFactory().createFoo(); + + assertTrue("Foo should inhertit from AbstractConfigurableBeanBase", + foo instanceof AbstractConfigurableBeanBase); + + assertAttributeValuesWithoutDefault(foo); + assertDefaultAttributeValues(foo); + assertDefaultElementValues(foo); + } + + public void testProviders() { + DatatypeConverter.setDatatypeConverter(new DatatypeConverterImpl()); + Foo foo = new org.apache.cxf.configuration.foo.ObjectFactory().createFoo(); + ConfigurationProvider provider = new ConfigurationProvider() { + + public Object getObject(String name) { + if ("integerAttr".equals(name) || "integerAttrNoDefault".equals(name)) { + return BigInteger.TEN; + } + return null; + } + }; + + + assertNull(foo.getIntegerAttrNoDefault()); + assertEquals(new BigInteger("111"), foo.getIntegerAttr()); + List providers = foo.getFallbackProviders(); + assertNull(providers); + providers = new ArrayList(); + providers.add(provider); + foo.setFallbackProviders(providers); + assertEquals(BigInteger.TEN, foo.getIntegerAttrNoDefault()); + assertEquals(new BigInteger("111"), foo.getIntegerAttr()); + providers = foo.getOverwriteProviders(); + assertNull(providers); + providers = new ArrayList(); + providers.add(provider); + foo.setOverwriteProviders(providers); + assertEquals(BigInteger.TEN, foo.getIntegerAttrNoDefault()); + assertEquals(BigInteger.TEN, foo.getIntegerAttr()); + } + + public void testNotifyPropertyChange() { + DatatypeConverter.setDatatypeConverter(new DatatypeConverterImpl()); + FooBean foo = new FooBean(); + + assertNull(foo.getIntegerAttrNoDefault()); + assertEquals(new BigInteger("11"), foo.getIntegerElem()); + assertNull(foo.getChangedProperty()); + foo.setIntegerElem(BigInteger.TEN); + assertEquals("integerElem", foo.getChangedProperty()); + assertEquals(BigInteger.TEN, foo.getIntegerElem()); + foo.setChangedProperty(null); + foo.setIntegerAttr(BigInteger.ONE); + assertEquals("integerAttr", foo.getChangedProperty()); + assertEquals(BigInteger.ONE, foo.getIntegerAttr()); + foo.setChangedProperty(null); + foo.setIntegerElem(null); + assertEquals("integerElem", foo.getChangedProperty()); + assertNull(foo.getIntegerElem()); + + } + + private void assertDefaultAttributeValues(Foo foo) { + assertEquals("Unexpected value for attribute stringAttr", + "hello", foo.getStringAttr()); + assertTrue("Unexpected value for attribute booleanAttr", + foo.isBooleanAttr()); + assertEquals("Unexpected value for attribute integerAttr", + new BigInteger("111"), foo.getIntegerAttr()); + assertEquals("Unexpected value for attribute intAttr", + new Integer(112), foo.getIntAttr()); + assertEquals("Unexpected value for attribute longAttr", + new Long(113L), foo.getLongAttr()); + assertEquals("Unexpected value for attribute shortAttr", + new Short((short)114), foo.getShortAttr()); + assertEquals("Unexpected value for attribute decimalAttr", + new BigDecimal("115"), foo.getDecimalAttr()); + assertEquals("Unexpected value for attribute floatAttr", + new Float(116F), foo.getFloatAttr()); + assertEquals("Unexpected value for attribute doubleAttr", + new Double(117D), foo.getDoubleAttr()); + assertEquals("Unexpected value for attribute byteAttr", + new Byte((byte)118), foo.getByteAttr()); + + byte[] expected = DatatypeConverter.parseBase64Binary("wxyz"); + byte[] effective = foo.getBase64BinaryAttr(); + + assertEquals("Unexpected value for attribute base64BinaryAttr", expected.length, effective.length); + for (int i = 0; i < expected.length; i++) { + assertEquals("Unexpected value for attribute base64BinaryAttr", expected[i], effective[i]); + } + + expected = new HexBinaryAdapter().unmarshal("aaaa"); + effective = foo.getHexBinaryAttr(); + assertEquals("Unexpected value for attribute hexBinaryAttr", expected.length, effective.length); + for (int i = 0; i < expected.length; i++) { + assertEquals("Unexpected value for attribute hexBinaryAttr", expected[i], effective[i]); + } + + QName qn = foo.getQnameAttr(); + assertEquals("Unexpected value for attribute qnameAttr", + "http://www.w3.org/2001/XMLSchema", qn.getNamespaceURI()); + assertEquals("Unexpected value for attribute qnameAttr", + "schema", qn.getLocalPart()); + + assertEquals("Unexpected value for attribute unsignedIntAttr", + new Long(119L), foo.getUnsignedIntAttr()); + assertEquals("Unexpected value for attribute unsignedShortAttr", + new Integer(120), foo.getUnsignedShortAttr()); + assertEquals("Unexpected value for attribute unsignedByteAttr", + new Short((short)121), foo.getUnsignedByteAttr()); + } + + /** + * @param foo + */ + private void assertAttributeValuesWithoutDefault(Foo foo) { + assertNull("Unexpected value for attribute stringAttrNoDefault", + foo.getStringAttrNoDefault()); + assertNull("Unexpected value for attribute booleanAttrNoDefault", + foo.isBooleanAttrNoDefault()); + assertNull("Unexpected value for attribute integerAttrNoDefault", + foo.getIntegerAttrNoDefault()); + assertNull("Unexpected value for attribute intAttrNoDefault", + foo.getIntAttrNoDefault()); + assertNull("Unexpected value for attribute longAttrNoDefault", + foo.getLongAttrNoDefault()); + assertNull("Unexpected value for attribute shortAttrNoDefault", + foo.getShortAttrNoDefault()); + assertNull("Unexpected value for attribute decimalAttrNoDefault", + foo.getDecimalAttrNoDefault()); + assertNull("Unexpected value for attribute floatAttrNoDefault", + foo.getFloatAttrNoDefault()); + assertNull("Unexpected value for attribute doubleAttrNoDefault", + foo.getDoubleAttrNoDefault()); + assertNull("Unexpected value for attribute byteAttrNoDefault", + foo.getByteAttrNoDefault()); + + assertNull("Unexpected value for attribute base64BinaryAttrNoDefault", + foo.getBase64BinaryAttrNoDefault()); + assertNull("Unexpected value for attribute hexBinaryAttrNoDefault", + foo.getHexBinaryAttrNoDefault()); + + assertNull("Unexpected value for attribute qnameAttrNoDefault", + foo.getQnameAttrNoDefault()); + + assertNull("Unexpected value for attribute unsignedIntAttrNoDefault", + foo.getUnsignedIntAttrNoDefault()); + assertNull("Unexpected value for attribute unsignedShortAttrNoDefault", + foo.getUnsignedShortAttrNoDefault()); + assertNull("Unexpected value for attribute unsignedByteAttrNoDefault", + foo.getUnsignedByteAttrNoDefault()); + } + + private void assertDefaultElementValues(Foo foo) { + assertEquals("Unexpected value for element stringElem", + "hello", foo.getStringElem()); + assertTrue("Unexpected value for element booleanElem", + foo.isBooleanElem()); + assertEquals("Unexpected value for element integerElem", + new BigInteger("11"), foo.getIntegerElem()); + assertEquals("Unexpected value for element intElem", + new Integer(12), foo.getIntElem()); + assertEquals("Unexpected value for element longElem", + new Long(13L), foo.getLongElem()); + assertEquals("Unexpected value for element shortElem", + new Short((short)14), foo.getShortElem()); + assertEquals("Unexpected value for element decimalElem", + new BigDecimal("15"), foo.getDecimalElem()); + assertEquals("Unexpected value for element floatElem", + new Float(16F), foo.getFloatElem()); + assertEquals("Unexpected value for element doubleElem", + new Double(17D), foo.getDoubleElem()); + assertEquals("Unexpected value for element byteElem", + new Byte((byte)18), foo.getByteElem()); + + byte[] expected = DatatypeConverter.parseBase64Binary("abcdefgh"); + byte[] effective = foo.getBase64BinaryElem(); + + assertEquals("Unexpected value for element base64BinaryElem", expected.length, effective.length); + for (int i = 0; i < expected.length; i++) { + assertEquals("Unexpected value for element base64BinaryElem", expected[i], effective[i]); + } + + expected = new HexBinaryAdapter().unmarshal("ffff"); + effective = foo.getHexBinaryElem(); + assertEquals("Unexpected value for element hexBinaryElem", expected.length, effective.length); + for (int i = 0; i < expected.length; i++) { + assertEquals("Unexpected value for element hexBinaryElem", expected[i], effective[i]); + } + + QName qn = foo.getQnameElem(); + assertEquals("Unexpected value for element qnameElem", + "http://www.w3.org/2001/XMLSchema", qn.getNamespaceURI()); + assertEquals("Unexpected value for element qnameElem", + "string", qn.getLocalPart()); + + assertEquals("Unexpected value for element unsignedIntElem", + new Long(19L), foo.getUnsignedIntElem()); + assertEquals("Unexpected value for element unsignedShortElem", + new Integer(20), foo.getUnsignedShortElem()); + assertEquals("Unexpected value for element unsignedByteElem", + new Short((short)21), foo.getUnsignedByteElem()); + } + + static class FooBean extends Foo { + + private String changedProperty; + + protected void notifyPropertyChange(String propertyName) { + super.notifyPropertyChange(propertyName); + changedProperty = propertyName; + } + + public void clearChange() { + changedProperty = null; + } + + public String getChangedProperty() { + return changedProperty; + } + + public void setChangedProperty(String changedProperty) { + this.changedProperty = changedProperty; + } + + + + } + + +} Propchange: incubator/cxf/trunk/tools/xjc/cfg-test/src/test/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPluginTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: incubator/cxf/trunk/tools/xjc/cfg-test/src/test/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPluginTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: incubator/cxf/trunk/tools/xjc/cfg-test/src/test/resources/schemas/configuration/foo.xsd URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/cfg-test/src/test/resources/schemas/configuration/foo.xsd?view=diff&rev=441603&r1=441602&r2=441603 ============================================================================== --- incubator/cxf/trunk/tools/xjc/cfg-test/src/test/resources/schemas/configuration/foo.xsd (original) +++ incubator/cxf/trunk/tools/xjc/cfg-test/src/test/resources/schemas/configuration/foo.xsd Fri Sep 8 11:33:28 2006 @@ -58,7 +58,7 @@ - + Modified: incubator/cxf/trunk/tools/xjc/cfg/src/main/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPlugin.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/tools/xjc/cfg/src/main/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPlugin.java?view=diff&rev=441603&r1=441602&r2=441603 ============================================================================== --- incubator/cxf/trunk/tools/xjc/cfg/src/main/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPlugin.java (original) +++ incubator/cxf/trunk/tools/xjc/cfg/src/main/java/org/apache/cxf/xjc/cfg/ConfigurableBeansPlugin.java Fri Sep 8 11:33:28 2006 @@ -21,6 +21,12 @@ import java.util.Collections; import java.util.List; +import java.util.Map; + +import javax.xml.bind.DatatypeConverter; +import javax.xml.bind.annotation.adapters.HexBinaryAdapter; +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; import org.xml.sax.ErrorHandler; @@ -30,16 +36,26 @@ import com.sun.codemodel.JExpr; import com.sun.codemodel.JExpression; import com.sun.codemodel.JFieldRef; +import com.sun.codemodel.JFieldVar; import com.sun.codemodel.JInvocation; import com.sun.codemodel.JMethod; import com.sun.codemodel.JOp; import com.sun.codemodel.JType; +import com.sun.codemodel.JVar; import com.sun.tools.xjc.Options; import com.sun.tools.xjc.Plugin; import com.sun.tools.xjc.model.CPluginCustomization; import com.sun.tools.xjc.outline.ClassOutline; import com.sun.tools.xjc.outline.FieldOutline; import com.sun.tools.xjc.outline.Outline; +import com.sun.tools.xjc.util.NamespaceContextAdapter; +import com.sun.xml.xsom.XSAttributeDecl; +import com.sun.xml.xsom.XSAttributeUse; +import com.sun.xml.xsom.XSElementDecl; +import com.sun.xml.xsom.XSParticle; +import com.sun.xml.xsom.XSTerm; +import com.sun.xml.xsom.XSType; +import com.sun.xml.xsom.XmlString; import org.apache.cxf.configuration.AbstractConfigurableBeanBase; @@ -87,42 +103,203 @@ JDefinedClass dc = co.implClass; dc._extends(AbstractConfigurableBeanBase.class); + + // set default values + + setDefaultValues(outline); // replace default getters by getters trying the registered providers - for (FieldOutline fo : co.getDeclaredFields()) { + updateGetters(co, dc); + + // modify default setters to notify property change - String fieldName = fo.getPropertyInfo().getName(false); - JType type = fo.getRawType(); - String typeName = type.fullName(); - String getterName = ("java.lang.Boolean".equals(typeName) ? "is" : "get") - + fo.getPropertyInfo().getName(true); + updateSetters(co, dc); + } + return true; + } + + private boolean setDefaultValues(Outline outline) { + for (ClassOutline co : outline.getClasses()) { + for (FieldOutline f : co.getDeclaredFields()) { - // retain existing javadoc, modifiers, type and name - JMethod method = dc.getMethod(getterName, new JType[0]); - JDocComment doc = method.javadoc(); - int mods = method.mods().getValue(); - JType mtype = method.type(); - dc.methods().remove(method); + // Use XML schema object model to determine if field is mapped + // from an element or attribute with a default value + // and get its default value. + + String fieldName = f.getPropertyInfo().getName(false); + XmlString xmlDefaultValue = null; + XSType xsType = null; + + if (f.getPropertyInfo().getSchemaComponent() instanceof XSParticle) { + XSParticle particle = (XSParticle)f.getPropertyInfo().getSchemaComponent(); + XSTerm term = particle.getTerm(); + XSElementDecl element = null; + + if (term.isElementDecl()) { + element = particle.getTerm().asElementDecl(); + xmlDefaultValue = element.getDefaultValue(); + xsType = element.getType(); + } + } else if (f.getPropertyInfo().getSchemaComponent() instanceof XSAttributeUse) { + XSAttributeUse attributeUse = (XSAttributeUse)f.getPropertyInfo().getSchemaComponent(); + XSAttributeDecl decl = attributeUse.getDecl(); + xmlDefaultValue = decl.getDefaultValue(); + xsType = decl.getType(); + } + + if (null == xmlDefaultValue) { + continue; + } + + String defaultValue = xmlDefaultValue.value; - method = dc.method(mods, mtype, getterName); - method.javadoc().append(doc); + if (null == defaultValue) { + continue; + } + + JType type = f.getRawType(); + String typeName = type.fullName(); + + JDefinedClass impl = co.implClass; + Map fields = impl.fields(); + JFieldVar var = fields.get(fieldName); + + if ("java.lang.Boolean".equals(typeName)) { + var.init(JExpr.direct(Boolean.valueOf(defaultValue) ? "Boolean.TRUE" : "Boolean.FALSE")); + } else if ("java.lang.Byte".equals(typeName)) { + var.init(JExpr._new(type) + .arg(JExpr.cast(type.unboxify(), + JExpr.lit(new Byte(Short.valueOf(defaultValue).byteValue()))))); + } else if ("java.lang.Double".equals(typeName)) { + var.init(JExpr._new(type) + .arg(JExpr.lit(new Double(Double.valueOf(defaultValue).doubleValue())))); + } else if ("java.lang.Float".equals(typeName)) { + var.init(JExpr._new(type) + .arg(JExpr.lit(new Float(Float.valueOf(defaultValue).floatValue())))); + } else if ("java.lang.Integer".equals(typeName)) { + var.init(JExpr._new(type) + .arg(JExpr.lit(new Integer(Integer.valueOf(defaultValue).intValue())))); + } else if ("java.lang.Long".equals(typeName)) { + var.init(JExpr._new(type) + .arg(JExpr.lit(new Long(Long.valueOf(defaultValue).longValue())))); + } else if ("java.lang.Short".equals(typeName)) { + var.init(JExpr._new(type) + .arg(JExpr.cast(type.unboxify(), + JExpr.lit(new Short(Short.valueOf(defaultValue).shortValue()))))); + } else if ("java.lang.String".equals(type.fullName())) { + var.init(JExpr.lit(defaultValue)); + } else if ("java.math.BigInteger".equals(type.fullName())) { + var.init(JExpr._new(type).arg(JExpr.lit(defaultValue))); + } else if ("java.math.BigDecimal".equals(type.fullName())) { + var.init(JExpr._new(type).arg(JExpr.lit(defaultValue))); + } else if ("byte[]".equals(type.fullName()) && xsType.isSimpleType()) { + while (!"anySimpleType".equals(xsType.getBaseType().getName())) { + xsType = xsType.getBaseType(); + } + if ("base64Binary".equals(xsType.getName())) { + var.init(outline.getCodeModel().ref(DatatypeConverter.class) + .staticInvoke("parseBase64Binary").arg(defaultValue)); + } else if ("hexBinary".equals(xsType.getName())) { + var.init(JExpr._new(outline.getCodeModel().ref(HexBinaryAdapter.class)) + .invoke("unmarshal").arg(defaultValue)); + } + } else if ("javax.xml.namespace.QName".equals(typeName)) { + NamespaceContext nsc = new NamespaceContextAdapter(xmlDefaultValue); + QName qn = DatatypeConverter.parseQName(xmlDefaultValue.value, nsc); + var.init(JExpr._new(outline.getCodeModel().ref(QName.class)) + .arg(qn.getNamespaceURI()) + .arg(qn.getLocalPart()) + .arg(qn.getPrefix())); + } + // TODO: GregorianCalendar, ... + } + } - JFieldRef fr = JExpr.ref(fieldName); + return true; + } + + private void updateGetters(ClassOutline co, JDefinedClass dc) { + for (FieldOutline fo : co.getDeclaredFields()) { + + String fieldName = fo.getPropertyInfo().getName(false); + JType type = fo.getRawType(); + String typeName = type.fullName(); + + String getterName = ("java.lang.Boolean".equals(typeName) ? "is" : "get") + + fo.getPropertyInfo().getName(true); + + // REVISIT: it seems that for Spring in order to inject a Boolean + // the getter name needs to be get + // so change it here + // getterName = "get" + fo.getPropertyInfo().getName(true); + + // retain existing javadoc, modifiers, and name + + JMethod method = dc.getMethod(getterName, new JType[0]); + JDocComment doc = method.javadoc(); + int mods = method.mods().getValue(); + + // ensure type is always the wrapped type + + JType mtype = method.type(); + + if (mtype.isPrimitive()) { + mtype = mtype.boxify(); + } + + + // remove existing method and define new one + + dc.methods().remove(method); + + method = dc.method(mods, mtype, getterName); + method.javadoc().append(doc); + + JFieldRef fr = JExpr.ref(fieldName); + + + JExpression test; + JConditional jc; + JInvocation invocation; + + invocation = JExpr.invoke("tryOverwrite"); + invocation.arg(JExpr.dotclass(type.boxify())); + invocation.arg(JExpr.lit(fieldName)); + JVar tmp = method.body().decl(type.boxify(), "_" + fieldName, + invocation); + test = JOp.not(JOp.eq(JExpr._null(), tmp)); + jc = method.body()._if(test); + jc._then()._return(tmp); + + test = JOp.eq(JExpr._null(), fr); + jc = method.body()._if(test); + invocation = JExpr.invoke("tryFallback"); + invocation.arg(JExpr.dotclass(type.boxify())); + invocation.arg(JExpr.lit(fieldName)); + jc._then()._return(invocation); + jc._else()._return(fr); - JExpression test = JOp.eq(fr, JExpr._null()); - JConditional jc = method.body()._if(test); - JInvocation invocation = JExpr.invoke("tryProviders"); - invocation.arg(JExpr.dotclass(type.boxify())); + } + } + + private void updateSetters(ClassOutline co, JDefinedClass dc) { + for (FieldOutline fo : co.getDeclaredFields()) { + + String fieldName = fo.getPropertyInfo().getName(false); + JType type = fo.getRawType(); + + String setterName = "set" + fo.getPropertyInfo().getName(true); + + // modify the setter to notify the property change + + JMethod method = dc.getMethod(setterName, new JType[] {type.boxify()}); + if (null != method) { + JInvocation invocation = JExpr.invoke("notifyPropertyChange"); invocation.arg(JExpr.lit(fieldName)); - jc._then()._return(invocation); - jc._else()._return(fr); - + method.body().add(invocation); } - } - - return true; } }