com.fitbur.mockito.bytebuddy.implementation.bytecode.constant.LongConstant Maven / Gradle / Ivy
package com.fitbur.mockito.bytebuddy.implementation.bytecode.constant;
import com.fitbur.mockito.bytebuddy.implementation.Implementation;
import com.fitbur.mockito.bytebuddy.implementation.bytecode.StackManipulation;
import com.fitbur.mockito.bytebuddy.implementation.bytecode.StackSize;
import com.fitbur.mockito.bytebuddy.jar.asm.MethodVisitor;
import com.fitbur.mockito.bytebuddy.jar.asm.Opcodes;
/**
* This class is responsible for loading any {@code long} constant onto the operand stack.
*/
public enum LongConstant implements StackManipulation {
/**
* A {@code long} constant of value {@code 0L}.
*/
ZERO(Opcodes.LCONST_0),
/**
* A {@code long} constant of value {@code 1L}.
*/
ONE(Opcodes.LCONST_1);
/**
* The size impact of loading a {@code double} constant onto the operand stack.
*/
private static final Size SIZE = StackSize.DOUBLE.toIncreasingSize();
/**
* The shortcut opcode for loading a {@code long} constant.
*/
private final int opcode;
/**
* Creates a new shortcut operation for loading a common {@code long} onto the operand stack.
*
* @param opcode The shortcut opcode for loading a {@code long} constant.
*/
LongConstant(int opcode) {
this.opcode = opcode;
}
/**
* Creates a stack manipulation for loading a {@code long} value onto the operand stack.
*
* This is achieved either by invoking a specific opcode, if any, or by creating a constant pool entry.
*
* @param value The {@code long} value to load onto the stack.
* @return A stack manipulation for loading the given {@code long} value.
*/
public static StackManipulation forValue(long value) {
if (value == 0L) {
return ZERO;
} else if (value == 1L) {
return ONE;
} else {
return new ConstantPool(value);
}
}
@Override
public boolean isValid() {
return true;
}
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
methodVisitor.visitInsn(opcode);
return SIZE;
}
@Override
public String toString() {
return "LongConstant." + name();
}
/**
* A stack manipulation for loading a {@code long} value from a class's constant pool onto the operand stack.
*/
protected static class ConstantPool implements StackManipulation {
/**
* The {@code long} value to be loaded onto the operand stack.
*/
private final long value;
/**
* Creates a new constant pool load operation.
*
* @param value The {@code long} value to be loaded onto the operand stack.
*/
protected ConstantPool(long value) {
this.value = value;
}
@Override
public boolean isValid() {
return true;
}
@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
methodVisitor.visitLdcInsn(value);
return SIZE;
}
@Override
public boolean equals(Object other) {
return this == other || !(other == null || getClass() != other.getClass())
&& value == ((ConstantPool) other).value;
}
@Override
public int hashCode() {
return (int) (value ^ (value >>> 32));
}
@Override
public String toString() {
return "LongConstant.ConstantPool{value=" + value + '}';
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy