List of usage examples for org.objectweb.asm MethodVisitor visitMaxs
public void visitMaxs(final int maxStack, final int maxLocals)
From source file:net.wpm.reflectasm.ClassAccess.java
License:Open Source License
static private void insertSetPrimitive(ClassWriter cw, String classNameInternal, List<Field> fields, Type primitiveType, String setterMethodName, int loadValueInstruction) { int maxStack = 6; int maxLocals = 5; final String typeNameInternal = primitiveType.getDescriptor(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, setterMethodName, "(Ljava/lang/Object;I" + typeNameInternal + ")V", null, null); mv.visitCode();// w ww . j av a 2 s . c om mv.visitVarInsn(ILOAD, 2); if (!fields.isEmpty()) { maxStack--; Label[] labels = new Label[fields.size()]; Label labelForInvalidTypes = new Label(); boolean hasAnyBadTypeLabel = false; for (int i = 0, n = labels.length; i < n; i++) { if (Type.getType(fields.get(i).getType()).equals(primitiveType)) labels[i] = new Label(); else { labels[i] = labelForInvalidTypes; hasAnyBadTypeLabel = true; } } Label defaultLabel = new Label(); mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels); for (int i = 0, n = labels.length; i < n; i++) { if (!labels[i].equals(labelForInvalidTypes)) { Field field = fields.get(i); mv.visitLabel(labels[i]); mv.visitFrame(F_SAME, 0, null, 0, null); if (isStatic(field.getModifiers())) { mv.visitVarInsn(loadValueInstruction, 3); mv.visitFieldInsn(PUTSTATIC, classNameInternal, field.getName(), typeNameInternal); } else { mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, classNameInternal); mv.visitVarInsn(loadValueInstruction, 3); mv.visitFieldInsn(PUTFIELD, classNameInternal, field.getName(), typeNameInternal); } mv.visitInsn(RETURN); } } // Rest of fields: different type if (hasAnyBadTypeLabel) { mv.visitLabel(labelForInvalidTypes); mv.visitFrame(F_SAME, 0, null, 0, null); insertThrowExceptionForFieldType(mv, primitiveType.getClassName()); } // Default: field not found mv.visitLabel(defaultLabel); mv.visitFrame(F_SAME, 0, null, 0, null); } mv = insertThrowExceptionForFieldNotFound(mv); mv.visitMaxs(maxStack, maxLocals); mv.visitEnd(); }
From source file:net.wpm.reflectasm.ClassAccess.java
License:Open Source License
static private void insertGetPrimitive(ClassWriter cw, String classNameInternal, List<Field> fields, Type primitiveType, String getterMethodName, int returnValueInstruction) { int maxStack = 6; final String typeNameInternal = primitiveType.getDescriptor(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getterMethodName, "(Ljava/lang/Object;I)" + typeNameInternal, null, null);/*www.j ava2 s . c o m*/ mv.visitCode(); mv.visitVarInsn(ILOAD, 2); if (!fields.isEmpty()) { maxStack--; Label[] labels = new Label[fields.size()]; Label labelForInvalidTypes = new Label(); boolean hasAnyBadTypeLabel = false; for (int i = 0, n = labels.length; i < n; i++) { if (Type.getType(fields.get(i).getType()).equals(primitiveType)) labels[i] = new Label(); else { labels[i] = labelForInvalidTypes; hasAnyBadTypeLabel = true; } } Label defaultLabel = new Label(); mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels); for (int i = 0, n = labels.length; i < n; i++) { Field field = fields.get(i); if (!labels[i].equals(labelForInvalidTypes)) { mv.visitLabel(labels[i]); mv.visitFrame(F_SAME, 0, null, 0, null); if (isStatic(field.getModifiers())) { mv.visitFieldInsn(GETSTATIC, classNameInternal, field.getName(), typeNameInternal); } else { mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(CHECKCAST, classNameInternal); mv.visitFieldInsn(GETFIELD, classNameInternal, field.getName(), typeNameInternal); } mv.visitInsn(returnValueInstruction); } } // Rest of fields: different type if (hasAnyBadTypeLabel) { mv.visitLabel(labelForInvalidTypes); mv.visitFrame(F_SAME, 0, null, 0, null); insertThrowExceptionForFieldType(mv, primitiveType.getClassName()); } // Default: field not found mv.visitLabel(defaultLabel); mv.visitFrame(F_SAME, 0, null, 0, null); } mv = insertThrowExceptionForFieldNotFound(mv); mv.visitMaxs(maxStack, 3); mv.visitEnd(); }
From source file:net.yrom.tools.WriteStyleablesProcessor.java
License:Apache License
private void writeClinit(ClassWriter writer) { Map<String, int[]> styleables = symbols.getStyleables(); MethodVisitor clinit = writer.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null); clinit.visitCode();/*from w w w .j a v a 2 s . c om*/ for (Map.Entry<String, int[]> entry : styleables.entrySet()) { final String field = entry.getKey(); final int[] value = entry.getValue(); final int length = value.length; pushInt(clinit, length); clinit.visitIntInsn(Opcodes.NEWARRAY, Opcodes.T_INT); for (int i = 0; i < length; i++) { clinit.visitInsn(Opcodes.DUP); // dup pushInt(clinit, i); pushInt(clinit, value[i]); clinit.visitInsn(Opcodes.IASTORE); // iastore } clinit.visitFieldInsn(Opcodes.PUTSTATIC, RSymbols.R_STYLEABLES_CLASS_NAME, field, "[I"); } clinit.visitInsn(Opcodes.RETURN); clinit.visitMaxs(0, 0); // auto compute clinit.visitEnd(); }
From source file:nova.core.wrapper.mc.forge.v17.asm.lib.ComponentInjector.java
License:Open Source License
private Class<? extends T> construct(List<Class<? extends Component>> components) { // Map components to specified wrapped interfaces Map<Class<?>, Class<? extends Component>> intfComponentMap = new HashMap<>(); for (Class<? extends Component> component : components) { for (Passthrough pt : component.getAnnotationsByType(Passthrough.class)) { Class<?> intf;//www . ja v a 2 s.c o m try { intf = Class.forName(pt.value()); } catch (ClassNotFoundException exec) { throw new ClassLoaderUtil.ClassLoaderException( "Invalid passthrough \"%s\" on component %s, the specified interface doesn't exist.", pt.value(), component); } if (!intf.isAssignableFrom(component)) { throw new ClassLoaderUtil.ClassLoaderException( "Invalid passthrough \"%s\" on component %s, the specified interface isn't implemented.", pt.value(), component); } if (intfComponentMap.containsKey(intf)) { throw new ClassLoaderUtil.ClassLoaderException( "Duplicate Passthrough interface found: %s (%s, %s)", pt.value(), component, intfComponentMap.get(intf)); } intfComponentMap.put(intf, component); } } // Create new ClassNode from cached bytes ClassNode clazzNode = new ClassNode(); String name = Type.getInternalName(baseClazz); String classname = name + "_$$_NOVA_" + cache.size(); // Inject block field clazzNode.visit(V1_8, ACC_PUBLIC | ACC_SUPER, classname, null, name, intfComponentMap.keySet().stream().map(Type::getInternalName).toArray(s -> new String[s])); clazzNode.visitField(ACC_PRIVATE | ACC_FINAL, "$$_provider", Type.getDescriptor(ComponentProvider.class), null, null).visitEnd(); // Add constructors for (Constructor<?> constructor : baseClazz.getConstructors()) { int mod = constructor.getModifiers(); String descr = Type.getConstructorDescriptor(constructor); if (Modifier.isFinal(mod) || Modifier.isPrivate(mod)) { continue; } MethodVisitor mv = clazzNode.visitMethod(mod, "<init>", descr, null, ASMHelper.getExceptionTypes(constructor)); // Call super constructor mv.visitCode(); // load this mv.visitVarInsn(ALOAD, 0); Class<?>[] parameters = constructor.getParameterTypes(); for (int i = 0; i < constructor.getParameterCount(); i++) { // variables mv.visitVarInsn(Type.getType(parameters[i]).getOpcode(ILOAD), i + 1); } mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(baseClazz), "<init>", descr, false); // return mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } // Add methods for (Class<?> intf : intfComponentMap.keySet()) { // Create class constant Type clazzConst = Type.getType(intf.getClass()); for (Method m : intf.getMethods()) { boolean isVoid = m.getReturnType() == null; String descr = Type.getMethodDescriptor(m); MethodVisitor mv = clazzNode.visitMethod(ACC_PUBLIC, m.getName(), descr, null, ASMHelper.getExceptionTypes(m)); mv.visitCode(); // load block instance mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classname, "$$_provider", Type.getDescriptor(ComponentProvider.class)); mv.visitLdcInsn(clazzConst); // load component instance mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(ComponentProvider.class), "get", Type.getMethodDescriptor(Type.getType(Component.class), Type.getType(Class.class)), false); // add parameters Class<?>[] parameters = m.getParameterTypes(); for (int i = 0; i < m.getParameterCount(); i++) { mv.visitVarInsn(Type.getType(parameters[i]).getOpcode(ILOAD), i + 1); } // invoke mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(intf), m.getName(), descr, true); mv.visitInsn(isVoid ? RETURN : Type.getType(m.getReturnType()).getOpcode(IRETURN)); mv.visitMaxs(0, 0); mv.visitEnd(); } } clazzNode.visitEnd(); return ASMHelper.defineClass(clazzNode, ClassWriter.COMPUTE_MAXS, baseClazz.getProtectionDomain()); }
From source file:one.nio.http.gen.RequestHandlerGenerator.java
License:Apache License
public RequestHandler generateFor(Method m, Object router) { if (Modifier.isStatic(m.getModifiers())) { throw new IllegalArgumentException("Method should not be static: " + m); }// w w w. jav a 2s .co m Class returnType = m.getReturnType(); if (returnType != void.class && returnType != Response.class) { throw new IllegalArgumentException("Invalid return type of " + m); } String className = "RequestHandler" + (count++) + "_" + m.getName(); String routerType = Type.getDescriptor(m.getDeclaringClass()); ClassWriter cv = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); cv.visit(V1_6, ACC_PUBLIC | ACC_FINAL, className, null, "java/lang/Object", new String[] { "one/nio/http/RequestHandler" }); // private final Object router; cv.visitField(ACC_PRIVATE | ACC_FINAL, "router", routerType, null, null).visitEnd(); // public RequestHandler(Object router); MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "(" + routerType + ")V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, className, "router", routerType); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); // public final void handleRequest(Request request, HttpSession session) throws IOException; mv = cv.visitMethod(ACC_PUBLIC | ACC_FINAL, "handleRequest", "(Lone/nio/http/Request;Lone/nio/http/HttpSession;)V", null, null); mv.visitCode(); if (m.getReturnType() == Response.class) { mv.visitVarInsn(ALOAD, 2); } mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, "router", routerType); setupArguments(mv, m); emitInvoke(mv, m); if (m.getReturnType() == Response.class) { mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/http/HttpSession", "writeResponse", "(Lone/nio/http/Response;)V"); } mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); cv.visitEnd(); return instantiate(cv.toByteArray(), m, router); }
From source file:one.nio.serial.gen.DelegateGenerator.java
License:Apache License
private static void generateConstructor(ClassVisitor cv) { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode();//from www . j av a2 s. co m mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, MAGIC_CLASS, "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:one.nio.serial.gen.DelegateGenerator.java
License:Apache License
private static void generateCalcSize(ClassVisitor cv, Class cls, FieldDescriptor[] fds) { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_FINAL, "calcSize", "(Ljava/lang/Object;Lone/nio/serial/CalcSizeStream;)V", null, new String[] { "java/io/IOException" }); mv.visitCode();//from w w w.j a va 2 s .c o m Method writeObjectMethod = JavaInternals.findMethodRecursively(cls, "writeObject", ObjectOutputStream.class); if (writeObjectMethod != null && !Repository.hasOptions(writeObjectMethod.getDeclaringClass(), Repository.SKIP_WRITE_OBJECT)) { mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(GETSTATIC, "one/nio/serial/gen/NullObjectOutputStream", "INSTANCE", "Lone/nio/serial/gen/NullObjectOutputStream;"); emitInvoke(mv, writeObjectMethod); } int primitiveFieldsSize = 0; for (FieldDescriptor fd : fds) { Field ownField = fd.ownField(); Class sourceClass = fd.type().resolve(); FieldType srcType = FieldType.valueOf(sourceClass); if (srcType != FieldType.Object) { primitiveFieldsSize += srcType.dataSize; } else if (ownField == null) { primitiveFieldsSize++; // 1 byte to encode null reference } else { mv.visitVarInsn(ALOAD, 2); mv.visitVarInsn(ALOAD, 1); if (fd.parentField() != null) emitGetField(mv, fd.parentField()); emitGetField(mv, ownField); emitTypeCast(mv, ownField.getType(), sourceClass); mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/serial/CalcSizeStream", "writeObject", "(Ljava/lang/Object;)V"); } } if (primitiveFieldsSize != 0) { mv.visitVarInsn(ALOAD, 2); mv.visitInsn(DUP); mv.visitFieldInsn(GETFIELD, "one/nio/serial/CalcSizeStream", "count", "I"); emitInt(mv, primitiveFieldsSize); mv.visitInsn(IADD); mv.visitFieldInsn(PUTFIELD, "one/nio/serial/CalcSizeStream", "count", "I"); } mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:one.nio.serial.gen.DelegateGenerator.java
License:Apache License
private static void generateWrite(ClassVisitor cv, Class cls, FieldDescriptor[] fds) { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_FINAL, "write", "(Ljava/lang/Object;Lone/nio/serial/DataStream;)V", null, new String[] { "java/io/IOException" }); mv.visitCode();/*from w w w . j a v a2 s. co m*/ Method writeObjectMethod = JavaInternals.findMethodRecursively(cls, "writeObject", ObjectOutputStream.class); if (writeObjectMethod != null && !Repository.hasOptions(writeObjectMethod.getDeclaringClass(), Repository.SKIP_WRITE_OBJECT)) { mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(GETSTATIC, "one/nio/serial/gen/NullObjectOutputStream", "INSTANCE", "Lone/nio/serial/gen/NullObjectOutputStream;"); emitInvoke(mv, writeObjectMethod); } for (FieldDescriptor fd : fds) { Field ownField = fd.ownField(); Class sourceClass = fd.type().resolve(); FieldType srcType = FieldType.valueOf(sourceClass); mv.visitVarInsn(ALOAD, 2); if (ownField == null) { mv.visitInsn(FieldType.Void.convertTo(srcType)); } else { mv.visitVarInsn(ALOAD, 1); if (fd.parentField() != null) emitGetField(mv, fd.parentField()); emitGetField(mv, ownField); emitTypeCast(mv, ownField.getType(), sourceClass); } mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/serial/DataStream", srcType.writeMethod(), srcType.writeSignature()); } mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:one.nio.serial.gen.DelegateGenerator.java
License:Apache License
private static void generateRead(ClassVisitor cv, Class cls, FieldDescriptor[] fds, List<Field> defaultFields) { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_FINAL, "read", "(Lone/nio/serial/DataStream;)Ljava/lang/Object;", null, new String[] { "java/io/IOException", "java/lang/ClassNotFoundException" }); mv.visitCode();//from w w w. j a v a 2s.c om mv.visitVarInsn(ALOAD, 1); mv.visitTypeInsn(NEW, Type.getInternalName(cls)); mv.visitInsn(DUP); mv.visitVarInsn(ASTORE, 2); mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/serial/DataStream", "register", "(Ljava/lang/Object;)V"); ArrayList<Field> parents = new ArrayList<Field>(1); for (FieldDescriptor fd : fds) { Field ownField = fd.ownField(); Field parentField = fd.parentField(); Class sourceClass = fd.type().resolve(); FieldType srcType = FieldType.valueOf(sourceClass); if (parentField != null && !parents.contains(parentField)) { parents.add(parentField); mv.visitFieldInsn(GETSTATIC, "one/nio/util/JavaInternals", "unsafe", "Lsun/misc/Unsafe;"); mv.visitVarInsn(ALOAD, 2); mv.visitLdcInsn(unsafe.objectFieldOffset(parentField)); mv.visitTypeInsn(NEW, Type.getInternalName(parentField.getType())); mv.visitMethodInsn(INVOKESPECIAL, "sun/misc/Unsafe", "putObject", "(Ljava/lang/Object;JLjava/lang/Object;)V"); } if (ownField == null) { mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/serial/DataStream", srcType.readMethod(), srcType.readSignature()); mv.visitInsn(srcType.convertTo(FieldType.Void)); } else if (Modifier.isFinal(ownField.getModifiers())) { FieldType dstType = FieldType.valueOf(ownField.getType()); mv.visitFieldInsn(GETSTATIC, "one/nio/util/JavaInternals", "unsafe", "Lsun/misc/Unsafe;"); mv.visitVarInsn(ALOAD, 2); if (parentField != null) emitGetField(mv, parentField); mv.visitLdcInsn(unsafe.objectFieldOffset(ownField)); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/serial/DataStream", srcType.readMethod(), srcType.readSignature()); if (srcType == FieldType.Object) emitTypeCast(mv, Object.class, sourceClass); emitTypeCast(mv, sourceClass, ownField.getType()); mv.visitMethodInsn(INVOKESPECIAL, "sun/misc/Unsafe", dstType.putMethod(), dstType.putSignature()); } else { mv.visitVarInsn(ALOAD, 2); if (parentField != null) emitGetField(mv, parentField); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/serial/DataStream", srcType.readMethod(), srcType.readSignature()); if (srcType == FieldType.Object) emitTypeCast(mv, Object.class, sourceClass); emitTypeCast(mv, sourceClass, ownField.getType()); emitPutField(mv, ownField); } } if (defaultFields != null && !defaultFields.isEmpty()) { for (Field defaultField : defaultFields) { String defaultValue = defaultField.getAnnotation(Default.class).value(); putFieldConstant(mv, defaultField, defaultValue); } } Method readObjectMethod = JavaInternals.findMethodRecursively(cls, "readObject", ObjectInputStream.class); if (readObjectMethod != null && !Repository.hasOptions(readObjectMethod.getDeclaringClass(), Repository.SKIP_READ_OBJECT)) { mv.visitVarInsn(ALOAD, 2); mv.visitFieldInsn(GETSTATIC, "one/nio/serial/gen/NullObjectInputStream", "INSTANCE", "Lone/nio/serial/gen/NullObjectInputStream;"); emitInvoke(mv, readObjectMethod); } mv.visitVarInsn(ALOAD, 2); mv.visitInsn(ARETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:one.nio.serial.gen.DelegateGenerator.java
License:Apache License
private static void generateSkip(ClassVisitor cv, FieldDescriptor[] fds) { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_FINAL, "skip", "(Lone/nio/serial/DataStream;)V", null, new String[] { "java/io/IOException", "java/lang/ClassNotFoundException" }); mv.visitCode();/* w ww .ja v a 2s . co m*/ int skipSize = 0; for (FieldDescriptor fd : fds) { Class sourceClass = fd.type().resolve(); FieldType srcType = FieldType.valueOf(sourceClass); if (srcType != FieldType.Object) { skipSize += srcType.dataSize; } else { if (skipSize > 0) { mv.visitVarInsn(ALOAD, 1); emitInt(mv, skipSize); mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/serial/DataStream", "skipBytes", "(I)I"); mv.visitInsn(POP); skipSize = 0; } mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/serial/DataStream", "readObject", "()Ljava/lang/Object;"); mv.visitInsn(POP); } } if (skipSize > 0) { mv.visitVarInsn(ALOAD, 1); emitInt(mv, skipSize); mv.visitMethodInsn(INVOKEVIRTUAL, "one/nio/serial/DataStream", "skipBytes", "(I)I"); mv.visitInsn(POP); } mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); }