Skip to content
Permalink
Browse files
Create an XJC plugin that can work around https://java.net/jira/brows…
  • Loading branch information
dkulp committed Sep 12, 2014
1 parent 718f8af commit 6ae96ffedb82f01c3dab3f76841a10395a5595d1
Showing 5 changed files with 280 additions and 0 deletions.
@@ -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>
@@ -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);
}

}
@@ -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;
}
}
@@ -0,0 +1 @@
com.sun.tools.xjc.addon.apache_cxf.bug986.Bug986Plugin
51 pom.xml
@@ -65,6 +65,7 @@
<module>wsdlextension</module>
<module>wsdlextension-test</module>
<module>bug671</module>
<module>bug986</module>
<module>property-listener</module>
<module>javadoc</module>
<module>runtime</module>
@@ -442,6 +443,56 @@
</execution>
</executions>
</plugin>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-checkstyle-plugin
</artifactId>
<versionRange>
[2.9.1,)
</versionRange>
<goals>
<goal>checkstyle</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-pmd-plugin
</artifactId>
<versionRange>
[3.0.1,)
</versionRange>
<goals>
<goal>check</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

0 comments on commit 6ae96ff

Please sign in to comment.