bytecode

Where's the code for a lambda located in a java class file?

别等时光非礼了梦想. 提交于 2020-02-16 08:14:16
问题 I've this java source file: import java.util.function.*; public class t { public static void main(String[] args) { Function<Integer,Integer> r = (a) -> a*a+2*a+1; System.out.println(r.apply(2)); } } I compile it and it works as expected. Here's the output of javap -c -v t , and I can't find the location of lambda in it. Where's the bytecode which tells the jvm to compute the expression with the input Integer whenever the lambda is envoked? 回答1: If you want to see the code of your lambda body

Is it possible to retrieve lambda expression at runtime

≯℡__Kan透↙ 提交于 2020-02-10 15:02:53
问题 I was playing with Java8 Lambda last night and I was wondering if it is possible to retrieve the Lambda expression at runtime. In short and as far as I understood, Lambda expression are converted into (static) methods at runtime and then called using InvokeDynamics. Let's take an example like this: people.filter(person -> person.getAge() >= minAge); where filter would be a custom method taking a Predicate<T> as a parameter. Inside this filter method, how could I retrieve the argument in a

Javassist: insert a method at the beginning of catch block

隐身守侯 提交于 2020-02-06 02:51:08
问题 I have code: ControlFlow cf = new ControlFlow(method); for (ControlFlow.Block block : cf.basicBlocks()) { ControlFlow.Catcher catchBlocks[] = block.catchers(); for (int i = 0;i < catchBlocks.length;i++) { int position = catchBlocks[i].block().position(); method.insertAt(position, "System.out.println(\"catch block\")") } } This code snippet inserts the print statement at beginning of the method, which is not what I want. I want the code to be placed like: void foo() { try { a(); } catch

Javassist: insert a method at the beginning of catch block

对着背影说爱祢 提交于 2020-02-06 02:50:13
问题 I have code: ControlFlow cf = new ControlFlow(method); for (ControlFlow.Block block : cf.basicBlocks()) { ControlFlow.Catcher catchBlocks[] = block.catchers(); for (int i = 0;i < catchBlocks.length;i++) { int position = catchBlocks[i].block().position(); method.insertAt(position, "System.out.println(\"catch block\")") } } This code snippet inserts the print statement at beginning of the method, which is not what I want. I want the code to be placed like: void foo() { try { a(); } catch

Does javac also inline?

試著忘記壹切 提交于 2020-01-24 13:12:12
问题 I was playing around with javap and some very simple code and that raised a - hopefully simple - question. here is the code first: public class Main { public static void main(String[] args) throws Exception { System.out.println(m1()); System.out.println(m2()); } private static String m1() { return new String("foobar"); } private static String m2() { String str = "foobar"; return new String(str); } } Now I compiled the code and looked at the output (omitting -verbose for now). $ javap -c Main

Why is integer divisions not optimised when compiling to bytecode?

那年仲夏 提交于 2020-01-24 07:48:07
问题 First, let me show a experiment I do: In [69]: dis.dis(lambda : 4 / 2 + 1.5 * 2 + (4 - 2)) 1 0 LOAD_CONST 1 (4) 3 LOAD_CONST 2 (2) 6 BINARY_DIVIDE 7 LOAD_CONST 4 (3.0) 10 BINARY_ADD 11 LOAD_CONST 5 (2) 14 BINARY_ADD 15 RETURN_VALUE As you can see in the output of dis.dis , 1.5 * 2 and 4 - 2 get compiled to LOAD_CONST instead of two LOAD_CONST followed by a binary operation. But 4 / 2 is not replaced with something like LOAD_CONST 4 (2) . I wonder why is division left out in the optimisation.

Missing branches when using assertTrue instead of assertNull

廉价感情. 提交于 2020-01-21 01:11:13
问题 In Java/Junit, I need to test for null with some object. There are a variety of ways I can test a condition but I have been using assertTrue for most of my tests. When I check for nulls in an assertTrue, EclEmma states that it is only testing one branch. When I resolve the statement into a variable manually (like setting the result to a boolean and passing it into assertTrue) the code coverage is deemed complete on the assert but not on the variable initializing line. Why is this happening?

Is it possible to store HHVM bytecode as a file?

可紊 提交于 2020-01-14 10:37:08
问题 Is there a way to save HHVM bytecode as a file, since HHVM uses JIT bytecode compilation instead of compiling? If not, are there any alternatives for modern PHP versions (5.5,5.6)? 回答1: You can generate bytecode, which is saved in internal SQLite database and then switch on Repo.Authoritative mode, under which HHVM will only use bytecode from SQLite db and never touch source .php files. See http://hhvm.com/blog/4061/go-faster 来源: https://stackoverflow.com/questions/25815519/is-it-possible-to

creating object instance without invoking initializer

笑着哭i 提交于 2020-01-14 08:13:51
问题 I'm trying to generate bytecode wich will create object instance without code initialization logic. Actually I want to reproduce generateSerializationConstructor behavior. { mv = cw.visitMethod(ACC_PUBLIC, "newObjectInstance", "()Ljava/lang/Object;", null, null); mv.visitCode(); mv.visitTypeInsn(NEW, classNameInternal); mv.visitInsn(DUP); classNameInternal = "java/lang/Object"; mv.visitMethodInsn(INVOKESPECIAL, classNameInternal, "<init>", "()V"); mv.visitInsn(ARETURN); mv.visitMaxs(0, 0); mv

android replace a method call at runtime

杀马特。学长 韩版系。学妹 提交于 2020-01-13 06:01:26
问题 I am developing an Android app with a 3rd-party library. I want to replace a method call in the library. Please note that I cannot obtain the library's source code, so that I have to change it at runtime. For example, let's assume there is doA() method in a class Foo in the library class Foo { doA() { //method body } ... } I want to replace the method body of doA() with my own code. I have done some exploration and found the following stackoverflow thread: Replacing a method call in a class