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

License:Open Source License

/**
 * Returns the enhanced class bytecode.//w ww  .j a v a2  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.boretti.drools.integration.drools5.DroolsClassVisitor.java

License:Open Source License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] value) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, value);
    if (isNeedChangeForField() && (access & Opcodes.ACC_STATIC) == 0) {
        if (name.equals("<init>")) {
            DroolsGoalExecutionLog dgl = new DroolsGoalExecutionLog(droolsGoalExecutionLog.getFileName(),
                    droolsGoalExecutionLog.getAction(), "Constructor instrumentalization " + desc);
            droolsGoalExecutionLog.getLogs().add(dgl);
            return new DroolsAddConstructorMethodVisitor(mv, access, name, desc, me, DROOLS_FIELD_NAME,
                    DROOLS_FIELD_RULE, dgl);
        } else if (name.startsWith("get")) {
            String fieldName = name.substring(3).toUpperCase();
            if (fieldType.containsKey(fieldName)) {
                byte b = fieldType.get(fieldName);
                if (b == 0) {
                    DroolsGoalExecutionLog dgl = new DroolsGoalExecutionLog(
                            droolsGoalExecutionLog.getFileName(), droolsGoalExecutionLog.getAction(),
                            "Method instrumentalization " + name + "/" + desc);
                    droolsGoalExecutionLog.getLogs().add(dgl);
                    return new DroolsAddGeneratedGetMethodVisitor(mv, access, name, desc, me, this,
                            DROOLS_FIELD_NAME, DROOLS_FIELD_RULE, DROOLS_METHOD_RUN, dgl);
                }/*from   w  w  w  .ja va  2  s .  c  o  m*/
            }
        } else if (name.startsWith("set")) {
            String fieldName = name.substring(3).toUpperCase();
            if (!fieldType.containsKey(fieldName)) {
                DroolsGoalExecutionLog dgl = new DroolsGoalExecutionLog(droolsGoalExecutionLog.getFileName(),
                        droolsGoalExecutionLog.getAction(), "Method instrumentalization " + name + "/" + desc);
                droolsGoalExecutionLog.getLogs().add(dgl);
                return new DroolsAddGeneratedSetMethodVisitor(mv, access, name, desc, me, this,
                        DROOLS_FIELD_NAME, dgl);
            }
        } else if (name.startsWith("is")) {
            String fieldName = name.substring(2).toUpperCase();
            if (fieldType.containsKey(fieldName)) {
                byte b = fieldType.get(fieldName);
                if (b == 0) {
                    DroolsGoalExecutionLog dgl = new DroolsGoalExecutionLog(
                            droolsGoalExecutionLog.getFileName(), droolsGoalExecutionLog.getAction(),
                            "Method instrumentalization " + name + "/" + desc);
                    droolsGoalExecutionLog.getLogs().add(dgl);
                    return new DroolsAddGeneratedGetMethodVisitor(mv, access, name, desc, me, this,
                            DROOLS_FIELD_NAME, DROOLS_FIELD_RULE, DROOLS_METHOD_RUN, dgl);
                }
            }
        }
    }
    if (isNeedChangeForMethod() && (access & Opcodes.ACC_STATIC) == 0) {
        if (!name.equals("<init>")) {
            return new DroolsAddGeneralMethodVisitor(mv, access, name, desc, me, this, DROOLS_FIELD_NAME,
                    DROOLS_FIELD_RULE, DROOLS_METHOD_RUN, droolsGoalExecutionLog);
        }
    }
    return mv;
}

From source file:org.brutusin.instrumentation.utils.Helper.java

License:Apache License

public static boolean isStatic(MethodNode m) {
    return (m.access & Opcodes.ACC_STATIC) != 0;
}

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  ww  w.  j a 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.aspectwerkz.reflect.StaticInitializationInfoImpl.java

License:Open Source License

/**
 * @see org.codehaus.aspectwerkz.reflect.ReflectionInfo#getModifiers()
 */
public int getModifiers() {
    return Opcodes.ACC_STATIC;
}

From source file:org.codehaus.griffon.compiler.ResolveVisitor.java

License:Apache License

private boolean resolveAliasFromModule(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 ww . j a  va2 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
     */
    while (true) {
        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 = ClassHelper
                        .make(importNode.getType().getName() + "$" + 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('.');
        if (index == -1)
            break;
    }
    return false;
}

From source file:org.codehaus.griffon.compiler.ResolveVisitor.java

License:Apache License

private boolean resolveFromModule(ClassNode type, boolean testModuleImports) {
    // we decided if we have a vanilla name starting with a lower case
    // letter that we will not try to resolve this name against .*
    // imports. Instead a full import is needed for these.
    // resolveAliasFromModule will do this check for us. This method
    // does also check the module contains a class in the same package
    // of this name. This check is not done for vanilla names starting
    // with a lower case letter anymore
    if (type instanceof LowerCaseClass) {
        return resolveAliasFromModule(type);
    }/* w  w  w  .ja  v a2s  .  c  om*/

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

    boolean newNameUsed = false;
    // we add a package if there is none yet and the module has one. But we
    // do not add that if the type is a ConstructedClassWithPackage. The code in ConstructedClassWithPackage
    // hasPackageName() will return true if ConstructedClassWithPackage#className has no dots.
    // but since the prefix may have them and the code there does ignore that
    // fact. We check here for ConstructedClassWithPackage.
    if (!type.hasPackageName() && module.hasPackageName() && !(type instanceof ConstructedClassWithPackage)) {
        type.setName(module.getPackageName() + name);
        newNameUsed = true;
    }
    // look into the module node if there is a class with that name
    List<ClassNode> moduleClasses = module.getClasses();
    for (ClassNode mClass : moduleClasses) {
        if (mClass.getName().equals(type.getName())) {
            if (mClass != type)
                type.setRedirect(mClass);
            return true;
        }
    }
    if (newNameUsed)
        type.setName(name);

    if (testModuleImports) {
        if (resolveAliasFromModule(type))
            return true;

        if (module.hasPackageName()) {
            // check package this class is defined in. The usage of ConstructedClassWithPackage here
            // means, that the module package will not be involved when the
            // compiler tries to find an inner class.
            ConstructedClassWithPackage tmp = new ConstructedClassWithPackage(module.getPackageName(), name);
            if (resolve(tmp, false, false, false)) {
                ambiguousClass(type, tmp, name);
                type.setRedirect(tmp.redirect());
                return true;
            }
        }

        // check module static imports (for static inner classes)
        for (ImportNode importNode : module.getStaticImports().values()) {
            if (importNode.getFieldName().equals(name)) {
                ClassNode tmp = ClassHelper.make(importNode.getType().getName() + "$" + name);
                if (resolve(tmp, false, false, true)) {
                    if ((tmp.getModifiers() & Opcodes.ACC_STATIC) != 0) {
                        type.setRedirect(tmp.redirect());
                        return true;
                    }
                }
            }
        }

        // check module node import packages
        for (ImportNode importNode : module.getStarImports()) {
            String packagePrefix = importNode.getPackageName();
            // We limit the inner class lookups here by using ConstructedClassWithPackage.
            // This way only the name will change, the packagePrefix will
            // not be included in the lookup. The case where the
            // packagePrefix is really a class is handled elsewhere.
            ConstructedClassWithPackage tmp = new ConstructedClassWithPackage(packagePrefix, name);
            if (resolve(tmp, false, false, true)) {
                ambiguousClass(type, tmp, name);
                type.setRedirect(tmp.redirect());
                return true;
            }
        }

        // check for star imports (import static pkg.Outer.*) matching static inner classes
        for (ImportNode importNode : module.getStaticStarImports().values()) {
            ClassNode tmp = ClassHelper.make(importNode.getClassName() + "$" + name);
            if (resolve(tmp, false, false, true)) {
                if ((tmp.getModifiers() & Opcodes.ACC_STATIC) != 0) {
                    ambiguousClass(type, tmp, name);
                    type.setRedirect(tmp.redirect());
                    return true;
                }
            }

        }
    }
    return false;
}

From source file:org.codehaus.griffon.compiler.ResolveVisitor.java

License:Apache License

private void checkThisAndSuperAsPropertyAccess(PropertyExpression expression) {
    if (expression.isImplicitThis())
        return;/*from   w  w w .  ja va  2s  . co m*/
    String prop = expression.getPropertyAsString();
    if (prop == null)
        return;
    if (!prop.equals("this") && !prop.equals("super"))
        return;

    //TODO: add support for super
    if (prop.equals("super")) {
        addError("Inner classes referencing outer classes using super is not supported yet.", expression);
    }

    if (!(expression.getObjectExpression() instanceof ClassExpression)) {
        addError("The usage of '.this' or '.super' requires an explicit class in front.", expression);
        return;
    }

    if (!(currentClass instanceof InnerClassNode)) {
        addError("The usage of '.this' and '.super' is only allowed in a inner class", expression);
        return;
    }

    ClassNode type = expression.getObjectExpression().getType();
    ClassNode iterType = currentClass;
    while (iterType != null) {
        if (iterType.equals(type))
            break;
        iterType = iterType.getOuterClass();
    }
    if (iterType == null) {
        addError("The class '" + type.getName() + "' needs to be an " + "outer class of '"
                + currentClass.getName() + "'.", expression);
    }

    if ((currentClass.getModifiers() & Opcodes.ACC_STATIC) == 0)
        return;
    if (!currentScope.isInStaticContext())
        return;
    addError("The usage of '.this' and '.super' is only in nonstatic context", expression);
}

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

License:Apache License

protected void innerClassDef(AST classDef) {
    List<AnnotationNode> annotations = new ArrayList<>();

    if (isType(TRAIT_DEF, classDef)) {
        annotations.add(new AnnotationNode(ClassHelper.make("groovy.transform.Trait")));
    }/*from ww w  . ja  v  a2s .  c om*/

    AST node = classDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(classDef, "Class", modifiers, Opcodes.ACC_SYNCHRONIZED, "synchronized");
        node = node.getNextSibling();
    }

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

    GenericsType[] genericsType = null;
    if (isType(TYPE_PARAMETERS, node)) {
        genericsType = makeGenericsType(node);
        node = node.getNextSibling();
    }

    ClassNode superClass = null;
    if (isType(EXTENDS_CLAUSE, node)) {
        superClass = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }

    ClassNode[] interfaces = ClassNode.EMPTY_ARRAY;
    if (isType(IMPLEMENTS_CLAUSE, node)) {
        interfaces = interfaces(node);
        node = node.getNextSibling();
    }

    // TODO read mixins
    MixinNode[] mixins = {};
    ClassNode outerClass = classNode;
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    if (classNode != null) {
        name = classNode.getNameWithoutPackage() + "$" + name;
        String fullName = dot(classNode.getPackageName(), name);
        if (classNode.isInterface()) {
            modifiers |= Opcodes.ACC_STATIC;
        }
        classNode = new InnerClassNode(classNode, fullName, modifiers, superClass, interfaces, mixins);
    } else {
        classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, mixins);
    }
    classNode.addAnnotations(annotations);
    classNode.setGenericsTypes(genericsType);
    classNode.setSyntheticPublic(syntheticPublic);
    configureAST(classNode, classDef);

    // 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
    output.addClass(classNode);

    assertNodeType(OBJBLOCK, node);
    objectBlock(node);

    classNode = outerClass;
}

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();/*  www.  jav a2 s.com*/
    }

    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);
}