Example usage for org.objectweb.asm Opcodes ACC_ABSTRACT

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

Introduction

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

Prototype

int ACC_ABSTRACT

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

Click Source Link

Usage

From source file:org.jacoco.core.internal.analysis.dua.DuaClassAnalyzer.java

License:Open Source License

public void analyze() {
    int methodId = 0;
    for (final MethodNode method : methods) {
        // Does not instrument:
        // 1. Abstract methods
        if ((method.access & Opcodes.ACC_ABSTRACT) != 0) {
            continue;
        }/*  w  w  w .  jav  a  2 s . co m*/
        // 2. Interfaces
        if ((method.access & Opcodes.ACC_INTERFACE) != 0) {
            continue;
        }
        // 3. Synthetic methods
        if ((method.access & Opcodes.ACC_SYNTHETIC) != 0) {
            continue;
        }
        // 4. Static class initialization
        if (method.name.equals("<clinit>")) {
            continue;
        }

        visitMethod(method, methodId++);
    }
}

From source file:org.jacoco.core.internal.instr.ProbeArrayStrategyFactoryTest.java

License:Open Source License

private IProbeArrayStrategy test(int version, int access, boolean clinit, boolean method,
        boolean abstractMethod) {
    final ClassWriter writer = new ClassWriter(0);
    writer.visit(version, access, "Foo", "java/lang/Object", null, null);
    if (clinit) {
        final MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "<clinit>", "()V",
                null, null);/*from  w  ww  .j  av  a  2  s.c o  m*/
        mv.visitCode();
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    if (method) {
        final MethodVisitor mv = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "doit", "()V",
                null, null);
        mv.visitCode();
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    if (abstractMethod) {
        final MethodVisitor mv = writer.visitMethod(Opcodes.ACC_ABSTRACT, "foo", "()V", null, null);
        mv.visitEnd();
    }
    writer.visitEnd();

    final IProbeArrayStrategy strategy = ProbeArrayStrategyFactory.createFor(0,
            new ClassReader(writer.toByteArray()), generator);

    strategy.addMembers(cv, 123);
    return strategy;
}

From source file:org.jacoco.core.internal.instr.ProbeCounter.java

License:Open Source License

@Override
public MethodProbesVisitor visitMethod(final int access, final String name, final String desc,
        final String signature, final String[] exceptions) {
    if (!InstrSupport.CLINIT_NAME.equals(name) && (access & Opcodes.ACC_ABSTRACT) == 0) {
        methods = true;// www  .  ja va2s .c o m
    }
    return null;
}

From source file:org.jacoco.core.internal.instr.ProbeCounterTest.java

License:Open Source License

@Test
public void testVisitAbstractMethod() {
    counter.visitMethod(Opcodes.ACC_ABSTRACT, "foo", null, null, null);
    assertFalse(counter.hasMethods());
}

From source file:org.jboss.byteman.agent.adapter.AccessCheckAdapter.java

License:Open Source License

public MethodVisitor visitMethod(final int access, final String name, final String desc, final String signature,
        final String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if ((access & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_SYNTHETIC)) == 0
            && matchTargetMethod(name, desc)) {
        setVisited();//from   w w  w.  ja v a 2 s  . c  om
        return new AccessCheckMethodAdapter(mv, getTransformContext(), access, name, desc, signature,
                exceptions);
    }

    return mv;
}

From source file:org.jboss.byteman.agent.adapter.RuleGeneratorAdapter.java

License:Open Source License

/**
 * Marks the end of the visited method.//from  www. ja  va  2s .  c  o  m
 */
public void endMethod() {
    if ((access & Opcodes.ACC_ABSTRACT) == 0) {
        visitMaxs(0, 0);
    }
    visitEnd();
}

From source file:org.jboss.byteman.agent.TransformContext.java

License:Open Source License

public boolean matchTargetMethod(int access, String name, String desc) {
    return ((access & (Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT | Opcodes.ACC_SYNTHETIC)) == 0
            && targetMethodName.equals(name)
            && (targetDescriptor.equals("") || TypeHelper.equalDescriptors(targetDescriptor, desc)));
}

From source file:org.mbte.groovypp.compiler.ClosureUtil.java

License:Apache License

public synchronized static List<MethodNode> isOneMethodAbstract(ClassNode node) {
    if ((node.getModifiers() & Opcodes.ACC_ABSTRACT) == 0 && !node.isInterface())
        return null;

    final ClassNodeCache.ClassNodeInfo info = ClassNodeCache.getClassNodeInfo(node);
    if (info.isOneMethodAbstract == null) {
        List<MethodNode> am = node.getAbstractMethods();

        if (am == null) {
            am = Collections.emptyList();
        }//w ww  . ja va 2 s.c o m

        MethodNode one = null;
        for (Iterator<MethodNode> it = am.iterator(); it.hasNext();) {
            MethodNode mn = it.next();
            if (!likeGetter(mn) && !likeSetter(mn) && !traitMethod(mn) && !objectMethod(mn)) {
                if (one != null) {
                    info.isOneMethodAbstract = NONE;
                    return null;
                }
                one = mn;
                it.remove();
            }
        }

        if (one != null)
            am.add(0, one);
        else if (am.size() != 1) {
            info.isOneMethodAbstract = NONE;
            return null;
        }

        info.isOneMethodAbstract = am;
    }

    if (info.isOneMethodAbstract == NONE)
        return null;

    return info.isOneMethodAbstract;
}

From source file:org.mbte.groovypp.compiler.SourceUnitContext.java

License:Apache License

public MethodNode getSuperMethodDelegate(MethodNode superMethod, ClassNode placeClass) {
    Integer num = generatedSuperMethodAccessorNumbers.get(superMethod);
    if (num == null) {
        num = syntheticAccessorNumber++;
        generatedSuperMethodAccessorNumbers.put(superMethod, num);
    }// ww w  .jav a 2s  . c  o  m
    String name = "delegate" + num;
    final ClassNode declaringClass = superMethod.getDeclaringClass();
    final Parameter[] superParams = superMethod.getParameters();
    Parameter[] params = new Parameter[superParams.length];
    for (int i = 0; i < params.length; i++) {
        ClassNode type = TypeUtil.mapTypeFromSuper(superParams[i].getType(), declaringClass, placeClass);
        params[i] = new Parameter(type, superParams[i].getName());
    }
    MethodNode delegate = placeClass.getMethod(name, superParams);
    if (delegate == null) {

        Expression[] exprs = new Expression[superParams.length];
        for (int i = 0; i < exprs.length; i++) {
            exprs[i] = new VariableExpression(params[i]);
        }
        Expression argList = new ArgumentListExpression(exprs);
        ClassNode ret = TypeUtil.mapTypeFromSuper(superMethod.getReturnType(), declaringClass, placeClass);
        final MethodCallExpression call = new MethodCallExpression(
                new VariableExpression("super", ClassHelper.DYNAMIC_TYPE), superMethod.getName(), argList);
        final Statement statement = ret != ClassHelper.VOID_TYPE ? new ReturnStatement(call)
                : new ExpressionStatement(call);
        final int modifiers = superMethod.getModifiers() & ~Opcodes.ACC_ABSTRACT;
        delegate = new MethodNode(name, modifiers, ret, params, new ClassNode[0], statement);
        delegate.setDeclaringClass(placeClass);
        placeClass.addMethod(delegate);
        ClassNodeCache.clearCache(placeClass);
    }
    return delegate;
}

From source file:org.mbte.groovypp.compiler.TraitASTTransform.java

License:Apache License

public void visit(ASTNode[] nodes, final SourceUnit source) {
    ModuleNode module = (ModuleNode) nodes[0];
    List<ClassNode> toProcess = new LinkedList<ClassNode>();
    final boolean forceTyped = source.getName().endsWith(".gpp");
    AnnotationNode pkgTypedAnn = getTypedAnnotation(module.getPackage());
    for (ClassNode classNode : module.getClasses()) {
        boolean process = false;
        boolean typed = false;
        for (AnnotationNode ann : classNode.getAnnotations()) {
            final String withoutPackage = ann.getClassNode().getNameWithoutPackage();
            if (withoutPackage.equals("Trait")) {
                process = true;//from w w  w  .  java 2  s . co m
            }
            if (withoutPackage.equals("Typed")) {
                typed = true;
                ann.getClassNode().setRedirect(TypeUtil.TYPED);
            }
        }

        if (forceTyped && !typed) {
            typed = true;
            classNode.addAnnotation(pkgTypedAnn != null ? pkgTypedAnn : new AnnotationNode(TypeUtil.TYPED));
        }

        if (process) {
            toProcess.add(classNode);
            if (!typed) {
                classNode.addAnnotation(pkgTypedAnn != null ? pkgTypedAnn : new AnnotationNode(TypeUtil.TYPED));
            }
        }
    }

    for (ClassNode classNode : toProcess) {
        if (classNode.isInterface() || classNode.isEnum() || classNode.isAnnotationDefinition()) {
            source.addError(new SyntaxException("@Trait can be applied only to <class>",
                    classNode.getLineNumber(), classNode.getColumnNumber()));
            continue;
        }

        if (classNode.getDeclaredConstructors().size() > 0) {
            ConstructorNode constructor = classNode.getDeclaredConstructors().get(0);
            source.addError(new SyntaxException("Constructors are not allowed in traits",
                    constructor.getLineNumber(), constructor.getColumnNumber()));
            continue;
        }

        if (classNode.getObjectInitializerStatements().size() > 0) {
            Statement initializer = classNode.getObjectInitializerStatements().get(0);
            source.addError(new SyntaxException("Object initializers are not allowed in traits",
                    initializer.getLineNumber(), initializer.getColumnNumber()));
            continue;
        }

        int mod = classNode.getModifiers();
        mod &= ~Opcodes.ACC_FINAL;
        mod |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE;
        classNode.setModifiers(mod);

        String name = classNode.getNameWithoutPackage() + "$TraitImpl";
        String fullName = ASTHelper.dot(classNode.getPackageName(), name);

        InnerClassNode innerClassNode = new InnerClassNode(classNode, fullName,
                ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, new ClassNode[] { classNode },
                null);
        AnnotationNode typedAnn = new AnnotationNode(TypeUtil.TYPED);
        final Expression member = getTypedAnnotation(classNode).getMember("debug");
        if (member != null && member instanceof ConstantExpression
                && ((ConstantExpression) member).getValue().equals(Boolean.TRUE))
            typedAnn.addMember("debug", ConstantExpression.TRUE);
        innerClassNode.addAnnotation(typedAnn);

        innerClassNode.setGenericsTypes(classNode.getGenericsTypes());

        ClassNode superClass = classNode.getSuperClass();
        if (!ClassHelper.OBJECT_TYPE.equals(superClass)) {
            ClassNode[] ifaces = classNode.getInterfaces();
            ClassNode[] newIfaces = new ClassNode[ifaces.length + 1];
            newIfaces[0] = superClass;
            System.arraycopy(ifaces, 0, newIfaces, 1, ifaces.length);
            classNode.setSuperClass(ClassHelper.OBJECT_TYPE);
            classNode.setInterfaces(newIfaces);
        }

        classNode.getModule().addClass(innerClassNode);

        innerClassNode.addMethod("getMetaClass", ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.METACLASS_TYPE,
                Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
        innerClassNode.addMethod("setMetaClass", ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.VOID_TYPE,
                new Parameter[] { new Parameter(ClassHelper.METACLASS_TYPE, "value") }, ClassNode.EMPTY_ARRAY,
                null);
        innerClassNode.addMethod("getProperty", ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE,
                new Parameter[] { new Parameter(ClassHelper.STRING_TYPE, "name") }, ClassNode.EMPTY_ARRAY,
                null);
        innerClassNode.addMethod("setProperty", ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.VOID_TYPE,
                new Parameter[] { new Parameter(ClassHelper.STRING_TYPE, "name"),
                        new Parameter(ClassHelper.OBJECT_TYPE, "value") },
                ClassNode.EMPTY_ARRAY, null);
        innerClassNode.addMethod("invokeMethod", ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE,
                new Parameter[] { new Parameter(ClassHelper.STRING_TYPE, "name"),
                        new Parameter(ClassHelper.OBJECT_TYPE, "args") },
                ClassNode.EMPTY_ARRAY, null);

        for (FieldNode fieldNode : classNode.getFields()) {
            //                if (fieldNode.isStatic())
            //                    continue;

            final String getterName = "get" + Verifier.capitalize(fieldNode.getName());
            MethodNode getter = classNode.getGetterMethod(getterName);
            if (getter == null) {
                getter = classNode.addMethod(getterName, ACC_PUBLIC | ACC_ABSTRACT, fieldNode.getType(),
                        Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
                addFieldAnnotation(innerClassNode, fieldNode, getter);
            }

            // We need the second getter (and setter) to compile non-synthetic first one referring to the field.
            // The references inside the first one will be retargeted to the second one.
            final MethodNode realGetter = classNode.addMethod("get$" + fieldNode.getName(),
                    ACC_PUBLIC | ACC_ABSTRACT, fieldNode.getType(), Parameter.EMPTY_ARRAY,
                    ClassNode.EMPTY_ARRAY, null);
            addFieldAnnotation(innerClassNode, fieldNode, realGetter);

            final String setterName = "set" + Verifier.capitalize(fieldNode.getName());
            Parameter valueParam = new Parameter(fieldNode.getType(), "$value");
            MethodNode setter = classNode.getSetterMethod(setterName);
            if (setter == null) {
                setter = classNode.addMethod(setterName, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.VOID_TYPE,
                        new Parameter[] { valueParam }, ClassNode.EMPTY_ARRAY, null);
                addFieldAnnotation(innerClassNode, fieldNode, setter);
            }

            final MethodNode realSetter = classNode.addMethod("set$" + fieldNode.getName(),
                    ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.VOID_TYPE, new Parameter[] { valueParam },
                    ClassNode.EMPTY_ARRAY, null);
            addFieldAnnotation(innerClassNode, fieldNode, realSetter);

            if (fieldNode.hasInitialExpression()) {
                final Expression initial = fieldNode.getInitialValueExpression();
                fieldNode.setInitialValueExpression(null);

                final MethodNode initMethod = innerClassNode.addMethod("__init_" + fieldNode.getName(),
                        ACC_PUBLIC | ACC_STATIC, ClassHelper.VOID_TYPE,
                        new Parameter[] { new Parameter(classNode, "$self") }, ClassNode.EMPTY_ARRAY,
                        new BlockStatement());

                final PropertyExpression prop = new PropertyExpression(new VariableExpression("$self"),
                        fieldNode.getName());
                prop.setSourcePosition(fieldNode);
                final CastExpression cast = new CastExpression(fieldNode.getType(), initial);
                cast.setSourcePosition(initial);
                final BinaryExpression assign = new BinaryExpression(prop,
                        Token.newSymbol(Types.ASSIGN, -1, -1), cast);
                assign.setSourcePosition(initial);
                final ExpressionStatement assignExpr = new ExpressionStatement(assign);
                assignExpr.setSourcePosition(initial);
                ((BlockStatement) initMethod.getCode()).addStatement(assignExpr);
            }

            innerClassNode.addField(fieldNode);
        }

        classNode.getFields().clear();
        classNode.getProperties().clear();

        for (MethodNode methodNode : classNode.getMethods()) {
            if (methodNode.getCode() == null)
                continue;

            if (methodNode.isStatic()) {
                source.addError(new SyntaxException("Static methods are not allowed in traits",
                        methodNode.getLineNumber(), methodNode.getColumnNumber()));
            }

            if (!methodNode.isPublic()) {
                source.addError(new SyntaxException("Non-public methods are not allowed in traits",
                        methodNode.getLineNumber(), methodNode.getColumnNumber()));
            }

            mod = methodNode.getModifiers();
            mod &= ~(Opcodes.ACC_FINAL | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE);
            mod |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_PUBLIC;
            methodNode.setModifiers(mod);

            Parameter[] parameters = methodNode.getParameters();

            Parameter[] newParameters = new Parameter[parameters.length + 1];
            final Parameter self = new Parameter(classNode, "$self");
            newParameters[0] = self;
            System.arraycopy(parameters, 0, newParameters, 1, parameters.length);

            MethodNode newMethod = innerClassNode.addMethod(methodNode.getName(), ACC_PUBLIC,
                    methodNode.getReturnType(), newParameters, ClassNode.EMPTY_ARRAY, methodNode.getCode());
            ArrayList<GenericsType> gt = new ArrayList<GenericsType>();
            if (classNode.getGenericsTypes() != null)
                for (int i = 0; i < classNode.getGenericsTypes().length; i++) {
                    GenericsType genericsType = classNode.getGenericsTypes()[i];
                    gt.add(genericsType);
                }
            if (methodNode.getGenericsTypes() != null)
                for (int i = 0; i < methodNode.getGenericsTypes().length; i++) {
                    GenericsType genericsType = methodNode.getGenericsTypes()[i];
                    gt.add(genericsType);
                }

            if (!gt.isEmpty())
                newMethod.setGenericsTypes(gt.toArray(new GenericsType[gt.size()]));

            AnnotationNode annotationNode = new AnnotationNode(TypeUtil.HAS_DEFAULT_IMPLEMENTATION);
            annotationNode.addMember("value", new ClassExpression(innerClassNode));
            methodNode.addAnnotation(annotationNode);

            methodNode.setCode(null);
        }
    }
}