/** * Visits a field instruction. A field instruction is an instruction that loads or stores the * value of a field of an object. * * @param opcode the opcode of the type instruction to be visited. This opcode is either * GETSTATIC, PUTSTATIC, GETFIELD or PUTFIELD. * @param owner the internal name of the field's owner class (see {@link Type#getInternalName()}). * @param name the field's name. * @param descriptor the field's descriptor (see {@link Type}). */ public void visitFieldInsn( final int opcode, final String owner, final String name, final String descriptor) { if (mv != null) { mv.visitFieldInsn(opcode, owner, name, descriptor); } }
/** * Visits a field instruction. A field instruction is an instruction that loads or stores the * value of a field of an object. * * @param opcode the opcode of the type instruction to be visited. This opcode is either * GETSTATIC, PUTSTATIC, GETFIELD or PUTFIELD. * @param owner the internal name of the field's owner class (see {@link Type#getInternalName()}). * @param name the field's name. * @param descriptor the field's descriptor (see {@link Type}). */ public void visitFieldInsn( final int opcode, final String owner, final String name, final String descriptor) { if (mv != null) { mv.visitFieldInsn(opcode, owner, name, descriptor); } }
if (this.type.isPrimitive()) { if (this.type == Boolean.TYPE) { mv.visitFieldInsn(GETSTATIC, "java/lang/Boolean", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Byte", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Character", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Double", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Float", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Integer", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Long", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Short", "TYPE", "Ljava/lang/Class;");
if (this.type.isPrimitive()) { if (this.type == Boolean.TYPE) { mv.visitFieldInsn(GETSTATIC, "java/lang/Boolean", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Byte", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Character", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Double", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Float", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Integer", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Long", "TYPE", "Ljava/lang/Class;"); mv.visitFieldInsn(GETSTATIC, "java/lang/Short", "TYPE", "Ljava/lang/Class;");
@Override public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) { boolean isStatic = Modifier.isStatic(this.member.getModifiers()); String descriptor = cf.lastDescriptor(); String classDesc = this.member.getDeclaringClass().getName().replace('.', '/'); if (!isStatic) { if (descriptor == null) { cf.loadTarget(mv); } if (descriptor == null || !classDesc.equals(descriptor.substring(1))) { mv.visitTypeInsn(CHECKCAST, classDesc); } } else { if (descriptor != null) { // A static field/method call will not consume what is on the stack, // it needs to be popped off. mv.visitInsn(POP); } } if (this.member instanceof Method) { mv.visitMethodInsn((isStatic ? INVOKESTATIC : INVOKEVIRTUAL), classDesc, this.member.getName(), CodeFlow.createSignatureDescriptor((Method) this.member), false); } else { mv.visitFieldInsn((isStatic ? GETSTATIC : GETFIELD), classDesc, this.member.getName(), CodeFlow.toJvmDescriptor(((Field) this.member).getType())); } } }
@Override public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) { boolean isStatic = Modifier.isStatic(this.member.getModifiers()); String descriptor = cf.lastDescriptor(); String classDesc = this.member.getDeclaringClass().getName().replace('.', '/'); if (!isStatic) { if (descriptor == null) { cf.loadTarget(mv); } if (descriptor == null || !classDesc.equals(descriptor.substring(1))) { mv.visitTypeInsn(CHECKCAST, classDesc); } } else { if (descriptor != null) { // A static field/method call will not consume what is on the stack, // it needs to be popped off. mv.visitInsn(POP); } } if (this.member instanceof Method) { mv.visitMethodInsn((isStatic ? INVOKESTATIC : INVOKEVIRTUAL), classDesc, this.member.getName(), CodeFlow.createSignatureDescriptor((Method) this.member), false); } else { mv.visitFieldInsn((isStatic ? GETSTATIC : GETFIELD), classDesc, this.member.getName(), CodeFlow.toJvmDescriptor(((Field) this.member).getType())); } } }
@Override public void generateCode(MethodVisitor mv, CodeFlow codeflow) { final String constantFieldName = "inlineList$" + codeflow.nextFieldId(); final String className = codeflow.getClassName(); codeflow.registerNewField((cw, cflow) -> cw.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, constantFieldName, "Ljava/util/List;", null, null)); codeflow.registerNewClinit((mVisitor, cflow) -> generateClinitCode(className, constantFieldName, mVisitor, cflow, false)); mv.visitFieldInsn(GETSTATIC, className, constantFieldName, "Ljava/util/List;"); codeflow.pushDescriptor("Ljava/util/List"); }
mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false); if (!nested) { mv.visitFieldInsn(PUTSTATIC, clazzname, constantFieldName, "Ljava/util/List;"); mv.visitFieldInsn(GETSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
mv.visitFieldInsn((isStatic ? GETSTATIC : GETFIELD), classDesc, member.getName(), CodeFlow.toJvmDescriptor(((Field) member).getType()));
@Override public void generateCode(MethodVisitor mv, CodeFlow codeflow) { final String constantFieldName = "inlineList$" + codeflow.nextFieldId(); final String className = codeflow.getClassName(); codeflow.registerNewField((cw, cflow) -> cw.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, constantFieldName, "Ljava/util/List;", null, null)); codeflow.registerNewClinit((mVisitor, cflow) -> generateClinitCode(className, constantFieldName, mVisitor, cflow, false)); mv.visitFieldInsn(GETSTATIC, className, constantFieldName, "Ljava/util/List;"); codeflow.pushDescriptor("Ljava/util/List"); }
mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false); if (!nested) { mv.visitFieldInsn(PUTSTATIC, clazzname, constantFieldName, "Ljava/util/List;"); mv.visitFieldInsn(GETSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
mv.visitFieldInsn((isStatic ? GETSTATIC : GETFIELD), classDesc, member.getName(), CodeFlow.toJvmDescriptor(((Field) member).getType()));
String descriptor = readUTF8(nameAndTypeCpInfoOffset + 2, charBuffer); if (opcode < Opcodes.INVOKEVIRTUAL) { methodVisitor.visitFieldInsn(opcode, owner, name, descriptor); } else { boolean isInterface =
String descriptor = readUTF8(nameAndTypeCpInfoOffset + 2, charBuffer); if (opcode < Opcodes.INVOKEVIRTUAL) { methodVisitor.visitFieldInsn(opcode, owner, name, descriptor); } else { boolean isInterface =
/** * Visits a field instruction. A field instruction is an instruction that loads or stores the * value of a field of an object. * * @param opcode the opcode of the type instruction to be visited. This opcode is either * GETSTATIC, PUTSTATIC, GETFIELD or PUTFIELD. * @param owner the internal name of the field's owner class (see {@link Type#getInternalName()}). * @param name the field's name. * @param descriptor the field's descriptor (see {@link Type}). */ public void visitFieldInsn( final int opcode, final String owner, final String name, final String descriptor) { if (mv != null) { mv.visitFieldInsn(opcode, owner, name, descriptor); } }
/** * Visits a field instruction. A field instruction is an instruction that loads or stores the * value of a field of an object. * * @param opcode the opcode of the type instruction to be visited. This opcode is either * GETSTATIC, PUTSTATIC, GETFIELD or PUTFIELD. * @param owner the internal name of the field's owner class (see {@link Type#getInternalName()}). * @param name the field's name. * @param descriptor the field's descriptor (see {@link Type}). */ public void visitFieldInsn( final int opcode, final String owner, final String name, final String descriptor) { if (mv != null) { mv.visitFieldInsn(opcode, owner, name, descriptor); } }
/** * 创建get方法 * @param cw * @param classGenerator */ public static void visitGetMethod(ClassWriter cw,ClassGeneratorEntity classGenerator) { classGenerator.getFieldGenerators().forEach(fieldGenerator -> { String methodName = "get" + String.valueOf(Character.toUpperCase(fieldGenerator.getName().charAt(0))) + fieldGenerator.getName().substring(1); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "()" + fieldGenerator.getDescriptor(), null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, classGenerator.getGeneratorName(), fieldGenerator.getName(), fieldGenerator.getDescriptor()); mv.visitInsn(ARETURN); mv.visitMaxs(0, 1); mv.visitEnd(); }); }
/** * 创建set方法 * @param cw * @param classGenerator */ public static void visitSetMethod(ClassWriter cw,ClassGeneratorEntity classGenerator) { classGenerator.getFieldGenerators().forEach(fieldGenerator -> { String methodName = "set" + String.valueOf(Character.toUpperCase(fieldGenerator.getName().charAt(0))) + fieldGenerator.getName().substring(1); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "(" + fieldGenerator.getDescriptor() + ")V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); // set 入参 mv.visitVarInsn(ALOAD, 1); mv.visitFieldInsn(PUTFIELD, classGenerator.getGeneratorName(), fieldGenerator.getName(), fieldGenerator.getDescriptor()); mv.visitInsn(RETURN); mv.visitMaxs(1, 2); mv.visitEnd(); }); }
@Override public void generateCode(MethodVisitor mv, CodeFlow codeflow) { final String constantFieldName = "inlineList$" + codeflow.nextFieldId(); final String className = codeflow.getClassName(); codeflow.registerNewField((cw, cflow) -> cw.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, constantFieldName, "Ljava/util/List;", null, null)); codeflow.registerNewClinit((mVisitor, cflow) -> generateClinitCode(className, constantFieldName, mVisitor, cflow, false)); mv.visitFieldInsn(GETSTATIC, className, constantFieldName, "Ljava/util/List;"); codeflow.pushDescriptor("Ljava/util/List"); }
private static void visitBeanGetter(PersistentEntity<?, ?> entity, String internalClassName, ClassWriter cw) { // public Object getBean() MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getBean", String.format("()%s", referenceName(JAVA_LANG_OBJECT)), null, null); mv.visitCode(); Label l0 = new Label(); // return this.bean mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, internalClassName, BEAN_FIELD, getAccessibleTypeReferenceName(entity)); mv.visitInsn(ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable(THIS_REF, referenceName(internalClassName), null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); }