Example usage for org.objectweb.asm Opcodes ACC_PROTECTED

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

Introduction

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

Prototype

int ACC_PROTECTED

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

Click Source Link

Usage

From source file:org.wisdom.ipojo.module.WisdomViewVisitorTest.java

License:Apache License

@Test
public void parseEmptyAnnotation() {
    Reporter reporter = new SystemReporter();
    ComponentWorkbench workbench = mock(ComponentWorkbench.class);
    when(workbench.getType()).thenReturn(Type.getType(MyComponent.class));
    when(workbench.getClassNode()).thenReturn(new ClassNode());
    when(workbench.getElements()).thenReturn(elements);

    FieldNode node = new FieldNode(Opcodes.ACC_PROTECTED, "template", Type.getDescriptor(Template.class), null,
            null);/*from   w w w .j a  v a2 s  .c  om*/

    WisdomViewVisitor visitor = new WisdomViewVisitor(workbench, reporter, node);
    visitor.visitEnd();

    assertThat(elements).hasSize(0);
}

From source file:org.wisdom.ipojo.module.WisdomViewVisitorTest.java

License:Apache License

@Test
public void parseViewOnNotTemplateField() {
    Reporter reporter = new SystemReporter();
    ComponentWorkbench workbench = mock(ComponentWorkbench.class);
    when(workbench.getType()).thenReturn(Type.getType(MyComponent.class));
    when(workbench.getClassNode()).thenReturn(new ClassNode());
    when(workbench.getElements()).thenReturn(elements);

    FieldNode node = new FieldNode(Opcodes.ACC_PROTECTED, "template", Type.getDescriptor(String.class), null,
            null);/* w  w w  . j  a  va 2  s  .co m*/

    WisdomViewVisitor visitor = new WisdomViewVisitor(workbench, reporter, node);
    visitor.visit("value", "index");
    visitor.visitEnd();

    assertThat(elements).hasSize(1);
    Element element = elements.keySet().iterator().next();
    assertThat(element.getName()).isEqualTo("requires");
    assertThat(element.getAttribute("field")).isEqualTo("template");
    assertThat(element.getAttribute("filter")).contains("=index)");
}

From source file:pl.asie.foamfix.coremod.BlockPosPatch.java

License:Open Source License

public static byte[] patchVec3i(byte[] data) {
    final ClassReader reader = new ClassReader(data);
    final ClassNode node = new ClassNode();
    reader.accept(node, 8);/*from w  w  w .  java 2s . c o  m*/
    for (FieldNode fn : node.fields) {
        if ("I".equals(fn.desc) && fn.access == (Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL)) {
            fn.access = Opcodes.ACC_PROTECTED;
        }
    }
    final ClassWriter writer = new ClassWriter(8);
    node.accept(writer);
    return writer.toByteArray();
}

From source file:pl.asie.foamfix.coremod.patches.BlockPosPatch.java

License:Open Source License

public static ClassNode patchVec3i(ClassNode node) {
    for (FieldNode fn : node.fields) {
        if ("I".equals(fn.desc) && fn.access == (Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL)) {
            fn.access = Opcodes.ACC_PROTECTED;
        }/*w w w.  j  av  a  2 s .co  m*/
    }
    return node;
}

From source file:pl.clareo.coroutines.core.ClassTransformer.java

License:Apache License

@SuppressWarnings("unchecked")
void transform() {
    for (MethodNode coroutine : coroutines) {
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Generating method for coroutine " + coroutine.name + coroutine.desc);
        }//from   w w w  .  j  av  a2  s  .  c o m
        String coroutineName = getCoroutineName(coroutine);
        MethodTransformer methodTransformer = new MethodTransformer(coroutine, thisType);
        MethodNode coroutineImpl = methodTransformer.transform(coroutineName, generateDebugCode);
        thisNode.methods.add(coroutineImpl);
        /*
         * generate co iterators and method stubs
         */
        log.finest("Generating CoIterator implementation and method stubs");
        String baseCoIteratorName;
        Map<String, Object> annotation = getCoroutineAnnotationValues(coroutine);
        if (getBoolean(annotation, "threadLocal")) {
            baseCoIteratorName = Type.getInternalName(ThreadLocalCoIterator.class);
        } else {
            baseCoIteratorName = Type.getInternalName(SingleThreadedCoIterator.class);
        }
        String coIteratorClassName = "pl/clareo/coroutines/core/CoIterator" + num;
        ClassNode coIteratorClass = new ClassNode();
        coIteratorClass.version = Opcodes.V1_6;
        coIteratorClass.access = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SUPER;
        coIteratorClass.name = coIteratorClassName;
        coIteratorClass.superName = baseCoIteratorName;
        if (generateDebugCode) {
            /*
             * If debugging code is emitted create field keeping JDK logger
             */
            FieldNode loggerField = new FieldNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC,
                    "logger", "Ljava/util/logging/Logger;", null, null);
            coIteratorClass.fields.add(loggerField);
            MethodNode clinit = new MethodNode();
            clinit.access = Opcodes.ACC_STATIC;
            clinit.name = "<clinit>";
            clinit.desc = "()V";
            clinit.exceptions = Collections.EMPTY_LIST;
            String loggerName = thisType.getClassName();
            InsnList clinitCode = clinit.instructions;
            clinitCode.add(new LdcInsnNode(loggerName));
            clinitCode.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/logging/Logger", "getLogger",
                    "(Ljava/lang/String;)Ljava/util/logging/Logger;"));
            clinitCode.add(new FieldInsnNode(Opcodes.PUTSTATIC, coIteratorClassName, "logger",
                    "Ljava/util/logging/Logger;"));
            clinitCode.add(new InsnNode(Opcodes.RETURN));
            clinit.maxStack = 1;
            clinit.maxLocals = 0;
            coIteratorClass.methods.add(clinit);
        }
        /*
         * Generate constructor
         */
        MethodNode init = new MethodNode();
        init.access = Opcodes.ACC_PUBLIC;
        init.name = "<init>";
        init.desc = CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR;
        init.exceptions = Collections.EMPTY_LIST;
        InsnList initCode = init.instructions;
        initCode.add(new VarInsnNode(Opcodes.ALOAD, 0));
        initCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
        initCode.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, baseCoIteratorName, "<init>",
                CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR));
        initCode.add(new InsnNode(Opcodes.RETURN));
        init.maxStack = 2;
        init.maxLocals = 2;
        coIteratorClass.methods.add(init);
        /*
         * Generate overriden call to coroutine
         */
        MethodNode call = new MethodNode();
        call.access = Opcodes.ACC_PROTECTED;
        call.name = "call";
        call.desc = CALL_METHOD_DESCRIPTOR;
        call.exceptions = Collections.EMPTY_LIST;
        InsnList callCode = call.instructions;
        /*
         * if debug needed generate call details
         */
        if (generateDebugCode) {
            String coroutineId = "Coroutine " + coroutine.name;
            callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER,
                    coroutineId + " call. Caller sent: ", 2));
            callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK));
            callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINEST,
                    coroutineId + " state ", 1));
            callCode.add(new FrameNode(Opcodes.F_SAME, 0, EMPTY_LOCALS, 0, EMPTY_STACK));
        }
        /*
         * push call arguments: this (if not static), frame, input, output
         */
        boolean isStatic = (coroutine.access & Opcodes.ACC_STATIC) != 0;
        if (!isStatic) {
            callCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
            callCode.add(
                    new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getThis", "()Ljava/lang/Object;"));
            callCode.add(new TypeInsnNode(Opcodes.CHECKCAST, thisType.getInternalName()));
        }
        callCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
        callCode.add(new InsnNode(Opcodes.ACONST_NULL));
        callCode.add(new VarInsnNode(Opcodes.ALOAD, 2));
        callCode.add(new MethodInsnNode(isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL,
                thisType.getInternalName(), coroutineName, COROUTINE_METHOD_DESCRIPTOR));
        // stack: *
        if (!generateDebugCode) {
            callCode.add(new InsnNode(Opcodes.ARETURN));
        } else {
            // save result display suspension point (two more locals
            // needed)
            callCode.add(new VarInsnNode(Opcodes.ASTORE, 3));
            callCode.add(new VarInsnNode(Opcodes.ALOAD, 1));
            callCode.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLineOfCode", "()I"));
            callCode.add(box_int(Type.INT));
            callCode.add(new VarInsnNode(Opcodes.ASTORE, 4));
            callCode.add(loggingInstructions(coIteratorClassName, "logger", Level.FINER,
                    "Coroutine suspended at line ", 4, ". Yielded:", 3));
            callCode.add(new FrameNode(Opcodes.F_APPEND, 2,
                    new Object[] { "java/lang/Object", "java/lang/Integer" }, 0, EMPTY_STACK));
            callCode.add(new VarInsnNode(Opcodes.ALOAD, 3));
            callCode.add(new InsnNode(Opcodes.ARETURN));
        }
        coIteratorClass.methods.add(call);
        // if debugging code is emitted it needs space for two
        // additional locals and 5 stack operand
        if (generateDebugCode) {
            call.maxStack = 5;
            call.maxLocals = 5;
        } else {
            if (isStatic) {
                call.maxStack = 3;
            } else {
                call.maxStack = 4;
            }
            call.maxLocals = 3;
        }
        /*
         * CoIterator created - define it in the runtime and verify if
         * needed
         */
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Generated class " + coIteratorClassName);
        }
        ClassWriter cw = new ClassWriter(0);
        coIteratorClass.accept(cw);
        byte[] classBytes = cw.toByteArray();
        try {
            CoroutineInstrumentator.dumpClass(coIteratorClassName, classBytes);
        } catch (IOException e) {
            throw new CoroutineGenerationException("Unable to write class " + coIteratorClassName, e);
        }
        /*
         * start generating method - new method is named as the method in
         * user code, it: returns instance of appropriate CoIterator (see
         * above), saves arguments of call
         */
        if (log.isLoggable(Level.FINEST)) {
            log.finest("Instrumenting method " + coroutine.name);
        }
        InsnList code = coroutine.instructions;
        code.clear();
        /*
         * create new Frame
         */
        boolean isDebugFramePossible = generateDebugCode && coroutine.localVariables != null;
        if (isDebugFramePossible) {
            code.add(createDebugFrame(coroutine));
        } else {
            code.add(createFrame(coroutine));
        }
        /*
         * save frame in the first, and locals array in the second local
         * variable
         */
        int argsSize = Type.getArgumentsAndReturnSizes(coroutine.desc) >> 2;
        if (isStatic) {
            argsSize -= 1;
        }
        code.add(new VarInsnNode(Opcodes.ASTORE, argsSize));
        code.add(new VarInsnNode(Opcodes.ALOAD, argsSize));
        code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, FRAME_NAME, "getLocals", "()[Ljava/lang/Object;"));
        int localsArrayIndex = argsSize + 1;
        code.add(new VarInsnNode(Opcodes.ASTORE, localsArrayIndex));
        /*
         * save all call arguments (along with this if this method is not
         * static) into locals array
         */
        Type[] argsTypes = Type.getArgumentTypes(coroutine.desc);
        if (!isStatic) {
            code.add(saveloc(localsArrayIndex, 0, 0, JAVA_LANG_OBJECT));
            code.add(savelocs(localsArrayIndex, 1, 1, argsTypes));
        } else {
            code.add(savelocs(localsArrayIndex, 0, 0, argsTypes));
        }
        /*
         * create CoIterator instance with saved frame, make initial call to
         * next if needed and return to caller
         */
        code.add(new TypeInsnNode(Opcodes.NEW, coIteratorClassName));
        code.add(new InsnNode(Opcodes.DUP));
        code.add(new VarInsnNode(Opcodes.ALOAD, argsSize));
        code.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, coIteratorClassName, "<init>",
                CO_ITERATOR_CONSTRUCTOR_DESCRIPTOR));
        if (!getBoolean(annotation, "generator", true)) {
            code.add(new InsnNode(Opcodes.DUP));
            code.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, coIteratorClassName, "next",
                    "()Ljava/lang/Object;"));
            code.add(new InsnNode(Opcodes.POP));
        }
        code.add(new InsnNode(Opcodes.ARETURN));
        /*
         * end method generation; maxs can be statically determined 3
         * operands on stack (call to frame setLocals and CoIterator
         * constructor) + 1 if any argument is long or double (debug frame
         * needs 7 operands for variable names creation); locals = argsSize
         * + 1 reference to frame + 1 array of locals
         */
        if (isDebugFramePossible) {
            coroutine.maxStack = 7;
        } else {
            boolean isCategory2ArgumentPresent = false;
            for (Type argType : argsTypes) {
                int sort = argType.getSort();
                if (sort == Type.LONG || sort == Type.DOUBLE) {
                    isCategory2ArgumentPresent = true;
                    break;
                }
            }
            coroutine.maxStack = isCategory2ArgumentPresent ? 4 : 3;
        }
        coroutine.maxLocals = localsArrayIndex + 1;
        coroutine.localVariables.clear();
        coroutine.tryCatchBlocks.clear();
        num++;
    }
}

From source file:pt.minha.kernel.instrument.SyncToMonitorClassVisitor.java

License:Open Source License

public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (trans.isSynchronized()) {
        if (name.equals("<clinit>")) {
            hasClinit = true;//  w  ww .  j  a  va 2 s  .com
            return new ClinitVisitor(super.visitMethod(access, name, desc, signature, exceptions));
        }

        if ((access & Opcodes.ACC_SYNCHRONIZED) != 0) {
            makeStub(access & ~Opcodes.ACC_SYNCHRONIZED, name, desc, signature, exceptions);

            return super.visitMethod(
                    (access & ~(Opcodes.ACC_SYNCHRONIZED | Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)
                            | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PRIVATE),
                    "_" + name, desc, signature, exceptions);
        }
    }
    return super.visitMethod(access, name, desc, signature, exceptions);
}

From source file:scouter.agent.asm.MethodASM.java

License:Apache License

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
    if (mv == null || mset.isA(name, desc) == false) {
        return mv;
    }/*from  w  w w .  j a  va 2  s.com*/
    if (AsmUtil.isSpecial(name)) {
        return mv;
    }

    // check exclude method set
    for (int i = 0; i < excludeTarget.size(); i++) {
        HookingSet excludeSet = excludeTarget.get(i);
        if (excludeSet.classMatch.include(className)) {
            if (excludeSet.isA(name, desc)) {
                return mv;
            }
        }
    }

    Configure conf = Configure.getInstance();
    boolean isPublic = conf.hook_method_access_public_enabled;
    boolean isProtected = conf.hook_method_access_protected_enabled;
    boolean isPrivate = conf.hook_method_access_private_enabled;
    boolean isNone = conf.hook_method_access_none_enabled;

    //lambda method
    if (conf.hook_method_lambda_enable && (access & Opcodes.ACC_SYNTHETIC) == Opcodes.ACC_SYNTHETIC
            && name.indexOf("lambda$") == 0) {
        //if lambda method with hook_method_lambda_enabled then go on without method accessor check
    } else { // non-lambda method
        switch (access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED | Opcodes.ACC_PRIVATE)) {
        case Opcodes.ACC_PUBLIC:
            if (isPublic == false)
                return mv;
            break;
        case Opcodes.ACC_PROTECTED:
            if (isProtected == false)
                return mv;
            break;
        case Opcodes.ACC_PRIVATE:
            if (isPrivate == false)
                return mv;
            break;
        default:
            if (isNone == false)
                return mv;
            break;
        }
    }

    // check prefix, to ignore simple method such as getter,setter
    if (conf.isIgnoreMethodPrefix(name))
        return mv;

    String fullname = AsmUtil.makeMethodFullName(className, name, desc);
    int fullname_hash = DataProxy.sendMethodName(fullname);

    return new MethodMV(access, desc, mv, fullname, fullname_hash);
}

From source file:the.bytecode.club.bytecodeviewer.decompilers.bytecode.ClassNodeDecompiler.java

License:Open Source License

public static String getAccessString(int access) {
    List<String> tokens = new ArrayList<String>();
    if ((access & Opcodes.ACC_PUBLIC) != 0)
        tokens.add("public");
    if ((access & Opcodes.ACC_PRIVATE) != 0)
        tokens.add("private");
    if ((access & Opcodes.ACC_PROTECTED) != 0)
        tokens.add("protected");
    if ((access & Opcodes.ACC_FINAL) != 0)
        tokens.add("final");
    if ((access & Opcodes.ACC_SYNTHETIC) != 0)
        tokens.add("synthetic");
    // if ((access & Opcodes.ACC_SUPER) != 0)
    // tokens.add("super"); implied by invokespecial insn
    if ((access & Opcodes.ACC_ABSTRACT) != 0)
        tokens.add("abstract");
    if ((access & Opcodes.ACC_INTERFACE) != 0)
        tokens.add("interface");
    if ((access & Opcodes.ACC_ENUM) != 0)
        tokens.add("enum");
    if ((access & Opcodes.ACC_ANNOTATION) != 0)
        tokens.add("annotation");
    if (!tokens.contains("interface") && !tokens.contains("enum") && !tokens.contains("annotation"))
        tokens.add("class");
    if (tokens.size() == 0)
        return "[Error parsing]";

    // hackery delimeters
    StringBuilder sb = new StringBuilder(tokens.get(0));
    for (int i = 1; i < tokens.size(); i++) {
        sb.append(" ");
        sb.append(tokens.get(i));// ww  w  .ja v  a 2s  .  c  om
    }
    return sb.toString();
}

From source file:the.bytecode.club.bytecodeviewer.decompilers.bytecode.FieldNodeDecompiler.java

License:Open Source License

private static String getAccessString(int access) {
    List<String> tokens = new ArrayList<String>();
    if ((access & Opcodes.ACC_PUBLIC) != 0)
        tokens.add("public");
    if ((access & Opcodes.ACC_PRIVATE) != 0)
        tokens.add("private");
    if ((access & Opcodes.ACC_PROTECTED) != 0)
        tokens.add("protected");
    if ((access & Opcodes.ACC_SYNTHETIC) != 0)
        tokens.add("synthetic");
    if ((access & Opcodes.ACC_STATIC) != 0)
        tokens.add("static");
    if ((access & Opcodes.ACC_FINAL) != 0)
        tokens.add("final");
    if ((access & Opcodes.ACC_TRANSIENT) != 0)
        tokens.add("transient");
    if ((access & Opcodes.ACC_VOLATILE) != 0)
        tokens.add("volatile");
    if (tokens.size() == 0)
        return "";
    // hackery delimeters
    StringBuilder sb = new StringBuilder(tokens.get(0));
    for (int i = 1; i < tokens.size(); i++) {
        sb.append(" ");
        sb.append(tokens.get(i));//w w w .j a v  a  2 s .  c o m
    }
    return sb.toString();
}

From source file:the.bytecode.club.bytecodeviewer.decompilers.bytecode.MethodNodeDecompiler.java

License:Open Source License

private static String getAccessString(int access) {
    // public, protected, private, abstract, static,
    // final, synchronized, native & strictfp are permitted
    List<String> tokens = new ArrayList<String>();
    if ((access & Opcodes.ACC_PUBLIC) != 0)
        tokens.add("public");
    if ((access & Opcodes.ACC_PRIVATE) != 0)
        tokens.add("private");
    if ((access & Opcodes.ACC_PROTECTED) != 0)
        tokens.add("protected");
    if ((access & Opcodes.ACC_STATIC) != 0)
        tokens.add("static");
    if ((access & Opcodes.ACC_ABSTRACT) != 0)
        tokens.add("abstract");
    if ((access & Opcodes.ACC_FINAL) != 0)
        tokens.add("final");
    if ((access & Opcodes.ACC_SYNCHRONIZED) != 0)
        tokens.add("synchronized");
    if ((access & Opcodes.ACC_NATIVE) != 0)
        tokens.add("native");
    if ((access & Opcodes.ACC_STRICT) != 0)
        tokens.add("strictfp");
    if ((access & Opcodes.ACC_BRIDGE) != 0)
        tokens.add("bridge");
    if ((access & Opcodes.ACC_VARARGS) != 0)
        tokens.add("varargs");
    if (tokens.size() == 0)
        return "";
    // hackery delimeters
    StringBuilder sb = new StringBuilder(tokens.get(0));
    for (int i = 1; i < tokens.size(); i++) {
        sb.append(" ");
        sb.append(tokens.get(i));/*  w  w  w.j  a  va  2 s .c  o m*/
    }
    return sb.toString();
}