Search in sources :

Example 6 with MethodVisitor

use of dyvilx.tools.asm.MethodVisitor in project Dyvil by Dyvil.

the class AnnotationProxyFactory method generateToString.

private void generateToString(@NonNull ClassWriter classWriter) {
    final MethodVisitor toString = classWriter.visitMethod(PUBLIC, "toString", "()Ljava/lang/String;", null, null);
    toString.visitCode();
    toString.visitLdcInsn('<' + this.annotationType.getName() + ">");
    toString.visitInsn(Opcodes.ARETURN);
    toString.visitMaxs(-1, -1);
    toString.visitEnd();
}
Also used : MethodVisitor(dyvilx.tools.asm.MethodVisitor)

Example 7 with MethodVisitor

use of dyvilx.tools.asm.MethodVisitor in project Dyvil by Dyvil.

the class AnnotationMetadata method write.

@Override
public void write(ClassWriter writer) throws BytecodeException {
    for (IParameter parameter : this.theClass.getParameters()) {
        final StringBuilder desc = new StringBuilder("()");
        parameter.getType().appendExtendedName(desc);
        final MethodVisitor methodVisitor = writer.visitMethod(Modifiers.PUBLIC | Modifiers.ABSTRACT, parameter.getInternalName(), desc.toString(), null, null);
        final IValue argument = parameter.getValue();
        if (argument != null && argument.isAnnotationConstant()) {
            final AnnotationVisitor av = methodVisitor.visitAnnotationDefault();
            argument.writeAnnotationValue(av, parameter.getInternalName());
            av.visitEnd();
        }
        methodVisitor.visitEnd();
    }
}
Also used : IParameter(dyvilx.tools.compiler.ast.parameter.IParameter) IValue(dyvilx.tools.compiler.ast.expression.IValue) AnnotationVisitor(dyvilx.tools.asm.AnnotationVisitor) MethodVisitor(dyvilx.tools.asm.MethodVisitor)

Example 8 with MethodVisitor

use of dyvilx.tools.asm.MethodVisitor in project Dyvil by Dyvil.

the class AnonymousClassLMF method generateFactory.

private void generateFactory() {
    MethodVisitor m = this.cw.visitMethod(PRIVATE | STATIC, NAME_FACTORY, this.invokedType.toMethodDescriptorString(), null, null);
    m.visitCode();
    m.visitTypeInsn(NEW, this.lambdaClassName);
    m.visitInsn(Opcodes.DUP);
    int parameterCount = this.parameterCount;
    for (int typeIndex = 0, varIndex = 0; typeIndex < parameterCount; typeIndex++) {
        Class<?> argType = this.invokedType.parameterType(typeIndex);
        m.visitVarInsn(getLoadOpcode(argType), varIndex);
        varIndex += getParameterSize(argType);
    }
    m.visitMethodInsn(INVOKESPECIAL, this.lambdaClassName, "<init>", this.constructorType.toMethodDescriptorString(), false);
    m.visitInsn(ARETURN);
    m.visitMaxs(-1, -1);
    m.visitEnd();
}
Also used : MethodVisitor(dyvilx.tools.asm.MethodVisitor)

Example 9 with MethodVisitor

use of dyvilx.tools.asm.MethodVisitor in project Dyvil by Dyvil.

the class AnonymousClassLMF method generateSAM.

private void generateSAM() {
    MethodVisitor mv = this.cw.visitMethod(PUBLIC, this.samMethodName, this.samMethodType.toMethodDescriptorString(), null, null);
    mv.visitCode();
    if (this.implKind == MethodHandleInfo.REF_newInvokeSpecial) {
        mv.visitTypeInsn(NEW, this.implMethodClassName);
        mv.visitInsn(DUP);
    }
    for (int i = 0; i < this.parameterCount; i++) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, this.lambdaClassName, this.argNames[i], this.argDescs[i]);
    }
    this.convertArgumentTypes(mv, this.samMethodType);
    // Invoke the method we want to forward to
    mv.visitMethodInsn(invocationOpcode(this.implKind), this.implMethodClassName, this.implMethodName, this.implMethodDesc, this.implDefiningClass.isInterface());
    // Convert the return value (if any) and return it
    // Note: if adapting from non-void to void, the 'return'
    // instruction will pop the unneeded result
    Class<?> samReturnClass = this.samMethodType.returnType();
    TypeConverter.convertType(mv, this.implMethodReturnClass, samReturnClass, samReturnClass);
    mv.visitInsn(getReturnOpcode(samReturnClass));
    // Maxs computed by ClassWriter.COMPUTE_MAXS,these arguments ignored
    mv.visitMaxs(-1, -1);
    mv.visitEnd();
}
Also used : MethodVisitor(dyvilx.tools.asm.MethodVisitor)

Example 10 with MethodVisitor

use of dyvilx.tools.asm.MethodVisitor in project Dyvil by Dyvil.

the class PropertyReferenceMetafactory method generateToString.

private void generateToString(@NonNull ClassWriter classWriter) {
    MethodVisitor toString = classWriter.visitMethod(PUBLIC, "toString", "()Ljava/lang/String;", null, null);
    toString.visitCode();
    toString.visitLdcInsn('<' + this.refClass.getName() + ">");
    toString.visitInsn(Opcodes.ARETURN);
    toString.visitMaxs(-1, -1);
    toString.visitEnd();
}
Also used : MethodVisitor(dyvilx.tools.asm.MethodVisitor)

Aggregations

MethodVisitor (dyvilx.tools.asm.MethodVisitor)15 AnnotationVisitor (dyvilx.tools.asm.AnnotationVisitor)1 IValue (dyvilx.tools.compiler.ast.expression.IValue)1 IParameter (dyvilx.tools.compiler.ast.parameter.IParameter)1