bytecode

Why bytecode calls Object->getClass() at a direct field access

只谈情不闲聊 提交于 2019-11-29 16:10:56
I decompiled Java (actually Dalvik) bytecode. In the beginning of a method, I access a field of an instance member directly (i.e. not through a getter). It seems tha Java calls Object.getClass() on the accessed instance member ( mOther ), but doesn't use the result anywhere. Is this some sort of check? Why is this call needed? I suspect it is because I access a field directly (which is defined in that class), but I don't see the connection. The Java code and the decompiled bytecode are as follows. (Note that the last instruction loads lifeTime as constant 0x0001 because in MyOtherClass , I

Is list join really faster than string concatenation in python?

安稳与你 提交于 2019-11-29 14:42:54
I find that string concatenation seems to have less python bytecode than list join. This is an example. test.py: a = ''.join(['a', 'b', 'c']) b = 'a' + 'b' + 'c' Then I execute python -m dis test.py . I got the following python bytecode (python 2.7): 1 0 LOAD_CONST 0 ('') 3 LOAD_ATTR 0 (join) 6 LOAD_CONST 1 ('a') 9 LOAD_CONST 2 ('b') 12 LOAD_CONST 3 ('c') 15 BUILD_LIST 3 18 CALL_FUNCTION 1 21 STORE_NAME 1 (a) 3 24 LOAD_CONST 6 ('abc') 27 STORE_NAME 2 (b) 30 LOAD_CONST 4 (None) 33 RETURN_VALUE Obviously, the bytecode number of string concatenation is less.It just load string 'abc' directly. Can

Python: LOAD_FAST vs. LOAD_DEREF with inplace addition

只谈情不闲聊 提交于 2019-11-29 14:42:01
问题 Last Friday I went to a job interview and had to answer the following question: why does this code raise an exception ( UnboundLocalError: local variable 'var' referenced before assignment on the line containing var += 1 )? def outer(): var = 1 def inner(): var += 1 return var return inner I couldn't give a proper answer; this fact really upset me, and when I came home I tried really hard to find a proper answer. Well, I have found the answer, but now there's something else that confuses me.

How does Arrays work in the ByteCode of Java [duplicate]

百般思念 提交于 2019-11-29 14:29:33
This question already has an answer here: Where is array's length property defined? 7 answers How does array class work in Java? 4 answers If I use a normal class like List, Vector or something else, I get a size() function which returns the length of the regarded class but if I use an array of a class or a default data type I get a public member length which returns the current length of the array. int a[] = new int[3]; a.length; // used without () Vector<Integer> v = new Vector<Integer>(); v.length(); // used with () Why is that? I mean an array is not an own class isn't it? So if it is no

Load Java-Byte-Code at Runtime

半世苍凉 提交于 2019-11-29 14:13:00
问题 I got some java-byte-code (so compiled java-source) which is generated in my program. Now I want to load this byte-code into the currently running Java-VM and run a specific function. I'm not sure how to accomplish this, I digged a little bit into the Java Classloaders but found no straight way. I found a solution which takes a class-file on the harddisk, but the bytecode I got is in a Byte-Array and I dont want to write it down to the disk but use it directly instead. Thanks! 回答1: you need

How to create a default constructor with Byte Buddy

送分小仙女□ 提交于 2019-11-29 13:53:45
I want to intercept some method calls on one of my classes but those classes dont have a default constructor. Given the following class, how would I setup Byte Buddy to also create a public no-argument constructor to be able to create the generated class? public class GetLoggedInUsersSaga extends AbstractSpaceSingleEventSaga { private final UserSessionRepository userSessionRepository; @Inject public GetLoggedInUsersSaga(final UserSessionRepository userSessionRepository) { this.userSessionRepository = userSessionRepository; } @StartsSaga public void handle(final GetLoggedInUsersRequest request)

What Java code will force javac 1.6 to use the 'swap' and 'nop' opcodes?

喜夏-厌秋 提交于 2019-11-29 13:50:43
I'm working on an amateur JVM implementation, and I'm trying to make sure I have test coverage for all of the opcodes in the spec. I've gotten it down to the last few, but nop and swap have been eluding me. For example, here's a simple function that might use swap : static int do_swap() { int a = 56; int b = 32; return b%a; } But the bytecode produced by javac 1.6 avoids swapping in lieu of local storage: static int do_swap(); Code: 0: bipush 56 2: istore_0 3: bipush 32 5: istore_1 6: iload_1 7: iload_0 8: irem 9: ireturn Any ideas? None. The Java Language Specification does not provide such

Any Java Bytecode Generation Guide? [closed]

你离开我真会死。 提交于 2019-11-29 11:11:52
we're writing some sort of compiler from Pascal to JVM Bytecode . And we've already implemented an expression tree generation, so the next step should be the creation of .class file. Can you suggest any guide/tutorial of how to generate any .class file at least from some static data? Because I've googled for 2 hours already and read JVM specification, but I really need some even simplest example to start developing the whole stuff. Someone has already written a widely-used byte code generation library: CGLIB . You'd have it knocked if you could figure out how to get your AST into CGLIB. Uko

Is it valid to have a JVM bytecode class without any constructor?

人走茶凉 提交于 2019-11-29 09:19:18
AFAIK, in Java implicit constructors are always generated for a class without constructors [1] , [2] . But in bytecode I could not find such restriction on the JVMS . So: is it valid according to the JVMS to define a class without constructor only to use its static methods as in the following jasmin hello world? does it have any further consequences besides not being able to create instances of it? I won't be able to use invokespecial to initialize instances, which renders new useless according to https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.10.2.4 (can't use

Can compiled bytecode files (.pyc) get generated in different directory? [duplicate]

北城以北 提交于 2019-11-29 09:15:35
Possible Duplicate: Way to have compiled python files in a separate folder? When python compiles modules to bytecode, it produces .pyc files from your .py files. My question is, is it possible to have these .pyc files written to a different directory than where the module resides? For example, I have a large directory of modules. Rather than having it littered with .pyc files, I would like to keep my source code in the directory and have a subdirectory like "bytecode" where all of the .pyc are stored. Is this possible? mikl This is answered in " Way to have compiled python files in a seperate