Example usage for org.objectweb.asm Opcodes ACC_SYNTHETIC

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

Introduction

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

Prototype

int ACC_SYNTHETIC

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

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 ww  w . j av  a 2 s  .  co  m*/
 * @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. j a v  a  2 s  .com
 * @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.apache.commons.weaver.privilizer.ActionGenerator.java

License:Apache License

/**
 * We must add special methods for inner classes to invoke their owners' methods, according to the scheme "access$n"
 * where n is the index into this (ordered) map. Additionally we will prefix the whole thing like we usually do
 * (__privileged_)://from  ww w.j a  v a 2s  .  co  m
 */
private void generateHelper() {
    owner.privilizer().env.debug("Generating static helper method %s.%s to call %s",
            owner.target.getClassName(), helper, impl);
    final GeneratorAdapter mgen = new GeneratorAdapter(Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC, helper, null,
            exceptions, owner);

    mgen.visitCode();
    mgen.loadArgs();
    if (implIsStatic) {
        mgen.invokeStatic(owner.target, impl);
    } else {
        mgen.invokeVirtual(owner.target, impl);
    }
    mgen.returnValue();
    mgen.endMethod();
}

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   w w  w . j  av a2 s  .co  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.BlueprintingVisitor.java

License:Apache License

private String importMethod(final Pair<Type, Method> key) {
    if (importedMethods.containsKey(key)) {
        return importedMethods.get(key);
    }/*from w w w  .j  a v  a 2  s.  c om*/
    final String result = new StringBuilder(key.getLeft().getInternalName().replace('/', '_')).append("$$")
            .append(key.getRight().getName()).toString();
    importedMethods.put(key, result);
    privilizer().env.debug("importing %s#%s as %s", key.getLeft().getClassName(), key.getRight(), result);
    final int access = Opcodes.ACC_PRIVATE + Opcodes.ACC_STATIC + Opcodes.ACC_SYNTHETIC;

    final MethodNode source = typeInfo(key.getLeft()).methods.get(key.getRight());

    final String[] exceptions = source.exceptions.toArray(ArrayUtils.EMPTY_STRING_ARRAY);

    // non-public fields accessed
    final Set<FieldAccess> fieldAccesses = new LinkedHashSet<>();

    source.accept(new MethodVisitor(Privilizer.ASM_VERSION) {
        @Override
        public void visitFieldInsn(final int opcode, final String owner, final String name, final String desc) {
            final FieldAccess fieldAccess = fieldAccess(Type.getObjectType(owner), name);

            super.visitFieldInsn(opcode, owner, name, desc);
            if (!Modifier.isPublic(fieldAccess.access)) {
                fieldAccesses.add(fieldAccess);
            }
        }
    });

    final MethodNode withAccessibleAdvice = new MethodNode(access, result, source.desc, source.signature,
            exceptions);

    // spider own methods:
    MethodVisitor mv = new NestedMethodInvocationHandler(withAccessibleAdvice, key); //NOPMD

    if (!fieldAccesses.isEmpty()) {
        mv = new AccessibleAdvisor(mv, access, result, source.desc, new ArrayList<>(fieldAccesses));
    }
    source.accept(mv);

    // private can only be called by other privileged methods, so no need to mark as privileged
    if (!Modifier.isPrivate(source.access)) {
        withAccessibleAdvice.visitAnnotation(Type.getType(Privileged.class).getDescriptor(), false).visitEnd();
    }
    withAccessibleAdvice.accept(this.cv);

    return result;
}

From source file:org.apache.felix.tool.mangen.ASMClassScanner.java

License:Apache License

public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    //System.out.println("visitField: " + name + " : " + desc + " : " + signature + " : " + value);
    // Capture field type dependency.
    addField(name, desc, (access & Opcodes.ACC_SYNTHETIC) != 0);
    return null;/*from  w w  w .  j  av  a  2  s.  c o m*/
}

From source file:org.apache.felix.tool.mangen.ASMClassScanner.java

License:Apache License

public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    //System.out.println("visitMethod: " + name + " : " + desc + " : " + signature);
    // Capture declared method exception type dependencies.
    for (int i = 0; (exceptions != null) && (i < exceptions.length); i++) {
        //System.out.println("visitField exceptions: " + exceptions[i]);
        addConstantClass(exceptions[i]);
    }/*from w  w w  .  j a  v  a  2s  .  com*/
    // Capture declared method signature type dependencies.
    addMethod(name, desc, (access & Opcodes.ACC_SYNTHETIC) != 0);
    return this;
}

From source file:org.apache.groovy.parser.antlr4.AstBuilder.java

License:Apache License

@Override
public ClassNode visitClassDeclaration(ClassDeclarationContext ctx) {
    String packageName = moduleNode.getPackageName();
    packageName = null != packageName ? packageName : "";

    List<ModifierNode> modifierNodeList = ctx.getNodeMetaData(TYPE_DECLARATION_MODIFIERS);
    Objects.requireNonNull(modifierNodeList, "modifierNodeList should not be null");

    ModifierManager modifierManager = new ModifierManager(this, modifierNodeList);
    int modifiers = modifierManager.getClassModifiersOpValue();

    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;

    final ClassNode outerClass = classNodeStack.peek();
    ClassNode classNode;/*from www.j av a  2  s  . c  o  m*/
    String className = this.visitIdentifier(ctx.identifier());

    if (VAR_STR.equals(className)) {
        throw createParsingFailedException("var cannot be used for type declarations", ctx.identifier());
    }

    if (asBoolean(ctx.ENUM())) {
        classNode = EnumHelper.makeEnumNode(asBoolean(outerClass) ? className : packageName + className,
                modifiers, null, outerClass);
    } else {
        if (asBoolean(outerClass)) {
            classNode = new InnerClassNode(outerClass, outerClass.getName() + "$" + className,
                    modifiers | (outerClass.isInterface() ? Opcodes.ACC_STATIC : 0), ClassHelper.OBJECT_TYPE);
        } else {
            classNode = new ClassNode(packageName + className, modifiers, ClassHelper.OBJECT_TYPE);
        }
    }

    configureAST(classNode, ctx);
    classNode.putNodeMetaData(CLASS_NAME, className);
    classNode.setSyntheticPublic(syntheticPublic);

    if (asBoolean(ctx.TRAIT())) {
        attachTraitAnnotation(classNode);
    }
    classNode.addAnnotations(modifierManager.getAnnotations());
    classNode.setGenericsTypes(this.visitTypeParameters(ctx.typeParameters()));

    boolean isInterface = asBoolean(ctx.INTERFACE()) && !asBoolean(ctx.AT());
    boolean isInterfaceWithDefaultMethods = false;

    // declaring interface with default method
    if (isInterface && this.containsDefaultMethods(ctx)) {
        isInterfaceWithDefaultMethods = true;
        attachTraitAnnotation(classNode);
        classNode.putNodeMetaData(IS_INTERFACE_WITH_DEFAULT_METHODS, true);
    }

    if (asBoolean(ctx.CLASS()) || asBoolean(ctx.TRAIT()) || isInterfaceWithDefaultMethods) { // class OR trait OR interface with default methods
        classNode.setSuperClass(this.visitType(ctx.sc));
        classNode.setInterfaces(this.visitTypeList(ctx.is));

        this.initUsingGenerics(classNode);
    } else if (isInterface) { // interface(NOT annotation)
        classNode.setModifiers(classNode.getModifiers() | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT);

        classNode.setSuperClass(ClassHelper.OBJECT_TYPE);
        classNode.setInterfaces(this.visitTypeList(ctx.scs));

        this.initUsingGenerics(classNode);

        this.hackMixins(classNode);
    } else if (asBoolean(ctx.ENUM())) { // enum
        classNode.setModifiers(classNode.getModifiers() | Opcodes.ACC_ENUM | Opcodes.ACC_FINAL);

        classNode.setInterfaces(this.visitTypeList(ctx.is));

        this.initUsingGenerics(classNode);
    } else if (asBoolean(ctx.AT())) { // annotation
        classNode.setModifiers(classNode.getModifiers() | Opcodes.ACC_INTERFACE | Opcodes.ACC_ABSTRACT
                | Opcodes.ACC_ANNOTATION);

        classNode.addInterface(ClassHelper.Annotation_TYPE);

        this.hackMixins(classNode);
    } else {
        throw createParsingFailedException("Unsupported class declaration: " + ctx.getText(), ctx);
    }

    // we put the class already in output to avoid the most inner classes
    // will be used as first class later in the loader. The first class
    // there determines what GCL#parseClass for example will return, so we
    // have here to ensure it won't be the inner class
    if (asBoolean(ctx.CLASS()) || asBoolean(ctx.TRAIT())) {
        classNodeList.add(classNode);
    }

    classNodeStack.push(classNode);
    ctx.classBody().putNodeMetaData(CLASS_DECLARATION_CLASS_NODE, classNode);
    this.visitClassBody(ctx.classBody());
    classNodeStack.pop();

    if (!(asBoolean(ctx.CLASS()) || asBoolean(ctx.TRAIT()))) {
        classNodeList.add(classNode);
    }

    groovydocManager.handle(classNode, ctx);

    return classNode;
}

From source file:org.apache.groovy.parser.antlr4.ModifierManager.java

License:Apache License

private int calcModifiersOpValue(int t) {
    int result = 0;

    for (ModifierNode modifierNode : modifierNodeList) {
        result |= modifierNode.getOpcode();
    }//from  w  ww  .  ja v  a  2s . c  om

    if (!this.containsVisibilityModifier()) {
        if (1 == t) {
            result |= Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PUBLIC;
        } else if (2 == t) {
            result |= Opcodes.ACC_PUBLIC;
        }
    }

    return result;
}

From source file:org.apache.lucene.expressions.js.XJavascriptCompiler.java

License:Apache License

private void beginCompile() {
    classWriter.visit(CLASSFILE_VERSION,
            Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC,
            COMPILED_EXPRESSION_INTERNAL, null, EXPRESSION_TYPE.getInternalName(), null);
    String clippedSourceText = (sourceText.length() <= MAX_SOURCE_LENGTH) ? sourceText
            : (sourceText.substring(0, MAX_SOURCE_LENGTH - 3) + "...");
    classWriter.visitSource(clippedSourceText, null);

    GeneratorAdapter constructor = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC,
            EXPRESSION_CTOR, null, null, classWriter);
    constructor.loadThis();//  w w w  . j  av a 2  s.  com
    constructor.loadArgs();
    constructor.invokeConstructor(EXPRESSION_TYPE, EXPRESSION_CTOR);
    constructor.returnValue();
    constructor.endMethod();

    gen = new GeneratorAdapter(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, EVALUATE_METHOD, null, null,
            classWriter);
}