Skip to content
Permalink
Browse files
Update the plugin to work with older JAXB xjc's
  • Loading branch information
dkulp committed Sep 16, 2014
1 parent 85252a5 commit 7d4b4fd5cc55fb2a3df39b45149b4d0c612582cb
Showing 1 changed file with 52 additions and 12 deletions.
@@ -24,6 +24,7 @@
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 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 boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
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 boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
}
}
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();
}
}

0 comments on commit 7d4b4fd

Please sign in to comment.