use of org.apache.cxf.common.util.OpcodesProxy in project cxf by apache.
the class WrapperClassGenerator method createWrapperClass.
// CHECKSTYLE:OFF
private void createWrapperClass(MessagePartInfo wrapperPart, MessageInfo messageInfo, OperationInfo op, Method method, boolean isRequest, Set<Class<?>> wrapperBeans, JaxWsServiceFactoryBean factory, InterfaceInfo interfaceInfo, boolean qualified) {
ASMHelper.ClassWriter cw = helper.createClassWriter();
if (cw == null) {
LOG.warning(op.getName() + " requires a wrapper bean but problems with" + " ASM has prevented creating one. Operation may not work correctly.");
return;
}
QName wrapperElement = messageInfo.getName();
boolean anonymous = factory.getAnonymousWrapperTypes();
String pkg = getPackageName(method) + ".jaxws_asm" + (anonymous ? "_an" : "");
String className = pkg + "." + StringUtils.capitalize(op.getName().getLocalPart());
if (!isRequest) {
className = className + "Response";
}
String pname = pkg + ".package-info";
Class<?> def = findClass(pname, method.getDeclaringClass());
if (def == null) {
generatePackageInfo(pname, wrapperElement.getNamespaceURI(), method.getDeclaringClass(), method, interfaceInfo, qualified);
}
def = findClass(className, method.getDeclaringClass());
String origClassName = className;
int count = 0;
while (def != null) {
Boolean b = messageInfo.getProperty("parameterized", Boolean.class);
if (b != null && b) {
className = origClassName + (++count);
def = findClass(className, method.getDeclaringClass());
} else {
wrapperPart.setTypeClass(def);
wrapperBeans.add(def);
return;
}
}
String classFileName = StringUtils.periodToSlashes(className);
OpcodesProxy opCodes = helper.getOpCodes();
cw.visit(opCodes.V1_5, opCodes.ACC_PUBLIC + opCodes.ACC_SUPER, classFileName, null, "java/lang/Object", null);
ASMHelper.AnnotationVisitor av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlRootElement;", true);
av0.visit("name", wrapperElement.getLocalPart());
av0.visit("namespace", wrapperElement.getNamespaceURI());
av0.visitEnd();
av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlAccessorType;", true);
av0.visitEnum("value", "Ljavax/xml/bind/annotation/XmlAccessType;", "FIELD");
av0.visitEnd();
av0 = cw.visitAnnotation("Ljavax/xml/bind/annotation/XmlType;", true);
if (!anonymous) {
av0.visit("name", wrapperElement.getLocalPart());
av0.visit("namespace", wrapperElement.getNamespaceURI());
} else {
av0.visit("name", "");
}
av0.visitEnd();
// add constructor
ASMHelper.MethodVisitor mv = cw.visitMethod(opCodes.ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
ASMHelper.Label lbegin = helper.createLabel();
mv.visitLabel(lbegin);
mv.visitVarInsn(opCodes.ALOAD, 0);
mv.visitMethodInsn(opCodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(opCodes.RETURN);
ASMHelper.Label lend = helper.createLabel();
mv.visitLabel(lend);
mv.visitLocalVariable("this", "L" + classFileName + ";", null, lbegin, lend, 0);
mv.visitMaxs(0, 0);
mv.visitEnd();
for (MessagePartInfo mpi : messageInfo.getMessageParts()) {
generateMessagePart(cw, mpi, method, classFileName, factory);
}
cw.visitEnd();
Class<?> clz = loadClass(className, method.getDeclaringClass(), cw.toByteArray());
wrapperPart.setTypeClass(clz);
wrapperBeans.add(clz);
}
use of org.apache.cxf.common.util.OpcodesProxy in project cxf by apache.
the class WrapperClassGenerator method generateMessagePart.
private void generateMessagePart(ASMHelper.ClassWriter cw, MessagePartInfo mpi, Method method, String className, JaxWsServiceFactoryBean factory) {
if (Boolean.TRUE.equals(mpi.getProperty(ReflectionServiceFactoryBean.HEADER))) {
return;
}
OpcodesProxy opCodes = helper.getOpCodes();
String classFileName = StringUtils.periodToSlashes(className);
String name = mpi.getName().getLocalPart();
Class<?> clz = mpi.getTypeClass();
Object obj = mpi.getProperty(ReflectionServiceFactoryBean.RAW_CLASS);
if (obj != null) {
clz = (Class<?>) obj;
}
Type genericType = (Type) mpi.getProperty(ReflectionServiceFactoryBean.GENERIC_TYPE);
if (genericType instanceof ParameterizedType) {
ParameterizedType tp = (ParameterizedType) genericType;
if (tp.getRawType() instanceof Class && Holder.class.isAssignableFrom((Class<?>) tp.getRawType())) {
genericType = tp.getActualTypeArguments()[0];
}
}
String classCode = helper.getClassCode(clz);
String fieldDescriptor = null;
if (genericType instanceof ParameterizedType) {
if (Collection.class.isAssignableFrom(clz) || clz.isArray()) {
ParameterizedType ptype = (ParameterizedType) genericType;
java.lang.reflect.Type[] types = ptype.getActualTypeArguments();
if (types.length > 0) {
if (types[0] instanceof Class) {
fieldDescriptor = helper.getClassCode(genericType);
} else if (types[0] instanceof GenericArrayType) {
fieldDescriptor = helper.getClassCode(genericType);
} else if (Collection.class.isAssignableFrom(clz)) {
fieldDescriptor = helper.getClassCode(genericType);
} else if (types[0] instanceof ParameterizedType) {
classCode = helper.getClassCode(((ParameterizedType) types[0]).getRawType());
fieldDescriptor = helper.getClassCode(genericType);
}
}
} else {
classCode = helper.getClassCode(((ParameterizedType) genericType).getRawType());
fieldDescriptor = helper.getClassCode(genericType);
}
}
String fieldName = JavaUtils.isJavaKeyword(name) ? JavaUtils.makeNonJavaKeyword(name) : name;
ASMHelper.FieldVisitor fv = cw.visitField(opCodes.ACC_PRIVATE, fieldName, classCode, fieldDescriptor, null);
List<Annotation> jaxbAnnos = getJaxbAnnos(mpi);
if (!addJAXBAnnotations(fv, jaxbAnnos, name)) {
ASMHelper.AnnotationVisitor av0 = fv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true);
av0.visit("name", name);
if (Boolean.TRUE.equals(factory.isWrapperPartQualified(mpi))) {
av0.visit("namespace", mpi.getConcreteName().getNamespaceURI());
}
if (factory.isWrapperPartNillable(mpi)) {
av0.visit("nillable", Boolean.TRUE);
}
if (factory.getWrapperPartMinOccurs(mpi) == 1) {
av0.visit("required", Boolean.TRUE);
}
av0.visitEnd();
}
fv.visitEnd();
String methodName = JAXBUtils.nameToIdentifier(name, JAXBUtils.IdentifierType.GETTER);
ASMHelper.MethodVisitor mv = cw.visitMethod(opCodes.ACC_PUBLIC, methodName, "()" + classCode, fieldDescriptor == null ? null : "()" + fieldDescriptor, null);
mv.visitCode();
mv.visitVarInsn(opCodes.ALOAD, 0);
mv.visitFieldInsn(opCodes.GETFIELD, classFileName, fieldName, classCode);
mv.visitInsn(helper.getType(classCode).getOpcode(opCodes.IRETURN));
mv.visitMaxs(0, 0);
mv.visitEnd();
methodName = JAXBUtils.nameToIdentifier(name, JAXBUtils.IdentifierType.SETTER);
mv = cw.visitMethod(opCodes.ACC_PUBLIC, methodName, "(" + classCode + ")V", fieldDescriptor == null ? null : "(" + fieldDescriptor + ")V", null);
mv.visitCode();
mv.visitVarInsn(opCodes.ALOAD, 0);
ASMHelper.ASMType setType = helper.getType(classCode);
mv.visitVarInsn(setType.getOpcode(opCodes.ILOAD), 1);
mv.visitFieldInsn(opCodes.PUTFIELD, className, fieldName, classCode);
mv.visitInsn(opCodes.RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.apache.cxf.common.util.OpcodesProxy in project cxf by apache.
the class ExceptionClassGenerator method createExceptionClass.
@Override
public Class<?> createExceptionClass(Class<?> bean) {
String newClassName = bean.getName() + "_Exception";
newClassName = newClassName.replaceAll("\\$", ".");
newClassName = StringUtils.periodToSlashes(newClassName);
Class<?> cls = findClass(StringUtils.slashesToPeriod(newClassName), bean);
if (cls == null) {
ASMHelper.ClassWriter cw = helper.createClassWriter();
OpcodesProxy opCodes = helper.getOpCodes();
cw.visit(opCodes.V1_5, opCodes.ACC_PUBLIC | opCodes.ACC_SUPER, newClassName, null, "java/lang/Exception", null);
ASMHelper.FieldVisitor fv;
ASMHelper.MethodVisitor mv;
String beanClassCode = helper.getClassCode(bean);
fv = cw.visitField(0, "faultInfo", beanClassCode, null, null);
fv.visitEnd();
mv = cw.visitMethod(opCodes.ACC_PUBLIC, "<init>", "(Ljava/lang/String;" + beanClassCode + ")V", null, null);
mv.visitCode();
mv.visitLabel(helper.createLabel());
mv.visitVarInsn(opCodes.ALOAD, 0);
mv.visitVarInsn(opCodes.ALOAD, 1);
mv.visitMethodInsn(opCodes.INVOKESPECIAL, "java/lang/Exception", "<init>", "(Ljava/lang/String;)V", false);
mv.visitLabel(helper.createLabel());
mv.visitVarInsn(opCodes.ALOAD, 0);
mv.visitVarInsn(opCodes.ALOAD, 2);
mv.visitFieldInsn(opCodes.PUTFIELD, newClassName, "faultInfo", beanClassCode);
mv.visitLabel(helper.createLabel());
mv.visitInsn(opCodes.RETURN);
mv.visitLabel(helper.createLabel());
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(opCodes.ACC_PUBLIC, "getFaultInfo", "()" + beanClassCode, null, null);
mv.visitCode();
mv.visitLabel(helper.createLabel());
mv.visitVarInsn(opCodes.ALOAD, 0);
mv.visitFieldInsn(opCodes.GETFIELD, newClassName, "faultInfo", beanClassCode);
mv.visitInsn(opCodes.ARETURN);
mv.visitLabel(helper.createLabel());
mv.visitMaxs(0, 0);
mv.visitEnd();
cw.visitEnd();
return loadClass(bean.getName() + "_Exception", bean, cw.toByteArray());
}
return cls;
}
use of org.apache.cxf.common.util.OpcodesProxy in project tomee by apache.
the class FactoryClassGenerator method createFactory.
@SuppressWarnings("unused")
public Class<?> createFactory(Class<?> cls) {
String newClassName = cls.getName() + "Factory";
Class<?> factoryClass = findClass(newClassName, cls);
if (factoryClass != null) {
return factoryClass;
}
Constructor<?> contructor = ReflectionUtil.getDeclaredConstructors(cls)[0];
OpcodesProxy opcodes = helper.getOpCodes();
ASMHelper.ClassWriter cw = helper.createClassWriter();
ASMHelper.MethodVisitor mv;
cw.visit(opcodes.V1_6, opcodes.ACC_PUBLIC + opcodes.ACC_SUPER, StringUtils.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(0, 0);
mv.visitEnd();
mv = cw.visitMethod(opcodes.ACC_PUBLIC, "create" + cls.getSimpleName(), "()L" + StringUtils.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(32).append('(');
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(0, 0);
mv.visitEnd();
cw.visitEnd();
return loadClass(newClassName, cls, cw.toByteArray());
}
use of org.apache.cxf.common.util.OpcodesProxy in project tomee by apache.
the class NamespaceClassGenerator method createNamespaceWrapperInternal.
private byte[] createNamespaceWrapperInternal(String postFix) {
String superName = "com/sun/xml/" + ("RI".equals(postFix) ? "" : "internal/") + "bind/marshaller/NamespacePrefixMapper";
String postFixedName = "org/apache/cxf/jaxb/NamespaceMapper" + postFix;
ASMHelper.ClassWriter cw = helper.createClassWriter();
if (cw == null) {
return null;
}
ASMHelper.FieldVisitor fv;
ASMHelper.MethodVisitor mv;
OpcodesProxy opcodes = helper.getOpCodes();
cw.visit(opcodes.V1_6, opcodes.ACC_PUBLIC + opcodes.ACC_FINAL + opcodes.ACC_SUPER, postFixedName, null, superName, null);
cw.visitSource("NamespaceMapper.java", null);
fv = cw.visitField(opcodes.ACC_PRIVATE + opcodes.ACC_FINAL, "nspref", "Ljava/util/Map;", "Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;", null);
fv.visitEnd();
fv = cw.visitField(opcodes.ACC_PRIVATE, "nsctxt", "[Ljava/lang/String;", null, null);
fv.visitEnd();
fv = cw.visitField(opcodes.ACC_PRIVATE + opcodes.ACC_FINAL + opcodes.ACC_STATIC, "EMPTY_STRING", "[Ljava/lang/String;", null, null);
fv.visitEnd();
mv = cw.visitMethod(opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
mv.visitCode();
ASMHelper.Label l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(30, l0);
mv.visitInsn(opcodes.ICONST_0);
mv.visitTypeInsn(opcodes.ANEWARRAY, "java/lang/String");
mv.visitFieldInsn(opcodes.PUTSTATIC, postFixedName, "EMPTY_STRING", "[Ljava/lang/String;");
mv.visitInsn(opcodes.RETURN);
mv.visitMaxs(0, 0);
mv.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();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(32, l0);
mv.visitVarInsn(opcodes.ALOAD, 0);
mv.visitMethodInsn(opcodes.INVOKESPECIAL, superName, "<init>", "()V", false);
ASMHelper.Label l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLineNumber(29, l1);
mv.visitVarInsn(opcodes.ALOAD, 0);
mv.visitFieldInsn(opcodes.GETSTATIC, postFixedName, "EMPTY_STRING", "[Ljava/lang/String;");
mv.visitFieldInsn(opcodes.PUTFIELD, postFixedName, "nsctxt", "[Ljava/lang/String;");
ASMHelper.Label l2 = helper.createLabel();
mv.visitLabel(l2);
mv.visitLineNumber(33, l2);
mv.visitVarInsn(opcodes.ALOAD, 0);
mv.visitVarInsn(opcodes.ALOAD, 1);
mv.visitFieldInsn(opcodes.PUTFIELD, postFixedName, "nspref", "Ljava/util/Map;");
ASMHelper.Label l3 = helper.createLabel();
mv.visitLabel(l3);
mv.visitLineNumber(34, l3);
mv.visitInsn(opcodes.RETURN);
ASMHelper.Label l4 = helper.createLabel();
mv.visitLabel(l4);
mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l4, 0);
mv.visitLocalVariable("nspref", "Ljava/util/Map;", "Ljava/util/Map<Ljava/lang/String;Ljava/lang/String;>;", l0, l4, 1);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(opcodes.ACC_PUBLIC, "getPreferredPrefix", "(Ljava/lang/String;Ljava/lang/String;Z)Ljava/lang/String;", null, null);
mv.visitCode();
l0 = helper.createLabel();
mv.visitLabel(l0);
mv.visitLineNumber(39, l0);
mv.visitVarInsn(opcodes.ALOAD, 0);
mv.visitFieldInsn(opcodes.GETFIELD, postFixedName, "nspref", "Ljava/util/Map;");
mv.visitVarInsn(opcodes.ALOAD, 1);
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, 4);
l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLineNumber(40, l1);
mv.visitVarInsn(opcodes.ALOAD, 4);
l2 = helper.createLabel();
mv.visitJumpInsn(opcodes.IFNULL, l2);
l3 = helper.createLabel();
mv.visitLabel(l3);
mv.visitLineNumber(41, l3);
mv.visitVarInsn(opcodes.ALOAD, 4);
mv.visitInsn(opcodes.ARETURN);
mv.visitLabel(l2);
mv.visitLineNumber(43, l2);
mv.visitFrame(opcodes.F_APPEND, 1, new Object[] { "java/lang/String" }, 0, null);
mv.visitVarInsn(opcodes.ALOAD, 2);
mv.visitInsn(opcodes.ARETURN);
l4 = helper.createLabel();
mv.visitLabel(l4);
mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l4, 0);
mv.visitLocalVariable("namespaceUri", "Ljava/lang/String;", null, l0, l4, 1);
mv.visitLocalVariable("suggestion", "Ljava/lang/String;", null, l0, l4, 2);
mv.visitLocalVariable("requirePrefix", "Z", null, l0, l4, 3);
mv.visitLocalVariable("prefix", "Ljava/lang/String;", null, l1, l4, 4);
mv.visitMaxs(0, 0);
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, postFixedName, "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" + postFixedName + ";", null, l0, l2, 0);
mv.visitLocalVariable("contextualNamespaceDecls", "[Ljava/lang/String;", null, l0, l2, 1);
mv.visitMaxs(0, 0);
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, postFixedName, "nsctxt", "[Ljava/lang/String;");
mv.visitInsn(opcodes.ARETURN);
l1 = helper.createLabel();
mv.visitLabel(l1);
mv.visitLocalVariable("this", "L" + postFixedName + ";", null, l0, l1, 0);
mv.visitMaxs(0, 0);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}
Aggregations