org.springframework.cglib.core.CollectionUtils Java Examples

The following examples show how to use org.springframework.cglib.core.CollectionUtils. 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: Enhancer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private static void getMethods(Class superclass, Class[] interfaces, List methods, List interfaceMethods, Set forcePublic) {
	ReflectUtils.addAllMethods(superclass, methods);
	List target = (interfaceMethods != null) ? interfaceMethods : methods;
	if (interfaces != null) {
		for (int i = 0; i < interfaces.length; i++) {
			if (interfaces[i] != Factory.class) {
				ReflectUtils.addAllMethods(interfaces[i], target);
			}
		}
	}
	if (interfaceMethods != null) {
		if (forcePublic != null) {
			forcePublic.addAll(MethodWrapper.createSet(interfaceMethods));
		}
		methods.addAll(interfaceMethods);
	}
	CollectionUtils.filter(methods, new RejectModifierPredicate(Constants.ACC_STATIC));
	CollectionUtils.filter(methods, new VisibilityPredicate(superclass, true));
	CollectionUtils.filter(methods, new DuplicatesPredicate());
	CollectionUtils.filter(methods, new RejectModifierPredicate(Constants.ACC_FINAL));
}
 
Example #2
Source File: Enhancer.java    From java-technology-stack with MIT License 6 votes vote down vote up
private static void getMethods(Class superclass, Class[] interfaces, List methods, List interfaceMethods, Set forcePublic) {
	ReflectUtils.addAllMethods(superclass, methods);
	List target = (interfaceMethods != null) ? interfaceMethods : methods;
	if (interfaces != null) {
		for (int i = 0; i < interfaces.length; i++) {
			if (interfaces[i] != Factory.class) {
				ReflectUtils.addAllMethods(interfaces[i], target);
			}
		}
	}
	if (interfaceMethods != null) {
		if (forcePublic != null) {
			forcePublic.addAll(MethodWrapper.createSet(interfaceMethods));
		}
		methods.addAll(interfaceMethods);
	}
	CollectionUtils.filter(methods, new RejectModifierPredicate(Constants.ACC_STATIC));
	CollectionUtils.filter(methods, new VisibilityPredicate(superclass, true));
	CollectionUtils.filter(methods, new DuplicatesPredicate());
	CollectionUtils.filter(methods, new RejectModifierPredicate(Constants.ACC_FINAL));
}
 
Example #3
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 #4
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();
}
 
Example #5
Source File: Enhancer.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Filter the list of constructors from the superclass. The
 * constructors which remain will be included in the generated
 * class. The default implementation is to filter out all private
 * constructors, but subclasses may extend Enhancer to override this
 * behavior.
 * @param sc the superclass
 * @param constructors the list of all declared constructors from the superclass
 * @throws IllegalArgumentException if there are no non-private constructors
 */
protected void filterConstructors(Class sc, List constructors) {
	CollectionUtils.filter(constructors, new VisibilityPredicate(sc, true));
	if (constructors.size() == 0)
		throw new IllegalArgumentException("No visible constructors in " + sc);
}
 
Example #6
Source File: Enhancer.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Filter the list of constructors from the superclass. The
 * constructors which remain will be included in the generated
 * class. The default implementation is to filter out all private
 * constructors, but subclasses may extend Enhancer to override this
 * behavior.
 * @param sc the superclass
 * @param constructors the list of all declared constructors from the superclass
 * @throws IllegalArgumentException if there are no non-private constructors
 */
protected void filterConstructors(Class sc, List constructors) {
	CollectionUtils.filter(constructors, new VisibilityPredicate(sc, true));
	if (constructors.size() == 0)
		throw new IllegalArgumentException("No visible constructors in " + sc);
}