Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Create an XJC plugin that can work around https://java.net/jira/brows…
- Loading branch information
Showing
5 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<!-- | ||
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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.apache.cxf.xjcplugins</groupId> | ||
<artifactId>cxf-xjc-bug986</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Apache CXF XJC Plugin To Workaround JAXB Bug 986</name> | ||
<url>http://cxf.apache.org</url> | ||
|
||
<parent> | ||
<groupId>org.apache.cxf.xjc-utils</groupId> | ||
<artifactId>xjc-utils</artifactId> | ||
<version>3.0.2-SNAPSHOT</version> | ||
</parent> | ||
|
||
|
||
<properties> | ||
<maven.test.skip>true</maven.test.skip> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.sun.xml.bind</groupId> | ||
<artifactId>jaxb-xjc</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.xml.bind</groupId> | ||
<artifactId>jaxb-core</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.sun.xml.bind</groupId> | ||
<artifactId>jaxb-impl</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-lang</groupId> | ||
<artifactId>commons-lang</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* 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 com.sun.tools.xjc.addon.apache_cxf.bug986; | ||
|
||
import org.xml.sax.ErrorHandler; | ||
import org.xml.sax.SAXException; | ||
|
||
import com.sun.tools.xjc.BadCommandLineException; | ||
import com.sun.tools.xjc.Options; | ||
import com.sun.tools.xjc.Plugin; | ||
import com.sun.tools.xjc.outline.Outline; | ||
|
||
/** | ||
* Thin wrapper around the Bug986Plugin. This must be in the com.sun.tools.xjc.addon package | ||
* for it to work with Java 6. See https://issues.apache.org/jira/browse/CXF-1880. | ||
*/ | ||
public class Bug986Plugin extends Plugin { | ||
|
||
org.apache.cxf.xjc.bug986.Bug986Plugin impl = new org.apache.cxf.xjc.bug986.Bug986Plugin(this); | ||
|
||
/* (non-Javadoc) | ||
* @see com.sun.tools.xjc.Plugin#getOptionName() | ||
*/ | ||
@Override | ||
public String getOptionName() { | ||
return impl.getOptionName(); | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see com.sun.tools.xjc.Plugin#getUsage() | ||
*/ | ||
@Override | ||
public String getUsage() { | ||
return impl.getUsage(); | ||
} | ||
public void onActivated(Options opts) throws BadCommandLineException { | ||
|
||
} | ||
/* (non-Javadoc) | ||
* @see com.sun.tools.xjc.Plugin#run(com.sun.tools.xjc.outline.Outline, | ||
* com.sun.tools.xjc.Options, org.xml.sax.ErrorHandler) | ||
*/ | ||
@Override | ||
public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) throws SAXException { | ||
return impl.run(outline, opt, errorHandler); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/** | ||
* 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.bug986; | ||
|
||
|
||
import java.io.StringWriter; | ||
import java.lang.reflect.Field; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Logger; | ||
|
||
import javax.xml.bind.annotation.XmlSchemaType; | ||
|
||
import org.xml.sax.ErrorHandler; | ||
|
||
import com.sun.codemodel.JAnnotationUse; | ||
import com.sun.codemodel.JAnnotationValue; | ||
import com.sun.codemodel.JFieldVar; | ||
import com.sun.codemodel.JFormatter; | ||
import com.sun.codemodel.JVar; | ||
import com.sun.tools.xjc.Options; | ||
import com.sun.tools.xjc.Plugin; | ||
import com.sun.tools.xjc.outline.ClassOutline; | ||
import com.sun.tools.xjc.outline.Outline; | ||
|
||
/** | ||
* Modifies the JAXB code model to handle package naming that run into: | ||
* https://jaxb.dev.java.net/issues/show_bug.cgi?id=671 | ||
*/ | ||
public class Bug986Plugin { | ||
private static final Logger LOG = Logger.getLogger(Bug986Plugin.class.getName()); //NOPMD | ||
|
||
final Plugin plugin; | ||
|
||
public Bug986Plugin(Plugin p) { | ||
plugin = p; | ||
} | ||
|
||
|
||
public String getOptionName() { | ||
return "Xbug986"; | ||
} | ||
|
||
public String getUsage() { | ||
return " -Xbug986 : Activate plugin remove XmlSchemaType(anySimpleType) from fields that shouldn't have it."; | ||
} | ||
|
||
public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) { | ||
// kind of a bogus thing to have to do to workaround bug: | ||
// https://java.net/jira/browse/JAXB-986 | ||
LOG.info("Running Bug986Plugin plugin."); | ||
for (ClassOutline classOutline : outline.getClasses()) { | ||
Map<String, JFieldVar> fields = classOutline.implClass.fields(); | ||
for (JFieldVar field : fields.values()) { | ||
Collection<JAnnotationUse> annotations = field.annotations(); | ||
List<JAnnotationUse> toRemove = new ArrayList<JAnnotationUse>(); | ||
for (JAnnotationUse j : annotations) { | ||
if (XmlSchemaType.class.getName().equals(j.getAnnotationClass().fullName())) { | ||
JAnnotationValue st = j.getAnnotationMembers().get("name"); | ||
StringWriter sw = new StringWriter(); | ||
st.generate(new JFormatter(sw)); | ||
if (sw.toString().equals("\"anySimpleType\"") | ||
&& field.type().fullName().startsWith("java.util.List") | ||
&& !field.type().fullName().contains("<java.lang.String>")) { | ||
toRemove.add(j); | ||
} | ||
} | ||
} | ||
for (JAnnotationUse j : toRemove) { | ||
try { | ||
Field f = JVar.class.getDeclaredField("annotations"); | ||
f.setAccessible(true); | ||
List<?> anns = (List<?>)f.get(field); | ||
anns.remove(j); | ||
} catch (Throwable t) { | ||
//ignore for now | ||
t.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.sun.tools.xjc.addon.apache_cxf.bug986.Bug986Plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters