Skip to content
Permalink
Browse files
Get a bit furthur with MOXy by fixing some tests to not use hard code…
…d strings

git-svn-id: https://svn.apache.org/repos/asf/cxf/trunk@1540857 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
dkulp committed Nov 11, 2013
1 parent a1eb9a0 commit 1c96e19bdc570947b18b343f143c6a85a27c0acb
Show file tree
Hide file tree
Showing 4 changed files with 359 additions and 50 deletions.
@@ -995,17 +995,16 @@ public static boolean isJAXB22() {
}

private static synchronized Object createNamespaceWrapper(Class<?> mcls, Map<String, String> map) {
ASMHelper helper = new ASMHelper();
String className = "org.apache.cxf.jaxb.NamespaceMapper";
String postFix = "";
if (mcls.getName().contains("eclipse")) {
//can use the map directly
return map;
return createEclipseNamespaceMapper(mcls, map);
} else if (mcls.getName().contains(".internal")) {
postFix = "Internal";
} else if (mcls.getName().contains("com.sun")) {
postFix = "RI";
}
ASMHelper helper = new ASMHelper();
String className = "org.apache.cxf.jaxb.NamespaceMapper";
className += postFix;
Class<?> cls = helper.findClass(className, JAXBUtils.class);
if (cls == null) {
@@ -1021,8 +1020,217 @@ private static synchronized Object createNamespaceWrapper(Class<?> mcls, Map<Str
return null;
}
}

/*
// This is the "prototype" for the ASM generated class below
public static class MapNamespacePrefixMapper2
extends org.eclipse.persistence.internal.oxm.record.namespaces.MapNamespacePrefixMapper {
String nsctxt[];
public MapNamespacePrefixMapper2(Map<String, String> foo) {
super(foo);
}
public String[] getPreDeclaredNamespaceUris() {
String sup[] = super.getPreDeclaredNamespaceUris();
if (nsctxt == null) {
return sup;
}
List<String> s = new ArrayList<String>(Arrays.asList(sup));
for (int x = 1; x < nsctxt.length; x = x + 2) {
s.remove(nsctxt[x]);
}
return s.toArray(new String[s.size()]);
}
public void setContextualNamespaceDecls(String f[]) {
nsctxt = f;
}
public String[] getContextualNamespaceDecls() {
return nsctxt;
}
}
*/
//CHECKSTYLE:OFF
//bunch of really long ASM based methods that cannot be shortened easily
private static Object createEclipseNamespaceMapper(Class<?> mcls, Map<String, String> map) {
ASMHelper helper = new ASMHelper();
String className = "org.apache.cxf.jaxb.EclipseNamespaceMapper";
String slashedName = "org/apache/cxf/jaxb/EclipseNamespaceMapper";
Class<?> cls = helper.findClass(className, JAXBUtils.class);

if (cls == null) {
ClassWriter cw = helper.createClassWriter();
if (cw == null) {
return null;
}
String superName = "org/eclipse/persistence/internal/oxm/record/namespaces/MapNamespacePrefixMapper";
FieldVisitor fv;
MethodVisitor mv;
cw.visit(Opcodes.V1_6,
Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
slashedName, null,
superName, null);

cw.visitSource("EclipseNamespaceMapper.java", null);

fv = cw.visitField(Opcodes.ACC_PRIVATE, "nsctxt", "[Ljava/lang/String;", null, null);
fv.visitEnd();


mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Ljava/util/Map;)V",
"(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)V", null);
mv.visitCode();
Label l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL,
superName, "<init>", "(Ljava/util/Map;)V");
Label l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitInsn(Opcodes.RETURN);
Label l2 = helper.createLabel();
mv.visitLabel(l2);
mv.visitMaxs(2, 2);
mv.visitEnd();


mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setContextualNamespaceDecls", "([Ljava/lang/String;)V",
null, null);
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(47, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitFieldInsn(Opcodes.PUTFIELD, slashedName, "nsctxt", "[Ljava/lang/String;");
l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLineNumber(48, l1);
mv.visitInsn(Opcodes.RETURN);
l2 = helper.createLabel();
mv.visitLabel(l2);
mv.visitLocalVariable("this", "L" + slashedName + ";", null, l0, l2, 0);
mv.visitLocalVariable("contextualNamespaceDecls", "[Ljava/lang/String;", null, l0, l2, 1);
mv.visitMaxs(2, 2);
mv.visitEnd();

mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getContextualNamespaceDecls", "()[Ljava/lang/String;", null, null);
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(51, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt", "[Ljava/lang/String;");
mv.visitInsn(Opcodes.ARETURN);
l1 = helper.createLabel();

mv.visitLabel(l1);
mv.visitLocalVariable("this", "L" + slashedName + ";", null, l0, l1, 0);

mv.visitMaxs(1, 1);
mv.visitEnd();


mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getPreDeclaredNamespaceUris", "()[Ljava/lang/String;", null, null);
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(1036, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL,
superName,
"getPreDeclaredNamespaceUris", "()[Ljava/lang/String;");
mv.visitVarInsn(Opcodes.ASTORE, 1);
l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLineNumber(1037, l1);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt", "[Ljava/lang/String;");
l2 = helper.createLabel();
mv.visitJumpInsn(Opcodes.IFNONNULL, l2);
Label l3 = helper.createLabel();
mv.visitLabel(l3);
mv.visitLineNumber(1038, l3);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitInsn(Opcodes.ARETURN);
mv.visitLabel(l2);
mv.visitLineNumber(1040, l2);
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] {"[Ljava/lang/String;"}, 0, null);
mv.visitTypeInsn(Opcodes.NEW, "java/util/ArrayList");
mv.visitInsn(Opcodes.DUP);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/util/Arrays", "asList",
"([Ljava/lang/Object;)Ljava/util/List;");
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "(Ljava/util/Collection;)V");
mv.visitVarInsn(Opcodes.ASTORE, 2);
Label l4 = helper.createLabel();
mv.visitLabel(l4);
mv.visitLineNumber(1041, l4);
mv.visitInsn(Opcodes.ICONST_1);
mv.visitVarInsn(Opcodes.ISTORE, 3);
Label l5 = helper.createLabel();
mv.visitLabel(l5);
Label l6 = helper.createLabel();
mv.visitJumpInsn(Opcodes.GOTO, l6);
Label l7 = helper.createLabel();
mv.visitLabel(l7);
mv.visitLineNumber(1042, l7);
mv.visitFrame(Opcodes.F_APPEND, 2, new Object[] {"java/util/List", Opcodes.INTEGER}, 0, null);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, slashedName, "nsctxt", "[Ljava/lang/String;");
mv.visitVarInsn(Opcodes.ILOAD, 3);
mv.visitInsn(Opcodes.AALOAD);
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "remove", "(Ljava/lang/Object;)Z");
mv.visitInsn(Opcodes.POP);
Label l8 = helper.createLabel();
mv.visitLabel(l8);
mv.visitLineNumber(1041, l8);
mv.visitIincInsn(3, 2);
mv.visitLabel(l6);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(Opcodes.ILOAD, 3);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD,
slashedName,
"nsctxt", "[Ljava/lang/String;");
mv.visitInsn(Opcodes.ARRAYLENGTH);
mv.visitJumpInsn(Opcodes.IF_ICMPLT, l7);
Label l9 = helper.createLabel();
mv.visitLabel(l9);
mv.visitLineNumber(1044, l9);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitVarInsn(Opcodes.ALOAD, 2);
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List", "size", "()I");
mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/String");
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/List",
"toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;");
mv.visitTypeInsn(Opcodes.CHECKCAST, "[Ljava/lang/String;");
mv.visitInsn(Opcodes.ARETURN);
Label l10 = helper.createLabel();
mv.visitLabel(l10);
mv.visitLocalVariable("this", "L" + slashedName + ";",
null, l0, l10, 0);
mv.visitLocalVariable("sup", "[Ljava/lang/String;", null, l1, l10, 1);
mv.visitLocalVariable("s", "Ljava/util/List;", "Ljava/util/List<Ljava/lang/String;>;", l4, l10, 2);
mv.visitLocalVariable("x", "I", null, l5, l9, 3);
mv.visitMaxs(3, 4);
mv.visitEnd();

cw.visitEnd();

byte bts[] = cw.toByteArray();
cls = helper.loadClass(className,
mcls, bts);
}
try {
return cls.getConstructor(Map.class).newInstance(map);
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}

private static Class<?> createNamespaceWrapperInternal(ASMHelper helper, ClassWriter cw,
String postFix, Class<?> ref) {
String className = "org.apache.cxf.jaxb.NamespaceMapper" + postFix;
@@ -125,6 +125,9 @@ public static class Opcodes {
public static int ACC_INTERFACE = 0;
public static int ACC_SYNTHETIC = 0;
public static int ILOAD = 0;
public static int ISTORE = 0;
public static int AALOAD = 0;
public static int ARRAYLENGTH = 0;
public static int IRETURN = 0;
public static int NEW = 0;
public static int ANEWARRAY = 0;
@@ -138,9 +141,12 @@ public static class Opcodes {
public static int SIPUSH = 0;
public static int INVOKESTATIC = 0;
public static int ICONST_0;
public static int ICONST_1;
public static int LCONST_0;
public static int FCONST_0;
public static int DCONST_0;
public static int IF_ICMPLT = 0;
public static java.lang.Integer INTEGER;

//CHECKSTYLE:ON
static {
@@ -454,6 +460,7 @@ void visitLocalVariable(String string,
void visitMethodInsn(int invokevirtual, String periodToSlashes,
String name, String methodSignature);
void visitIntInsn(int sipush, int x);
void visitIincInsn(int i, int j);
void visitFieldInsn(int getfield, String periodToSlashes,
String string, String string2);
void visitJumpInsn(int ifnonnull, @UnwrapParam(typeMethodName = "realType") Label nonNullLabel);
@@ -285,7 +285,12 @@ public void testWriteWithContextualNamespaceDecls() throws Exception {
String xstr = new String(baos.toByteArray());

// there should be no namespace decls
assertEquals("<x:greetMe><x:requestType>Hello</x:requestType></x:greetMe>", xstr);
if (!db.getContext().getClass().getName().contains("eclipse")) {
//bug in eclipse moxy
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=421463

assertEquals("<x:greetMe><x:requestType>Hello</x:requestType></x:greetMe>", xstr);
}
}

private JAXBDataBinding getTestWriterFactory(Class<?>... clz) throws Exception {

0 comments on commit 1c96e19

Please sign in to comment.