use of org.apache.cxf.common.util.ASMHelper in project cxf by apache.
the class JAXBContextInitializer method createFactory.
@SuppressWarnings("unused")
private Object createFactory(Class<?> cls, Constructor<?> contructor) {
String newClassName = cls.getName() + "Factory";
ASMHelper helper = new ASMHelper();
ClassWriter cw = helper.createClassWriter();
MethodVisitor mv;
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, ASMHelper.periodToSlashes(newClassName), null, "java/lang/Object", null);
cw.visitSource(cls.getSimpleName() + "Factory" + ".java", null);
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "create" + cls.getSimpleName(), "()L" + ASMHelper.periodToSlashes(cls.getName()) + ";", null, null);
mv.visitCode();
String name = cls.getName().replace(".", "/");
mv.visitTypeInsn(Opcodes.NEW, name);
mv.visitInsn(Opcodes.DUP);
StringBuilder paraString = new StringBuilder("(");
for (Class<?> paraClass : contructor.getParameterTypes()) {
mv.visitInsn(Opcodes.ACONST_NULL);
paraString.append("Ljava/lang/Object;");
}
paraString.append(")V");
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, name, "<init>", paraString.toString(), false);
mv.visitInsn(Opcodes.ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
cw.visitEnd();
Class<?> factoryClass = helper.loadClass(newClassName, cls, cw.toByteArray());
try {
return factoryClass.newInstance();
} catch (Exception e) {
// ignore
}
return null;
}
use of org.apache.cxf.common.util.ASMHelper in project cxf by apache.
the class JAXBExtensionHelper method createExtensionClass.
// CHECKSTYLE:OFF - very complicated ASM code
private static Class<?> createExtensionClass(Class<?> cls, QName qname, ClassLoader loader) {
String className = ASMHelper.periodToSlashes(cls.getName());
ASMHelper helper = new ASMHelper();
Class<?> extClass = helper.findClass(className + "Extensibility", loader);
if (extClass != null) {
return extClass;
}
ClassWriter cw = helper.createClassWriter();
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER + Opcodes.ACC_SYNTHETIC, className + "Extensibility", null, className, new String[] { "javax/wsdl/extensions/ExtensibilityElement" });
cw.visitSource(cls.getSimpleName() + "Extensibility.java", null);
fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;", null, null);
fv.visitEnd();
fv = cw.visitField(0, "qn", "Ljavax/xml/namespace/QName;", null, null);
fv.visitEnd();
boolean hasAttributes = false;
try {
Method m = cls.getDeclaredMethod("getOtherAttributes");
if (m != null && m.getReturnType() == Map.class) {
hasAttributes = true;
}
} catch (Throwable t) {
// ignore
}
if (hasAttributes) {
mv = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
mv.visitCode();
Label l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(64, l0);
mv.visitTypeInsn(Opcodes.NEW, "javax/xml/namespace/QName");
mv.visitInsn(Opcodes.DUP);
mv.visitLdcInsn("http://schemas.xmlsoap.org/wsdl/");
mv.visitLdcInsn("required");
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "javax/xml/namespace/QName", "<init>", "(Ljava/lang/String;Ljava/lang/String;)V", false);
mv.visitFieldInsn(Opcodes.PUTSTATIC, className + "Extensibility", "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;");
mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(4, 0);
mv.visitEnd();
} else {
fv = cw.visitField(Opcodes.ACC_PRIVATE, "required", "Ljava/lang/Boolean;", null, null);
fv.visitEnd();
}
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
Label l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(33, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", "()V", false);
Label l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLineNumber(31, l1);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitTypeInsn(Opcodes.NEW, "javax/xml/namespace/QName");
mv.visitInsn(Opcodes.DUP);
mv.visitLdcInsn(qname.getNamespaceURI());
mv.visitLdcInsn(qname.getLocalPart());
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "javax/xml/namespace/QName", "<init>", "(Ljava/lang/String;Ljava/lang/String;)V", false);
mv.visitFieldInsn(Opcodes.PUTFIELD, className + "Extensibility", "qn", "Ljavax/xml/namespace/QName;");
Label l2 = helper.createLabel();
mv.visitLabel(l2);
mv.visitLineNumber(34, l2);
mv.visitInsn(Opcodes.RETURN);
Label l3 = helper.createLabel();
mv.visitLabel(l3);
mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l3, 0);
mv.visitMaxs(5, 1);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setElementType", "(Ljavax/xml/namespace/QName;)V", null, null);
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(37, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitFieldInsn(Opcodes.PUTFIELD, className + "Extensibility", "qn", "Ljavax/xml/namespace/QName;");
l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLineNumber(38, l1);
mv.visitInsn(Opcodes.RETURN);
l2 = helper.createLabel();
mv.visitLabel(l2);
mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l2, 0);
mv.visitLocalVariable("elementType", "Ljavax/xml/namespace/QName;", null, l0, l2, 1);
mv.visitMaxs(2, 2);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getElementType", "()Ljavax/xml/namespace/QName;", null, null);
av0 = mv.visitAnnotation("Ljavax/xml/bind/annotation/XmlTransient;", true);
av0.visitEnd();
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(40, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, className + "Extensibility", "qn", "Ljavax/xml/namespace/QName;");
mv.visitInsn(Opcodes.ARETURN);
l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l1, 0);
mv.visitMaxs(1, 1);
mv.visitEnd();
if (hasAttributes) {
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getRequired", "()Ljava/lang/Boolean;", null, null);
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(66, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className + "Extensibility", "getOtherAttributes", "()Ljava/util/Map;", false);
mv.visitFieldInsn(Opcodes.GETSTATIC, className + "Extensibility", "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;");
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "get", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
mv.visitTypeInsn(Opcodes.CHECKCAST, "java/lang/String");
mv.visitVarInsn(Opcodes.ASTORE, 1);
l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLineNumber(67, l1);
mv.visitVarInsn(Opcodes.ALOAD, 1);
l2 = helper.createLabel();
mv.visitJumpInsn(Opcodes.IFNONNULL, l2);
mv.visitInsn(Opcodes.ACONST_NULL);
l3 = helper.createLabel();
mv.visitJumpInsn(Opcodes.GOTO, l3);
mv.visitLabel(l2);
mv.visitFrame(Opcodes.F_APPEND, 1, new Object[] { "java/lang/String" }, 0, null);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Ljava/lang/String;)Ljava/lang/Boolean;", false);
mv.visitLabel(l3);
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Boolean" });
mv.visitInsn(Opcodes.ARETURN);
Label l4 = helper.createLabel();
mv.visitLabel(l4);
mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l4, 0);
mv.visitLocalVariable("s", "Ljava/lang/String;", null, l1, l4, 1);
mv.visitMaxs(2, 2);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setRequired", "(Ljava/lang/Boolean;)V", null, null);
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(76, l0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
l1 = helper.createLabel();
mv.visitJumpInsn(Opcodes.IFNONNULL, l1);
l2 = helper.createLabel();
mv.visitLabel(l2);
mv.visitLineNumber(77, l2);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className + "Extensibility", "getOtherAttributes", "()Ljava/util/Map;", false);
mv.visitFieldInsn(Opcodes.GETSTATIC, className + "Extensibility", "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;");
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "remove", "(Ljava/lang/Object;)Ljava/lang/Object;", true);
mv.visitInsn(Opcodes.POP);
l3 = helper.createLabel();
mv.visitLabel(l3);
mv.visitLineNumber(78, l3);
l4 = helper.createLabel();
mv.visitJumpInsn(Opcodes.GOTO, l4);
mv.visitLabel(l1);
mv.visitLineNumber(79, l1);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className + "Extensibility", "getOtherAttributes", "()Ljava/util/Map;", false);
mv.visitFieldInsn(Opcodes.GETSTATIC, className + "Extensibility", "WSDL_REQUIRED", "Ljavax/xml/namespace/QName;");
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Boolean", "toString", "()Ljava/lang/String;", false);
mv.visitMethodInsn(Opcodes.INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", true);
mv.visitInsn(Opcodes.POP);
mv.visitLabel(l4);
mv.visitLineNumber(81, l4);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitInsn(Opcodes.RETURN);
Label l5 = helper.createLabel();
mv.visitLabel(l5);
mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l5, 0);
mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l5, 1);
mv.visitMaxs(3, 2);
mv.visitEnd();
} else {
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "getRequired", "()Ljava/lang/Boolean;", null, null);
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(68, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(Opcodes.GETFIELD, className + "Extensibility", "required", "Ljava/lang/Boolean;");
mv.visitInsn(Opcodes.ARETURN);
l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l1, 0);
mv.visitMaxs(1, 1);
mv.visitEnd();
mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "setRequired", "(Ljava/lang/Boolean;)V", null, null);
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(71, l0);
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Opcodes.ALOAD, 1);
mv.visitFieldInsn(Opcodes.PUTFIELD, className + "Extensibility", "required", "Ljava/lang/Boolean;");
l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLineNumber(72, l1);
mv.visitInsn(Opcodes.RETURN);
l2 = helper.createLabel();
mv.visitLabel(l2);
mv.visitLocalVariable("this", "L" + className + "Extensibility;", null, l0, l2, 0);
mv.visitLocalVariable("b", "Ljava/lang/Boolean;", null, l0, l2, 1);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
cw.visitEnd();
byte[] bytes = cw.toByteArray();
return helper.loadClass(className + "Extensibility", loader, bytes);
}
use of org.apache.cxf.common.util.ASMHelper in project cxf by apache.
the class CorbaAnyHelper method createFixedAnyConstructor.
private static synchronized void createFixedAnyConstructor() {
if (fixedAnyConstructor != null) {
return;
}
ASMHelper helper = new ASMHelper();
ClassWriter cw = helper.createClassWriter();
FieldVisitor fv;
cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, "org/apache/cxf/binding/corba/utils/FixedAnyImpl", null, "com/sun/corba/se/impl/corba/AnyImpl", null);
cw.visitSource("FixedAnyImpl.java", null);
fv = cw.visitField(0, "obj", "Lorg/omg/CORBA/portable/Streamable;", null, null);
fv.visitEnd();
addFixedAnyConstructor(helper, cw);
addInsertOverride(helper, cw);
addExtractOverride(helper, cw);
addWriteOverride(helper, cw);
addReadOverride(helper, cw);
cw.visitEnd();
byte[] b = cw.toByteArray();
Class<?> c = helper.loadClass("org.apache.cxf.binding.corba.utils.FixedAnyImpl", CorbaAnyHelper.class, b);
try {
fixedAnyConstructor = c.getConstructor(ORB.class, Any.class);
} catch (Exception e) {
// shouldn't happen since we generated that constructor
}
}
use of org.apache.cxf.common.util.ASMHelper in project cxf by apache.
the class ClientServerMiscTest method runDocLitTest.
private void runDocLitTest(DocLitWrappedCodeFirstService port) throws Exception {
assertEquals("snarf", port.doBug2692("snarf"));
CXF2411Result<CXF2411SubClass> o = port.doCXF2411();
assertNotNull(o);
assertNotNull(o.getContent());
Object[] ar = o.getContent();
assertTrue(ar[0] instanceof CXF2411SubClass);
Foo foo = new Foo();
foo.setName("blah");
assertEquals("blah", port.modifyFoo(foo).getName());
assertEquals("hello", port.outOnly(new Holder<String>(), new Holder<String>()));
long start = System.currentTimeMillis();
port.doOneWay();
assertTrue((System.currentTimeMillis() - start) < 500);
assertEquals("Hello", port.echoStringNotReallyAsync("Hello"));
Set<Foo> fooSet = port.getFooSet();
assertEquals(2, fooSet.size());
assertEquals("size: 2", port.doFooList(new ArrayList<>(fooSet)));
assertEquals(24, port.echoIntDifferentWrapperName(24));
String echoMsg = port.echo("Hello");
assertEquals("Hello", echoMsg);
List<String> rev = new ArrayList<>(Arrays.asList(DocLitWrappedCodeFirstServiceImpl.DATA));
Collections.reverse(rev);
String s;
String[] arrayOut = port.arrayOutput();
assertNotNull(arrayOut);
assertEquals(3, arrayOut.length);
for (int x = 0; x < 3; x++) {
assertEquals(DocLitWrappedCodeFirstServiceImpl.DATA[x], arrayOut[x]);
}
List<String> listOut = port.listOutput();
assertNotNull(listOut);
assertEquals(3, listOut.size());
for (int x = 0; x < 3; x++) {
assertEquals(DocLitWrappedCodeFirstServiceImpl.DATA[x], listOut.get(x));
}
s = port.arrayInput(DocLitWrappedCodeFirstServiceImpl.DATA);
assertEquals("string1string2string3", s);
s = port.listInput(java.util.Arrays.asList(DocLitWrappedCodeFirstServiceImpl.DATA));
assertEquals("string1string2string3", s);
s = port.multiListInput(Arrays.asList(DocLitWrappedCodeFirstServiceImpl.DATA), rev, "Hello", 24);
assertEquals("string1string2string3string3string2string1Hello24", s);
s = port.listInput(new ArrayList<>());
assertEquals("", s);
s = port.listInput(null);
assertEquals("", s);
s = port.multiListInput(Arrays.asList(DocLitWrappedCodeFirstServiceImpl.DATA), rev, null, 24);
assertEquals("string1string2string3string3string2string1<null>24", s);
Holder<String> a = new Holder<String>();
Holder<String> b = new Holder<String>("Hello");
Holder<String> c = new Holder<String>();
Holder<String> d = new Holder<String>(" ");
Holder<String> e = new Holder<String>("world!");
Holder<String> f = new Holder<String>();
Holder<String> g = new Holder<String>();
s = port.multiInOut(a, b, c, d, e, f, g);
assertEquals("Hello world!", s);
assertEquals("a", a.value);
assertEquals("b", b.value);
assertEquals("c", c.value);
assertEquals("d", d.value);
assertEquals("e", e.value);
assertEquals("f", f.value);
assertEquals("g", g.value);
List<Foo> foos = port.listObjectOutput();
assertEquals(2, foos.size());
assertEquals("a", foos.get(0).getName());
assertEquals("b", foos.get(1).getName());
List<Foo[]> foos2 = port.listObjectArrayOutput();
assertNotNull(foos2);
assertEquals(2, foos2.size());
assertEquals(2, foos2.get(0).length);
assertEquals(2, foos2.get(1).length);
int[] ints = port.echoIntArray(new int[] { 1, 2, 3 }, null);
assertEquals(3, ints.length);
assertEquals(1, ints[0]);
if (new ASMHelper().createClassWriter() != null) {
// doing the type adapter things and such really
// requires the ASM generated helper classes
assertEquals("Val", port.createBar("Val").getName());
}
testExceptionCases(port);
}
use of org.apache.cxf.common.util.ASMHelper in project cxf by apache.
the class JAXBUtils method createNamespaceWrapper.
private static synchronized Object createNamespaceWrapper(Class<?> mcls, Map<String, String> map) {
String postFix = "";
if (mcls.getName().contains("eclipse")) {
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);
Throwable t = null;
if (cls == null) {
try {
ClassWriter cw = helper.createClassWriter();
if (cw != null) {
cls = createNamespaceWrapperInternal(helper, cw, postFix, mcls);
}
} catch (RuntimeException ex) {
// continue
t = ex;
}
}
if (cls == null && (!mcls.getName().contains(".internal.") && mcls.getName().contains("com.sun"))) {
try {
cls = ClassLoaderUtils.loadClass("org.apache.cxf.common.jaxb.NamespaceMapper", JAXBUtils.class);
} catch (Throwable ex2) {
// ignore
t = ex2;
}
}
if (cls != null) {
try {
return cls.getConstructor(Map.class).newInstance(map);
} catch (Exception e) {
// ignore
t = e;
}
}
LOG.log(Level.INFO, "Could not create a NamespaceMapper compatible with Marshaller class " + mcls.getName(), t);
return null;
}
Aggregations