Example usage for org.objectweb.asm Opcodes ACC_FINAL

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

Introduction

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

Prototype

int ACC_FINAL

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

Click Source Link

Usage

From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Creates and loads the bean's factory class.
 * @param beanClass the Bean class/*from  w w  w.  j  ava  2 s.com*/
 * @param generatedBeanClassName the name of the class that this factory will produce
 * @param bcd the bean class descriptor
 * @param synchronizeInitializers true to synchronize the initializer invocations (actors
 *       do this), false otherwise
 * @return the new factory
 */
public static BeanFactory generateFactoryClass(Class<?> beanClass, String generatedBeanClassName,
        BeanClassDescriptor bcd, boolean synchronizeInitializers) {
    String className = String.format("%s__BEANFACTORY", beanClass.getName());
    String classNameInternal = className.replace('.', '/');

    String generatedBeanClassNameInternal = generatedBeanClassName.replace('.', '/');

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;
    cw.visit(codeVersion, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_SYNTHETIC,
            classNameInternal, null, "java/lang/Object",
            new String[] { Type.getInternalName(BeanFactory.class) });

    cw.visitSource(null, null);

    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(Opcodes.RETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "createNewInstance",
                "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)Ljava/lang/Object;",
                null, null);
        mv.visitCode();
        final int initCount = bcd.getInitializerCount();
        Label tryStart = new Label();
        Label tryEnd = new Label();
        Label tryFinally = new Label();
        Label tryFinallyEnd = new Label();
        if (synchronizeInitializers && (initCount > 0)) {
            mv.visitTryCatchBlock(tryStart, tryEnd, tryFinally, null);
            mv.visitTryCatchBlock(tryFinally, tryFinallyEnd, tryFinally, null);
        }

        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitTypeInsn(Opcodes.NEW, generatedBeanClassNameInternal);
        mv.visitInsn(Opcodes.DUP);
        mv.visitVarInsn(Opcodes.ALOAD, 1);
        mv.visitVarInsn(Opcodes.ALOAD, 2);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, generatedBeanClassNameInternal, "<init>",
                "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)V");

        if (synchronizeInitializers) {
            mv.visitInsn(Opcodes.DUP);
            mv.visitInsn(Opcodes.MONITORENTER);
            mv.visitLabel(tryStart);
        }

        for (int i = 0; i < initCount; i++) {
            Method m = bcd.getInitializers(i);
            mv.visitInsn(Opcodes.DUP);
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, generatedBeanClassNameInternal, m.getName(),
                    Type.getMethodDescriptor(m));
        }

        if (synchronizeInitializers) {
            if (initCount > 0) {
                mv.visitInsn(Opcodes.DUP);
                mv.visitInsn(Opcodes.MONITOREXIT);
                mv.visitLabel(tryEnd);
                mv.visitJumpInsn(Opcodes.GOTO, tryFinallyEnd);
            }
            mv.visitLabel(tryFinally);
            mv.visitInsn(Opcodes.DUP);
            mv.visitInsn(Opcodes.MONITOREXIT);
            mv.visitLabel(tryFinallyEnd);
        }

        mv.visitInsn(Opcodes.ARETURN);
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", "L" + classNameInternal + ";", null, l0, l1, 0);
        mv.visitLocalVariable("controller", "Lorg/actorsguildframework/internal/Controller;", null, l0, l1, 1);
        mv.visitLocalVariable("props", "Lorg/actorsguildframework/Props;", null, l0, l1, 2);
        mv.visitLocalVariable("synchronizeInitializer", "Z", null, l0, l1, 3);
        mv.visitMaxs(4, 3);
        mv.visitEnd();
    }
    cw.visitEnd();

    Class<?> newClass = GenerationUtils.loadClass(className, cw.toByteArray());
    try {
        return (BeanFactory) newClass.newInstance();
    } catch (Exception e) {
        throw new ConfigurationException("Failure loading ActorProxyFactory", e);
    }
}

From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Creates and loads the bean implementation class.
 * @param beanClass the bean class//from  www  .  jav  a  2s . c om
 * @param bcd the BeanClassDescriptor to use
 * @throws ConfigurationException if the agent is not configured correctly
 */
private static Class<?> generateBeanClass(Class<?> beanClass, BeanClassDescriptor bcd)
        throws NoSuchMethodException {

    String className = String.format("%s__BEAN", beanClass.getName());
    String classNameInternal = className.replace('.', '/');

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    MethodVisitor mv;
    cw.visit(codeVersion, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_SYNTHETIC,
            classNameInternal, null, Type.getInternalName(beanClass), new String[] {});

    cw.visitSource(null, null);

    // write @Prop fields
    writePropFields(bcd, cw);

    // static constructor
    {
        mv = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }

    // constructor(Controller, Props)
    writeConstructor(beanClass, bcd, classNameInternal, cw, null);

    // Write @Prop accessors
    writePropAccessors(bcd, classNameInternal, cw);

    cw.visitEnd();

    try {
        return (Class<?>) GenerationUtils.loadClass(className, cw.toByteArray());
    } catch (Exception e) {
        throw new ConfigurationException("Failure loading generated Bean", e);
    }
}

From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java

License:Apache License

/**
 * Write the fields of @Prop properties.
 * @param bcd the class descriptor//ww w .  j  a v  a  2 s. c  o m
 * @param cw the ClassWriter to write to
 */
public static void writePropFields(BeanClassDescriptor bcd, ClassWriter cw) {
    for (int i = 0; i < bcd.getPropertyCount(); i++) {
        PropertyDescriptor pd = bcd.getProperty(i);
        if (pd.getPropertySource() != PropertySource.ABSTRACT_METHOD)
            continue;

        cw.visitField(Opcodes.ACC_PRIVATE + (pd.getAccess().isWritable() ? 0 : Opcodes.ACC_FINAL),
                String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()),
                Type.getDescriptor(pd.getPropertyClass()), GenericTypeHelper.getSignature(pd.getPropertyType()),
                null).visitEnd();
    }
}

From source file:org.adjective.stout.writer.ByteCodeWriter.java

License:Apache License

private int getModifierCode(Set<ElementModifier> modifiers, MemberType type) {
    int code = getModifierCode(modifiers, (Sort) null);
    int illegal = Opcodes.ACC_ANNOTATION | Opcodes.ACC_BRIDGE | Opcodes.ACC_ENUM | Opcodes.ACC_INTERFACE
            | Opcodes.ACC_SUPER;/* www  . j a v a2  s .c  o  m*/
    switch (type) {
    case FIELD:
        illegal |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_NATIVE | Opcodes.ACC_STRICT | Opcodes.ACC_SYNCHRONIZED;
        break;
    case METHOD:
        if (isBitSet(Opcodes.ACC_ABSTRACT, code)) {
            illegal |= Opcodes.ACC_NATIVE | Opcodes.ACC_STRICT | Opcodes.ACC_SYNCHRONIZED | Opcodes.ACC_STATIC
                    | Opcodes.ACC_FINAL;
        }
        break;
    }
    if (isBitSet(illegal, code)) {
        throw new IllegalStateException(
                "Illegal combination of modifier codes: " + code + " (illegal codes: " + illegal + ")");
    }
    return code;
}

From source file:org.apache.asterix.runtime.evaluators.staticcodegen.EvaluatorMissingCheckVisitor.java

License:Apache License

@Override
public void visitEnd() {
    if (cv != null) {
        cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, TYPE_CHECKER_NAME, TYPE_CHECKER_DESC, null,
                null);//from  w ww . j  a v a 2  s.  c o m
        cv.visitEnd();
    }
}

From source file:org.apache.cassandra.cql3.functions.UDFByteCodeVerifier.java

License:Apache License

public Set<String> verify(byte[] bytes) {
    Set<String> errors = new TreeSet<>(); // it's a TreeSet for unit tests
    ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM5) {
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            errors.add("field declared: " + name);
            return null;
        }//from   www  .  ja v  a2 s. c om

        public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                String[] exceptions) {
            if ("<init>".equals(name) && CTOR_SIG.equals(desc)) {
                if (Opcodes.ACC_PUBLIC != access)
                    errors.add("constructor not public");
                // allowed constructor - JavaUDF(TypeCodec returnCodec, TypeCodec[] argCodecs)
                return new ConstructorVisitor(errors);
            }
            if ("executeImpl".equals(name) && "(ILjava/util/List;)Ljava/nio/ByteBuffer;".equals(desc)) {
                if (Opcodes.ACC_PROTECTED != access)
                    errors.add("executeImpl not protected");
                // the executeImpl method - ByteBuffer executeImpl(int protocolVersion, List<ByteBuffer> params)
                return new ExecuteImplVisitor(errors);
            }
            if ("<clinit>".equals(name)) {
                errors.add("static initializer declared");
            } else {
                errors.add("not allowed method declared: " + name + desc);
                return new ExecuteImplVisitor(errors);
            }
            return null;
        }

        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
            if (!JAVA_UDF_NAME.equals(superName)) {
                errors.add("class does not extend " + JavaUDF.class.getName());
            }
            if (access != (Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER)) {
                errors.add("class not public final");
            }
            super.visit(version, access, name, signature, superName, interfaces);
        }

        public void visitInnerClass(String name, String outerName, String innerName, int access) {
            errors.add("class declared as inner class");
            super.visitInnerClass(name, outerName, innerName, access);
        }
    };

    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classVisitor, ClassReader.SKIP_DEBUG);

    return errors;
}

From source file:org.apache.commons.javaflow.providers.asm3.ContinuableClassVisitor.java

License:Apache License

@Override
public void visitEnd() {
    if (!skipEnchancing) {
        super.visitField(
                (isInterface ? Opcodes.ACC_PUBLIC : Opcodes.ACC_PRIVATE) + Opcodes.ACC_FINAL
                        + Opcodes.ACC_STATIC,
                MaybeContinuableClassVisitor.MARKER_FIELD_NAME, "Ljava/lang/String;", null, "A").visitEnd();
    }//from   w  ww  .j  a va  2s  . c om
    super.visitEnd();
}

From source file:org.apache.commons.weaver.privilizer.ActionGenerator.java

License:Apache License

@SuppressWarnings("PMD.UseVarargs") //not needed
private static Field[] fields(final Type[] args) {
    final Field[] result = new Field[args.length];

    for (int i = 0; i < args.length; i++) {
        final String name = new StringBuilder("f").append(i + 1).toString();
        result[i] = new Field(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, name, args[i]);
    }//from w w  w .  j a v  a  2 s.  c o  m
    return result;
}

From source file:org.apache.commons.weaver.privilizer.ActionGenerator.java

License:Apache License

private void begin() {
    owner.visitInnerClass(action.getInternalName(), owner.className, simpleName,
            Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC);

    final SignatureWriter type = new SignatureWriter();
    final SignatureVisitor actionImplemented = type.visitInterface();
    actionImplemented.visitClassType(actionInterface.getInternalName());
    final SignatureVisitor visitTypeArgument = actionImplemented.visitTypeArgument('=');
    new SignatureReader(Privilizer.wrap(methd.getReturnType()).getDescriptor()).accept(visitTypeArgument);
    actionImplemented.visitEnd();//from  www  .ja  v a  2  s.c  o  m

    final String signature = type.toString();

    visit(Opcodes.V1_5, Opcodes.ACC_SUPER | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_FINAL, action.getInternalName(),
            signature, Type.getType(Object.class).getInternalName(),
            new String[] { actionInterface.getInternalName() });
}

From source file:org.apache.commons.weaver.privilizer.PrivilizingVisitor.java

License:Apache License

@Override
public void visitEnd() {
    annotate();/*from w  w w . ja v  a 2  s  . c om*/
    if (privilizer().policy == Policy.ON_INIT) {
        final String fieldName = privilizer().generateName("hasSecurityManager");

        visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL, fieldName,
                Type.BOOLEAN_TYPE.getDescriptor(), null, null).visitEnd();

        final GeneratorAdapter mgen = new GeneratorAdapter(Opcodes.ACC_STATIC, new Method("<clinit>", "()V"),
                null, Privilizer.EMPTY_TYPE_ARRAY, this);
        checkSecurityManager(mgen);
        mgen.putStatic(target, fieldName, Type.BOOLEAN_TYPE);
        mgen.returnValue();
        mgen.endMethod();
    }
    super.visitEnd();
}