org.springframework.asm.Type Java Examples

The following examples show how to use org.springframework.asm.Type. 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: ReflectUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public static ClassInfo getClassInfo(final Class clazz) {
	final Type type = Type.getType(clazz);
	final Type sc = (clazz.getSuperclass() == null) ? null : Type.getType(clazz.getSuperclass());
	return new ClassInfo() {
		public Type getType() {
			return type;
		}
		public Type getSuperType() {
			return sc;
		}
		public Type[] getInterfaces() {
			return TypeUtils.getTypes(clazz.getInterfaces());
		}
		public int getModifiers() {
			return clazz.getModifiers();
		}
	};
}
 
Example #2
Source File: MicaBeanMapEmitter.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void generateGetPropertyType(final Map allProps, String[] allNames) {
	final CodeEmitter e = begin_method(Constants.ACC_PUBLIC, GET_PROPERTY_TYPE, null);
	e.load_arg(0);
	EmitUtils.string_switch(e, allNames, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {
		@Override
		public void processCase(Object key, Label end) {
			PropertyDescriptor pd = (PropertyDescriptor) allProps.get(key);
			EmitUtils.load_class(e, Type.getType(pd.getPropertyType()));
			e.return_value();
		}

		@Override
		public void processDefault() {
			e.aconst_null();
			e.return_value();
		}
	});
	e.end_method();
}
 
Example #3
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 #4
Source File: SimpleMethodMetadataReadingVisitor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public String toString() {
	String value = this.toStringValue;
	if (value == null) {
		StringBuilder builder = new StringBuilder();
		builder.append(this.declaringClassName);
		builder.append(".");
		builder.append(this.name);
		Type[] argumentTypes = Type.getArgumentTypes(this.descriptor);
		builder.append("(");
		for (Type type : argumentTypes) {
			builder.append(type.getClassName());
		}
		builder.append(")");
		value = builder.toString();
		this.toStringValue = value;
	}
	return value;
}
 
Example #5
Source File: MicaBeanMapEmitter.java    From mica with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void generateGet(Class type, final Map<String, PropertyDescriptor> getters) {
	final CodeEmitter e = begin_method(Constants.ACC_PUBLIC, BEAN_MAP_GET, null);
	e.load_arg(0);
	e.checkcast(Type.getType(type));
	e.load_arg(1);
	e.checkcast(Constants.TYPE_STRING);
	EmitUtils.string_switch(e, getNames(getters), Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {
		@Override
		public void processCase(Object key, Label end) {
			PropertyDescriptor pd = getters.get(key);
			MethodInfo method = ReflectUtils.getMethodInfo(pd.getReadMethod());
			e.invoke(method);
			e.box(method.getSignature().getReturnType());
			e.return_value();
		}

		@Override
		public void processDefault() {
			e.aconst_null();
			e.return_value();
		}
	});
	e.end_method();
}
 
Example #6
Source File: ReflectUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
public static ClassInfo getClassInfo(final Class clazz) {
	final Type type = Type.getType(clazz);
	final Type sc = (clazz.getSuperclass() == null) ? null : Type.getType(clazz.getSuperclass());
	return new ClassInfo() {
		public Type getType() {
			return type;
		}
		public Type getSuperType() {
			return sc;
		}
		public Type[] getInterfaces() {
			return TypeUtils.getTypes(clazz.getInterfaces());
		}
		public int getModifiers() {
			return clazz.getModifiers();
		}
	};
}
 
Example #7
Source File: OperatorInstanceof.java    From java-technology-stack 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 #8
Source File: LocalVariableTableParameterNameDiscoverer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public LocalVariableTableVisitor(Class<?> clazz, Map<Member, String[]> map, String name, String desc, boolean isStatic) {
	super(SpringAsmInfo.ASM_VERSION);
	this.clazz = clazz;
	this.memberMap = map;
	this.name = name;
	this.args = Type.getArgumentTypes(desc);
	this.parameterNames = new String[this.args.length];
	this.isStatic = isStatic;
	this.lvtSlotIndex = computeLvtSlotIndices(isStatic, this.args);
}
 
Example #9
Source File: ConfigurationClassEnhancer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected ClassGenerator transform(ClassGenerator cg) throws Exception {
	ClassEmitterTransformer transformer = new ClassEmitterTransformer() {
		@Override
		public void end_class() {
			declare_field(Constants.ACC_PUBLIC, BEAN_FACTORY_FIELD, Type.getType(BeanFactory.class), null);
			super.end_class();
		}
	};
	return new TransformingClassGenerator(cg, transformer);
}
 
Example #10
Source File: RecursiveAnnotationArrayVisitor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public AnnotationVisitor visitAnnotation(String attributeName, String asmTypeDescriptor) {
	String annotationType = Type.getType(asmTypeDescriptor).getClassName();
	AnnotationAttributes nestedAttributes = new AnnotationAttributes(annotationType, this.classLoader);
	this.allNestedAttributes.add(nestedAttributes);
	return new RecursiveAnnotationAttributesVisitor(annotationType, nestedAttributes, this.classLoader);
}
 
Example #11
Source File: AbstractRecursiveAnnotationVisitor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public AnnotationVisitor visitAnnotation(String attributeName, String asmTypeDescriptor) {
	String annotationType = Type.getType(asmTypeDescriptor).getClassName();
	AnnotationAttributes nestedAttributes = new AnnotationAttributes(annotationType, this.classLoader);
	this.attributes.put(attributeName, nestedAttributes);
	return new RecursiveAnnotationAttributesVisitor(annotationType, nestedAttributes, this.classLoader);
}
 
Example #12
Source File: MethodMetadataReadingVisitor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
	String className = Type.getType(desc).getClassName();
	this.methodMetadataSet.add(this);
	return new AnnotationAttributesReadingVisitor(
			className, this.attributesMap, this.metaAnnotationMap, this.classLoader);
}
 
Example #13
Source File: AnnotationMetadataReadingVisitor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
	// Skip bridge methods - we're only interested in original annotation-defining user methods.
	// On JDK 8, we'd otherwise run into double detection of the same annotated method...
	if ((access & Opcodes.ACC_BRIDGE) != 0) {
		return super.visitMethod(access, name, desc, signature, exceptions);
	}
	return new MethodMetadataReadingVisitor(name, access, getClassName(),
			Type.getReturnType(desc).getClassName(), this.classLoader, this.methodMetadataSet);
}
 
Example #14
Source File: LocalVariableTableParameterNameDiscoverer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static int[] computeLvtSlotIndices(boolean isStatic, Type[] paramTypes) {
	int[] lvtIndex = new int[paramTypes.length];
	int nextIndex = (isStatic ? 0 : 1);
	for (int i = 0; i < paramTypes.length; i++) {
		lvtIndex[i] = nextIndex;
		if (isWideType(paramTypes[i])) {
			nextIndex += 2;
		}
		else {
			nextIndex++;
		}
	}
	return lvtIndex;
}
 
Example #15
Source File: ReflectUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
public static MethodInfo getMethodInfo(final Member member, final int modifiers) {
	final Signature sig = getSignature(member);
	return new MethodInfo() {
		private ClassInfo ci;

		public ClassInfo getClassInfo() {
			if (ci == null)
				ci = ReflectUtils.getClassInfo(member.getDeclaringClass());
			return ci;
		}

		public int getModifiers() {
			return modifiers;
		}

		public Signature getSignature() {
			return sig;
		}

		public Type[] getExceptionTypes() {
			return ReflectUtils.getExceptionTypes(member);
		}

		public Attribute getAttribute() {
			return null;
		}
	};
}
 
Example #16
Source File: OperatorInstanceof.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);
	CodeFlow.insertBoxIfNecessary(mv, cf.lastDescriptor());
	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 #17
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 #18
Source File: AnnotationMetadataReadingVisitor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
	// Skip bridge methods - we're only interested in original annotation-defining user methods.
	// On JDK 8, we'd otherwise run into double detection of the same annotated method...
	if ((access & Opcodes.ACC_BRIDGE) != 0) {
		return super.visitMethod(access, name, desc, signature, exceptions);
	}
	return new MethodMetadataReadingVisitor(name, access, getClassName(),
			Type.getReturnType(desc).getClassName(), this.classLoader, this.methodMetadataSet);
}
 
Example #19
Source File: AnnotationMetadataReadingVisitor.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
	String className = Type.getType(desc).getClassName();
	this.annotationSet.add(className);
	return new AnnotationAttributesReadingVisitor(
			className, this.attributesMap, this.metaAnnotationMap, this.classLoader);
}
 
Example #20
Source File: ReflectUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
public static Method[] findMethods(String[] namesAndDescriptors, Method[] methods) {
	Map map = new HashMap();
	for (int i = 0; i < methods.length; i++) {
		Method method = methods[i];
		map.put(method.getName() + Type.getMethodDescriptor(method), method);
	}
	Method[] result = new Method[namesAndDescriptors.length / 2];
	for (int i = 0; i < result.length; i++) {
		result[i] = (Method) map.get(namesAndDescriptors[i * 2] + namesAndDescriptors[i * 2 + 1]);
		if (result[i] == null) {
			// TODO: error?
		}
	}
	return result;
}
 
Example #21
Source File: LocalVariableTableParameterNameDiscoverer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private static int[] computeLvtSlotIndices(boolean isStatic, Type[] paramTypes) {
	int[] lvtIndex = new int[paramTypes.length];
	int nextIndex = (isStatic ? 0 : 1);
	for (int i = 0; i < paramTypes.length; i++) {
		lvtIndex[i] = nextIndex;
		if (isWideType(paramTypes[i])) {
			nextIndex += 2;
		}
		else {
			nextIndex++;
		}
	}
	return lvtIndex;
}
 
Example #22
Source File: ReflectUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
public static Signature getSignature(Member member) {
	if (member instanceof Method) {
		return new Signature(member.getName(), Type.getMethodDescriptor((Method) member));
	}
	else if (member instanceof Constructor) {
		Type[] types = TypeUtils.getTypes(((Constructor) member).getParameterTypes());
		return new Signature(Constants.CONSTRUCTOR_NAME,
				Type.getMethodDescriptor(Type.VOID_TYPE, types));

	}
	else {
		throw new IllegalArgumentException("Cannot get signature of a field");
	}
}
 
Example #23
Source File: ReflectUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
public static Type[] getExceptionTypes(Member member) {
	if (member instanceof Method) {
		return TypeUtils.getTypes(((Method) member).getExceptionTypes());
	}
	else if (member instanceof Constructor) {
		return TypeUtils.getTypes(((Constructor) member).getExceptionTypes());
	}
	else {
		throw new IllegalArgumentException("Cannot get exception types of a field");
	}
}
 
Example #24
Source File: KeyFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
public boolean customize(CodeEmitter e, Type type) {
	if (Constants.TYPE_TYPE.equals(type)) {
		e.invoke_virtual(type, GET_SORT);
		return true;
	}
	return false;
}
 
Example #25
Source File: LocalVariableTableParameterNameDiscoverer.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public LocalVariableTableVisitor(Class<?> clazz, Map<Member, String[]> map, String name, String desc, boolean isStatic) {
	super(SpringAsmInfo.ASM_VERSION);
	this.clazz = clazz;
	this.memberMap = map;
	this.name = name;
	this.args = Type.getArgumentTypes(desc);
	this.parameterNames = new String[this.args.length];
	this.isStatic = isStatic;
	this.lvtSlotIndex = computeLvtSlotIndices(isStatic, this.args);
}
 
Example #26
Source File: Enhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Type getThisType(CodeEmitter e) {
	if (currentData == null) {
		return e.getClassEmitter().getClassType();
	}
	else {
		return Type.getType(currentData.generatedClass);
	}
}
 
Example #27
Source File: Enhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void emitNewInstanceCallbacks(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, NEW_INSTANCE, null);
	Type thisType = getThisType(e);
	e.load_arg(0);
	e.invoke_static(thisType, SET_THREAD_CALLBACKS);
	emitCommonNewInstance(e);
}
 
Example #28
Source File: Enhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void validate() {
	if (classOnly ^ (callbacks == null)) {
		if (classOnly) {
			throw new IllegalStateException("createClass does not accept callbacks");
		}
		else {
			throw new IllegalStateException("Callbacks are required");
		}
	}
	if (classOnly && (callbackTypes == null)) {
		throw new IllegalStateException("Callback types are required");
	}
	if (validateCallbackTypes) {
		callbackTypes = null;
	}
	if (callbacks != null && callbackTypes != null) {
		if (callbacks.length != callbackTypes.length) {
			throw new IllegalStateException("Lengths of callback and callback types array must be the same");
		}
		Type[] check = CallbackInfo.determineTypes(callbacks);
		for (int i = 0; i < check.length; i++) {
			if (!check[i].equals(callbackTypes[i])) {
				throw new IllegalStateException("Callback " + check[i] + " is not assignable to " + callbackTypes[i]);
			}
		}
	}
	else if (callbacks != null) {
		callbackTypes = CallbackInfo.determineTypes(callbacks);
	}
	if (interfaces != null) {
		for (int i = 0; i < interfaces.length; i++) {
			if (interfaces[i] == null) {
				throw new IllegalStateException("Interfaces cannot be null");
			}
			if (!interfaces[i].isInterface()) {
				throw new IllegalStateException(interfaces[i] + " is not an interface");
			}
		}
	}
}
 
Example #29
Source File: ConfigurationClassEnhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected ClassGenerator transform(ClassGenerator cg) throws Exception {
	ClassEmitterTransformer transformer = new ClassEmitterTransformer() {
		@Override
		public void end_class() {
			declare_field(Constants.ACC_PUBLIC, BEAN_FACTORY_FIELD, Type.getType(BeanFactory.class), null);
			super.end_class();
		}
	};
	return new TransformingClassGenerator(cg, transformer);
}
 
Example #30
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);
}