Repository: cxf-xjc-utils Updated Branches: refs/heads/master 85252a559 -> 7d4b4fd5c
Update the plugin to work with older JAXB xjc's Project: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/commit/7d4b4fd5 Tree: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/tree/7d4b4fd5 Diff: http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/diff/7d4b4fd5 Branch: refs/heads/master Commit: 7d4b4fd5cc55fb2a3df39b45149b4d0c612582cb Parents: 85252a5 Author: Daniel Kulp <[email protected]> Authored: Tue Sep 16 13:22:00 2014 -0400 Committer: Daniel Kulp <[email protected]> Committed: Tue Sep 16 13:22:00 2014 -0400 ---------------------------------------------------------------------- .../org/apache/cxf/xjc/bug986/Bug986Plugin.java | 64 ++++++++++++++++---- 1 file changed, 52 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf-xjc-utils/blob/7d4b4fd5/bug986/src/main/java/org/apache/cxf/xjc/bug986/Bug986Plugin.java ---------------------------------------------------------------------- diff --git a/bug986/src/main/java/org/apache/cxf/xjc/bug986/Bug986Plugin.java b/bug986/src/main/java/org/apache/cxf/xjc/bug986/Bug986Plugin.java index 676308f..7f48ea5 100644 --- a/bug986/src/main/java/org/apache/cxf/xjc/bug986/Bug986Plugin.java +++ b/bug986/src/main/java/org/apache/cxf/xjc/bug986/Bug986Plugin.java @@ -24,6 +24,7 @@ import java.io.StringWriter; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.logging.Logger; @@ -34,6 +35,7 @@ import org.xml.sax.ErrorHandler; import com.sun.codemodel.JAnnotationUse; import com.sun.codemodel.JAnnotationValue; +import com.sun.codemodel.JClass; import com.sun.codemodel.JFieldVar; import com.sun.codemodel.JFormatter; import com.sun.codemodel.JVar; @@ -72,11 +74,11 @@ public class Bug986Plugin { for (ClassOutline classOutline : outline.getClasses()) { Map<String, JFieldVar> fields = classOutline.implClass.fields(); for (JFieldVar field : fields.values()) { - Collection<JAnnotationUse> annotations = field.annotations(); + Collection<JAnnotationUse> annotations = getAnnotations(field); List<JAnnotationUse> toRemove = new ArrayList<JAnnotationUse>(); for (JAnnotationUse j : annotations) { - if (XmlSchemaType.class.getName().equals(j.getAnnotationClass().fullName())) { - JAnnotationValue st = j.getAnnotationMembers().get("name"); + if (XmlSchemaType.class.getName().equals(getAnnotationClass(j).fullName())) { + JAnnotationValue st = getAnnotationMember(j, "name"); StringWriter sw = new StringWriter(); st.generate(new JFormatter(sw)); if (sw.toString().equals("\"anySimpleType\"") @@ -87,18 +89,56 @@ public class Bug986Plugin { } } 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(); - } + annotations.remove(j); } } } return true; } + + private JAnnotationValue getAnnotationMember(JAnnotationUse ju, String name) { + try { + Field f = JAnnotationUse.class.getDeclaredField("memberValues"); + f.setAccessible(true); + @SuppressWarnings("unchecked") + Map<String, JAnnotationValue> memberValues = (Map<String, JAnnotationValue>)f.get(ju); + if (memberValues == null) { + return null; + } + return memberValues.get(name); + } catch (Throwable t) { + //ignore for now + t.printStackTrace(); + } + return null; + } + + private JClass getAnnotationClass(JAnnotationUse ju) { + try { + Field f = JAnnotationUse.class.getDeclaredField("clazz"); + f.setAccessible(true); + return (JClass)f.get(ju); + } catch (Throwable t) { + //ignore for now + t.printStackTrace(); + } + return null; + } + + @SuppressWarnings("unchecked") + private List<JAnnotationUse> getAnnotations(JFieldVar field) { + try { + Field f = JVar.class.getDeclaredField("annotations"); + f.setAccessible(true); + List<?> anns = (List<?>)f.get(field); + if (anns == null) { + anns = Collections.emptyList(); + } + return (List<JAnnotationUse>)anns; + } catch (Throwable t) { + //ignore for now + t.printStackTrace(); + } + return Collections.emptyList(); + } }
