Java Code Examples for org.springframework.asm.MethodVisitor

The following examples show how to use org.springframework.asm.MethodVisitor. These examples are extracted from open source projects. 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 Project: spring4-understanding   Source File: SpelNodeImpl.java    License: 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 2
Source Project: spring-analysis-note   Source File: CodeFlow.java    License: 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 3
Source Project: spring4-understanding   Source File: OpAnd.java    License: 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 4
Source Project: java-technology-stack   Source File: SpelNodeImpl.java    License: 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 5
Source Project: spring4-understanding   Source File: CodeFlow.java    License: 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 6
Source Project: lams   Source File: CodeFlow.java    License: 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 7
Source Project: spring-analysis-note   Source File: OpAnd.java    License: 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 8
Source Project: spring-analysis-note   Source File: OpPlus.java    License: 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 9
Source Project: lams   Source File: Elvis.java    License: 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 10
Source Project: spring-analysis-note   Source File: OperatorInstanceof.java    License: 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 11
Source Project: java-technology-stack   Source File: OpPlus.java    License: 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 12
Source Project: java-technology-stack   Source File: CodeFlow.java    License: 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 13
Source Project: java-technology-stack   Source File: ConstructorReference.java    License: 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 Project: spring4-understanding   Source File: OpMultiply.java    License: 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 15
@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 16
@Override
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(clazz, memberMap, name, desc, isStatic(access));
	}
	return null;
}
 
Example 17
Source Project: spring4-understanding   Source File: CodeFlow.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Determine the appropriate boxing instruction for a specific type (if it needs
 * boxing) and insert the instruction into the supplied visitor.
 * @param mv the target visitor for the new instructions
 * @param ch the descriptor of the type that might need boxing
 */
public static void insertBoxIfNecessary(MethodVisitor mv, char ch) {
	switch (ch) {
		case 'Z':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
			break;
		case 'B':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;", false);
			break;
		case 'C':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;", false);
			break;
		case 'D':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;", false);
			break;
		case 'F':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;", false);
			break;
		case 'I':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
			break;
		case 'J':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
			break;
		case 'S':
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;", false);
			break;
		case 'L':
		case 'V':
		case '[':
			// no box needed
			break;
		default:
			throw new IllegalArgumentException("Boxing should not be attempted for descriptor '" + ch + "'");
	}
}
 
Example 18
@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 19
Source Project: java-technology-stack   Source File: BooleanLiteral.java    License: MIT License 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 20
Source Project: lams   Source File: OpMultiply.java    License: 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(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 21
Source Project: spring4-understanding   Source File: InlineList.java    License: Apache License 2.0 5 votes vote down vote up
void generateClinitCode(String clazzname, String constantFieldName, MethodVisitor mv, CodeFlow codeflow, boolean nested) {
	mv.visitTypeInsn(NEW, "java/util/ArrayList");
	mv.visitInsn(DUP);
	mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
	if (!nested) {
		mv.visitFieldInsn(PUTSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
	}
	int childcount = getChildCount();		
	for (int c=0; c < childcount; c++) {
		if (!nested) {
			mv.visitFieldInsn(GETSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
		}
		else {
			mv.visitInsn(DUP);
		}
		// The children might be further lists if they are not constants. In this
		// situation do not call back into generateCode() because it will register another clinit adder.
		// Instead, directly build the list here:
		if (children[c] instanceof InlineList) {
			((InlineList)children[c]).generateClinitCode(clazzname, constantFieldName, mv, codeflow, true);
		}
		else {
			children[c].generateCode(mv, codeflow);
			if (CodeFlow.isPrimitive(codeflow.lastDescriptor())) {
				CodeFlow.insertBoxIfNecessary(mv, codeflow.lastDescriptor().charAt(0));
			}
		}
		mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
		mv.visitInsn(POP);
	}
}
 
Example 22
Source Project: spring-analysis-note   Source File: CodeFlow.java    License: MIT License 5 votes vote down vote up
/**
 * For use in mathematical operators, handles converting from a (possibly boxed)
 * number on the stack to a primitive numeric type.
 * <p>For example, from a Integer to a double, just need to call 'Number.doubleValue()'
 * but from an int to a double, need to use the bytecode 'i2d'.
 * @param mv the method visitor when instructions should be appended
 * @param stackDescriptor a descriptor of the operand on the stack
 * @param targetDescriptor a primitive type descriptor
 */
public static void insertNumericUnboxOrPrimitiveTypeCoercion(
		MethodVisitor mv, @Nullable String stackDescriptor, char targetDescriptor) {

	if (!CodeFlow.isPrimitive(stackDescriptor)) {
		CodeFlow.insertUnboxNumberInsns(mv, targetDescriptor, stackDescriptor);
	}
	else {
		CodeFlow.insertAnyNecessaryTypeConversionBytecodes(mv, targetDescriptor, stackDescriptor);
	}
}
 
Example 23
Source Project: spring4-understanding   Source File: IntLiteral.java    License: Apache License 2.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 24
Source Project: spring-analysis-note   Source File: InlineList.java    License: MIT License 5 votes vote down vote up
void generateClinitCode(String clazzname, String constantFieldName, MethodVisitor mv, CodeFlow codeflow, boolean nested) {
	mv.visitTypeInsn(NEW, "java/util/ArrayList");
	mv.visitInsn(DUP);
	mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
	if (!nested) {
		mv.visitFieldInsn(PUTSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
	}
	int childCount = getChildCount();
	for (int c = 0; c < childCount; c++) {
		if (!nested) {
			mv.visitFieldInsn(GETSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
		}
		else {
			mv.visitInsn(DUP);
		}
		// The children might be further lists if they are not constants. In this
		// situation do not call back into generateCode() because it will register another clinit adder.
		// Instead, directly build the list here:
		if (this.children[c] instanceof InlineList) {
			((InlineList)this.children[c]).generateClinitCode(clazzname, constantFieldName, mv, codeflow, true);
		}
		else {
			this.children[c].generateCode(mv, codeflow);
			String lastDesc = codeflow.lastDescriptor();
			if (CodeFlow.isPrimitive(lastDesc)) {
				CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
			}
		}
		mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
		mv.visitInsn(POP);
	}
}
 
Example 25
Source Project: spring4-understanding   Source File: CodeFlow.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * For numbers, use the appropriate method on the number to convert it to the primitive type requested.
 * @param mv the method visitor into which instructions should be inserted
 * @param targetDescriptor the primitive type desired as output
 * @param stackDescriptor the descriptor of the type on top of the stack
 */
public static void insertUnboxNumberInsns(MethodVisitor mv, char targetDescriptor, String stackDescriptor) {
	switch (targetDescriptor) {
		case 'D':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "doubleValue", "()D", false);
			break;
		case 'F':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "floatValue", "()F", false);
			break;
		case 'J':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "longValue", "()J", false);
			break;
		case 'I':
			if (stackDescriptor.equals("Ljava/lang/Object")) {
				mv.visitTypeInsn(CHECKCAST, "java/lang/Number");
			}
			mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Number", "intValue", "()I", false);
			break;
		// does not handle Z, B, C, S
		default:
			throw new IllegalArgumentException("Unboxing should not be attempted for descriptor '" + targetDescriptor + "'");
	}
}
 
Example 26
Source Project: lams   Source File: TypeReference.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	// TODO Future optimization - if followed by a static method call, skip generating code here
	if (this.type.isPrimitive()) {
		if (this.type == Integer.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Integer", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Boolean.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Boolean", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Byte.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Byte", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Short.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Short", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Double.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Double", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Character.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Character", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Float.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Float", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Long.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Long", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Boolean.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Boolean", "TYPE", "Ljava/lang/Class;");
        }
	}
	else {
		mv.visitLdcInsn(Type.getType(this.type));
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 27
@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 Project: lams   Source File: IntLiteral.java    License: 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 29
Source Project: java-technology-stack   Source File: CompoundExpression.java    License: 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 30
Source Project: lams   Source File: CodeFlow.java    License: 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);
	}
}