Getting “ArrayIndexOutOfBoundsException: null” with NO stack trace

String concatenated with a null reference might get you such a message: Object obj = null; throw new ArrayIndexOutOfBoundsException(“” + obj); If you’re using an Oracle JVM you may want to add -XX:-OmitStackTraceInFastThrow as an additional parameter to see if it helps. For some basic exceptions, the JVM reuses the same exception instance after a … Read more

Why is the number of local variables used in a Java bytecode method not the most economical?

There are several reasons. First off, it’s not necessary for performance. The JVM already optimizes things at runtime, so there’s no point in adding redundant complexity to the compiler. However, another major reason noone has mentioned here is debugging. Making the bytecode as close to the original source as possible makes it a lot easier … Read more

How to emit and execute Java bytecode at runtime?

Here is a working “hello world” made with ObjectWeb ASM (a library which I recommend): package hello; import java.lang.reflect.Method; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; public class HelloWorldASM implements Opcodes { public static byte[] compile(String name) { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, “hello/HelloWorld”, null, “java/lang/Object”, null); cw.visitSource(“HelloWorld.java”, … Read more

Javassist. What is the main idea and where real use?

A common application is to generate proxy classes at runtime, i.e. to create a subclass at runtime that intercepts all method invocations. Examples: Hibernate uses Proxies to intercept method invocations on entities to implement lazy loading, i.e. fetching the object from the database when it is first accessed. The Spring Framework uses Proxies to implement … Read more

In Java Lambda’s why is getClass() called on a captured variable

Yes, calling getClass() has become a canonical “test for null” idiom, as getClass() is expected to be a cheap intrinsic operation and, I suppose, HotSpot might be capable of detecting this pattern and reduce the operation to an intrinsic null-check operation, if the result of getClass() is not used. Another example is creating an inner … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)