Java Code Examples for org.springframework.asm.MethodVisitor#visitFieldInsn()

The following examples show how to use org.springframework.asm.MethodVisitor#visitFieldInsn() . 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: InlineList.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow codeflow) {
	final String constantFieldName = "inlineList$" + codeflow.nextFieldId();
	final String className = codeflow.getClassName();

	codeflow.registerNewField((cw, cflow) ->
			cw.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, constantFieldName, "Ljava/util/List;", null, null));

	codeflow.registerNewClinit((mVisitor, cflow) ->
			generateClinitCode(className, constantFieldName, mVisitor, cflow, false));

	mv.visitFieldInsn(GETSTATIC, className, constantFieldName, "Ljava/util/List;");
	codeflow.pushDescriptor("Ljava/util/List");
}
 
Example 2
Source File: InlineList.java    From spring-analysis-note with 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 3
Source File: TypeReference.java    From spring-analysis-note with MIT License 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
	Assert.state(this.type != null, "No type available");
	if (this.type.isPrimitive()) {
		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 == Character.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Character", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Double.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Double", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Float.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Float", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Integer.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Integer", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Long.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Long", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Short.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Short", "TYPE", "Ljava/lang/Class;");
		}
	}
	else {
		mv.visitLdcInsn(Type.getType(this.type));
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 4
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 5
Source File: InlineList.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow codeflow) {
	final String constantFieldName = "inlineList$" + codeflow.nextFieldId();
	final String className = codeflow.getClassName();

	codeflow.registerNewField((cw, cflow) ->
			cw.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, constantFieldName, "Ljava/util/List;", null, null));

	codeflow.registerNewClinit((mVisitor, cflow) ->
			generateClinitCode(className, constantFieldName, mVisitor, cflow, false));

	mv.visitFieldInsn(GETSTATIC, className, constantFieldName, "Ljava/util/List;");
	codeflow.pushDescriptor("Ljava/util/List");
}
 
Example 6
Source File: InlineList.java    From java-technology-stack with 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 7
Source File: TypeReference.java    From java-technology-stack with MIT License 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
	Assert.state(this.type != null, "No type available");
	if (this.type.isPrimitive()) {
		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 == Character.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Character", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Double.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Double", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Float.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Float", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Integer.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Integer", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Long.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Long", "TYPE", "Ljava/lang/Class;");
		}
		else if (this.type == Short.TYPE) {
			mv.visitFieldInsn(GETSTATIC, "java/lang/Short", "TYPE", "Ljava/lang/Class;");
		}
	}
	else {
		mv.visitLdcInsn(Type.getType(this.type));
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example 8
Source File: ReflectivePropertyAccessor.java    From java-technology-stack 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 9
Source File: InlineList.java    From lams with GNU General Public License v2.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 10
Source File: TypeReference.java    From lams with 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 11
Source File: ReflectivePropertyAccessor.java    From lams with GNU General Public License v2.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 12
Source File: InlineList.java    From spring4-understanding with 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 13
Source File: TypeReference.java    From spring4-understanding with Apache License 2.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 14
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()));
	}
}