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.kantega.notsoserial.CreateBytesIT.java

License:Apache License

private byte[] createTransletBytes() {

    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, "com/example/InvokingTranslet", null,
            Type.getType(AbstractTranslet.class).getInternalName(),
            new String[] { Type.getType(Serializable.class).getInternalName() });

    MethodVisitor init = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    init.visitCode();/*from w w w.ja v  a 2s .co m*/
    init.visitVarInsn(Opcodes.ALOAD, 0);
    init.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getType(AbstractTranslet.class).getInternalName(),
            "<init>", "()V");
    init.visitVarInsn(Opcodes.ALOAD, 0);
    init.visitIntInsn(Opcodes.BIPUSH, 101);
    init.visitFieldInsn(Opcodes.PUTFIELD, Type.getType(AbstractTranslet.class).getInternalName(),
            "transletVersion", "I");
    init.visitInsn(Opcodes.RETURN);
    init.visitMaxs(2, 2);
    init.visitEnd();

    MethodVisitor transformMethod = cw.visitMethod(Opcodes.ACC_PUBLIC, "transform",
            Type.getMethodDescriptor(Type.VOID_TYPE,
                    new Type[] { Type.getType(DOM.class), Type.getType(DTMAxisIterator.class) }),
            null, new String[] { Type.getType(TransletException.class).getInternalName() });

    transformMethod.visitCode();
    transformMethod.visitInsn(Opcodes.RETURN);
    transformMethod.visitEnd();

    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn("HMM..");
    mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    mv.visitLdcInsn("pwned");
    mv.visitLdcInsn("true");
    mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/System", "setProperty",
            "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
    mv.visitInsn(Opcodes.POP);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(2, 0);
    mv.visitEnd();
    cw.visitEnd();

    return cw.toByteArray();
}

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

License:Apache License

public static boolean isAccessible(int modifiers, ClassNode declaringClass, ClassNode placeClass,
        ClassNode accessType) {/*from ww  w .ja  v a  2  s .  co m*/
    if (accessType != null
            && !isAccessible(accessType.getModifiers(), accessType.getOuterClass(), placeClass, null))
        return false;
    if (declaringClass == null)
        return true;
    if ((modifiers & Opcodes.ACC_PRIVATE) != 0) {
        return getToplevelClass(declaringClass).equals(getToplevelClass(placeClass));
    } else if ((modifiers & Opcodes.ACC_PROTECTED) != 0) {
        if (samePackage(declaringClass, placeClass))
            return true;
        while (placeClass != null) {
            if (placeClass.isDerivedFrom(declaringClass)) {
                return accessType == null || accessType.isDerivedFrom(placeClass);
            }
            if ((placeClass.getModifiers() & Opcodes.ACC_STATIC) != 0)
                break;
            placeClass = placeClass.getOuterClass();
        }
        return false;
    } else if ((modifiers & Opcodes.ACC_PUBLIC) != 0) {
        return true;
    }
    // package local
    if (!samePackage(declaringClass, placeClass))
        return false;
    // check the entire inheritance chain.
    ClassNode clazz = null;
    if (accessType != null) {
        clazz = accessType;
    } else {
        while (placeClass != null) {
            if (placeClass.isDerivedFrom(declaringClass)) {
                clazz = placeClass;
                break;
            }
            if ((placeClass.getModifiers() & Opcodes.ACC_STATIC) != 0)
                break;
            placeClass = placeClass.getOuterClass();
        }
    }
    while (clazz != null && !clazz.equals(declaringClass)) {
        if (!samePackage(declaringClass, clazz))
            return false;
        clazz = clazz.getSuperClass();
    }
    return true;
}

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

License:Apache License

private static DGM createDGM(Class klazz, MethodNode method, ClassNode declaringClass, ClassNode[] exs,
        Parameter[] params, boolean isStatic) {
    DGM mn = new DGM(method.getName(), Opcodes.ACC_PUBLIC | (isStatic ? Opcodes.ACC_STATIC : 0),
            method.getReturnType(), params, exs, null);
    mn.setDeclaringClass(declaringClass);
    mn.callClassInternalName = BytecodeHelper.getClassInternalName(klazz);
    mn.descr = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameters());
    mn.setGenericsTypes(method.getGenericsTypes());
    mn.original = method;//from w  w w . j  a v  a 2s .co  m
    return mn;
}

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

License:Apache License

private void initAccessors(FieldNode field) {
    int num = syntheticAccessorNumber++;
    String getterName = "getField" + num;
    int modifiers = field.getModifiers() & Opcodes.ACC_STATIC;
    MethodNode getter = new MethodNode(getterName, modifiers, field.getType(), new Parameter[0],
            new ClassNode[0], new ReturnStatement(new VariableExpression(field)));
    ClassNode clazz = field.getDeclaringClass();
    getter.setDeclaringClass(clazz);/*from  ww  w.  ja  va 2 s . c  om*/
    clazz.addMethod(getter);
    generatedFieldGetters.put(field, getter);
    if (!field.isFinal()) {
        String setterName = "setField" + num;
        Parameter[] setterParams = { new Parameter(field.getType(), "p") };
        ExpressionStatement code = new ExpressionStatement(new BinaryExpression(new VariableExpression(field),
                Token.newSymbol(Types.ASSIGN, -1, -1), new VariableExpression("p")));
        MethodNode setter = new MethodNode(setterName, modifiers, ClassHelper.VOID_TYPE, setterParams,
                new ClassNode[0], code);
        setter.setDeclaringClass(clazz);
        clazz.addMethod(setter);
        generatedFieldSetters.put(field, setter);
    }
    ClassNodeCache.clearCache(clazz);
}

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

License:Apache License

public MethodNode getMethodDelegate(MethodNode method) {
    MethodNode delegate = generatedMethodDelegates.get(method);
    if (delegate == null) {
        int num = syntheticAccessorNumber++;
        String name = "delegate" + num;
        int modifiers = method.getModifiers() & Opcodes.ACC_STATIC;

        Expression[] exprs = new Expression[method.getParameters().length];
        for (int i = 0; i < exprs.length; i++) {
            exprs[i] = new VariableExpression(method.getParameters()[i]);
        }// ww  w .j av a2 s .  com
        Expression argList = new ArgumentListExpression(exprs);
        delegate = new MethodNode(name, modifiers, method.getReturnType(), method.getParameters(),
                new ClassNode[0], new ReturnStatement(new MethodCallExpression(
                        new VariableExpression("this", ClassHelper.DYNAMIC_TYPE), method.getName(), argList)));
        delegate.setGenericsTypes(method.getGenericsTypes());
        generatedMethodDelegates.put(method, delegate);
        ClassNode clazz = method.getDeclaringClass();
        delegate.setDeclaringClass(clazz);
        clazz.addMethod(delegate);
        ClassNodeCache.clearCache(clazz);
    }
    return delegate;
}

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

License:Apache License

public MethodNode getConstructorDelegate(MethodNode constructor) {
    MethodNode delegate = generatedMethodDelegates.get(constructor);
    if (delegate == null) {
        int num = syntheticAccessorNumber++;
        String name = "delegate" + num;
        int modifiers = Opcodes.ACC_STATIC;

        Expression[] exprs = new Expression[constructor.getParameters().length];
        for (int i = 0; i < exprs.length; i++) {
            exprs[i] = new VariableExpression(constructor.getParameters()[i]);
        }//  w w  w.ja  v  a2s . c o  m
        Expression argList = new ArgumentListExpression(exprs);
        delegate = new MethodNode(name, modifiers, constructor.getDeclaringClass(), constructor.getParameters(),
                new ClassNode[0],
                new ReturnStatement(new ConstructorCallExpression(constructor.getDeclaringClass(), argList)));
        generatedMethodDelegates.put(constructor, delegate);
        ClassNode clazz = constructor.getDeclaringClass();
        delegate.setDeclaringClass(clazz);
        clazz.addMethod(delegate);
        ClassNodeCache.clearCache(clazz);
    }
    return delegate;
}

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

License:Apache License

private void makeImplementationMethodsStatic(final ClassNode classNode, final SourceUnit source) {
    for (final MethodNode methodNode : classNode.getMethods()) {
        if (methodNode.isStatic() || methodNode.isSynthetic() || methodNode.isAbstract())
            continue;

        final Parameter[] parameters = methodNode.getParameters();

        methodNode.setModifiers(methodNode.getModifiers() | Opcodes.ACC_STATIC);
        methodNode.getVariableScope().setInStaticContext(true);
        final VariableExpression self = new VariableExpression(parameters[0]);
        final String propNameCapitalized = getPropertyNameCapitalized(methodNode);

        ClassCodeExpressionTransformer thisToSelf = new ClassCodeExpressionTransformer() {
            protected SourceUnit getSourceUnit() {
                return source;
            }//from   www  .j a  va 2s. c om

            public Expression transform(Expression exp) {
                if (exp instanceof VariableExpression && isExplicitThis((VariableExpression) exp))
                    return self;
                else if (exp instanceof PropertyExpression) {
                    final PropertyExpression propExp = (PropertyExpression) exp;
                    if (propExp.isImplicitThis() || (propExp.getObjectExpression() instanceof VariableExpression
                            && isExplicitThis((VariableExpression) propExp.getObjectExpression()))) {
                        final String name = propExp.getPropertyAsString();
                        final FieldNode field = classNode.getField(name);
                        if (field != null && Verifier.capitalize(field.getName()).equals(propNameCapitalized))
                            return new PropertyExpression(self, "$" + name);
                    }
                }
                return super.transform(exp);
            }

            private boolean isExplicitThis(VariableExpression exp) {
                return "this".equals(exp.getName());
            }
        };
        thisToSelf.visitMethod(methodNode);
    }
}

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

License:Apache License

private static MethodNode findConstructorVariatingArgs(ClassNode type, ClassNode[] argTypes,
        CompilerTransformer compiler, int firstNonVariating, ClassNode contextClass) {
    MethodNode foundMethod = compiler.findConstructor(type, argTypes, contextClass);
    if (foundMethod != null) {
        return foundMethod;
    }//  ww w.jav a2 s  .  c  o  m

    if (argTypes.length > 0) {
        for (int i = firstNonVariating + 1; i < argTypes.length; ++i) {
            final ClassNode oarg = argTypes[i];
            if (oarg == null)
                continue;

            if (oarg.implementsInterface(TypeUtil.TCLOSURE)) {
                foundMethod = findConstructorVariatingArgs(type, argTypes, compiler, i, contextClass);

                if (foundMethod != null) {
                    Parameter p[] = foundMethod.getParameters();
                    if (p.length == argTypes.length) {
                        return foundMethod;
                    }
                }

                argTypes[i] = null;
                foundMethod = findConstructorVariatingArgs(type, argTypes, compiler, i, contextClass);
                if (foundMethod != null) {
                    Parameter p[] = foundMethod.getParameters();
                    if (p.length == argTypes.length) {
                        ClassNode argType = p[i].getType();
                        if (argType.equals(ClassHelper.CLOSURE_TYPE)) {
                            ClosureUtil.improveClosureType(oarg, ClassHelper.CLOSURE_TYPE);
                            compiler.replaceMethodCode(oarg, ((ClosureClassNode) oarg).getDoCallMethod());
                        } else {
                            List<MethodNode> one = ClosureUtil.isOneMethodAbstract(argType);
                            GenericsType[] methodTypeVars = foundMethod.getGenericsTypes();
                            if (methodTypeVars != null && methodTypeVars.length > 0) {
                                ArrayList<ClassNode> formals = new ArrayList<ClassNode>(2);
                                ArrayList<ClassNode> instantiateds = new ArrayList<ClassNode>(2);

                                if (!foundMethod.isStatic()) {
                                    formals.add(foundMethod.getDeclaringClass());
                                    instantiateds.add(type);
                                }

                                for (int j = 0; j != i; j++) {
                                    formals.add(foundMethod.getParameters()[j].getType());
                                    instantiateds.add(argTypes[j]);
                                }

                                ClassNode[] unified = TypeUnification.inferTypeArguments(methodTypeVars,
                                        formals.toArray(new ClassNode[formals.size()]),
                                        instantiateds.toArray(new ClassNode[instantiateds.size()]));
                                argType = TypeUtil.getSubstitutedType(argType, foundMethod, unified);
                            }
                            MethodNode doCall = ClosureUtil.isMatch(one, (ClosureClassNode) oarg, argType,
                                    compiler);
                            if (doCall == null) {
                                foundMethod = null;
                            }
                        }
                    }
                }
                argTypes[i] = oarg;
                return foundMethod;
            } else {
                if (oarg.implementsInterface(TypeUtil.TMAP) || oarg.implementsInterface(TypeUtil.TLIST)
                        || oarg.implementsInterface(TypeUtil.TTERNARY)) {
                    return structural(type, argTypes, compiler, contextClass, i, oarg);
                } else {
                    if (oarg.implementsInterface(TypeUtil.TTHIS)) {
                        ClassNode run = oarg.getOuterClass();
                        while (run != null) {
                            argTypes[i] = run;
                            foundMethod = findConstructorVariatingArgs(type, argTypes, compiler, i,
                                    contextClass);
                            if (foundMethod != null)
                                return foundMethod;
                            if ((run.getModifiers() & Opcodes.ACC_STATIC) != 0)
                                break;
                            run = run.getOuterClass();
                        }
                        argTypes[i] = oarg.getOuterClass();
                    }
                }
            }
        }
    }
    return null;
}

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

License:Apache License

private Expression inCaseOfInner(final PropertyExpression exp, final CompilerTransformer compiler,
        String propName) {/*w w  w .  j  a va2 s .  c om*/
    BytecodeExpr object;

    ClassNode thisType = compiler.classNode;
    boolean isThis = true;
    boolean onlyStatic = false;
    while (thisType != null) {
        Object prop = PropertyUtil.resolveGetProperty(thisType, propName, compiler, onlyStatic, isThis);
        if (prop != null) {
            boolean isStatic = PropertyUtil.isStatic(prop);
            if (!isStatic && exp.isStatic())
                return null;
            object = isStatic ? null : new InnerThisBytecodeExpr(exp, thisType, compiler);

            return PropertyUtil.createGetProperty(exp, compiler, propName, thisType, object, prop);
        }

        isThis = false;

        if (thisType.implementsInterface(TypeUtil.DELEGATING)) {
            final MethodNode gd = compiler.findMethod(thisType, "getDelegate", ClassNode.EMPTY_ARRAY, false);
            if (gd != null) {
                final InnerThisBytecodeExpr innerThis = new InnerThisBytecodeExpr(exp, thisType, compiler);
                final BytecodeExpr delegate = ResolvedMethodBytecodeExpr.create(exp, gd, innerThis,
                        ArgumentListExpression.EMPTY_ARGUMENTS, compiler);
                prop = PropertyUtil.resolveGetProperty(delegate.getType(), propName, compiler, onlyStatic,
                        false);
                if (prop != null) {
                    boolean isStatic = PropertyUtil.isStatic(prop);
                    if (!isStatic && exp.isStatic())
                        return null;
                    object = isStatic ? null : delegate;

                    return PropertyUtil.createGetProperty(exp, compiler, propName, delegate.getType(), object,
                            prop);
                }
            }
        }

        onlyStatic |= (thisType.getModifiers() & Opcodes.ACC_STATIC) != 0;
        thisType = thisType.getOuterClass();

        if (thisType != null && thisType.getName().endsWith("$TraitImpl")) {
            thisType = thisType.getOuterClass();
        }
    }

    if (compiler.policy == TypePolicy.STATIC) {
        compiler.addError(MessageFormat.format("Cannot resolve property {0}.{1}",
                PresentationUtil.getText(compiler.classNode), propName), exp);
        return null;
    } else {
        object = new InnerThisBytecodeExpr(exp, compiler.classNode, compiler);
        return new UnresolvedLeftExpr(exp, null, object, propName);
    }
}

From source file:org.mustbe.consulo.ikvm.psi.stubBuilding.JavaMethodStubBuilder.java

License:Apache License

@Override
public void buildToBytecode(ClassWriter parent) {
    int access = 0;
    access = BitUtil.set(access, Opcodes.ACC_STATIC, myModifiers.contains(PsiModifier.STATIC));
    access = BitUtil.set(access, Opcodes.ACC_PUBLIC, myModifiers.contains(PsiModifier.PUBLIC));

    StringBuilder descBuilder = new StringBuilder();
    descBuilder.append("(");
    for (JavaParameterStubBuilder parameter : myParameters) {
        appendType(parameter.getType(), descBuilder);
    }//from ww  w  . ja  v  a2s . co  m
    descBuilder.append(")");
    appendType(myReturnType, descBuilder);

    try {
        parent.visitMethod(access, myName, descBuilder.toString(), null, null).visitEnd();
    } catch (Exception e) {
        e.printStackTrace();
    }
}