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.mbte.groovypp.compiler.bytecode.PropertyUtil.java

License:Apache License

public static Object resolveSetProperty(ClassNode type, String name, ClassNode arg,
        CompilerTransformer compiler, boolean isSameObject) {
    FieldNode field = compiler.findField(type, name);
    isSameObject &= !isTraitImpl(type);
    if (field != null && field.getDeclaringClass() == compiler.classNode && isSameObject)
        return field;

    final String setterName = "set" + Verifier.capitalize(name);
    MethodNode mn = compiler.findMethod(type, setterName, new ClassNode[] { arg }, false);
    if (mn != null && mn.getReturnType() == ClassHelper.VOID_TYPE) {
        return mn;
    }//from w  w w  .  j a va 2s. c om

    final PropertyNode pnode = type.getProperty(name);
    if (pnode != null && (pnode.getModifiers() & Opcodes.ACC_FINAL) == 0) {
        return pnode;
    }

    if (field != null && (field.getModifiers() & Opcodes.ACC_FINAL) != 0) {
        if (field.getDeclaringClass() != compiler.classNode && !isFieldInitializer(compiler.methodNode))
            return null;
    }
    return field;
}

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

License:Apache License

public static void addFields(ClosureExpression ce, ClassNode newType, CompilerTransformer compiler) {
    for (Iterator<Variable> it = ce.getVariableScope().getReferencedLocalVariablesIterator(); it.hasNext();) {
        Variable astVar = it.next();//from w w  w.  ja v  a 2 s .  c om
        final Register var = compiler.compileStack.getRegister(astVar.getName(), false);

        ClassNode vtype;
        if (var != null) {
            vtype = compiler.getLocalVarInferenceTypes().get(astVar);
            if (vtype == null)
                vtype = var.getType();
        } else {
            if (astVar instanceof VariableExpression
                    && ((VariableExpression) astVar).getAccessedVariable() instanceof FieldNode) {
                continue;
            } else
                vtype = compiler.methodNode.getDeclaringClass().getField(astVar.getName()).getType();
        }

        if (newType.getDeclaredField(astVar.getName()) == null) {
            newType.addField(astVar.getName(), Opcodes.ACC_FINAL, vtype, null);
        }
    }
    ClassNodeCache.clearCache(newType);
}

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

License:Apache License

private static boolean isAssignableOrInference(ClassNode classToTransformTo, ClassNode classToTransformFrom) {
    if (classToTransformFrom == null) {
        if (!ClassHelper.isPrimitiveType(classToTransformTo)
                || ClassHelper.boolean_TYPE.equals(classToTransformTo))
            return true;
        else//from   w ww.j  a  v a2 s. c o  m
            return false;
    }

    if (TypeUtil.isAssignableFrom(classToTransformTo, classToTransformFrom))
        return true;

    if (classToTransformFrom.equals(TypeUtil.TCLOSURE_NULL)) {
        if (classToTransformTo.equals(ClassHelper.CLOSURE_TYPE)) {
            return true;
        } else {
            List<MethodNode> one = ClosureUtil.isOneMethodAbstract(classToTransformTo);
            if (one != null) {

                MethodNode missing = one.get(0);
                Parameter[] missingMethodParameters = missing.getParameters();
                List<MethodNode> methods = classToTransformFrom.getGenericsTypes()[0].getType()
                        .getDeclaredMethods("doCall");

                for (MethodNode method : methods) {
                    Parameter[] closureParameters = method.getParameters();

                    if (closureParameters.length != missingMethodParameters.length)
                        continue;

                    return true;
                }
            }
            return false;
        }
    }

    if (classToTransformFrom.equals(TypeUtil.TLIST_NULL)) {
        return true;
    }

    if (classToTransformFrom.equals(TypeUtil.TMAP_NULL)) {
        return !classToTransformTo.isArray() && ((classToTransformTo.getModifiers() & Opcodes.ACC_FINAL) == 0);
    }

    return false;
}

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

License:Apache License

public static void closureToMethod(ClassNode type, CompilerTransformer compiler, ClassNode objType,
        String keyName, ClosureExpression ce) {
    if (ce.getParameters() != null && ce.getParameters().length == 0) {
        final VariableScope scope = ce.getVariableScope();
        ce = new ClosureExpression(new Parameter[1], ce.getCode());
        ce.setVariableScope(scope);/*from w  ww.j a v  a 2 s .  c  o  m*/
        ce.getParameters()[0] = new Parameter(OBJECT_TYPE, "it", new ConstantExpression(null));
    }

    final ClosureMethodNode _doCallMethod = new ClosureMethodNode(keyName,
            Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL, TypeUtil.IMPROVE_TYPE,
            ce.getParameters() == null ? Parameter.EMPTY_ARRAY : ce.getParameters(), ce.getCode());

    if (objType != type)
        objType.addMethod(_doCallMethod);

    _doCallMethod.createDependentMethods(objType);

    Object methods = ClassNodeCache.getMethods(type, keyName);
    if (methods != null) {
        if (methods instanceof MethodNode) {
            MethodNode baseMethod = (MethodNode) methods;
            _doCallMethod.checkOverride(baseMethod, type);
        } else {
            FastArray methodsArr = (FastArray) methods;
            int methodCount = methodsArr.size();
            for (int j = 0; j != methodCount; ++j) {
                MethodNode baseMethod = (MethodNode) methodsArr.get(j);
                _doCallMethod.checkOverride(baseMethod, type);
            }
        }
    }

    if (objType == type)
        objType.addMethod(_doCallMethod);

    ClassNodeCache.clearCache(_doCallMethod.getDeclaringClass());
    compiler.replaceMethodCode(_doCallMethod.getDeclaringClass(), _doCallMethod);
}

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;/*  www . ja v a 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);
        }
    }
}

From source file:org.mbte.groovypp.compiler.transformers.CastExpressionTransformer.java

License:Apache License

private ClassNode createNewType(ClassNode type, Expression exp, CompilerTransformer compiler) {
    ClassNode objType;/*from  w w w.j ava2s . c om*/
    if ((type.getModifiers() & Opcodes.ACC_FINAL) != 0) {
        compiler.addError("You are not allowed to overwrite the final class '" + type.getName() + "'", exp);
        return null;
    }

    objType = new InnerClassNode(compiler.classNode, compiler.getNextClosureName(),
            ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC, ClassHelper.OBJECT_TYPE);
    if (type.isInterface()) {
        objType.setInterfaces(new ClassNode[] { type });
    } else {
        objType.setSuperClass(type);
    }
    objType.setModule(compiler.classNode.getModule());

    if (!compiler.methodNode.isStatic() || compiler.classNode.getName().endsWith("$TraitImpl"))
        objType.addField("this$0", ACC_PUBLIC | ACC_FINAL | ACC_SYNTHETIC,
                !compiler.methodNode.isStatic() ? compiler.classNode
                        : compiler.methodNode.getParameters()[0].getType(),
                null);

    if (compiler.policy == TypePolicy.STATIC)
        CleaningVerifier.improveVerifier(objType);

    return objType;
}

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

License:Apache License

private static boolean canHaveCommonSubtype(ClassNode t1, ClassNode t2) {
    return (t1.isInterface() && (t2.getModifiers() & Opcodes.ACC_FINAL) == 0)
            || (t2.isInterface() && (t1.getModifiers() & Opcodes.ACC_FINAL) == 0);
}

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

License:Apache License

public UncertainClassNode(T expression, MethodNode owner, String name) {
    super(owner.getDeclaringClass(), name, Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, ClassHelper.OBJECT_TYPE,
            ClassNode.EMPTY_ARRAY, null);
    TypeUtil.detachInnerClass(this);
    this.expression = expression;
    setSourcePosition(expression);/* ww w.jav a2 s.co  m*/
    expression.setType(this);
    outerMethod = owner;
    setEnclosingMethod(outerMethod);
}

From source file:org.mule.tools.maven.plugin.module.analyze.asm.AccessUtils.java

License:Open Source License

public static boolean isFinal(int access) {
    return (access & Opcodes.ACC_FINAL) == Opcodes.ACC_FINAL;
}

From source file:org.openjdk.jmh.generators.asm.ASMClassInfo.java

License:Open Source License

@Override
public boolean isFinal() {
    return (access & Opcodes.ACC_FINAL) > 0;
}