Java Code Examples for org.objectweb.asm.MethodVisitor#visitTableSwitchInsn()

The following examples show how to use org.objectweb.asm.MethodVisitor#visitTableSwitchInsn() . 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
@Override
public void toCode(MethodVisitor visitor, BytecodeFrontend assembler) {
	if (needsSort()) {
		sort();
	}

	int[] cases = new int[targets.size()];
	Label[] labels = new Label[targets.size()];
	int j = 0;
	for (Entry<Integer, BasicBlock> e : targets.entrySet()) {
		cases[j] = e.getKey();
		labels[j++] = assembler.getLabel(e.getValue());
	}

	expression.toCode(visitor, assembler);
	int[] cast = TypeUtils.getPrimitiveCastOpcodes(expression.getType(), Type.INT_TYPE); // widen
	for (int i = 0; i < cast.length; i++) {
		visitor.visitInsn(cast[i]);
	}
	boolean fitsIntoTable = fitsIntoTableSwitch();
	if (fitsIntoTable) {
		visitor.visitTableSwitchInsn(cases[0], cases[cases.length - 1], assembler.getLabel(defaultTarget), labels);
	} else {
		visitor.visitLookupSwitchInsn(assembler.getLabel(defaultTarget), cases, labels);
	}
}
 
Example 2
@Override
public void accept(final MethodVisitor methodVisitor) {
	Label[] labelsArray = new Label[this.labels.size()];
	for (int i = 0, n = labelsArray.length; i < n; ++i) {
		labelsArray[i] = this.labels.get(i).getLabel();
	}
	methodVisitor.visitTableSwitchInsn(min, max, dflt.getLabel(), labelsArray);
	acceptAnnotations(methodVisitor);
}
 
Example 3
Source Project: Concurnas   File: TableSwitchInsnNode.java    License: MIT License 5 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  Label[] labelsArray = new Label[this.labels.size()];
  for (int i = 0, n = labelsArray.length; i < n; ++i) {
    labelsArray[i] = this.labels.get(i).getLabel();
  }
  methodVisitor.visitTableSwitchInsn(min, max, dflt.getLabel(), labelsArray);
  acceptAnnotations(methodVisitor);
}
 
Example 4
@Override
public void accept(final MethodVisitor mv) {
  Label[] labels = new Label[this.labels.size()];
  for (int i = 0; i < labels.length; ++i) {
    labels[i] = this.labels.get(i).getLabel();
  }
  mv.visitTableSwitchInsn(min, max, dflt.getLabel(), labels);
  acceptAnnotations(mv);
}
 
Example 5
Source Project: JReFrameworker   File: TableSwitchInsnNode.java    License: MIT License 5 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  Label[] labelsArray = new Label[this.labels.size()];
  for (int i = 0, n = labelsArray.length; i < n; ++i) {
    labelsArray[i] = this.labels.get(i).getLabel();
  }
  methodVisitor.visitTableSwitchInsn(min, max, dflt.getLabel(), labelsArray);
  acceptAnnotations(methodVisitor);
}
 
Example 6
Source Project: JReFrameworker   File: TableSwitchInsnNode.java    License: MIT License 5 votes vote down vote up
@Override
public void accept(final MethodVisitor methodVisitor) {
  Label[] labelsArray = new Label[this.labels.size()];
  for (int i = 0, n = labelsArray.length; i < n; ++i) {
    labelsArray[i] = this.labels.get(i).getLabel();
  }
  methodVisitor.visitTableSwitchInsn(min, max, dflt.getLabel(), labelsArray);
  acceptAnnotations(methodVisitor);
}
 
Example 7
@Override
public void accept(MethodVisitor visitor)
{
	visitor.visitTableSwitchInsn(low, high, defi.getLabel(),
		branchi.stream().map(l -> l.getLabel()).toArray(org.objectweb.asm.Label[]::new));
}
 
Example 8
Source Project: jadira   File: AsmClassAccess.java    License: Apache License 2.0 4 votes vote down vote up
private static void enhanceForGetValueObject(ClassVisitor cw, String accessClassNm, String clazzNm, Field[] fields) {

		MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getValue", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;", null, null);

		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0);
		mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
		mv.visitVarInsn(ALOAD, 2);
		mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch", "([Ljava/lang/Object;Ljava/lang/Object;)I");
		mv.visitVarInsn(ISTORE, 3);
		mv.visitVarInsn(ILOAD, 3);

		final int maxStack;

		if (fields.length > 0) {
			maxStack = 5;
			Label[] labels = constructLabels(fields);

			Label defaultLabel = new Label();
			mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

			for (int i = 0, n = labels.length; i < n; i++) {
				Field field = fields[i];
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, clazzNm);
				mv.visitFieldInsn(GETFIELD, clazzNm, field.getName(), Type.getDescriptor(field.getType()));

				Type fieldType = Type.getType(field.getType());
				switch (fieldType.getSort()) {
				case Type.BOOLEAN:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
					break;
				case Type.BYTE:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
					break;
				case Type.CHAR:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
					break;
				case Type.SHORT:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
					break;
				case Type.INT:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
					break;
				case Type.FLOAT:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
					break;
				case Type.LONG:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
					break;
				case Type.DOUBLE:
					mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
					break;
				}

				mv.visitInsn(ARETURN);
			}

			mv.visitLabel(defaultLabel);
			mv.visitFrame(F_SAME, 0, null, 0, null);
		} else {
			maxStack = 6;
		}
		enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;", ALOAD, 2);
		mv.visitMaxs(maxStack, 4);
		mv.visitEnd();
	}
 
Example 9
Source Project: jadira   File: AsmClassAccess.java    License: Apache License 2.0 4 votes vote down vote up
private static void enhanceForPutValueObject(ClassVisitor cw, String accessClassNm, String clazzNm, Field[] fields) {

		int maxStack = 6;
		MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "putValue", "(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)V", null, null);

		mv.visitCode();

		mv.visitVarInsn(ALOAD, 0);
		mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
		mv.visitVarInsn(ALOAD, 2);
		mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch", "([Ljava/lang/Object;Ljava/lang/Object;)I");
		mv.visitVarInsn(ISTORE, 4);
		mv.visitVarInsn(ILOAD, 4);

		if (fields.length > 0) {
			maxStack = 5;
			Label[] labels = constructLabels(fields);

			Label defaultLabel = new Label();
			mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

			for (int i = 0, n = labels.length; i < n; i++) {
				Field field = fields[i];

				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, clazzNm);

				mv.visitVarInsn(ALOAD, 3);

				Type fieldType = Type.getType(field.getType());
				switch (fieldType.getSort()) {
				case Type.BOOLEAN:
					mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
					mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
					break;
				case Type.BYTE:
					mv.visitTypeInsn(CHECKCAST, "java/lang/Byte");
					mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
					break;
				case Type.CHAR:
					mv.visitTypeInsn(CHECKCAST, "java/lang/Character");
					mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
					break;
				case Type.SHORT:
					mv.visitTypeInsn(CHECKCAST, "java/lang/Short");
					mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
					break;
				case Type.INT:
					mv.visitTypeInsn(CHECKCAST, "java/lang/Integer");
					mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
					break;
				case Type.FLOAT:
					mv.visitTypeInsn(CHECKCAST, "java/lang/Float");
					mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
					break;
				case Type.LONG:
					mv.visitTypeInsn(CHECKCAST, "java/lang/Long");
					mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
					break;
				case Type.DOUBLE:
					mv.visitTypeInsn(CHECKCAST, "java/lang/Double");
					mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
					break;
				case Type.ARRAY:
					mv.visitTypeInsn(CHECKCAST, fieldType.getDescriptor());
					break;
				case Type.OBJECT:
					mv.visitTypeInsn(CHECKCAST, fieldType.getInternalName());
					break;
				}

				mv.visitFieldInsn(PUTFIELD, clazzNm, field.getName(), fieldType.getDescriptor());
				mv.visitInsn(RETURN);
			}

			mv.visitLabel(defaultLabel);
			mv.visitFrame(F_SAME, 0, null, 0, null);
		} else {
			maxStack = 6;
		}
		enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;", ALOAD, 2);
		mv.visitMaxs(maxStack, 5);
		mv.visitEnd();
	}
 
Example 10
Source Project: jadira   File: AsmClassAccess.java    License: Apache License 2.0 4 votes vote down vote up
private static void enhanceForGetValuePrimitive(ClassVisitor cw, String accessClassNm, String clazzNm, Field[] fields, Type type) {

		String methodName;
		final String typeNm = type.getDescriptor();
		final int instruction;

		switch (type.getSort()) {
		case Type.BOOLEAN:
			methodName = "getBooleanValue";
			instruction = IRETURN;
			break;
		case Type.BYTE:
			methodName = "getByteValue";
			instruction = IRETURN;
			break;
		case Type.CHAR:
			methodName = "getCharValue";
			instruction = IRETURN;
			break;
		case Type.SHORT:
			methodName = "getShortValue";
			instruction = IRETURN;
			break;
		case Type.INT:
			methodName = "getIntValue";
			instruction = IRETURN;
			break;
		case Type.FLOAT:
			methodName = "getFloatValue";
			instruction = FRETURN;
			break;
		case Type.LONG:
			methodName = "getLongValue";
			instruction = LRETURN;
			break;
		case Type.DOUBLE:
			methodName = "getDoubleValue";
			instruction = DRETURN;
			break;
		default:
			methodName = "getValue";
			instruction = ARETURN;
			break;
		}

		MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, methodName, "(Ljava/lang/Object;Ljava/lang/String;)" + typeNm, null, null);

		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0);
		mv.visitFieldInsn(GETFIELD, accessClassNm, "fieldNames", "[Ljava/lang/String;");
		mv.visitVarInsn(ALOAD, 2);
		mv.visitMethodInsn(INVOKESTATIC, "java/util/Arrays", "binarySearch", "([Ljava/lang/Object;Ljava/lang/Object;)I");
		mv.visitVarInsn(ISTORE, 3);
		mv.visitVarInsn(ILOAD, 3);

		final int maxStack;

		if (fields.length > 0) {
			maxStack = 5;
			Label[] labels = constructLabels(fields);

			Label defaultLabel = new Label();
			mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

			for (int i = 0, n = labels.length; i < n; i++) {
				Field field = fields[i];
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, clazzNm);
				mv.visitFieldInsn(GETFIELD, clazzNm, field.getName(), typeNm);
				mv.visitInsn(instruction);
			}

			mv.visitLabel(defaultLabel);
			mv.visitFrame(F_SAME, 0, null, 0, null);
		} else {
			maxStack = 6;
		}
		enhanceForThrowingException(mv, IllegalArgumentException.class, "Field was not found", "Ljava/lang/Object;", ALOAD, 2);
		mv.visitMaxs(maxStack, 4);
		mv.visitEnd();
	}
 
Example 11
static private void insertSetObject (ClassWriter cw, String classNameInternal, ArrayList<Field> fields) {
	int maxStack = 6;
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "set", "(Ljava/lang/Object;ILjava/lang/Object;)V", null, null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		for (int i = 0, n = labels.length; i < n; i++)
			labels[i] = new Label();
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			Field field = fields.get(i);
			Type fieldType = Type.getType(field.getType());

			mv.visitLabel(labels[i]);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitTypeInsn(CHECKCAST, classNameInternal);
			mv.visitVarInsn(ALOAD, 3);

			switch (fieldType.getSort()) {
			case Type.BOOLEAN:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Boolean");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z");
				break;
			case Type.BYTE:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Byte");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Byte", "byteValue", "()B");
				break;
			case Type.CHAR:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Character");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Character", "charValue", "()C");
				break;
			case Type.SHORT:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Short");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Short", "shortValue", "()S");
				break;
			case Type.INT:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Integer");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Integer", "intValue", "()I");
				break;
			case Type.FLOAT:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Float");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Float", "floatValue", "()F");
				break;
			case Type.LONG:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Long");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "longValue", "()J");
				break;
			case Type.DOUBLE:
				mv.visitTypeInsn(CHECKCAST, "java/lang/Double");
				mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Double", "doubleValue", "()D");
				break;
			case Type.ARRAY:
				mv.visitTypeInsn(CHECKCAST, fieldType.getDescriptor());
				break;
			case Type.OBJECT:
				mv.visitTypeInsn(CHECKCAST, fieldType.getInternalName());
				break;
			}

			mv.visitFieldInsn(PUTFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
				fieldType.getDescriptor());
			mv.visitInsn(RETURN);
		}

		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	mv = insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, 4);
	mv.visitEnd();
}
 
Example 12
static private void insertGetObject (ClassWriter cw, String classNameInternal, ArrayList<Field> fields) {
	int maxStack = 6;
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "get", "(Ljava/lang/Object;I)Ljava/lang/Object;", null, null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		for (int i = 0, n = labels.length; i < n; i++)
			labels[i] = new Label();
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			Field field = fields.get(i);

			mv.visitLabel(labels[i]);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitTypeInsn(CHECKCAST, classNameInternal);
			mv.visitFieldInsn(GETFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
				Type.getDescriptor(field.getType()));

			Type fieldType = Type.getType(field.getType());
			switch (fieldType.getSort()) {
			case Type.BOOLEAN:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
				break;
			case Type.BYTE:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
				break;
			case Type.CHAR:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
				break;
			case Type.SHORT:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
				break;
			case Type.INT:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
				break;
			case Type.FLOAT:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
				break;
			case Type.LONG:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
				break;
			case Type.DOUBLE:
				mv.visitMethodInsn(INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
				break;
			}

			mv.visitInsn(ARETURN);
		}

		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, 3);
	mv.visitEnd();
}
 
Example 13
static private void insertGetString (ClassWriter cw, String classNameInternal, ArrayList<Field> fields) {
	int maxStack = 6;
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "getString", "(Ljava/lang/Object;I)Ljava/lang/String;", null, null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		Label labelForInvalidTypes = new Label();
		boolean hasAnyBadTypeLabel = false;
		for (int i = 0, n = labels.length; i < n; i++) {
			if (fields.get(i).getType().equals(String.class))
				labels[i] = new Label();
			else {
				labels[i] = labelForInvalidTypes;
				hasAnyBadTypeLabel = true;
			}
		}
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			if (!labels[i].equals(labelForInvalidTypes)) {
				Field field = fields.get(i);
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, classNameInternal);
				mv.visitFieldInsn(GETFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
					"Ljava/lang/String;");
				mv.visitInsn(ARETURN);
			}
		}
		// Rest of fields: different type
		if (hasAnyBadTypeLabel) {
			mv.visitLabel(labelForInvalidTypes);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			insertThrowExceptionForFieldType(mv, "String");
		}
		// Default: field not found
		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, 3);
	mv.visitEnd();
}
 
Example 14
static private void insertSetPrimitive (ClassWriter cw, String classNameInternal, ArrayList<Field> fields,
	Type primitiveType) {
	int maxStack = 6;
	int maxLocals = 4; // See correction below for LLOAD and DLOAD
	final String setterMethodName;
	final String typeNameInternal = primitiveType.getDescriptor();
	final int loadValueInstruction;
	switch (primitiveType.getSort()) {
	case Type.BOOLEAN:
		setterMethodName = "setBoolean";
		loadValueInstruction = ILOAD;
		break;
	case Type.BYTE:
		setterMethodName = "setByte";
		loadValueInstruction = ILOAD;
		break;
	case Type.CHAR:
		setterMethodName = "setChar";
		loadValueInstruction = ILOAD;
		break;
	case Type.SHORT:
		setterMethodName = "setShort";
		loadValueInstruction = ILOAD;
		break;
	case Type.INT:
		setterMethodName = "setInt";
		loadValueInstruction = ILOAD;
		break;
	case Type.FLOAT:
		setterMethodName = "setFloat";
		loadValueInstruction = FLOAD;
		break;
	case Type.LONG:
		setterMethodName = "setLong";
		loadValueInstruction = LLOAD;
		maxLocals++; // (LLOAD and DLOAD actually load two slots)
		break;
	case Type.DOUBLE:
		setterMethodName = "setDouble";
		loadValueInstruction = DLOAD;
		maxLocals++; // (LLOAD and DLOAD actually load two slots)
		break;
	default:
		setterMethodName = "set";
		loadValueInstruction = ALOAD;
		break;
	}
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, setterMethodName, "(Ljava/lang/Object;I" + typeNameInternal + ")V", null,
		null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		Label labelForInvalidTypes = new Label();
		boolean hasAnyBadTypeLabel = false;
		for (int i = 0, n = labels.length; i < n; i++) {
			if (Type.getType(fields.get(i).getType()).equals(primitiveType))
				labels[i] = new Label();
			else {
				labels[i] = labelForInvalidTypes;
				hasAnyBadTypeLabel = true;
			}
		}
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			if (!labels[i].equals(labelForInvalidTypes)) {
				Field field = fields.get(i);
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, classNameInternal);
				mv.visitVarInsn(loadValueInstruction, 3);
				mv.visitFieldInsn(PUTFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
					typeNameInternal);
				mv.visitInsn(RETURN);
			}
		}
		// Rest of fields: different type
		if (hasAnyBadTypeLabel) {
			mv.visitLabel(labelForInvalidTypes);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			insertThrowExceptionForFieldType(mv, primitiveType.getClassName());
		}
		// Default: field not found
		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	mv = insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, maxLocals);
	mv.visitEnd();
}
 
Example 15
static private void insertGetPrimitive (ClassWriter cw, String classNameInternal, ArrayList<Field> fields,
	Type primitiveType) {
	int maxStack = 6;
	final String getterMethodName;
	final String typeNameInternal = primitiveType.getDescriptor();
	final int returnValueInstruction;
	switch (primitiveType.getSort()) {
	case Type.BOOLEAN:
		getterMethodName = "getBoolean";
		returnValueInstruction = IRETURN;
		break;
	case Type.BYTE:
		getterMethodName = "getByte";
		returnValueInstruction = IRETURN;
		break;
	case Type.CHAR:
		getterMethodName = "getChar";
		returnValueInstruction = IRETURN;
		break;
	case Type.SHORT:
		getterMethodName = "getShort";
		returnValueInstruction = IRETURN;
		break;
	case Type.INT:
		getterMethodName = "getInt";
		returnValueInstruction = IRETURN;
		break;
	case Type.FLOAT:
		getterMethodName = "getFloat";
		returnValueInstruction = FRETURN;
		break;
	case Type.LONG:
		getterMethodName = "getLong";
		returnValueInstruction = LRETURN;
		break;
	case Type.DOUBLE:
		getterMethodName = "getDouble";
		returnValueInstruction = DRETURN;
		break;
	default:
		getterMethodName = "get";
		returnValueInstruction = ARETURN;
		break;
	}
	MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getterMethodName, "(Ljava/lang/Object;I)" + typeNameInternal, null, null);
	mv.visitCode();
	mv.visitVarInsn(ILOAD, 2);

	if (!fields.isEmpty()) {
		maxStack--;
		Label[] labels = new Label[fields.size()];
		Label labelForInvalidTypes = new Label();
		boolean hasAnyBadTypeLabel = false;
		for (int i = 0, n = labels.length; i < n; i++) {
			if (Type.getType(fields.get(i).getType()).equals(primitiveType))
				labels[i] = new Label();
			else {
				labels[i] = labelForInvalidTypes;
				hasAnyBadTypeLabel = true;
			}
		}
		Label defaultLabel = new Label();
		mv.visitTableSwitchInsn(0, labels.length - 1, defaultLabel, labels);

		for (int i = 0, n = labels.length; i < n; i++) {
			Field field = fields.get(i);
			if (!labels[i].equals(labelForInvalidTypes)) {
				mv.visitLabel(labels[i]);
				mv.visitFrame(F_SAME, 0, null, 0, null);
				mv.visitVarInsn(ALOAD, 1);
				mv.visitTypeInsn(CHECKCAST, classNameInternal);
				mv.visitFieldInsn(GETFIELD, field.getDeclaringClass().getName().replace('.', '/'), field.getName(),
					typeNameInternal);
				mv.visitInsn(returnValueInstruction);
			}
		}
		// Rest of fields: different type
		if (hasAnyBadTypeLabel) {
			mv.visitLabel(labelForInvalidTypes);
			mv.visitFrame(F_SAME, 0, null, 0, null);
			insertThrowExceptionForFieldType(mv, primitiveType.getClassName());
		}
		// Default: field not found
		mv.visitLabel(defaultLabel);
		mv.visitFrame(F_SAME, 0, null, 0, null);
	}
	mv = insertThrowExceptionForFieldNotFound(mv);
	mv.visitMaxs(maxStack, 3);
	mv.visitEnd();
}
 
Example 16
Source Project: OpenModsLib   File: BlockSpaceTransform.java    License: MIT License 4 votes vote down vote up
private static void createTransformMethod(MethodVisitor mv, boolean invert) {
	// 0 - this (unused)
	// 1 - orientation
	// 2,3 - x
	// 4,5 - y
	// 6,7 - z
	mv.visitCode();

	mv.visitVarInsn(Opcodes.ALOAD, 1);

	final String enumType = Type.getInternalName(Enum.class);
	mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, enumType, "ordinal", Type.getMethodDescriptor(Type.INT_TYPE), false);

	final Orientation[] orientations = Orientation.values();

	final Label defaultLabel = new Label();
	final Label[] targets = new Label[orientations.length];
	for (int i = 0; i < orientations.length; i++)
		targets[i] = new Label();

	mv.visitTableSwitchInsn(0, orientations.length - 1, defaultLabel, targets);

	{
		mv.visitLabel(defaultLabel);
		final String excType = Type.getInternalName(IllegalArgumentException.class);
		final Type stringType = Type.getType(String.class);

		mv.visitTypeInsn(Opcodes.NEW, excType);
		mv.visitInsn(Opcodes.DUP);
		mv.visitVarInsn(Opcodes.ALOAD, 1);
		mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getInternalName(Object.class), "toString", Type.getMethodDescriptor(stringType), false);
		mv.visitMethodInsn(Opcodes.INVOKESPECIAL, excType, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, stringType), false);
		mv.visitInsn(Opcodes.ATHROW);
	}

	{
		final String createVectorDesc = Type.getMethodDescriptor(Type.getType(Vec3d.class), Type.DOUBLE_TYPE, Type.DOUBLE_TYPE, Type.DOUBLE_TYPE);
		final String selfType = Type.getInternalName(BlockSpaceTransform.class);
		for (int i = 0; i < orientations.length; i++) {
			mv.visitLabel(targets[i]);
			final Orientation orientation = orientations[i];
			final Matrix3f mat = orientation.getLocalToWorldMatrix();
			if (invert) mat.invert();

			createRowVectorMultiplication(mv, mat.m00, mat.m01, mat.m02);
			createRowVectorMultiplication(mv, mat.m10, mat.m11, mat.m12);
			createRowVectorMultiplication(mv, mat.m20, mat.m21, mat.m22);

			mv.visitMethodInsn(Opcodes.INVOKESTATIC, selfType, "createVector", createVectorDesc, false);
			mv.visitInsn(Opcodes.ARETURN);
		}
	}

	mv.visitMaxs(0, 0);
	mv.visitEnd();
}
 
Example 17
public static byte[] dump() throws Exception {

		ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
		FieldVisitor fv;
		MethodVisitor mv;
		AnnotationVisitor av0;

		cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER,
				"com/beetl/performance/lab/asm/UserAsmAccessor1", null,
				"java/lang/Object",
				new String[] { "com/beetl/performance/lab/asm/Access" });

		{
			mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
			mv.visitCode();
			mv.visitVarInsn(ALOAD, 0);
			mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>",
					"()V");
			mv.visitInsn(RETURN);
			mv.visitMaxs(1, 1);
			mv.visitEnd();
		}
		{
			mv = cw.visitMethod(
					ACC_PUBLIC,
					"get",
					"(Ljava/lang/Object;I)Ljava/lang/Object;",
					null,
					new String[] { "com/beetl/performance/lab/asm/ASMCastException" });
			mv.visitCode();
			Label l0 = new Label();
			Label l1 = new Label();
			Label l2 = new Label();
			mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
			mv.visitInsn(ACONST_NULL);
			mv.visitVarInsn(ASTORE, 3);
			mv.visitLabel(l0);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitTypeInsn(CHECKCAST, "com/beetl/performance/lab/User");
			mv.visitVarInsn(ASTORE, 3);
			mv.visitLabel(l1);
			Label l3 = new Label();
			mv.visitJumpInsn(GOTO, l3);
			mv.visitLabel(l2);
			mv.visitFrame(Opcodes.F_FULL, 4, new Object[] {
					"com/beetl/performance/lab/asm/UserAsmAccessor",
					"java/lang/Object", Opcodes.INTEGER,
					"com/beetl/performance/lab/User" }, 1,
					new Object[] { "java/lang/Exception" });
			mv.visitVarInsn(ASTORE, 4);
			mv.visitTypeInsn(NEW,
					"com/beetl/performance/lab/asm/ASMCastException");
			mv.visitInsn(DUP);
			mv.visitMethodInsn(INVOKESPECIAL,
					"com/beetl/performance/lab/asm/ASMCastException", "<init>",
					"()V");
			mv.visitInsn(ATHROW);
			mv.visitLabel(l3);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ILOAD, 2);
			Label l4 = new Label();
			Label l5 = new Label();
			Label l6 = new Label();
			mv.visitTableSwitchInsn(1, 2, l6, new Label[] { l4, l5 });
			mv.visitLabel(l4);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 3);
			mv.visitMethodInsn(INVOKEVIRTUAL, "com/beetl/performance/lab/User",
					"getName", "()Ljava/lang/String;");
			mv.visitInsn(ARETURN);
			mv.visitLabel(l5);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 3);
			mv.visitMethodInsn(INVOKEVIRTUAL, "com/beetl/performance/lab/User",
					"getId", "()I");
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf",
					"(I)Ljava/lang/Integer;");
			mv.visitInsn(ARETURN);
			mv.visitLabel(l6);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
			mv.visitInsn(DUP);
			mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException",
					"<init>", "()V");
			mv.visitInsn(ATHROW);
			mv.visitMaxs(2, 5);
			mv.visitEnd();
		}
		cw.visitEnd();

		return cw.toByteArray();
	}
 
Example 18
public static byte[] dump() throws Exception {

		ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
		FieldVisitor fv;
		MethodVisitor mv;
		AnnotationVisitor av0;

		cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER,
				"com/beetl/performance/lab/asm/UserAsmAccessor1", null,
				"java/lang/Object",
				new String[] { "com/beetl/performance/lab/asm/Access" });

		{
			mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
			mv.visitCode();
			mv.visitVarInsn(ALOAD, 0);
			mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>",
					"()V");
			mv.visitInsn(RETURN);
			mv.visitMaxs(1, 1);
			mv.visitEnd();
		}
		{
			mv = cw.visitMethod(
					ACC_PUBLIC,
					"get",
					"(Ljava/lang/Object;I)Ljava/lang/Object;",
					null,
					new String[] { "com/beetl/performance/lab/asm/ASMCastException" });
			mv.visitCode();
			Label l0 = new Label();
			Label l1 = new Label();
			Label l2 = new Label();
			mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
			mv.visitInsn(ACONST_NULL);
			mv.visitVarInsn(ASTORE, 3);
			mv.visitLabel(l0);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitTypeInsn(CHECKCAST, "com/beetl/performance/lab/User");
			mv.visitVarInsn(ASTORE, 3);
			mv.visitLabel(l1);
			Label l3 = new Label();
			mv.visitJumpInsn(GOTO, l3);
			mv.visitLabel(l2);
			mv.visitFrame(Opcodes.F_FULL, 4, new Object[] {
					"com/beetl/performance/lab/asm/UserAsmAccessor",
					"java/lang/Object", Opcodes.INTEGER,
					"com/beetl/performance/lab/User" }, 1,
					new Object[] { "java/lang/Exception" });
			mv.visitVarInsn(ASTORE, 4);
			mv.visitTypeInsn(NEW,
					"com/beetl/performance/lab/asm/ASMCastException");
			mv.visitInsn(DUP);
			mv.visitMethodInsn(INVOKESPECIAL,
					"com/beetl/performance/lab/asm/ASMCastException", "<init>",
					"()V");
			mv.visitInsn(ATHROW);
			mv.visitLabel(l3);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ILOAD, 2);
			Label l4 = new Label();
			Label l5 = new Label();
			Label l6 = new Label();
			mv.visitTableSwitchInsn(1, 2, l6, new Label[] { l4, l5 });
			mv.visitLabel(l4);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 3);
			mv.visitMethodInsn(INVOKEVIRTUAL, "com/beetl/performance/lab/User",
					"getName", "()Ljava/lang/String;");
			mv.visitInsn(ARETURN);
			mv.visitLabel(l5);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitVarInsn(ALOAD, 3);
			mv.visitMethodInsn(INVOKEVIRTUAL, "com/beetl/performance/lab/User",
					"getId", "()I");
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf",
					"(I)Ljava/lang/Integer;");
			mv.visitInsn(ARETURN);
			mv.visitLabel(l6);
			mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
			mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
			mv.visitInsn(DUP);
			mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException",
					"<init>", "()V");
			mv.visitInsn(ATHROW);
			mv.visitMaxs(2, 5);
			mv.visitEnd();
		}
		cw.visitEnd();

		return cw.toByteArray();
	}