This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/boolean in repository https://gitbox.apache.org/repos/asf/cxf-xjc-utils.git
commit 830176ec78c466e913311afd9c91892f11232405 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Wed Oct 14 06:06:40 2026 +0100 Avoid duplicate getters in BooleanGetterPlugin --- boolean-test/pom.xml | 7 ++++ .../apache/cxf/xjc/bg/BooleanGetterPluginTest.java | 23 ++++++++++++ .../test/resources/schemas/configuration/foo2.xsd | 42 ++++++++++++++++++++++ .../org/apache/cxf/xjc/bg/BooleanGetterPlugin.java | 22 ++++++++++-- 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/boolean-test/pom.xml b/boolean-test/pom.xml index 0e7449c..35de374 100644 --- a/boolean-test/pom.xml +++ b/boolean-test/pom.xml @@ -73,6 +73,13 @@ <arg>-Xbg</arg> </extensionArgs> </xsdOption> + <xsdOption> + <extension>true</extension> + <xsd>${basedir}/src/test/resources/schemas/configuration/foo2.xsd</xsd> + <extensionArgs> + <arg>-Xbg</arg> + </extensionArgs> + </xsdOption> <xsdOption> <extension>true</extension> <xsd>${basedir}/src/test/resources/schemas/configuration/foo1.xsd</xsd> diff --git a/boolean-test/src/test/java/org/apache/cxf/xjc/bg/BooleanGetterPluginTest.java b/boolean-test/src/test/java/org/apache/cxf/xjc/bg/BooleanGetterPluginTest.java index 16ad608..38a5e01 100644 --- a/boolean-test/src/test/java/org/apache/cxf/xjc/bg/BooleanGetterPluginTest.java +++ b/boolean-test/src/test/java/org/apache/cxf/xjc/bg/BooleanGetterPluginTest.java @@ -19,7 +19,9 @@ package org.apache.cxf.xjc.bg; import org.apache.cxf.configuration.foo.Foo; +import org.apache.cxf.configuration.foo2.Toggle; +import org.junit.Assert; import org.junit.Test; public class BooleanGetterPluginTest { @@ -32,4 +34,25 @@ public class BooleanGetterPluginTest { foo.getBooleanAttr(); } + @Test + public void testExistingGetterIsNotDuplicated() throws Exception { + // Toggle only compiles if isSetFlag() was not renamed to clash with getSetFlag() + Toggle toggle = new org.apache.cxf.configuration.foo2.ObjectFactory().createToggle(); + toggle.setFlag(Boolean.TRUE); + toggle.setSetFlag("text"); + + Assert.assertEquals(Boolean.TRUE, toggle.getFlag()); + Assert.assertEquals("text", toggle.getSetFlag()); + Assert.assertTrue(toggle.isSetFlag()); + Assert.assertTrue(toggle.getSetSetFlag()); + Assert.assertEquals(String.class, Toggle.class.getMethod("getSetFlag").getReturnType()); + Assert.assertNotNull(Toggle.class.getMethod("get1St")); + try { + Toggle.class.getMethod("isFlag"); + Assert.fail("isFlag() should have been renamed"); + } catch (NoSuchMethodException e) { + // expected + } + } + } diff --git a/boolean-test/src/test/resources/schemas/configuration/foo2.xsd b/boolean-test/src/test/resources/schemas/configuration/foo2.xsd new file mode 100644 index 0000000..1b1f0b6 --- /dev/null +++ b/boolean-test/src/test/resources/schemas/configuration/foo2.xsd @@ -0,0 +1,42 @@ +<?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. +--> +<!-- isSetFlag() would be renamed to getSetFlag(), clashing with the getter of setFlag --> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb" + xmlns:tns="http://cxf.apache.org/configuration/foo2" targetNamespace="http://cxf.apache.org/configuration/foo2" + elementFormDefault="qualified" jaxb:version="3.0"> + + <xs:annotation> + <xs:appinfo> + <jaxb:globalBindings generateIsSetMethod="true"/> + </xs:appinfo> + </xs:annotation> + + <xs:element name="toggle" type="tns:toggle"/> + + <xs:complexType name="toggle"> + <xs:sequence> + <xs:element name="flag" type="xs:boolean" minOccurs="0"/> + <xs:element name="setFlag" type="xs:string" minOccurs="0"/> + <!-- getter is is1St() --> + <xs:element name="_1st" type="xs:boolean" minOccurs="0"/> + </xs:sequence> + </xs:complexType> + +</xs:schema> diff --git a/boolean/src/main/java/org/apache/cxf/xjc/bg/BooleanGetterPlugin.java b/boolean/src/main/java/org/apache/cxf/xjc/bg/BooleanGetterPlugin.java index 1602872..a8b4335 100644 --- a/boolean/src/main/java/org/apache/cxf/xjc/bg/BooleanGetterPlugin.java +++ b/boolean/src/main/java/org/apache/cxf/xjc/bg/BooleanGetterPlugin.java @@ -23,7 +23,9 @@ import java.util.logging.Logger; import org.xml.sax.ErrorHandler; +import com.sun.codemodel.JDefinedClass; import com.sun.codemodel.JMethod; +import com.sun.codemodel.JType; import com.sun.tools.xjc.Options; import com.sun.tools.xjc.outline.ClassOutline; import com.sun.tools.xjc.outline.Outline; @@ -51,10 +53,16 @@ public class BooleanGetterPlugin { public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) { LOG.info("Running boolean getter plugin."); for (ClassOutline classOutline : outline.getClasses()) { - Collection<JMethod> methods = classOutline.implClass.methods(); + JDefinedClass implClass = classOutline.implClass; + Collection<JMethod> methods = implClass.methods(); for (JMethod method : methods) { - if (method.name().startsWith("is")) { + if (isBooleanGetter(method)) { String newName = "get" + method.name().substring(2); + if (implClass.getMethod(newName, new JType[0]) != null) { + LOG.warning("Not renaming " + implClass.fullName() + "." + method.name() + + "() as " + newName + "() already exists"); + continue; + } LOG.info("Changing method name from " + method.name() + " to " + newName); method.javadoc().add( "\nThis getter has been renamed from " + method.name() + "() to " @@ -66,4 +74,14 @@ public class BooleanGetterPlugin { return true; } + private static boolean isBooleanGetter(JMethod method) { + String name = method.name(); + if (!name.startsWith("is") || name.length() > 2 && Character.isLowerCase(name.charAt(2)) + || !method.params().isEmpty() || method.hasVarArgs()) { + return false; + } + String type = method.type().fullName(); + return "boolean".equals(type) || "java.lang.Boolean".equals(type); + } + }
