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.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_)://  w ww.ja  v  a  2 s .c  o 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  ww w  . j av 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.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 .  jav  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.commons.weaver.privilizer.PrivilizingVisitor.java

License:Apache License

@Override
public void visitEnd() {
    annotate();//from w  w  w  . j a  v  a2 s. c  o m
    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();
}

From source file:org.apache.drill.exec.compile.InnerClassAccessStripper.java

License:Apache License

@Override
public void visit(final int version, final int access, final String name, final String signature,
        final String superName, final String[] interfaces) {
    /*/*w  w w.j a  v a  2 s  .  co  m*/
     * Record the original access bits so we can restore them before the next
     * link in the visitor chain.
     */
    originalClassAccess = access;
    accessCaptured = true;

    // If we're checking an inner class, suppress access bits that ASM chokes on.
    int checkClassAccess = access;
    if (name.indexOf('$') >= 0) {
        checkClassAccess &= ~(Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC);
    }
    super.visit(version, checkClassAccess, name, signature, superName, interfaces);
}

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 ww w .j  av  a  2s  .co 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.AstBuilder.java

License:Apache License

private DeclarationListStatement createFieldDeclarationListStatement(VariableDeclarationContext ctx,
        ModifierManager modifierManager, ClassNode variableType,
        List<DeclarationExpression> declarationExpressionList, ClassNode classNode) {
    for (int i = 0, n = declarationExpressionList.size(); i < n; i++) {
        DeclarationExpression declarationExpression = declarationExpressionList.get(i);
        VariableExpression variableExpression = (VariableExpression) declarationExpression.getLeftExpression();

        String fieldName = variableExpression.getName();

        int modifiers = modifierManager.getClassMemberModifiersOpValue();

        Expression initialValue = declarationExpression.getRightExpression() instanceof EmptyExpression ? null
                : declarationExpression.getRightExpression();
        Object defaultValue = findDefaultValueByType(variableType);

        if (classNode.isInterface()) {
            if (!asBoolean(initialValue)) {
                initialValue = !asBoolean(defaultValue) ? null : new ConstantExpression(defaultValue);
            }//from  ww w.  j a  v  a 2 s.  c  om

            modifiers |= Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
        }

        if (isFieldDeclaration(modifierManager, classNode)) {
            declareField(ctx, modifierManager, variableType, classNode, i, variableExpression, fieldName,
                    modifiers, initialValue);
        } else {
            declareProperty(ctx, modifierManager, variableType, classNode, i, variableExpression, fieldName,
                    modifiers, initialValue);
        }
    }

    return null;
}

From source file:org.apache.maven.diagrams.connectors.classes.model.ModifierModel.java

License:Apache License

/**
 * Transforms maps of com.sun.org.apache.bcel.internal.Opcodes.ACC_... modifiers into the EnumSet.
 * /*from  w w w  .  j  a v a2s  .com*/
 * @param access -
 *            combination of modifier's Opcodes
 * @return
 */
public static EnumSet<ModifierModel> accessContantsToModifiers(int access) {
    EnumSet<ModifierModel> result = EnumSet.noneOf(ModifierModel.class);

    if ((access & Opcodes.ACC_PRIVATE) == Opcodes.ACC_PRIVATE)
        result.add(PRIVATE);
    if ((access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC)
        result.add(PUBLIC);
    if ((access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED)
        result.add(PROTECTED);
    if ((access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC)
        result.add(STATIC);

    return result;
}

From source file:org.apache.tika.parser.asm.XHTMLClassVisitor.java

License:Apache License

/**
 * Visits a field./*  w w w.j av a  2 s . c  o  m*/
 */
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    if (!isSet(access, Opcodes.ACC_SYNTHETIC)) {
        try {
            xhtml.characters("    ");
            writeAccess(access);
            writeType(Type.getType(desc));
            writeSpace();
            writeIdentifier(name);

            if (isSet(access, Opcodes.ACC_STATIC) && value != null) {
                xhtml.characters(" = ");
                xhtml.characters(value.toString());
            }

            writeSemicolon();
            writeNewline();
        } catch (SAXException e) {
            throw new RuntimeException(e);
        }
    }

    return null;
}

From source file:org.apache.tika.parser.asm.XHTMLClassVisitor.java

License:Apache License

private void writeAccess(int access) throws SAXException {
    writeAccess(access, Opcodes.ACC_PRIVATE, "private");
    writeAccess(access, Opcodes.ACC_PROTECTED, "protected");
    writeAccess(access, Opcodes.ACC_PUBLIC, "public");
    writeAccess(access, Opcodes.ACC_STATIC, "static");
    writeAccess(access, Opcodes.ACC_FINAL, "final");
    writeAccess(access, Opcodes.ACC_ABSTRACT, "abstract");
    writeAccess(access, Opcodes.ACC_SYNCHRONIZED, "synchronized");
    writeAccess(access, Opcodes.ACC_TRANSIENT, "transient");
    writeAccess(access, Opcodes.ACC_VOLATILE, "volatile");
    writeAccess(access, Opcodes.ACC_NATIVE, "native");
}