private static synchronized Object createNamespaceWrapper(Map<String, String> map) { ASMHelper helper = new ASMHelper(); String className = "org.apache.cxf.jaxb.NamespaceMapperInternal"; Class<?> cls = helper.findClass(className, JAXBUtils.class); if (cls == null) { ClassWriter cw = helper.createClassWriter(); if (cw == null) { return null; } cls = createNamespaceWrapperInternal(helper, cw); } try { return cls.getConstructor(Map.class).newInstance(map); } catch (Exception e) { return null; } } private static Class<?> createNamespaceWrapperInternal(ASMHelper helper, ClassWriter cw) {
public static String getClassCode(Class<?> cl) { if (cl == Void.TYPE) { return "V"; } if (cl.isPrimitive()) { return PRIMITIVE_MAP.get(cl); } if (cl.isArray()) { return "[" + getClassCode(cl.getComponentType()); } return "L" + periodToSlashes(cl.getName()) + ";"; } public static String getClassCode(java.lang.reflect.Type type) {
public Class<?> findClass(String className, Class<?> clz) { TypeHelperClassLoader loader = getTypeHelperClassLoader(clz); return loader.lookupDefinedClass(className); } public Class<?> findClass(String className, ClassLoader l) {
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); ClassWriter cw = helper.createClassWriter(); if (cw == null) { return null; "(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.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", "(Ljava/util/Map;)V", false); Label l1 = helper.createLabel(); mv.visitLabel(l1); mv.visitInsn(Opcodes.RETURN); Label l2 = helper.createLabel(); mv.visitLabel(l2); mv.visitMaxs(2, 2); null, null); mv.visitCode(); l0 = helper.createLabel(); mv.visitLabel(l0); mv.visitLineNumber(47, l0);
@SuppressWarnings("unused") private Object createFactory(Class<?> cls, Constructor<?> contructor) { String newClassName = cls.getName() + "Factory"; ASMHelper helper = new ASMHelper(); ClassWriter cw = helper.createClassWriter(); MethodVisitor mv; ASMHelper.periodToSlashes(newClassName), null, "java/lang/Object", null); "()L" + ASMHelper.periodToSlashes(cls.getName()) + ";", null, null); mv.visitCode(); String name = cls.getName().replace(".", "/"); Class<?> factoryClass = helper.loadClass(newClassName, cls, cw.toByteArray()); try { return factoryClass.newInstance();
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 } }
Label l0 = helper.createLabel(); mv.visitLabel(l0); mv.visitLineNumber(30, l0); "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)V", null); mv.visitCode(); l0 = helper.createLabel(); mv.visitLabel(l0); mv.visitLineNumber(32, l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", "()V", false); Label l1 = helper.createLabel(); mv.visitLabel(l1); mv.visitLineNumber(29, l1); mv.visitFieldInsn(Opcodes.GETSTATIC, postFixedName, "EMPTY_STRING", "[Ljava/lang/String;"); mv.visitFieldInsn(Opcodes.PUTFIELD, postFixedName, "nsctxt", "[Ljava/lang/String;"); Label l2 = helper.createLabel(); mv.visitLabel(l2); mv.visitLineNumber(33, l2); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTFIELD, postFixedName, "nspref", "Ljava/util/Map;"); Label l3 = helper.createLabel(); mv.visitLabel(l3); mv.visitLineNumber(34, l3); mv.visitInsn(Opcodes.RETURN); Label l4 = helper.createLabel(); mv.visitLabel(l4);
assertEquals(1, ints[0]); if (new ASMHelper().createClassWriter() != null) {
Method method = ASMHelper.class.getDeclaredMethod("getASMClass", null); method.setAccessible(true); Class asmClass = (Class) method.invoke(new ASMHelper(), null); System.out.println("ASM location: " + asmClass.getProtectionDomain().getCodeSource().getLocation().getPath()); List<URL> classpaths = Arrays.asList(((URLClassLoader)asmClass.getClassLoader()).getURLs()); System.out.println("Classpath of classloader: " + classpaths);
protected static String getMethodSignature(Method m) { StringBuilder buf = new StringBuilder("("); for (Class<?> cl : m.getParameterTypes()) { buf.append(getClassCode(cl)); } buf.append(")"); buf.append(getClassCode(m.getReturnType())); return buf.toString(); }
"()Lorg/omg/CORBA/portable/Streamable;", null, null); mv.visitCode(); Label l0 = helper.createLabel(); mv.visitLabel(l0); mv.visitLineNumber(47, l0); mv.visitFieldInsn(Opcodes.GETFIELD, "org/apache/cxf/binding/corba/utils/FixedAnyImpl", "obj", "Lorg/omg/CORBA/portable/Streamable;"); Label l1 = helper.createLabel(); mv.visitJumpInsn(Opcodes.IFNULL, l1); Label l2 = helper.createLabel(); mv.visitLabel(l2); mv.visitLineNumber(48, l2); "extract_Streamable", "()Lorg/omg/CORBA/portable/Streamable;", false); mv.visitInsn(Opcodes.ARETURN); Label l3 = helper.createLabel(); mv.visitLabel(l3); mv.visitLocalVariable("this", "Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;", null, l0, l3, 0);
return helper.loadClass(className, cls, bts);
newClassName = periodToSlashes(newClassName); Class<?> cls = super.findClass(newClassName.replace('/', '.'), wrapperType); while (cls != null) { try { newClassName = newClassName.replaceAll("\\$", "."); newClassName = periodToSlashes(newClassName); cls = super.findClass(newClassName.replace('/', '.'), wrapperType); } else { return helper;
if (cwClass == null) { try { cwClass = getASMClass(); } catch (Throwable error) { badASM = true;
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); ClassWriter cw = helper.createClassWriter(); if (cw == null) { return null; "(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.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", "(Ljava/util/Map;)V", false); Label l1 = helper.createLabel(); mv.visitLabel(l1); mv.visitInsn(Opcodes.RETURN); Label l2 = helper.createLabel(); mv.visitLabel(l2); mv.visitMaxs(2, 2); null, null); mv.visitCode(); l0 = helper.createLabel(); mv.visitLabel(l0); mv.visitLineNumber(47, l0);
Label l0 = helper.createLabel(); mv.visitLabel(l0); mv.visitLineNumber(30, l0); "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;)V", null); mv.visitCode(); l0 = helper.createLabel(); mv.visitLabel(l0); mv.visitLineNumber(32, l0); mv.visitVarInsn(Opcodes.ALOAD, 0); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", "()V", false); Label l1 = helper.createLabel(); mv.visitLabel(l1); mv.visitLineNumber(29, l1); mv.visitFieldInsn(Opcodes.GETSTATIC, postFixedName, "EMPTY_STRING", "[Ljava/lang/String;"); mv.visitFieldInsn(Opcodes.PUTFIELD, postFixedName, "nsctxt", "[Ljava/lang/String;"); Label l2 = helper.createLabel(); mv.visitLabel(l2); mv.visitLineNumber(33, l2); mv.visitVarInsn(Opcodes.ALOAD, 1); mv.visitFieldInsn(Opcodes.PUTFIELD, postFixedName, "nspref", "Ljava/util/Map;"); Label l3 = helper.createLabel(); mv.visitLabel(l3); mv.visitLineNumber(34, l3); mv.visitInsn(Opcodes.RETURN); Label l4 = helper.createLabel(); mv.visitLabel(l4);
protected static String getMethodSignature(Method m) { StringBuilder buf = new StringBuilder("("); for (Class<?> cl : m.getParameterTypes()) { buf.append(getClassCode(cl)); } buf.append(")"); buf.append(getClassCode(m.getReturnType())); return buf.toString(); }
"(Lorg/omg/CORBA/portable/Streamable;)V", null, null); mv.visitCode(); Label l0 = helper.createLabel(); mv.visitLabel(l0); mv.visitLineNumber(43, l0); "insert_Streamable", "(Lorg/omg/CORBA/portable/Streamable;)V", false); Label l1 = helper.createLabel(); mv.visitLabel(l1); mv.visitLineNumber(44, l1); "org/apache/cxf/binding/corba/utils/FixedAnyImpl", "obj", "Lorg/omg/CORBA/portable/Streamable;"); Label l2 = helper.createLabel(); mv.visitLabel(l2); mv.visitLineNumber(45, l2); mv.visitInsn(Opcodes.RETURN); Label l3 = helper.createLabel(); mv.visitLabel(l3); mv.visitLocalVariable("this", "Lorg/apache/cxf/binding/corba/utils/FixedAnyImpl;",