You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Brian Hayward <bh...@gmail.com> on 2011/04/13 07:35:18 UTC

ClassName "is already defined in" during compilation after code generation.

I'm trying to use an existing wsdl published by someone else and am
having compilation issues when using the CXF maven plugin (2.3.3) when
a child element has the same as a parent.

Example scenario:

                           <xs:schema>
                                   <xs:element name="responseClass">
                                       <xs:complexType>
                                            <xs:sequence>
                                                <xs:element
minOccurs="0" name="responseData">
                                                        <xs:complexType>
                                                                <xs:sequence>

 <xs:element maxOccurs="unbounded" name="data">

         <xs:complexType>

                 <xs:sequence>

                         <xs:element name="data">

                                 <xs:complexType>
.....etc


It will generate a java file named: "ResponseClass.java"
The class structure will be:
public class ResponseClass
    |
    --> public static class ResponseData
            |
            --> public static class Data
                     |
                     --> public static class Data

This will fail to compile with the following error:

ResponseClass.java:[652,26]
com.mypackage.ResponseClass.ResponseData.Data is already defined in
com.mypackage.ResponseClass.ResponseData

Renaming the second (child) "data" element to something else fixes the problem.

Any thoughts or work-arounds?

Thanks,
Brian

Re: ClassName "is already defined in" during compilation after code generation.

Posted by Aki Yoshida <el...@googlemail.com>.
Hi,
You can resolve this problem by creating a custom binding
configuration to change the class name.

For example, you create mybinding.xml file that looks like:

<jaxws:bindings version="1.0"
  xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"  >
  <jaxws:bindings wsdlLocation="mywsdl.wsdl">
    <jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema">
      <jaxb:bindings
node="xs:element[@name='responseClass']/xs:complexType/xs:sequence/xs:element[@name='responseData']/xs:complexType/xs:sequence/xs:element[@name='data']/xs:complexType/xs:sequence/xs:element[@name='data']/xs:complexType">
        <jaxb:class name="SecondData"/>
      </jaxb:bindings>
    </jaxws:bindings>
  </jaxws:bindings>
</jaxws:bindings>

Basically, the idea is to match the complex type associated with the
second data element and rename its associated class to SecondData.

When you use this binding file to generate classes from your wsdl
file, you will resolve the naming conflict .

For example, if you are using cxf-codegen-plugin, you can add the
bindingFiles entry in your pom as:
    ...
   <wsdlOption>
     <wsdl>${basedir}/...../mywsdl.wsdl</wsdl>
     <bindingFiles>
      <bindingFile>${basedir}/..../mybinding.xml</bindingFile>
      </bindingFiles>

Alternatively, if you can modify the wsdl/schema itself, you can make
one of the inner types to a top-level type with a distinct name. In
this case, you won't need the binding file to change the class name.

Regards, Aki

2011/4/13 Brian Hayward <bh...@gmail.com>:
> I'm trying to use an existing wsdl published by someone else and am
> having compilation issues when using the CXF maven plugin (2.3.3) when
> a child element has the same as a parent.
>
> Example scenario:
>
>                           <xs:schema>
>                                   <xs:element name="responseClass">
>                                       <xs:complexType>
>                                            <xs:sequence>
>                                                <xs:element
> minOccurs="0" name="responseData">
>                                                        <xs:complexType>
>                                                                <xs:sequence>
>
>  <xs:element maxOccurs="unbounded" name="data">
>
>         <xs:complexType>
>
>                 <xs:sequence>
>
>                         <xs:element name="data">
>
>                                 <xs:complexType>
> .....etc
>
>
> It will generate a java file named: "ResponseClass.java"
> The class structure will be:
> public class ResponseClass
>    |
>    --> public static class ResponseData
>            |
>            --> public static class Data
>                     |
>                     --> public static class Data
>
> This will fail to compile with the following error:
>
> ResponseClass.java:[652,26]
> com.mypackage.ResponseClass.ResponseData.Data is already defined in
> com.mypackage.ResponseClass.ResponseData
>
> Renaming the second (child) "data" element to something else fixes the problem.
>
> Any thoughts or work-arounds?
>
> Thanks,
> Brian
>