org.springframework.asm.MethodVisitor Java Examples

The following examples show how to use org.springframework.asm.MethodVisitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpPlus.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Walk through a possible tree of nodes that combine strings and append
 * them all to the same (on stack) StringBuilder.
 */
private void walk(MethodVisitor mv, CodeFlow cf, @Nullable SpelNodeImpl operand) {
	if (operand instanceof OpPlus) {
		OpPlus plus = (OpPlus)operand;
		walk(mv, cf, plus.getLeftOperand());
		walk(mv, cf, plus.getRightOperand());
	}
	else if (operand != null) {
		cf.enterCompilationScope();
		operand.generateCode(mv,cf);
		if (!"Ljava/lang/String".equals(cf.lastDescriptor())) {
			mv.visitTypeInsn(CHECKCAST, "java/lang/String");
		}
		cf.exitCompilationScope();
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
	}
}
 
Example #2
Source File: OpPlus.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Walk through a possible tree of nodes that combine strings and append
 * them all to the same (on stack) StringBuilder.
 */
private void walk(MethodVisitor mv, CodeFlow cf, @Nullable SpelNodeImpl operand) {
	if (operand instanceof OpPlus) {
		OpPlus plus = (OpPlus)operand;
		walk(mv, cf, plus.getLeftOperand());
		walk(mv, cf, plus.getRightOperand());
	}
	else if (operand != null) {
		cf.enterCompilationScope();
		operand.generateCode(mv,cf);
		if (!"Ljava/lang/String".equals(cf.lastDescriptor())) {
			mv.visitTypeInsn(CHECKCAST, "java/lang/String");
		}
		cf.exitCompilationScope();
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
	}
}
 
Example #3
Source File: CodeFlow.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Insert the appropriate CHECKCAST instruction for the supplied descriptor.
 * @param mv the target visitor into which the instruction should be inserted
 * @param descriptor the descriptor of the type to cast to
 */
public static void insertCheckCast(MethodVisitor mv, @Nullable String descriptor) {
	if (descriptor != null && descriptor.length() != 1) {
		if (descriptor.charAt(0) == '[') {
			if (isPrimitiveArray(descriptor)) {
				mv.visitTypeInsn(CHECKCAST, descriptor);
			}
			else {
				mv.visitTypeInsn(CHECKCAST, descriptor + ";");
			}
		}
		else {
			if (!descriptor.equals("Ljava/lang/Object")) {
				// This is chopping off the 'L' to leave us with "java/lang/String"
				mv.visitTypeInsn(CHECKCAST, descriptor.substring(1));
			}
		}
	}
}
 
Example #4
Source File: SpelNodeImpl.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Ask an argument to generate its bytecode and then follow it up
 * with any boxing/unboxing/checkcasting to ensure it matches the expected parameter descriptor.
 */
protected static void generateCodeForArgument(MethodVisitor mv, CodeFlow cf, SpelNodeImpl argument, String paramDesc) {
	cf.enterCompilationScope();
	argument.generateCode(mv, cf);
	boolean primitiveOnStack = CodeFlow.isPrimitive(cf.lastDescriptor());
	// Check if need to box it for the method reference?
	if (primitiveOnStack && paramDesc.charAt(0) == 'L') {
		CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor().charAt(0));
	}
	else if (paramDesc.length() == 1 && !primitiveOnStack) {
		CodeFlow.insertUnboxInsns(mv, paramDesc.charAt(0), cf.lastDescriptor());
	}
	else if (!cf.lastDescriptor().equals(paramDesc)) {
		// This would be unnecessary in the case of subtyping (e.g. method takes Number but Integer passed in)
		CodeFlow.insertCheckCast(mv, paramDesc);
	}
	cf.exitCompilationScope();
}
 
Example #5
Source File: CodeFlow.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Called after the main expression evaluation method has been generated, this
 * method will callback any registered FieldAdders or ClinitAdders to add any
 * extra information to the class representing the compiled expression.
 */
public void finish() {
	if (this.fieldAdders != null) {
		for (FieldAdder fieldAdder : this.fieldAdders) {
			fieldAdder.generateField(this.classWriter, this);
		}
	}
	if (this.clinitAdders != null) {
		MethodVisitor mv = this.classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
		mv.visitCode();
		this.nextFreeVariableId = 0;  // to 0 because there is no 'this' in a clinit
		for (ClinitAdder clinitAdder : this.clinitAdders) {
			clinitAdder.generateCode(mv, this);
		}
		mv.visitInsn(RETURN);
		mv.visitMaxs(0,0);  // not supplied due to COMPUTE_MAXS
		mv.visitEnd();
	}
}
 
Example #6
Source File: OpAnd.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// Pseudo: if (!leftOperandValue) { result=false; } else { result=rightOperandValue; }
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	cf.enterCompilationScope();
	getLeftOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitJumpInsn(IFNE, elseTarget);
	mv.visitLdcInsn(0); // FALSE
	mv.visitJumpInsn(GOTO,endOfIf);
	mv.visitLabel(elseTarget);
	cf.enterCompilationScope();
	getRightOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #7
Source File: SpelNodeImpl.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Ask an argument to generate its bytecode and then follow it up
 * with any boxing/unboxing/checkcasting to ensure it matches the expected parameter descriptor.
 */
protected static void generateCodeForArgument(MethodVisitor mv, CodeFlow cf, SpelNodeImpl argument, String paramDesc) {
	cf.enterCompilationScope();
	argument.generateCode(mv, cf);
	String lastDesc = cf.lastDescriptor();
	Assert.state(lastDesc != null, "No last descriptor");
	boolean primitiveOnStack = CodeFlow.isPrimitive(lastDesc);
	// Check if need to box it for the method reference?
	if (primitiveOnStack && paramDesc.charAt(0) == 'L') {
		CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
	}
	else if (paramDesc.length() == 1 && !primitiveOnStack) {
		CodeFlow.insertUnboxInsns(mv, paramDesc.charAt(0), lastDesc);
	}
	else if (!paramDesc.equals(lastDesc)) {
		// This would be unnecessary in the case of subtyping (e.g. method takes Number but Integer passed in)
		CodeFlow.insertCheckCast(mv, paramDesc);
	}
	cf.exitCompilationScope();
}
 
Example #8
Source File: CodeFlow.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Produce appropriate bytecode to store a stack item in an array. The
 * instruction to use varies depending on whether the type
 * is a primitive or reference type.
 * @param mv where to insert the bytecode
 * @param arrayElementType the type of the array elements
 */
public static void insertArrayStore(MethodVisitor mv, String arrayElementType) {
	if (arrayElementType.length()==1) {
		switch (arrayElementType.charAt(0)) {
			case 'I': mv.visitInsn(IASTORE); break;
			case 'J': mv.visitInsn(LASTORE); break;
			case 'F': mv.visitInsn(FASTORE); break;
			case 'D': mv.visitInsn(DASTORE); break;
			case 'B': mv.visitInsn(BASTORE); break;
			case 'C': mv.visitInsn(CASTORE); break;
			case 'S': mv.visitInsn(SASTORE); break;
			case 'Z': mv.visitInsn(BASTORE); break;
			default:
				throw new IllegalArgumentException("Unexpected arraytype "+arrayElementType.charAt(0));
		}
	}
	else {
		mv.visitInsn(AASTORE);
	}
}
 
Example #9
Source File: OperatorInstanceof.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	getLeftOperand().generateCode(mv, cf);
	CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor());
	Assert.state(this.type != null, "No type available");
	if (this.type.isPrimitive()) {
		// always false - but left operand code always driven
		// in case it had side effects
		mv.visitInsn(POP);
		mv.visitInsn(ICONST_0); // value of false
	}
	else {
		mv.visitTypeInsn(INSTANCEOF, Type.getInternalName(this.type));
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #10
Source File: CodeFlow.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Called after the main expression evaluation method has been generated, this
 * method will callback any registered FieldAdders or ClinitAdders to add any
 * extra information to the class representing the compiled expression.
 */
public void finish() {
	if (this.fieldAdders != null) {
		for (FieldAdder fieldAdder : this.fieldAdders) {
			fieldAdder.generateField(this.classWriter, this);
		}
	}
	if (this.clinitAdders != null) {
		MethodVisitor mv = this.classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
		mv.visitCode();
		this.nextFreeVariableId = 0;  // to 0 because there is no 'this' in a clinit
		for (ClinitAdder clinitAdder : this.clinitAdders) {
			clinitAdder.generateCode(mv, this);
		}
		mv.visitInsn(RETURN);
		mv.visitMaxs(0,0);  // not supplied due to COMPUTE_MAXS
		mv.visitEnd();
	}
}
 
Example #11
Source File: Elvis.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// exit type descriptor can be null if both components are literal expressions
	computeExitTypeDescriptor();
	this.children[0].generateCode(mv, cf);
	CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor().charAt(0));
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	mv.visitInsn(DUP);
	mv.visitJumpInsn(IFNULL, elseTarget);
	// Also check if empty string, as per the code in the interpreted version
	mv.visitInsn(DUP);
	mv.visitLdcInsn("");
	mv.visitInsn(SWAP);
	mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z",false);
	mv.visitJumpInsn(IFEQ, endOfIf);  // if not empty, drop through to elseTarget
	mv.visitLabel(elseTarget);
	mv.visitInsn(POP);
	this.children[1].generateCode(mv, cf);
	if (!CodeFlow.isPrimitive(this.exitTypeDescriptor)) {
		CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor().charAt(0));
	}
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #12
Source File: OpAnd.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// Pseudo: if (!leftOperandValue) { result=false; } else { result=rightOperandValue; }
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	cf.enterCompilationScope();
	getLeftOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitJumpInsn(IFNE, elseTarget);
	mv.visitLdcInsn(0); // FALSE
	mv.visitJumpInsn(GOTO,endOfIf);
	mv.visitLabel(elseTarget);
	cf.enterCompilationScope();
	getRightOperand().generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	cf.exitCompilationScope();
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #13
Source File: ConstructorReference.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	ReflectiveConstructorExecutor executor = ((ReflectiveConstructorExecutor) this.cachedExecutor);
	Assert.state(executor != null, "No cached executor");

	Constructor<?> constructor = executor.getConstructor();
	String classDesc = constructor.getDeclaringClass().getName().replace('.', '/');
	mv.visitTypeInsn(NEW, classDesc);
	mv.visitInsn(DUP);

	// children[0] is the type of the constructor, don't want to include that in argument processing
	SpelNodeImpl[] arguments = new SpelNodeImpl[this.children.length - 1];
	System.arraycopy(this.children, 1, arguments, 0, this.children.length - 1);
	generateCodeForArguments(mv, cf, constructor, arguments);
	mv.visitMethodInsn(INVOKESPECIAL, classDesc, "<init>", CodeFlow.createSignatureDescriptor(constructor), false);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #14
Source File: OpEQ.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	cf.loadEvaluationContext(mv);
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	String rightDesc = getRightOperand().exitTypeDescriptor;
	boolean leftPrim = CodeFlow.isPrimitive(leftDesc);
	boolean rightPrim = CodeFlow.isPrimitive(rightDesc);

	cf.enterCompilationScope();
	getLeftOperand().generateCode(mv, cf);
	cf.exitCompilationScope();
	if (leftPrim) {
		CodeFlow.insertBoxIfNecessary(mv, leftDesc.charAt(0));
	}
	cf.enterCompilationScope();
	getRightOperand().generateCode(mv, cf);
	cf.exitCompilationScope();
	if (rightPrim) {
		CodeFlow.insertBoxIfNecessary(mv, rightDesc.charAt(0));
	}

	String operatorClassName = Operator.class.getName().replace('.', '/');
	String evaluationContextClassName = EvaluationContext.class.getName().replace('.', '/');
	mv.visitMethodInsn(INVOKESTATIC, operatorClassName, "equalityCheck",
			"(L" + evaluationContextClassName + ";Ljava/lang/Object;Ljava/lang/Object;)Z", false);
	cf.pushDescriptor("Z");
}
 
Example #15
Source File: CompoundExpression.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	for (SpelNodeImpl child : this.children) {
		child.generateCode(mv, cf);
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #16
Source File: ReflectivePropertyAccessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
	boolean isStatic = Modifier.isStatic(this.member.getModifiers());
	String descriptor = cf.lastDescriptor();
	String classDesc = this.member.getDeclaringClass().getName().replace('.', '/');

	if (!isStatic) {
		if (descriptor == null) {
			cf.loadTarget(mv);
		}
		if (descriptor == null || !classDesc.equals(descriptor.substring(1))) {
			mv.visitTypeInsn(CHECKCAST, classDesc);
		}
	}
	else {
		if (descriptor != null) {
			// A static field/method call will not consume what is on the stack,
			// it needs to be popped off.
			mv.visitInsn(POP);
		}
	}

	if (this.member instanceof Method) {
		mv.visitMethodInsn((isStatic ? INVOKESTATIC : INVOKEVIRTUAL), classDesc, this.member.getName(),
				CodeFlow.createSignatureDescriptor((Method) this.member), false);
	}
	else {
		mv.visitFieldInsn((isStatic ? GETSTATIC : GETFIELD), classDesc, this.member.getName(),
				CodeFlow.toJvmDescriptor(((Field) this.member).getType()));
	}
}
 
Example #17
Source File: MapAccessor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
	String descriptor = cf.lastDescriptor();
	if (descriptor == null || !descriptor.equals("Ljava/util/Map")) {
		if (descriptor == null) {
			cf.loadTarget(mv);
		}
		CodeFlow.insertCheckCast(mv, "Ljava/util/Map");
	}
	mv.visitLdcInsn(propertyName);
	mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "get","(Ljava/lang/Object;)Ljava/lang/Object;",true);
}
 
Example #18
Source File: LocalVariableTableParameterNameDiscoverer.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
	// exclude synthetic + bridged && static class initialization
	if (!isSyntheticOrBridged(access) && !STATIC_CLASS_INIT.equals(name)) {
		return new LocalVariableTableVisitor(this.clazz, this.memberMap, name, desc, isStatic(access));
	}
	return null;
}
 
Example #19
Source File: Ternary.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// May reach here without it computed if all elements are literals
	computeExitTypeDescriptor();
	cf.enterCompilationScope();
	this.children[0].generateCode(mv, cf);
	String lastDesc = cf.lastDescriptor();
	Assert.state(lastDesc != null, "No last descriptor");
	if (!CodeFlow.isPrimitive(lastDesc)) {
		CodeFlow.insertUnboxInsns(mv, 'Z', lastDesc);
	}
	cf.exitCompilationScope();
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	mv.visitJumpInsn(IFEQ, elseTarget);
	cf.enterCompilationScope();
	this.children[1].generateCode(mv, cf);
	if (!CodeFlow.isPrimitive(this.exitTypeDescriptor)) {
		lastDesc = cf.lastDescriptor();
		Assert.state(lastDesc != null, "No last descriptor");
		CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
	}
	cf.exitCompilationScope();
	mv.visitJumpInsn(GOTO, endOfIf);
	mv.visitLabel(elseTarget);
	cf.enterCompilationScope();
	this.children[2].generateCode(mv, cf);
	if (!CodeFlow.isPrimitive(this.exitTypeDescriptor)) {
		lastDesc = cf.lastDescriptor();
		Assert.state(lastDesc != null, "No last descriptor");
		CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
	}
	cf.exitCompilationScope();
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #20
Source File: VariableReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.name.equals(ROOT)) {
		mv.visitVarInsn(ALOAD,1);
	}
	else {
		mv.visitVarInsn(ALOAD, 2);
		mv.visitLdcInsn(name);
		mv.visitMethodInsn(INVOKEINTERFACE, "org/springframework/expression/EvaluationContext", "lookupVariable", "(Ljava/lang/String;)Ljava/lang/Object;",true);
	}
	CodeFlow.insertCheckCast(mv,this.exitTypeDescriptor);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #21
Source File: OpMultiply.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	getLeftOperand().generateCode(mv, cf);
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, this.exitTypeDescriptor.charAt(0));
	if (this.children.length > 1) {
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		String rightDesc = getRightOperand().exitTypeDescriptor;
		cf.exitCompilationScope();
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, this.exitTypeDescriptor.charAt(0));
		switch (this.exitTypeDescriptor.charAt(0)) {
			case 'I':
				mv.visitInsn(IMUL);
				break;
			case 'J':
				mv.visitInsn(LMUL);
				break;
			case 'F': 
				mv.visitInsn(FMUL);
				break;
			case 'D':
				mv.visitInsn(DMUL);
				break;				
			default:
				throw new IllegalStateException(
						"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #22
Source File: OperatorNot.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	this.children[0].generateCode(mv, cf);
	cf.unboxBooleanIfNecessary(mv);
	Label elseTarget = new Label();
	Label endOfIf = new Label();
	mv.visitJumpInsn(IFNE,elseTarget);
	mv.visitInsn(ICONST_1); // TRUE
	mv.visitJumpInsn(GOTO,endOfIf);
	mv.visitLabel(elseTarget);
	mv.visitInsn(ICONST_0); // FALSE
	mv.visitLabel(endOfIf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #23
Source File: ReflectivePropertyAccessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(String propertyName, MethodVisitor mv, CodeFlow cf) {
	boolean isStatic = Modifier.isStatic(this.member.getModifiers());
	String descriptor = cf.lastDescriptor();
	String classDesc = this.member.getDeclaringClass().getName().replace('.', '/');

	if (!isStatic) {
		if (descriptor == null) {
			cf.loadTarget(mv);
		}
		if (descriptor == null || !classDesc.equals(descriptor.substring(1))) {
			mv.visitTypeInsn(CHECKCAST, classDesc);
		}
	}
	else {
		if (descriptor != null) {
			// A static field/method call will not consume what is on the stack,
			// it needs to be popped off.
			mv.visitInsn(POP);
		}
	}

	if (this.member instanceof Method) {
		mv.visitMethodInsn((isStatic ? INVOKESTATIC : INVOKEVIRTUAL), classDesc, this.member.getName(),
				CodeFlow.createSignatureDescriptor((Method) this.member), false);
	}
	else {
		mv.visitFieldInsn((isStatic ? GETSTATIC : GETFIELD), classDesc, this.member.getName(),
				CodeFlow.toJvmDescriptor(((Field) this.member).getType()));
	}
}
 
Example #24
Source File: CodeFlow.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create the optimal instruction for loading a number on the stack.
 * @param mv where to insert the bytecode
 * @param value the value to be loaded
 */
public static void insertOptimalLoad(MethodVisitor mv, int value) {
	if (value < 6) {
		mv.visitInsn(ICONST_0+value);
	}
	else if (value < Byte.MAX_VALUE) {
		mv.visitIntInsn(BIPUSH, value);
	}
	else if (value < Short.MAX_VALUE) {
		mv.visitIntInsn(SIPUSH, value);
	}
	else {
		mv.visitLdcInsn(value);
	}
}
 
Example #25
Source File: CompoundExpression.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	for (int i = 0; i < this.children.length;i++) {
		this.children[i].generateCode(mv, cf);
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #26
Source File: IntLiteral.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	int intValue = (Integer) this.value.getValue();
	if (intValue == -1) {
		// Not sure we can get here because -1 is OpMinus
		mv.visitInsn(ICONST_M1);
	}
	else if (intValue >= 0 && intValue < 6) {
		mv.visitInsn(ICONST_0 + intValue);
	}
	else {
		mv.visitLdcInsn(intValue);
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #27
Source File: PropertyOrFieldReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	PropertyAccessor accessorToUse = this.cachedReadAccessor;
	if (!(accessorToUse instanceof CompilablePropertyAccessor)) {
		throw new IllegalStateException("Property accessor is not compilable: " + accessorToUse);
	}
	((CompilablePropertyAccessor) accessorToUse).generateCode(this.name, mv, cf);
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #28
Source File: OpDivide.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	getLeftOperand().generateCode(mv, cf);
	String leftDesc = getLeftOperand().exitTypeDescriptor;
	CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, leftDesc, this.exitTypeDescriptor.charAt(0));
	if (this.children.length > 1) {
		cf.enterCompilationScope();
		getRightOperand().generateCode(mv, cf);
		String rightDesc = getRightOperand().exitTypeDescriptor;
		cf.exitCompilationScope();
		CodeFlow.insertNumericUnboxOrPrimitiveTypeCoercion(mv, rightDesc, this.exitTypeDescriptor.charAt(0));
		switch (this.exitTypeDescriptor.charAt(0)) {
			case 'I':
				mv.visitInsn(IDIV);
				break;
			case 'J':
				mv.visitInsn(LDIV);
				break;
			case 'F': 
				mv.visitInsn(FDIV);
				break;
			case 'D':
				mv.visitInsn(DDIV);
				break;				
			default:
				throw new IllegalStateException(
						"Unrecognized exit type descriptor: '" + this.exitTypeDescriptor + "'");
		}
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #29
Source File: BooleanLiteral.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.value == BooleanTypedValue.TRUE) {
		mv.visitLdcInsn(1);		
	}
	else {
		mv.visitLdcInsn(0);
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #30
Source File: CodeFlow.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create the optimal instruction for loading a number on the stack.
 * @param mv where to insert the bytecode
 * @param value the value to be loaded
 */
public static void insertOptimalLoad(MethodVisitor mv, int value) {
	if (value < 6) {
		mv.visitInsn(ICONST_0+value);
	}
	else if (value < Byte.MAX_VALUE) {
		mv.visitIntInsn(BIPUSH, value);
	}
	else if (value < Short.MAX_VALUE) {
		mv.visitIntInsn(SIPUSH, value);
	}
	else {
		mv.visitLdcInsn(value);
	}
}