jvm-bytecode

Inserting to InsnList before several nodes

无人久伴 提交于 2019-12-07 19:23:25
I am trying to: 1) Iterate instructions and find all relevant nodes 2) Insert custom code before found nodes I used streams and iterator to make it and insert, which works only for the first node InsnList instructions = methodNode.instructions; InsnList addition = ... //It work: found n nodes for n return instructions Stream<AbstractInsnNode> returnNodes = Stream.iterate(instructions.getFirst(), AbstractInsnNode::getNext).limit(instructions.size()) .filter(n -> returnOpcodes.contains(n.getOpcode())); //It not work: inserted only before first node returnNodes.forEach(n -> instructions

ASM Try/Catch Block with an output value

北城余情 提交于 2019-12-06 06:13:41
I am currently trying make my custom compiler allow using try/catch as an expression, i.e. leaving a value on the stack. The type checker and the backend already support this, but the problem seems to be ASM's COMPUTE_FRAMES . With the below code for instrumentation: private void write(MethodWriter writer, boolean expression) { org.objectweb.asm.Label tryStart = new org.objectweb.asm.Label(); org.objectweb.asm.Label tryEnd = new org.objectweb.asm.Label(); org.objectweb.asm.Label endLabel = new org.objectweb.asm.Label(); boolean hasFinally = this.finallyBlock != null; writer.writeLabel(tryStart

Why is there no ICMP instruction?

心已入冬 提交于 2019-12-05 14:15:16
As some of you might know, we have a ton of opcodes for comparing different types of primitive values: LCMP FCMPL FCMPG DCMPL DCMPG IFEQ IFNE IFLT IFGE IFGT IFLE IF_ICMPEQ IF_ICMPNE IF_ICMPLT IF_ICMPGE IF_ICMPGT IF_ICMPLE IF_ACMPEQ IF_ACMPNE ... For obvious reasons the creators of the instruction set did not bother to add all IF_LCMPEQ , IF_FCMPLT , ... instructions, but I am wondering why there is no ICMP instruction, seeing that it would be very useful especially for booleans or Integer.compare(int, int) . There are already two "primarily opinion based" close votes. Indeed, nobody can give a

Status of JSR/RET in JVM spec

为君一笑 提交于 2019-12-05 02:39:30
There are some parts of the JVM specification which suggest that the operations JSR (Jump SubRoutine) , JSR_W (Jump SubRoutine Wide) and RET (RETurn from subroutine) may be used only up to class file version 50.0 (JDK 1.6): 3.13 Compiling Finally (This section assumes a compiler generates class files with version number 50.0 or below, so that the jsr instruction may be used. See also §4.10.2.5 .) And later: 4.10.2.5. Exceptions and finally To implement the try - finally construct, a compiler for the Java programming language that generates class files with version number 50.0 or below may use

Java bytecode decompiler in IntelliJIDEA for Scala

旧城冷巷雨未停 提交于 2019-12-03 11:53:50
I'm using IntellijIDEA Ultimate Edition. Browsing .class files that compiled from java source code is easy: I can just double-click on .class file and IDEA will decompile it. However, with .class files that were compiled from scala source code it's not working. It seems that IDEA just referencing to the scala source file. I've noticed that IDEA behaves like that only with scala plugin installed. It works fine without it. Is there a way to decompile using IDEA without switching off scala plugin ? Finally this feature was released in Intellij. From official website : You can decompile your Scala

Java Bytecode Signatures

走远了吗. 提交于 2019-12-01 18:17:23
As part of the compiler for the programming language I am working on, I came across generic signatures in the bytecode, which I am trying to parse and convert to an AST. The parsing algorithm mostly works, but there seems to be a special case in which the format of these signatures behaves a bit strangely. Here are a few of these cases: java.util.Arrays#parallelSort: <T::Ljava/lang/Comparable<-TT;>;>([TT;)V java.util.Arrays#parallelSort: <T::Ljava/lang/Comparable<-TT;>;>([TT;II)V java.lang.Class#getAnnotation: <A::Ljava/lang/annotation/Annotation;>(Ljava/lang/Class<TA;>;)TA; java.lang.Class

Java Bytecode Signatures

こ雲淡風輕ζ 提交于 2019-12-01 18:08:39
问题 As part of the compiler for the programming language I am working on, I came across generic signatures in the bytecode, which I am trying to parse and convert to an AST. The parsing algorithm mostly works, but there seems to be a special case in which the format of these signatures behaves a bit strangely. Here are a few of these cases: java.util.Arrays#parallelSort: <T::Ljava/lang/Comparable<-TT;>;>([TT;)V java.util.Arrays#parallelSort: <T::Ljava/lang/Comparable<-TT;>;>([TT;II)V java.lang

How to get bytecode as byte array from Class

回眸只為那壹抹淺笑 提交于 2019-11-30 20:50:30
问题 Given an arbitrary Class instance, including one that's runtime generated (no .class file on disk), is there any way to get the class bytes? 回答1: In general, this is not possible. While loading a class, JVM parses its bytecode and converts it to the internal representation. After that JVM is free to forget the original bytecode, and that's what really happens with HotSpot JVM. However, with the certain hacks it is possible to inspect the internal class representation and convert it back to a

Dynamic Java Bytecode Manipulation Framework Comparison

孤街浪徒 提交于 2019-11-29 20:18:46
There are some frameworks out there for dynamic bytecode generation, manipulation and weaving (BCEL, CGLIB, javassist, ASM, MPS). I want to learn about them, but since I don't have much time to know all the details about all of them, I would like to see a sort of comparison chart saying the advantages and disadvantages of one versus the others and an explanation of why. Here in SO, I found a lot of questions asking something similar, and the answers normally said "you can use cglib or ASM", or "javassist is better than cglib", or "BCEL is old and is dying" or "ASM is the best because it gives

Is there a clever way to determine the length of Java bytecode instructions?

落爺英雄遲暮 提交于 2019-11-29 18:00:57
I'm creating a static analysis tool for Java, and there's some information about the programs I'm analyzing that will be easier to get if I can get it from the bytecode in .class files. I don't care about every single one the instructions that might be in the class file. E.g., I might only need to see if there are any getfield instructions. The problem is that since each instruction has a variable length, it seems that in the general case, I need to (in my code) specify the length of every single opcode before I can determine where the (e.g.) getfield instructions start and end. For some other