Search in sources :

Example 11 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project intellij-community by JetBrains.

the class BytecodeAnalysisIndex method collectKeys.

@NotNull
private static Map<Bytes, Void> collectKeys(byte[] content) throws NoSuchAlgorithmException {
    HashMap<Bytes, Void> map = new HashMap<>();
    MessageDigest md = BytecodeAnalysisConverter.getMessageDigest();
    new ClassReader(content).accept(new KeyedMethodVisitor() {

        @Nullable
        @Override
        MethodVisitor visitMethod(MethodNode node, Key key) {
            map.put(ClassDataIndexer.compressKey(md, key), null);
            return null;
        }
    }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    return map;
}
Also used : MethodNode(org.jetbrains.org.objectweb.asm.tree.MethodNode) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) MessageDigest(java.security.MessageDigest) Nullable(org.jetbrains.annotations.Nullable) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project android by JetBrains.

the class ResourceClassGenerator method generateIntArrayFromCache.

private static void generateIntArrayFromCache(@NotNull ClassWriter cw, String className, Map<String, List<Integer>> styleableCache) {
    // Generate the field declarations.
    for (String name : styleableCache.keySet()) {
        cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, name, "[I", null, null);
    }
    // Generate class initializer block to initialize the arrays declared above.
    MethodVisitor mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
    mv.visitCode();
    for (Map.Entry<String, List<Integer>> entry : styleableCache.entrySet()) {
        List<Integer> values = entry.getValue();
        if (!values.isEmpty()) {
            generateArrayInitialization(mv, className, entry.getKey(), values);
        }
    }
    mv.visitInsn(RETURN);
    mv.visitMaxs(4, 0);
    mv.visitEnd();
}
Also used : List(java.util.List) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) Map(java.util.Map) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 13 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project intellij-community by JetBrains.

the class StringPropertyCodeGenerator method generateLoadTextMethod.

private void generateLoadTextMethod(final AsmCodeGenerator.FormClassVisitor visitor, final String methodName, final String componentClass, final String setMnemonicMethodName) {
    MethodVisitor mv = visitor.visitNewMethod(ACC_PRIVATE | ACC_SYNTHETIC, methodName, "(L" + componentClass + ";Ljava/lang/String;)V", null, null);
    mv.visitCode();
    mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
    mv.visitInsn(DUP);
    mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "()V", false);
    mv.visitVarInsn(ASTORE, 3);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 4);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 5);
    mv.visitInsn(ICONST_M1);
    mv.visitVarInsn(ISTORE, 6);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ISTORE, 7);
    Label l0 = new Label();
    mv.visitLabel(l0);
    mv.visitVarInsn(ILOAD, 7);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "length", "()I", false);
    Label l1 = new Label();
    mv.visitJumpInsn(IF_ICMPGE, l1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitVarInsn(ILOAD, 7);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
    mv.visitIntInsn(BIPUSH, 38);
    Label l2 = new Label();
    mv.visitJumpInsn(IF_ICMPNE, l2);
    mv.visitIincInsn(7, 1);
    mv.visitVarInsn(ILOAD, 7);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "length", "()I", false);
    Label l3 = new Label();
    mv.visitJumpInsn(IF_ICMPNE, l3);
    mv.visitJumpInsn(GOTO, l1);
    mv.visitLabel(l3);
    mv.visitVarInsn(ILOAD, 4);
    mv.visitJumpInsn(IFNE, l2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitVarInsn(ILOAD, 7);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
    mv.visitIntInsn(BIPUSH, 38);
    mv.visitJumpInsn(IF_ICMPEQ, l2);
    mv.visitInsn(ICONST_1);
    mv.visitVarInsn(ISTORE, 4);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitVarInsn(ILOAD, 7);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
    mv.visitVarInsn(ISTORE, 5);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "length", "()I", false);
    mv.visitVarInsn(ISTORE, 6);
    mv.visitLabel(l2);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitVarInsn(ILOAD, 7);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(C)Ljava/lang/StringBuffer;", false);
    mv.visitInsn(POP);
    mv.visitIincInsn(7, 1);
    mv.visitJumpInsn(GOTO, l0);
    mv.visitLabel(l1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 3);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "toString", "()Ljava/lang/String;", false);
    mv.visitMethodInsn(INVOKEVIRTUAL, componentClass, "setText", "(Ljava/lang/String;)V", false);
    mv.visitVarInsn(ILOAD, 4);
    Label l4 = new Label();
    mv.visitJumpInsn(IFEQ, l4);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ILOAD, 5);
    mv.visitMethodInsn(INVOKEVIRTUAL, componentClass, setMnemonicMethodName, "(C)V", false);
    if (myHaveSetDisplayedMnemonicIndex) {
        mv.visitVarInsn(ALOAD, 1);
        mv.visitVarInsn(ILOAD, 6);
        mv.visitMethodInsn(INVOKEVIRTUAL, componentClass, "setDisplayedMnemonicIndex", "(I)V", false);
    }
    mv.visitLabel(l4);
    mv.visitInsn(RETURN);
    mv.visitMaxs(3, 8);
    mv.visitEnd();
}
Also used : Label(org.jetbrains.org.objectweb.asm.Label) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 14 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project intellij-community by JetBrains.

the class LocalVariablesUtil method collectVariablesFromBytecode.

@NotNull
private static List<DecompiledLocalVariable> collectVariablesFromBytecode(VirtualMachineProxyImpl vm, Location location, MultiMap<Integer, String> namesMap) {
    if (!vm.canGetBytecodes()) {
        return Collections.emptyList();
    }
    try {
        LOG.assertTrue(location != null);
        final com.sun.jdi.Method method = location.method();
        final Location methodLocation = method.location();
        if (methodLocation == null || methodLocation.codeIndex() < 0) {
            // native or abstract method
            return Collections.emptyList();
        }
        long codeIndex = location.codeIndex();
        if (codeIndex > 0) {
            final byte[] bytecodes = method.bytecodes();
            if (bytecodes != null && bytecodes.length > 0) {
                final int firstLocalVariableSlot = getFirstLocalsSlot(method);
                final HashMap<Integer, DecompiledLocalVariable> usedVars = new HashMap<>();
                MethodBytecodeUtil.visit(method, codeIndex, new MethodVisitor(Opcodes.API_VERSION) {

                    @Override
                    public void visitVarInsn(int opcode, int slot) {
                        if (slot >= firstLocalVariableSlot) {
                            DecompiledLocalVariable variable = usedVars.get(slot);
                            String typeSignature = MethodBytecodeUtil.getVarInstructionType(opcode).getDescriptor();
                            if (variable == null || !typeSignature.equals(variable.getSignature())) {
                                variable = new DecompiledLocalVariable(slot, false, typeSignature, namesMap.get(slot));
                                usedVars.put(slot, variable);
                            }
                        }
                    }
                }, false);
                if (usedVars.isEmpty()) {
                    return Collections.emptyList();
                }
                List<DecompiledLocalVariable> vars = new ArrayList<>(usedVars.values());
                vars.sort(Comparator.comparingInt(DecompiledLocalVariable::getSlot));
                return vars;
            }
        }
    } catch (UnsupportedOperationException ignored) {
    } catch (Exception e) {
        LOG.error(e);
    }
    return Collections.emptyList();
}
Also used : com.sun.jdi(com.sun.jdi) EvaluateException(com.intellij.debugger.engine.evaluation.EvaluateException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with MethodVisitor

use of org.jetbrains.org.objectweb.asm.MethodVisitor in project intellij-community by JetBrains.

the class PreviewNestedFormLoader method generateStubClass.

private void generateStubClass(final LwRootContainer rootContainer, final String generatedClassName) throws IOException, CodeGenerationException {
    @NonNls ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, generatedClassName, null, "java/lang/Object", ArrayUtil.EMPTY_STRING_ARRAY);
    cw.visitField(Opcodes.ACC_PUBLIC, PreviewFormAction.PREVIEW_BINDING_FIELD, "Ljavax/swing/JComponent;", null, null);
    @NonNls MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
    mv.visitCode();
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    mv.visitInsn(Opcodes.RETURN);
    mv.visitMaxs(1, 1);
    mv.visitEnd();
    cw.visitEnd();
    ByteArrayInputStream bais = new ByteArrayInputStream(cw.toByteArray());
    AsmCodeGenerator acg = new AsmCodeGenerator(rootContainer, myFinder, this, true, new PsiClassWriter(myModule));
    byte[] data = acg.patchClass(bais);
    FormErrorInfo[] errors = acg.getErrors();
    if (errors.length > 0) {
        throw new CodeGenerationException(errors[0].getComponentId(), errors[0].getErrorMessage());
    }
    FileUtil.writeToFile(new File(myTempPath, generatedClassName + ".class"), data);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) CodeGenerationException(com.intellij.uiDesigner.compiler.CodeGenerationException) ByteArrayInputStream(java.io.ByteArrayInputStream) PsiClassWriter(com.intellij.compiler.PsiClassWriter) FormErrorInfo(com.intellij.uiDesigner.compiler.FormErrorInfo) AsmCodeGenerator(com.intellij.uiDesigner.compiler.AsmCodeGenerator) File(java.io.File) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter) PsiClassWriter(com.intellij.compiler.PsiClassWriter) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Aggregations

MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)23 NotNull (org.jetbrains.annotations.NotNull)7 InstructionAdapter (org.jetbrains.org.objectweb.asm.commons.InstructionAdapter)7 Type (org.jetbrains.org.objectweb.asm.Type)5 KotlinType (org.jetbrains.kotlin.types.KotlinType)4 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)3 Label (org.jetbrains.org.objectweb.asm.Label)3 MethodNode (org.jetbrains.org.objectweb.asm.tree.MethodNode)3 SourcePosition (com.intellij.debugger.SourcePosition)2 DebuggerUtilsEx (com.intellij.debugger.impl.DebuggerUtilsEx)2 MethodBytecodeUtil (com.intellij.debugger.jdi.MethodBytecodeUtil)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 Logger (com.intellij.openapi.diagnostic.Logger)2 Document (com.intellij.openapi.editor.Document)2 com.intellij.psi (com.intellij.psi)2 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)2 Opcodes (org.jetbrains.org.objectweb.asm.Opcodes)2