Example usage for org.objectweb.asm Opcodes ACC_PUBLIC

List of usage examples for org.objectweb.asm Opcodes ACC_PUBLIC

Introduction

In this page you can find the example usage for org.objectweb.asm Opcodes ACC_PUBLIC.

Prototype

int ACC_PUBLIC

To view the source code for org.objectweb.asm Opcodes ACC_PUBLIC.

Click Source Link

Usage

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds an empty constructor.// ww w .j a  va 2 s.com
 * @param writer the target class
 * @param block the constructor block
 */
public static void defineEmptyConstructor(ClassWriter writer, Consumer<MethodVisitor> block) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE), null, null);
    block.accept(method);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds an empty constructor./*from   w w  w.  ja  v  a  2  s  . c o m*/
 * @param writer the target class
 * @param superClass the super class
 * @param body the constructor body
 */
public static void defineEmptyConstructor(ClassWriter writer, Class<?> superClass,
        Consumer<MethodVisitor> body) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE), null, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(superClass).getInternalName(), CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE), false);
    body.accept(method);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds an adapter constructor.//from w  ww  .j  a  v a2s .c  o m
 * @param writer the target class
 * @param superClass the super class
 * @param body the constructor body
 */
public static void defineAdapterConstructor(ClassWriter writer, Class<?> superClass,
        Consumer<MethodVisitor> body) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(VertexProcessorContext.class)), null, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitVarInsn(Opcodes.ALOAD, 1);
    method.visitMethodInsn(Opcodes.INVOKESPECIAL, typeOf(superClass).getInternalName(), CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(VertexProcessorContext.class)), false);
    body.accept(method);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds a getter-like method.// w ww  .j  av  a  2s.  c  o m
 * @param writer the target class
 * @param type the method result type
 * @param name the method name
 * @param body the method body
 */
public static void defineGetter(ClassWriter writer, Type type, String name, Consumer<MethodVisitor> body) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, name, Type.getMethodDescriptor(type), null,
            new String[0]);
    body.accept(method);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds {@link Result#add(Object)} method.
 * @param writer the current writer//ww w.  j a  v  a2  s .c om
 * @param callback the callback
 */
public static void defineResultAdd(ClassVisitor writer, Consumer<MethodVisitor> callback) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "add",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class)), null, null);
    callback.accept(method);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

/**
 * Adds constant {@code toString()} method.
 * @param writer the current writer//w w  w  . j  a  va  2s .c o  m
 * @param value the constant value
 */
public static void defineToString(ClassVisitor writer, String value) {
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, "toString",
            Type.getMethodDescriptor(typeOf(String.class)), null, null);
    getConst(method, value);
    method.visitInsn(Opcodes.ARETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java

License:Apache License

private static List<Tuple<VertexElement, FieldRef>> defineDependenciesConstructor0(ClassDescription aClass,
        ClassVisitor writer, Iterable<? extends VertexElement> dependencies,
        Consumer<MethodVisitor> superConstructor, Consumer<MethodVisitor> callback) {
    List<Tuple<VertexElement, FieldRef>> results = new ArrayList<>();
    int index = 0;
    int varIndex = 1;
    List<Type> parameterTypes = new ArrayList<>();
    List<Consumer<MethodVisitor>> ctor = new ArrayList<>();
    for (VertexElement element : dependencies) {
        int current = index++;
        int currentVar = varIndex;
        String name = getDependencyId(current);
        Type type = typeOf(element.getRuntimeType());
        parameterTypes.add(type);/*from  ww w.  jav  a  2 s  .  c  o m*/
        FieldRef ref = defineField(writer, aClass, name, type);
        ctor.add(v -> {
            v.visitVarInsn(Opcodes.ALOAD, 0);
            v.visitVarInsn(loadOpcodeOf(element.getRuntimeType()), currentVar);
            putField(v, ref);
        });
        varIndex += categoryOf(element.getRuntimeType());
        results.add(new Tuple<>(element, ref));
    }
    MethodVisitor method = writer.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME,
            Type.getMethodDescriptor(Type.VOID_TYPE, parameterTypes.stream().toArray(Type[]::new)), null, null);
    superConstructor.accept(method);
    ctor.forEach(c -> c.accept(method));
    callback.accept(method);
    method.visitInsn(Opcodes.RETURN);
    method.visitMaxs(0, 0);
    method.visitEnd();
    return results;
}

From source file:com.asakusafw.dag.compiler.codegen.DataComparatorGenerator.java

License:Apache License

private static void defineCompare(ClassWriter writer, DataModelReference reference,
        List<Group.Ordering> orderings) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, "compare", DESC_COMPARE, null,
            new String[] { typeOf(IOException.class).getInternalName(), });
    LocalVarRef a = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef b = new LocalVarRef(Opcodes.ALOAD, 2);
    for (Group.Ordering ordering : orderings) {
        PropertyReference property = Invariants
                .requireNonNull(reference.findProperty(ordering.getPropertyName()));

        // int diff = ValueOptionSerDe.compareT({a, b}, {b, a});
        switch (ordering.getDirection()) {
        case ASCENDANT:
            a.load(v);//from w  w  w .ja v  a2 s.co  m
            b.load(v);
            break;
        case DESCENDANT:
            b.load(v);
            a.load(v);
            break;
        default:
            throw new AssertionError(ordering);
        }
        v.visitMethodInsn(Opcodes.INVOKESTATIC, typeOf(ValueOptionSerDe.class).getInternalName(),
                Invariants.requireNonNull(METHOD_NAMES.get(property.getType())), DESC_COMPARE, false);
        LocalVarRef cmp = putLocalVar(v, Type.INT, 3);
        Label eq = new Label();

        // if (diff != 0) {
        cmp.load(v);
        v.visitJumpInsn(Opcodes.IFEQ, eq);

        // return diff;
        cmp.load(v);
        v.visitInsn(Opcodes.IRETURN);

        // } @ eq
        v.visitLabel(eq);
    }
    getConst(v, 0);
    v.visitInsn(Opcodes.IRETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.EdgeDataTableAdapterGenerator.java

License:Apache License

private static void defineBuildKey(ClassGeneratorContext context, ClassWriter writer, TypeDescription dataType,
        Group group) {//w w  w.j  a  v  a  2 s .co m
    DataModelReference type = context.getDataModelLoader().load(dataType);
    List<PropertyReference> props = group.getGrouping().stream()
            .map(p -> Invariants.requireNonNull(type.findProperty(p))).collect(Collectors.toList());

    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, "buildKey",
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(KeyBuffer.class), typeOf(Object.class)), null,
            null);

    LocalVarRef key = new LocalVarRef(Opcodes.ALOAD, 1);
    LocalVarRef object = cast(v, 2, dataType);
    for (PropertyReference p : props) {
        key.load(v);
        object.load(v);
        getOption(v, p);
        v.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(KeyBuffer.class).getInternalName(), "append",
                Type.getMethodDescriptor(typeOf(KeyBuffer.class), typeOf(Object.class)), true);
        v.visitInsn(Opcodes.POP);
    }

    v.visitInsn(Opcodes.RETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}

From source file:com.asakusafw.dag.compiler.codegen.KeyValueSerDeGenerator.java

License:Apache License

private static void putSerialize(String methodName, DataModelReference reference,
        List<PropertyReference> properties, ClassWriter writer) {
    MethodVisitor v = writer.visitMethod(Opcodes.ACC_PUBLIC, methodName,
            Type.getMethodDescriptor(Type.VOID_TYPE, typeOf(Object.class), typeOf(DataOutput.class)), null,
            new String[] { typeOf(IOException.class).getInternalName(),
                    typeOf(InterruptedException.class).getInternalName(), });
    if (properties.isEmpty()) {
        LocalVarRef output = new LocalVarRef(Opcodes.ALOAD, 2);
        output.load(v);/*www . j  a  v a 2  s . c o  m*/
        getConst(v, 0);
        v.visitMethodInsn(Opcodes.INVOKEINTERFACE, typeOf(DataOutput.class).getInternalName(), "writeByte",
                Type.getMethodDescriptor(Type.VOID_TYPE, Type.INT_TYPE), true);
    } else {
        LocalVarRef object = cast(v, 1, reference.getDeclaration());
        LocalVarRef output = new LocalVarRef(Opcodes.ALOAD, 2);
        for (PropertyReference property : properties) {
            object.load(v);
            getOption(v, property);
            output.load(v);
            v.visitMethodInsn(Opcodes.INVOKESTATIC, SERDE.getInternalName(), "serialize", Type
                    .getMethodDescriptor(Type.VOID_TYPE, typeOf(property.getType()), typeOf(DataOutput.class)),
                    false);
        }
    }
    v.visitInsn(Opcodes.RETURN);
    v.visitMaxs(0, 0);
    v.visitEnd();
}