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.batoo.jpa.core.impl.instance.Enhancer.java

License:Open Source License

/**
 * Returns the enhanced class bytecode.//from  ww  w  .j  a  va 2 s .c o  m
 * 
 * @param clazz
 *            the class to enhance
 * @return the enhanced class
 * @throws Exception
 *             thrown in case of an error
 * 
 * @since 2.0.0
 */
//@formatter:off
public static byte[] create(Class<?> clazz) throws Exception {

    final String enhancingClassName = Type.getInternalName(clazz);
    final String enhancedClassName = enhancingClassName + Enhancer.SUFFIX_ENHANCED;
    final String descEnhancer = Enhancer.makeClassDesc(enhancedClassName);

    final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, enhancedClassName, null, enhancingClassName,
            new String[] { Type.getInternalName(EnhancedInstance.class) });

    // Field: serialVersionUID
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
            Enhancer.FIELD_SERIAL_VERSION_UID, Type.getDescriptor(Long.TYPE), null, Long.valueOf(1L))
            .visitEnd();

    // Container fields
    cw.visitField(Opcodes.ACC_PRIVATE, Enhancer.FIELD_ENHANCED_INITIALIZED, Enhancer.DESCRIPTOR_BOOLEAN, null,
            null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE, Enhancer.FIELD_ENHANCED_INTERNAL, Enhancer.DESCRIPTOR_BOOLEAN, null,
            null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_TRANSIENT, Enhancer.FIELD_ENHANCED_ID,
            Enhancer.DESCRIPTOR_OBJECT, null, null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_TRANSIENT, Enhancer.FIELD_ENHANCED_TYPE,
            Enhancer.DESCRIPTOR_CLASS, null, null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL + Opcodes.ACC_TRANSIENT,
            Enhancer.FIELD_ENHANCED_SESSION, Enhancer.DESCRIPTOR_SESSION, null, null).visitEnd();
    cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_TRANSIENT, Enhancer.FIELD_ENHANCED_MANAGED_INSTANCE,
            Enhancer.DESCRIPTOR_MANAGED_INSTANCE, null, null).visitEnd();

    // Constructors
    Enhancer.createNoArgConstructor(enhancingClassName, enhancedClassName, descEnhancer, cw);
    Enhancer.createContainerConstructor(enhancingClassName, enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodIsInitialized(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodSetInitialized(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodCheck(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodGetManagedInstance(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodSetManagedInstance(enhancedClassName, descEnhancer, cw);
    Enhancer.createMethodSetInternal(enhancedClassName, descEnhancer, cw);

    final Map<String, Method> methods = Maps.newHashMap();

    Class<?> currentClass = clazz;
    while (currentClass != Object.class) { // we are not interested in Object.class
        for (final Method method : currentClass.getDeclaredMethods()) {
            int modifiers = method.getModifiers();

            if (Modifier.isAbstract(modifiers) || Modifier.isStatic(modifiers) || Modifier.isPrivate(modifiers)
                    || method.isSynthetic() || method.isBridge()) {
                continue;
            }

            // Filter out the details that we are not interested
            modifiers &= Modifier.ABSTRACT;
            modifiers &= Modifier.FINAL;
            modifiers &= Modifier.NATIVE;
            modifiers &= Modifier.PRIVATE;
            modifiers &= Modifier.PROTECTED;
            modifiers &= Modifier.STATIC;
            modifiers &= Modifier.STRICT;

            if ((modifiers == Modifier.PUBLIC) || (modifiers == 0)) {
                // we are not interested in the return type to omit the overridden methods
                final String desc = method.getName()
                        + Enhancer.makeDescription(Void.TYPE, method.getParameterTypes());

                if (methods.get(desc) == null) {
                    methods.put(desc, method);
                }
            }
        }

        currentClass = currentClass.getSuperclass();
    }

    for (final Method method : methods.values()) {
        if (!Enhancer.IGNORED_METHODS.contains(method.getName())) {
            Enhancer.createOverrriddenMethod(enhancingClassName, enhancedClassName, descEnhancer, cw, method);
        }
    }

    cw.visitEnd();

    return cw.toByteArray();
}

From source file:org.cacheonix.impl.transformer.CacheonixClassAdapter.java

License:LGPL

public void visitEnd() {

    final String signature = null;

    // Add CacheonixConfig Field Now
    if (!bCacheonixConfigPresent) {
        final FieldVisitor fv = cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL,
                CACHEONIX_CONFIG_FILE_FIELD, Type.getType(String.class).getDescriptor(), signature,
                cacheonixConfigFileFieldValue);
        if (fv != null) {
            fv.visitEnd();/*from  w  w  w  .  ja v  a 2 s  .c  o  m*/
        }
    }

    // Add CacheonixCacheName Field Now
    if (!bCacheonixCacheName) {
        final FieldVisitor fv = cv.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL,
                CACHE_NAME_FIELD, Type.getType(String.class).getDescriptor(), signature,
                cacheonixCacheFieldValue);
        if (fv != null) {
            fv.visitEnd();
        }
    }

    cv.visitEnd();
}

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected void enumConstantDef(AST node) {
    enumConstantBeingDef = true;//from w  w w  . java 2 s  .  com
    assertNodeType(ENUM_CONSTANT_DEF, node);
    List<AnnotationNode> annotations = new ArrayList<>();
    AST element = node.getFirstChild();
    if (isType(ANNOTATIONS, element)) {
        processAnnotations(annotations, element);
        element = element.getNextSibling();
    }
    String identifier = identifier(element);
    Expression init = null;
    element = element.getNextSibling();

    if (element != null) {
        init = expression(element);
        ClassNode innerClass;
        if (element.getNextSibling() == null) {
            innerClass = getAnonymousInnerClassNode(init);
            if (innerClass != null) {
                init = null;
            }
        } else {
            element = element.getNextSibling();
            Expression next = expression(element);
            innerClass = getAnonymousInnerClassNode(next);
        }

        if (innerClass != null) {
            // we have to handle an enum constant with a class overriding
            // a method in which case we need to configure the inner class
            innerClass.setSuperClass(classNode.getPlainNodeReference());
            innerClass.setModifiers(classNode.getModifiers() | Opcodes.ACC_FINAL);
            // we use a ClassExpression for transportation to EnumVisitor
            Expression inner = new ClassExpression(innerClass);
            if (init == null) {
                ListExpression le = new ListExpression();
                le.addExpression(inner);
                init = le;
            } else {
                if (init instanceof ListExpression) {
                    ((ListExpression) init).addExpression(inner);
                } else {
                    ListExpression le = new ListExpression();
                    le.addExpression(init);
                    le.addExpression(inner);
                    init = le;
                }
            }
            // and remove the final modifier from classNode to allow the sub class
            classNode.setModifiers(classNode.getModifiers() & ~Opcodes.ACC_FINAL);
        } else if (isType(ELIST, element)) {
            if (init instanceof ListExpression && !((ListExpression) init).isWrapped()) {
                ListExpression le = new ListExpression();
                le.addExpression(init);
                init = le;
            }
        }
    }
    FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
    enumField.addAnnotations(annotations);
    configureAST(enumField, node);
    enumConstantBeingDef = false;
}

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected void constructorDef(AST constructorDef) {
    List<AnnotationNode> annotations = new ArrayList<>();
    AST node = constructorDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_STATIC, "static");
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_FINAL, "final");
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_ABSTRACT, "abstract");
        checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_NATIVE, "native");
        node = node.getNextSibling();//ww  w  .  j  a  va 2s  . c o  m
    }

    assertNodeType(PARAMETERS, node);
    Parameter[] parameters = parameters(node);
    if (parameters == null)
        parameters = Parameter.EMPTY_ARRAY;
    node = node.getNextSibling();

    ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
    if (isType(LITERAL_throws, node)) {
        AST throwsNode = node.getFirstChild();
        List<ClassNode> exceptionList = new ArrayList<>();
        throwsList(throwsNode, exceptionList);
        exceptions = exceptionList.toArray(exceptions);
        node = node.getNextSibling();
    }

    assertNodeType(SLIST, node);
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    ConstructorNode constructorNode = classNode.addConstructor(modifiers, parameters, exceptions, null);
    MethodNode oldMethod = methodNode;
    methodNode = constructorNode;
    Statement code = statementList(node);
    methodNode = oldMethod;
    constructorNode.setCode(code);
    constructorNode.setSyntheticPublic(syntheticPublic);
    constructorNode.addAnnotations(annotations);
    configureAST(constructorNode, constructorDef);
}

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected void fieldDef(AST fieldDef) {
    List<AnnotationNode> annotations = new ArrayList<>();
    AST node = fieldDef.getFirstChild();

    int modifiers = 0;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        node = node.getNextSibling();/*from  w w w  .j a v a 2  s .  com*/
    }

    if (classNode.isInterface()) {
        modifiers |= Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
        if ((modifiers & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
            modifiers |= Opcodes.ACC_PUBLIC;
        }
    }

    ClassNode type = null;
    if (isType(TYPE, node)) {
        type = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }

    String name = identifier(node);
    node = node.getNextSibling();

    Expression initialValue = null;
    if (node != null) {
        assertNodeType(ASSIGN, node);
        initialValue = expression(node.getFirstChild());
    }

    if (classNode.isInterface() && initialValue == null && type != null) {
        initialValue = getDefaultValueForPrimitive(type);
    }

    FieldNode fieldNode = new FieldNode(name, modifiers, type, classNode, initialValue);
    fieldNode.addAnnotations(annotations);
    configureAST(fieldNode, fieldDef);

    if (!hasVisibility(modifiers)) {
        // let's set the modifiers on the field
        int fieldModifiers = 0;
        int flags = Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT | Opcodes.ACC_VOLATILE | Opcodes.ACC_FINAL;

        if (!hasVisibility(modifiers)) {
            modifiers |= Opcodes.ACC_PUBLIC;
            fieldModifiers |= Opcodes.ACC_PRIVATE;
        }

        // let's pass along any other modifiers we need
        fieldModifiers |= (modifiers & flags);
        fieldNode.setModifiers(fieldModifiers);
        fieldNode.setSynthetic(true);

        // in the case that there is already a field, we would
        // like to use that field, instead of the default field
        // for the property
        FieldNode storedNode = classNode.getDeclaredField(fieldNode.getName());
        if (storedNode != null && !classNode.hasProperty(name)) {
            fieldNode = storedNode;
            // we remove it here, because addProperty will add it
            // again and we want to avoid it showing up multiple
            // times in the fields list.
            classNode.getFields().remove(storedNode);
        }

        PropertyNode propertyNode = new PropertyNode(fieldNode, modifiers, null, null);
        configureAST(propertyNode, fieldDef);
        classNode.addProperty(propertyNode);
    } else {
        fieldNode.setModifiers(modifiers);
        // if there is a property of that name, then a field of that
        // name already exists, which means this new field here should
        // be used instead of the field the property originally has.
        PropertyNode pn = classNode.getProperty(name);
        if (pn != null && pn.getField().isSynthetic()) {
            classNode.getFields().remove(pn.getField());
            pn.setField(fieldNode);
        }
        classNode.addField(fieldNode);
    }
}

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected int modifiers(AST modifierNode, List<AnnotationNode> annotations, int defaultModifiers) {
    assertNodeType(MODIFIERS, modifierNode);

    boolean access = false;
    int answer = 0;

    for (AST node = modifierNode.getFirstChild(); node != null; node = node.getNextSibling()) {
        int type = node.getType();
        switch (type) {
        case STATIC_IMPORT:
            // ignore
            break;

        // annotations
        case ANNOTATION:
            annotations.add(annotation(node));
            break;

        // core access scope modifiers
        case LITERAL_private:
            answer = setModifierBit(node, answer, Opcodes.ACC_PRIVATE);
            access = setAccessTrue(node, access);
            break;

        case LITERAL_protected:
            answer = setModifierBit(node, answer, Opcodes.ACC_PROTECTED);
            access = setAccessTrue(node, access);
            break;

        case LITERAL_public:
            answer = setModifierBit(node, answer, Opcodes.ACC_PUBLIC);
            access = setAccessTrue(node, access);
            break;

        // other modifiers
        case ABSTRACT:
            answer = setModifierBit(node, answer, Opcodes.ACC_ABSTRACT);
            break;

        case FINAL:
            answer = setModifierBit(node, answer, Opcodes.ACC_FINAL);
            break;

        case LITERAL_native:
            answer = setModifierBit(node, answer, Opcodes.ACC_NATIVE);
            break;

        case LITERAL_static:
            answer = setModifierBit(node, answer, Opcodes.ACC_STATIC);
            break;

        case STRICTFP:
            answer = setModifierBit(node, answer, Opcodes.ACC_STRICT);
            break;

        case LITERAL_synchronized:
            answer = setModifierBit(node, answer, Opcodes.ACC_SYNCHRONIZED);
            break;

        case LITERAL_transient:
            answer = setModifierBit(node, answer, Opcodes.ACC_TRANSIENT);
            break;

        case LITERAL_volatile:
            answer = setModifierBit(node, answer, Opcodes.ACC_VOLATILE);
            break;

        default:/*from w  ww .  j  a va2  s . c  om*/
            unknownAST(node);
        }
    }
    if (!access) {
        answer |= defaultModifiers;
        // ACC_SYNTHETIC isn't used here, use it as a special flag
        if (defaultModifiers == Opcodes.ACC_PUBLIC)
            answer |= Opcodes.ACC_SYNTHETIC;
    }
    return answer;
}

From source file:org.codehaus.groovy.antlr.AntlrParserPlugin.java

License:Apache License

protected Statement forStatement(AST forNode) {
    AST inNode = forNode.getFirstChild();
    Expression collectionExpression;
    Parameter forParameter;/*from   w w w  .  java2 s  . c o m*/
    if (isType(CLOSURE_LIST, inNode)) {
        forStatementBeingDef = true;
        ClosureListExpression clist = closureListExpression(inNode);
        forStatementBeingDef = false;
        int size = clist.getExpressions().size();
        if (size != 3) {
            throw new ASTRuntimeException(inNode,
                    "3 expressions are required for the classic for loop, you gave " + size);
        }
        collectionExpression = clist;
        forParameter = ForStatement.FOR_LOOP_DUMMY;
    } else {
        AST variableNode = inNode.getFirstChild();
        AST collectionNode = variableNode.getNextSibling();

        ClassNode type = ClassHelper.OBJECT_TYPE;
        if (isType(VARIABLE_DEF, variableNode)) {
            AST node = variableNode.getFirstChild();
            // skip the final modifier if it's present
            if (isType(MODIFIERS, node)) {
                int modifiersMask = modifiers(node, new ArrayList<>(), 0);
                // only final modifier allowed
                if ((modifiersMask & ~Opcodes.ACC_FINAL) != 0) {
                    throw new ASTRuntimeException(node,
                            "Only the 'final' modifier is allowed in front of the for loop variable.");
                }
                node = node.getNextSibling();
            }
            type = makeTypeWithArguments(node);

            variableNode = node.getNextSibling();
        }
        String variable = identifier(variableNode);

        collectionExpression = expression(collectionNode);
        forParameter = new Parameter(type, variable);
        configureAST(forParameter, variableNode);
    }

    final AST node = inNode.getNextSibling();
    Statement block;
    if (isType(SEMI, node)) {
        block = EmptyStatement.INSTANCE;
    } else {
        block = statement(node);
    }
    ForStatement forStatement = new ForStatement(forParameter, collectionExpression, block);
    configureAST(forStatement, forNode);
    return forStatement;
}

From source file:org.codehaus.groovy.antlr.EnumHelper.java

License:Apache License

public static ClassNode makeEnumNode(String name, int modifiers, ClassNode[] interfaces, ClassNode outerClass) {
    modifiers = modifiers | Opcodes.ACC_FINAL | Opcodes.ACC_ENUM;
    ClassNode enumClass;/* w w  w .  j  a v a2s . co  m*/
    if (outerClass == null) {
        enumClass = new ClassNode(name, modifiers, null, interfaces, MixinNode.EMPTY_ARRAY);
    } else {
        name = outerClass.getName() + "$" + name;
        modifiers |= Opcodes.ACC_STATIC;
        enumClass = new InnerClassNode(outerClass, name, modifiers, null, interfaces, MixinNode.EMPTY_ARRAY);
    }

    // set super class and generics info
    // "enum X" -> class X extends Enum<X>
    GenericsType gt = new GenericsType(enumClass);
    ClassNode superClass = ClassHelper.makeWithoutCaching("java.lang.Enum");
    superClass.setGenericsTypes(new GenericsType[] { gt });
    enumClass.setSuperClass(superClass);
    superClass.setRedirect(ClassHelper.Enum_Type);

    return enumClass;
}

From source file:org.codehaus.groovy.classgen.asm.sc.StaticTypesMethodReferenceExpressionWriter.java

License:Apache License

private MethodNode addSyntheticMethodForDGSM(MethodNode mn) {
    Parameter[] parameters = removeFirstParameter(mn.getParameters());
    ArgumentListExpression args = args(parameters);
    args.getExpressions().add(0, ConstantExpression.NULL);

    MethodNode syntheticMethodNode = controller.getClassNode().addSyntheticMethod(
            "dgsm$$" + mn.getParameters()[0].getType().getName().replace(".", "$") + "$$" + mn.getName(),
            Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC,
            mn.getReturnType(), parameters, ClassNode.EMPTY_ARRAY,
            block(returnS(callX(new ClassExpression(mn.getDeclaringClass()), mn.getName(), args))));

    syntheticMethodNode.addAnnotation(new AnnotationNode(ClassHelper.make(Generated.class)));
    syntheticMethodNode//  w  ww.ja va 2  s .  c om
            .addAnnotation(new AnnotationNode(ClassHelper.make(groovy.transform.CompileStatic.class)));

    return syntheticMethodNode;
}

From source file:org.codehaus.groovy.classgen.asm.sc.StaticTypesMethodReferenceExpressionWriter.java

License:Apache License

private MethodNode addSyntheticMethodForConstructorReference(String syntheticMethodName, ClassNode returnType,
        Parameter[] parametersWithExactType) {
    ArgumentListExpression ctorArgs = args(parametersWithExactType);

    MethodNode syntheticMethodNode = controller.getClassNode()
            .addSyntheticMethod(syntheticMethodName,
                    Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC,
                    returnType, parametersWithExactType, ClassNode.EMPTY_ARRAY,
                    block(returnS(//w w w  .  j  a  v  a 2  s  .  c  o  m
                            returnType.isArray()
                                    ? new ArrayExpression(
                                            ClassHelper.make(
                                                    ArrayTypeUtils.elementType(returnType.getTypeClass())),
                                            null, ctorArgs.getExpressions())
                                    : ctorX(returnType, ctorArgs))));

    syntheticMethodNode.addAnnotation(new AnnotationNode(ClassHelper.make(Generated.class)));
    syntheticMethodNode
            .addAnnotation(new AnnotationNode(ClassHelper.make(groovy.transform.CompileStatic.class)));

    return syntheticMethodNode;
}