This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/introspection in repository https://gitbox.apache.org/repos/asf/cxf-xjc-utils.git
commit 9f56ba2a2c0e9f784ca6bef0510885481dbc62f6 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Wed Oct 28 08:05:56 2026 +0000 Fix DefaultValuePlugin with -enableIntrospection and report code write errors --- .../apache/cxf/maven_plugin/XSDToJavaRunner.java | 2 +- dv-test/pom.xml | 21 +++++++++++ .../org/apache/cxf/xjc/dv/IntrospectionTest.java | 43 ++++++++++++++++++++++ .../schemas/configuration/introspection.xsd | 32 ++++++++++++++++ .../org/apache/cxf/xjc/dv/DefaultValuePlugin.java | 27 ++++++++------ 5 files changed, 113 insertions(+), 12 deletions(-) diff --git a/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/XSDToJavaRunner.java b/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/XSDToJavaRunner.java index 1b4f452..15d2386 100644 --- a/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/XSDToJavaRunner.java +++ b/cxf-xjc-plugin/src/main/java/org/apache/cxf/maven_plugin/XSDToJavaRunner.java @@ -272,7 +272,7 @@ public class XSDToJavaRunner { CodeWriter cw = opt.createCodeWriter(); model.codeModel.build(cw); } catch (IOException e) { - listener.error(e); + listener.error("Failed to write generated code", e); return -1; } } diff --git a/dv-test/pom.xml b/dv-test/pom.xml index ec7f5d9..71c7f87 100644 --- a/dv-test/pom.xml +++ b/dv-test/pom.xml @@ -122,6 +122,27 @@ </extensions> </configuration> </execution> + <execution> + <id>introspection</id> + <goals> + <goal>xsdtojava-tests</goal> + </goals> + <configuration> + <xsdOptions> + <xsdOption> + <extension>true</extension> + <xsd>${basedir}/src/test/resources/schemas/configuration/introspection.xsd</xsd> + <extensionArgs> + <arg>-Xdv</arg> + <arg>-enableIntrospection</arg> + </extensionArgs> + </xsdOption> + </xsdOptions> + <extensions> + <extension>org.apache.cxf.xjcplugins:cxf-xjc-dv:${project.version}</extension> + </extensions> + </configuration> + </execution> </executions> </plugin> </plugins> diff --git a/dv-test/src/test/java/org/apache/cxf/xjc/dv/IntrospectionTest.java b/dv-test/src/test/java/org/apache/cxf/xjc/dv/IntrospectionTest.java new file mode 100644 index 0000000..9dd2b6b --- /dev/null +++ b/dv-test/src/test/java/org/apache/cxf/xjc/dv/IntrospectionTest.java @@ -0,0 +1,43 @@ +/** + * 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.dv; + +import org.apache.cxf.configuration.introspection.Flags; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Default values with -enableIntrospection, where the getter of a Boolean is getXXX(). + */ +public class IntrospectionTest extends Assert { + + @Test + public void testBooleanDefaults() { + Flags flags = new Flags(); + assertTrue(flags.getBooleanElem()); + assertTrue(flags.getBooleanAttr()); + + flags.setBooleanElem(false); + flags.setBooleanAttr(false); + assertFalse(flags.getBooleanElem()); + assertFalse(flags.getBooleanAttr()); + } +} diff --git a/dv-test/src/test/resources/schemas/configuration/introspection.xsd b/dv-test/src/test/resources/schemas/configuration/introspection.xsd new file mode 100644 index 0000000..0b2ffbf --- /dev/null +++ b/dv-test/src/test/resources/schemas/configuration/introspection.xsd @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<!-- + Boolean defaults compiled with -Xdv -enableIntrospection, which names the getters getXXX() + rather than isXXX(). +--> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" + targetNamespace="http://cxf.apache.org/configuration/introspection" elementFormDefault="qualified"> + <xs:complexType name="flags"> + <xs:sequence> + <xs:element name="booleanElem" type="xs:boolean" default="true" minOccurs="0"/> + </xs:sequence> + <xs:attribute name="booleanAttr" type="xs:boolean" default="true"/> + </xs:complexType> +</xs:schema> diff --git a/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java b/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java index f1ec49b..c222e8e 100644 --- a/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java +++ b/dv/src/main/java/org/apache/cxf/xjc/dv/DefaultValuePlugin.java @@ -513,13 +513,9 @@ public class DefaultValuePlugin { boolean remapRet) { 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); - JMethod method = dc.getMethod(getterName, new JType[0]); + JMethod method = getGetter(fo, dc); + String getterName = method.name(); JDocComment doc = method.javadoc(); int mods = method.mods().getValue(); JType mtype = method.type(); @@ -544,16 +540,25 @@ public class DefaultValuePlugin { } method.body()._return(fr); } + /* + * The getter of a Boolean is isXXX(), except with -enableIntrospection, where it is getXXX() + */ + private static JMethod getGetter(FieldOutline fo, JDefinedClass dc) { + String name = fo.getPropertyInfo().getName(true); + boolean isBoolean = "java.lang.Boolean".equals(fo.getRawType().fullName()); + JMethod method = dc.getMethod((isBoolean ? "is" : "get") + name, new JType[0]); + if (method == null && isBoolean) { + method = dc.getMethod("get" + name, new JType[0]); + } + return method; + } + private void updateSetter(ClassOutline co, FieldOutline fo, JDefinedClass dc) { 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); - JMethod method = dc.getMethod(getterName, new JType[0]); + JMethod method = getGetter(fo, dc); JType mtype = method.type(); String setterName = "set" + fo.getPropertyInfo().getName(true); method = dc.getMethod(setterName, new JType[] {mtype});
