org.springframework.asm.ClassVisitor Java Examples

The following examples show how to use org.springframework.asm.ClassVisitor. 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: MergedAnnotationMetadataVisitorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void loadFrom(Class<?> type) {
	ClassVisitor visitor = new ClassVisitor(SpringAsmInfo.ASM_VERSION) {

		@Override
		public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
			return MergedAnnotationReadingVisitor.get(getClass().getClassLoader(),
					null, descriptor, visible,
					annotation -> MergedAnnotationMetadataVisitorTests.this.annotation = annotation);
		}

	};
	try {
		new ClassReader(type.getName()).accept(visitor, ClassReader.SKIP_DEBUG
				| ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
	}
	catch (IOException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #2
Source File: MicaBeanMapEmitter.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
public MicaBeanMapEmitter(ClassVisitor v, String className, Class type, int require) {
	super(v);

	begin_class(Constants.V1_2, Constants.ACC_PUBLIC, className, BEAN_MAP, null, Constants.SOURCE_FILE);
	EmitUtils.null_constructor(this);
	EmitUtils.factory_method(this, NEW_INSTANCE);
	generateConstructor();

	Map<String, PropertyDescriptor> getters = makePropertyMap(ReflectUtil.getBeanGetters(type));
	Map<String, PropertyDescriptor> setters = makePropertyMap(ReflectUtil.getBeanSetters(type));
	Map<String, PropertyDescriptor> allProps = new HashMap<>(32);
	allProps.putAll(getters);
	allProps.putAll(setters);

	if (require != 0) {
		for (Iterator it = allProps.keySet().iterator(); it.hasNext(); ) {
			String name = (String) it.next();
			if ((((require & MicaBeanMap.REQUIRE_GETTER) != 0) && !getters.containsKey(name)) ||
				(((require & MicaBeanMap.REQUIRE_SETTER) != 0) && !setters.containsKey(name))) {
				it.remove();
				getters.remove(name);
				setters.remove(name);
			}
		}
	}
	generateGet(type, getters);
	generatePut(type, setters);

	String[] allNames = getNames(allProps);
	generateKeySet(allNames);
	generateGetPropertyType(allProps, allNames);
	end_class();
}
 
Example #3
Source File: MicaBeanMap.java    From mica with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void generateClass(ClassVisitor v) throws Exception {
	new MicaBeanMapEmitter(v, getClassName(), beanClass, require);
}
 
Example #4
Source File: Enhancer.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void generateClass(ClassVisitor v) throws Exception {
	Class sc = (superclass == null) ? Object.class : superclass;

	if (TypeUtils.isFinal(sc.getModifiers()))
		throw new IllegalArgumentException("Cannot subclass final class " + sc.getName());
	List constructors = new ArrayList(Arrays.asList(sc.getDeclaredConstructors()));
	filterConstructors(sc, constructors);

	// Order is very important: must add superclass, then
	// its superclass chain, then each interface and
	// its superinterfaces.
	List actualMethods = new ArrayList();
	List interfaceMethods = new ArrayList();
	final Set forcePublic = new HashSet();
	getMethods(sc, interfaces, actualMethods, interfaceMethods, forcePublic);

	List methods = CollectionUtils.transform(actualMethods, new Transformer() {
		public Object transform(Object value) {
			Method method = (Method) value;
			int modifiers = Constants.ACC_FINAL
					| (method.getModifiers()
					& ~Constants.ACC_ABSTRACT
					& ~Constants.ACC_NATIVE
					& ~Constants.ACC_SYNCHRONIZED);
			if (forcePublic.contains(MethodWrapper.create(method))) {
				modifiers = (modifiers & ~Constants.ACC_PROTECTED) | Constants.ACC_PUBLIC;
			}
			return ReflectUtils.getMethodInfo(method, modifiers);
		}
	});

	ClassEmitter e = new ClassEmitter(v);
	if (currentData == null) {
		e.begin_class(Constants.V1_2,
				Constants.ACC_PUBLIC,
				getClassName(),
				Type.getType(sc),
				(useFactory ?
						TypeUtils.add(TypeUtils.getTypes(interfaces), FACTORY) :
						TypeUtils.getTypes(interfaces)),
				Constants.SOURCE_FILE);
	}
	else {
		e.begin_class(Constants.V1_2,
				Constants.ACC_PUBLIC,
				getClassName(),
				null,
				new Type[]{FACTORY},
				Constants.SOURCE_FILE);
	}
	List constructorInfo = CollectionUtils.transform(constructors, MethodInfoTransformer.getInstance());

	e.declare_field(Constants.ACC_PRIVATE, BOUND_FIELD, Type.BOOLEAN_TYPE, null);
	e.declare_field(Constants.ACC_PUBLIC | Constants.ACC_STATIC, FACTORY_DATA_FIELD, OBJECT_TYPE, null);
	if (!interceptDuringConstruction) {
		e.declare_field(Constants.ACC_PRIVATE, CONSTRUCTED_FIELD, Type.BOOLEAN_TYPE, null);
	}
	e.declare_field(Constants.PRIVATE_FINAL_STATIC, THREAD_CALLBACKS_FIELD, THREAD_LOCAL, null);
	e.declare_field(Constants.PRIVATE_FINAL_STATIC, STATIC_CALLBACKS_FIELD, CALLBACK_ARRAY, null);
	if (serialVersionUID != null) {
		e.declare_field(Constants.PRIVATE_FINAL_STATIC, Constants.SUID_FIELD_NAME, Type.LONG_TYPE, serialVersionUID);
	}

	for (int i = 0; i < callbackTypes.length; i++) {
		e.declare_field(Constants.ACC_PRIVATE, getCallbackField(i), callbackTypes[i], null);
	}
	// This is declared private to avoid "public field" pollution
	e.declare_field(Constants.ACC_PRIVATE | Constants.ACC_STATIC, CALLBACK_FILTER_FIELD, OBJECT_TYPE, null);

	if (currentData == null) {
		emitMethods(e, methods, actualMethods);
		emitConstructors(e, constructorInfo);
	}
	else {
		emitDefaultConstructor(e);
	}
	emitSetThreadCallbacks(e);
	emitSetStaticCallbacks(e);
	emitBindCallbacks(e);

	if (useFactory || currentData != null) {
		int[] keys = getCallbackKeys();
		emitNewInstanceCallbacks(e);
		emitNewInstanceCallback(e);
		emitNewInstanceMultiarg(e, constructorInfo);
		emitGetCallback(e, keys);
		emitSetCallback(e, keys);
		emitGetCallbacks(e);
		emitSetCallbacks(e);
	}

	e.end_class();
}
 
Example #5
Source File: Enhancer.java    From java-technology-stack with MIT License 4 votes vote down vote up
public void generateClass(ClassVisitor v) throws Exception {
	Class sc = (superclass == null) ? Object.class : superclass;

	if (TypeUtils.isFinal(sc.getModifiers()))
		throw new IllegalArgumentException("Cannot subclass final class " + sc.getName());
	List constructors = new ArrayList(Arrays.asList(sc.getDeclaredConstructors()));
	filterConstructors(sc, constructors);

	// Order is very important: must add superclass, then
	// its superclass chain, then each interface and
	// its superinterfaces.
	List actualMethods = new ArrayList();
	List interfaceMethods = new ArrayList();
	final Set forcePublic = new HashSet();
	getMethods(sc, interfaces, actualMethods, interfaceMethods, forcePublic);

	List methods = CollectionUtils.transform(actualMethods, new Transformer() {
		public Object transform(Object value) {
			Method method = (Method) value;
			int modifiers = Constants.ACC_FINAL
					| (method.getModifiers()
					& ~Constants.ACC_ABSTRACT
					& ~Constants.ACC_NATIVE
					& ~Constants.ACC_SYNCHRONIZED);
			if (forcePublic.contains(MethodWrapper.create(method))) {
				modifiers = (modifiers & ~Constants.ACC_PROTECTED) | Constants.ACC_PUBLIC;
			}
			return ReflectUtils.getMethodInfo(method, modifiers);
		}
	});

	ClassEmitter e = new ClassEmitter(v);
	if (currentData == null) {
		e.begin_class(Constants.V1_2,
				Constants.ACC_PUBLIC,
				getClassName(),
				Type.getType(sc),
				(useFactory ?
						TypeUtils.add(TypeUtils.getTypes(interfaces), FACTORY) :
						TypeUtils.getTypes(interfaces)),
				Constants.SOURCE_FILE);
	}
	else {
		e.begin_class(Constants.V1_2,
				Constants.ACC_PUBLIC,
				getClassName(),
				null,
				new Type[]{FACTORY},
				Constants.SOURCE_FILE);
	}
	List constructorInfo = CollectionUtils.transform(constructors, MethodInfoTransformer.getInstance());

	e.declare_field(Constants.ACC_PRIVATE, BOUND_FIELD, Type.BOOLEAN_TYPE, null);
	e.declare_field(Constants.ACC_PUBLIC | Constants.ACC_STATIC, FACTORY_DATA_FIELD, OBJECT_TYPE, null);
	if (!interceptDuringConstruction) {
		e.declare_field(Constants.ACC_PRIVATE, CONSTRUCTED_FIELD, Type.BOOLEAN_TYPE, null);
	}
	e.declare_field(Constants.PRIVATE_FINAL_STATIC, THREAD_CALLBACKS_FIELD, THREAD_LOCAL, null);
	e.declare_field(Constants.PRIVATE_FINAL_STATIC, STATIC_CALLBACKS_FIELD, CALLBACK_ARRAY, null);
	if (serialVersionUID != null) {
		e.declare_field(Constants.PRIVATE_FINAL_STATIC, Constants.SUID_FIELD_NAME, Type.LONG_TYPE, serialVersionUID);
	}

	for (int i = 0; i < callbackTypes.length; i++) {
		e.declare_field(Constants.ACC_PRIVATE, getCallbackField(i), callbackTypes[i], null);
	}
	// This is declared private to avoid "public field" pollution
	e.declare_field(Constants.ACC_PRIVATE | Constants.ACC_STATIC, CALLBACK_FILTER_FIELD, OBJECT_TYPE, null);

	if (currentData == null) {
		emitMethods(e, methods, actualMethods);
		emitConstructors(e, constructorInfo);
	}
	else {
		emitDefaultConstructor(e);
	}
	emitSetThreadCallbacks(e);
	emitSetStaticCallbacks(e);
	emitBindCallbacks(e);

	if (useFactory || currentData != null) {
		int[] keys = getCallbackKeys();
		emitNewInstanceCallbacks(e);
		emitNewInstanceCallback(e);
		emitNewInstanceMultiarg(e, constructorInfo);
		emitGetCallback(e, keys);
		emitSetCallback(e, keys);
		emitGetCallbacks(e);
		emitSetCallbacks(e);
	}

	e.end_class();
}