Example usage for org.objectweb.asm Opcodes ACC_STATIC

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

Introduction

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

Prototype

int ACC_STATIC

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

Click Source Link

Usage

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  ww w  . j  a va2s. c  o m
    }

    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://w w  w.j  a v a 2s.  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.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;//from   w w  w . j a  v  a  2s . c om
    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.ast.MixinASTTransformation.java

License:Apache License

public void visit(ASTNode nodes[], SourceUnit source) {
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        throw new RuntimeException(
                "Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
    }/* w ww  . ja  va2 s  . c om*/
    AnnotationNode node = (AnnotationNode) nodes[0];
    AnnotatedNode parent = (AnnotatedNode) nodes[1];

    if (!MY_TYPE.equals(node.getClassNode()))
        return;

    final Expression expr = node.getMember("value");
    if (expr == null) {
        return;
    }

    Expression useClasses = null;
    if (expr instanceof ClassExpression) {
        useClasses = expr;
    } else if (expr instanceof ListExpression) {
        ListExpression listExpression = (ListExpression) expr;
        for (Expression ex : listExpression.getExpressions()) {
            if (!(ex instanceof ClassExpression))
                return;
        }
        useClasses = expr;
    }

    if (useClasses == null)
        return;

    if (parent instanceof ClassNode) {
        ClassNode annotatedClass = (ClassNode) parent;

        final Parameter[] noparams = new Parameter[0];
        MethodNode clinit = annotatedClass.getDeclaredMethod("<clinit>", noparams);
        if (clinit == null) {
            clinit = annotatedClass.addMethod("<clinit>",
                    Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC, ClassHelper.VOID_TYPE,
                    noparams, null, new BlockStatement());
            clinit.setSynthetic(true);
        }

        final BlockStatement code = (BlockStatement) clinit.getCode();
        code.addStatement(new ExpressionStatement(new MethodCallExpression(
                new PropertyExpression(new ClassExpression(annotatedClass), "metaClass"), "mixin",
                useClasses)));
    }
}

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//from   w  w  w.  j a v  a  2  s  .  c  o m
            .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;
}

From source file:org.codehaus.groovy.classgen.InnerClassCompletionVisitor.java

License:Apache License

private void addDefaultMethods(InnerClassNode node) {
    final boolean isStatic = isStatic(node);

    ClassNode outerClass = node.getOuterClass();
    final String classInternalName = BytecodeHelper.getClassInternalName(node);
    final String outerClassInternalName = getInternalName(outerClass, isStatic);
    final String outerClassDescriptor = getTypeDescriptor(outerClass, isStatic);
    final int objectDistance = getObjectDistance(outerClass);

    // add missing method dispatcher
    Parameter[] parameters = new Parameter[] { new Parameter(ClassHelper.STRING_TYPE, "name"),
            new Parameter(ClassHelper.OBJECT_TYPE, "args") };

    String methodName = "methodMissing";
    if (isStatic)
        addCompilationErrorOnCustomMethodNode(node, methodName, parameters);

    MethodNode method = node.addSyntheticMethod(methodName, Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE,
            parameters, ClassNode.EMPTY_ARRAY, null);

    BlockStatement block = new BlockStatement();
    if (isStatic) {
        setMethodDispatcherCode(block, new ClassExpression(outerClass), parameters);
    } else {/*w w  w  . j ava 2 s.  co m*/
        block.addStatement(new BytecodeSequence(new BytecodeInstruction() {
            public void visit(MethodVisitor mv) {
                getThis(mv, classInternalName, outerClassDescriptor, outerClassInternalName);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEVIRTUAL, outerClassInternalName, "this$dist$invoke$" + objectDistance,
                        "(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;", false);
                mv.visitInsn(ARETURN);
            }
        }));
    }
    method.setCode(block);

    // add static missing method dispatcher
    methodName = "$static_methodMissing";
    if (isStatic)
        addCompilationErrorOnCustomMethodNode(node, methodName, parameters);

    method = node.addSyntheticMethod(methodName, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            ClassHelper.OBJECT_TYPE, parameters, ClassNode.EMPTY_ARRAY, null);

    block = new BlockStatement();
    setMethodDispatcherCode(block, new ClassExpression(outerClass), parameters);
    method.setCode(block);

    // add property setter dispatcher
    parameters = new Parameter[] { new Parameter(ClassHelper.STRING_TYPE, "name"),
            new Parameter(ClassHelper.OBJECT_TYPE, "val") };

    methodName = "propertyMissing";
    if (isStatic)
        addCompilationErrorOnCustomMethodNode(node, methodName, parameters);

    method = node.addSyntheticMethod(methodName, Opcodes.ACC_PUBLIC, ClassHelper.VOID_TYPE, parameters,
            ClassNode.EMPTY_ARRAY, null);

    block = new BlockStatement();
    if (isStatic) {
        setPropertySetterDispatcher(block, new ClassExpression(outerClass), parameters);
    } else {
        block.addStatement(new BytecodeSequence(new BytecodeInstruction() {
            public void visit(MethodVisitor mv) {
                getThis(mv, classInternalName, outerClassDescriptor, outerClassInternalName);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitVarInsn(ALOAD, 2);
                mv.visitMethodInsn(INVOKEVIRTUAL, outerClassInternalName, "this$dist$set$" + objectDistance,
                        "(Ljava/lang/String;Ljava/lang/Object;)V", false);
                mv.visitInsn(RETURN);
            }
        }));
    }
    method.setCode(block);

    // add static property missing setter dispatcher
    methodName = "$static_propertyMissing";
    if (isStatic)
        addCompilationErrorOnCustomMethodNode(node, methodName, parameters);

    method = node.addSyntheticMethod(methodName, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, ClassHelper.VOID_TYPE,
            parameters, ClassNode.EMPTY_ARRAY, null);

    block = new BlockStatement();
    setPropertySetterDispatcher(block, new ClassExpression(outerClass), parameters);
    method.setCode(block);

    // add property getter dispatcher
    parameters = new Parameter[] { new Parameter(ClassHelper.STRING_TYPE, "name") };

    methodName = "propertyMissing";
    if (isStatic)
        addCompilationErrorOnCustomMethodNode(node, methodName, parameters);

    method = node.addSyntheticMethod(methodName, Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE, parameters,
            ClassNode.EMPTY_ARRAY, null);

    block = new BlockStatement();
    if (isStatic) {
        setPropertyGetterDispatcher(block, new ClassExpression(outerClass), parameters);
    } else {
        block.addStatement(new BytecodeSequence(new BytecodeInstruction() {
            public void visit(MethodVisitor mv) {
                getThis(mv, classInternalName, outerClassDescriptor, outerClassInternalName);
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEVIRTUAL, outerClassInternalName, "this$dist$get$" + objectDistance,
                        "(Ljava/lang/String;)Ljava/lang/Object;", false);
                mv.visitInsn(ARETURN);
            }
        }));
    }
    method.setCode(block);

    // add static property missing getter dispatcher
    methodName = "$static_propertyMissing";
    if (isStatic)
        addCompilationErrorOnCustomMethodNode(node, methodName, parameters);

    method = node.addSyntheticMethod(methodName, Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
            ClassHelper.OBJECT_TYPE, parameters, ClassNode.EMPTY_ARRAY, null);

    block = new BlockStatement();
    setPropertyGetterDispatcher(block, new ClassExpression(outerClass), parameters);
    method.setCode(block);
}

From source file:org.codehaus.groovy.classgen.InnerClassVisitorHelper.java

License:Apache License

protected static boolean shouldHandleImplicitThisForInnerClass(ClassNode cn) {
    if (cn.isEnum() || cn.isInterface())
        return false;
    if ((cn.getModifiers() & Opcodes.ACC_STATIC) != 0)
        return false;

    if (!(cn instanceof InnerClassNode))
        return false;
    InnerClassNode innerClass = (InnerClassNode) cn;
    // scope != null means aic, we don't handle that here
    if (innerClass.getVariableScope() != null)
        return false;
    // static inner classes don't need this$0
    return (innerClass.getModifiers() & Opcodes.ACC_STATIC) == 0;
}

From source file:org.codehaus.groovy.control.OptimizerVisitor.java

License:Apache License

private void setConstField(ConstantExpression constantExpression) {
    final Object n = constantExpression.getValue();
    if (!(n instanceof Number))
        return;/*from w w  w  .  jav a2s  .  com*/
    if (n instanceof Integer || n instanceof Double)
        return;
    if (n instanceof Long && (0L == (Long) n || 1L == (Long) n))
        return; // LCONST_0, LCONST_1

    boolean isPrimitive = isPrimitiveType(constantExpression.getType());
    FieldNode field = isPrimitive ? const2Prims.get(n) : const2Objects.get(n);
    if (field != null) {
        constantExpression.setConstantName(field.getName());
        return;
    }
    String name;
    do {
        name = "$const$" + index++;
    } while (currentClass.getDeclaredField(name) != null);
    // TODO consider moving initcode to <clinit> and remaking field final
    field = new FieldNode(name, Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC,
            constantExpression.getType(), currentClass, constantExpression);
    field.setSynthetic(true);
    missingFields.add(field);
    constantExpression.setConstantName(field.getName());
    if (isPrimitive) {
        const2Prims.put(n, field);
    } else {
        const2Objects.put(n, field);
    }
}

From source file:org.codehaus.groovy.control.ResolveVisitor.java

License:Apache License

private boolean resolveAliasFromModule(final ClassNode type) {
    // In case of getting a ConstructedClassWithPackage here we do not do checks for partial
    // matches with imported classes. The ConstructedClassWithPackage is already a constructed
    // node and any subclass resolving will then take place elsewhere
    if (type instanceof ConstructedClassWithPackage)
        return false;

    ModuleNode module = currentClass.getModule();
    if (module == null)
        return false;
    String name = type.getName();

    // check module node imports aliases
    // the while loop enables a check for inner classes which are not fully imported,
    // but visible as the surrounding class is imported and the inner class is public/protected static
    String pname = name;/* w w w .j a  v  a2  s  .  c o m*/
    int index = name.length();
    /*
     * we have a name foo.bar and an import foo.foo. This means foo.bar is possibly
     * foo.foo.bar rather than foo.bar. This means to cut at the dot in foo.bar and
     * foo for import
     */
    do {
        pname = name.substring(0, index);
        ClassNode aliasedNode = null;
        ImportNode importNode = module.getImport(pname);
        if (importNode != null && importNode != currImportNode) {
            aliasedNode = importNode.getType();
        }
        if (aliasedNode == null) {
            importNode = module.getStaticImports().get(pname);
            if (importNode != null && importNode != currImportNode) {
                // static alias only for inner classes and must be at end of chain
                ClassNode tmp = new ConstructedNestedClass(importNode.getType(), importNode.getFieldName());
                if (resolve(tmp, false, false, true)) {
                    if ((tmp.getModifiers() & Opcodes.ACC_STATIC) != 0) {
                        type.setRedirect(tmp.redirect());
                        return true;
                    }
                }
            }
        }

        if (aliasedNode != null) {
            if (pname.length() == name.length()) {
                // full match

                // We can compare here by length, because pname is always
                // a substring of name, so same length means they are equal.
                type.setRedirect(aliasedNode);
                return true;
            } else {
                //partial match

                // At this point we know that we have a match for pname. This may
                // mean, that name[pname.length()..<-1] is a static inner class.
                // For this the rest of the name does not need any dots in its name.
                // It is either completely a inner static class or it is not.
                // Since we do not want to have useless lookups we create the name
                // completely and use a ConstructedClassWithPackage to prevent lookups against the package.
                String className = aliasedNode.getNameWithoutPackage() + "$"
                        + name.substring(pname.length() + 1).replace('.', '$');
                ConstructedClassWithPackage tmp = new ConstructedClassWithPackage(
                        aliasedNode.getPackageName() + ".", className);
                if (resolve(tmp, true, true, false)) {
                    type.setRedirect(tmp.redirect());
                    return true;
                }
            }
        }
        index = pname.lastIndexOf('.');
    } while (index != -1);
    return false;
}