Java Code Examples for org.springframework.cglib.core.ReflectUtils

The following examples show how to use org.springframework.cglib.core.ReflectUtils. 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
Source Project: spring-analysis-note   Source File: Enhancer.java    License: MIT License 6 votes vote down vote up
public EnhancerFactoryData(Class generatedClass, Class[] primaryConstructorArgTypes, boolean classOnly) {
	this.generatedClass = generatedClass;
	try {
		setThreadCallbacks = getCallbacksSetter(generatedClass, SET_THREAD_CALLBACKS_NAME);
		if (classOnly) {
			this.primaryConstructorArgTypes = null;
			this.primaryConstructor = null;
		}
		else {
			this.primaryConstructorArgTypes = primaryConstructorArgTypes;
			this.primaryConstructor = ReflectUtils.getConstructor(generatedClass, primaryConstructorArgTypes);
		}
	}
	catch (NoSuchMethodException e) {
		throw new CodeGenerationException(e);
	}
}
 
Example 2
Source Project: spring-analysis-note   Source File: Enhancer.java    License: MIT License 6 votes vote down vote up
/**
 * Creates proxy instance for given argument types, and assigns the callbacks.
 * Ideally, for each proxy class, just one set of argument types should be used,
 * otherwise it would have to spend time on constructor lookup.
 * Technically, it is a re-implementation of {@link Enhancer#createUsingReflection(Class)},
 * with "cache {@link #setThreadCallbacks} and {@link #primaryConstructor}"
 * @param argumentTypes constructor argument types
 * @param arguments constructor arguments
 * @param callbacks callbacks to set for the new instance
 * @return newly created proxy
 * @see #createUsingReflection(Class)
 */
public Object newInstance(Class[] argumentTypes, Object[] arguments, Callback[] callbacks) {
	setThreadCallbacks(callbacks);
	try {
		// Explicit reference equality is added here just in case Arrays.equals does not have one
		if (primaryConstructorArgTypes == argumentTypes ||
				Arrays.equals(primaryConstructorArgTypes, argumentTypes)) {
			// If we have relevant Constructor instance at hand, just call it
			// This skips "get constructors" machinery
			return ReflectUtils.newInstance(primaryConstructor, arguments);
		}
		// Take a slow path if observing unexpected argument types
		return ReflectUtils.newInstance(generatedClass, argumentTypes, arguments);
	}
	finally {
		// clear thread callbacks to allow them to be gc'd
		setThreadCallbacks(null);
	}

}
 
Example 3
Source Project: spring-analysis-note   Source File: Enhancer.java    License: 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 4
Source Project: spring-analysis-note   Source File: Enhancer.java    License: MIT License 6 votes vote down vote up
/**
 * Instantiates a proxy instance and assigns callback values.
 * Implementation detail: java.lang.reflect instances are not cached, so this method should not
 * be used on a hot path.
 * This method is used when {@link #setUseCache(boolean)} is set to {@code false}.
 * @param type class to instantiate
 * @return newly created instance
 */
private Object createUsingReflection(Class type) {
	setThreadCallbacks(type, callbacks);
	try {

		if (argumentTypes != null) {

			return ReflectUtils.newInstance(type, argumentTypes, arguments);

		}
		else {

			return ReflectUtils.newInstance(type);

		}
	}
	finally {
		// clear thread callbacks to allow them to be gc'd
		setThreadCallbacks(type, null);
	}
}
 
Example 5
Source Project: java-technology-stack   Source File: Enhancer.java    License: MIT License 6 votes vote down vote up
public EnhancerFactoryData(Class generatedClass, Class[] primaryConstructorArgTypes, boolean classOnly) {
	this.generatedClass = generatedClass;
	try {
		setThreadCallbacks = getCallbacksSetter(generatedClass, SET_THREAD_CALLBACKS_NAME);
		if (classOnly) {
			this.primaryConstructorArgTypes = null;
			this.primaryConstructor = null;
		}
		else {
			this.primaryConstructorArgTypes = primaryConstructorArgTypes;
			this.primaryConstructor = ReflectUtils.getConstructor(generatedClass, primaryConstructorArgTypes);
		}
	}
	catch (NoSuchMethodException e) {
		throw new CodeGenerationException(e);
	}
}
 
Example 6
Source Project: java-technology-stack   Source File: Enhancer.java    License: MIT License 6 votes vote down vote up
/**
 * Creates proxy instance for given argument types, and assigns the callbacks.
 * Ideally, for each proxy class, just one set of argument types should be used,
 * otherwise it would have to spend time on constructor lookup.
 * Technically, it is a re-implementation of {@link Enhancer#createUsingReflection(Class)},
 * with "cache {@link #setThreadCallbacks} and {@link #primaryConstructor}"
 * @param argumentTypes constructor argument types
 * @param arguments constructor arguments
 * @param callbacks callbacks to set for the new instance
 * @return newly created proxy
 * @see #createUsingReflection(Class)
 */
public Object newInstance(Class[] argumentTypes, Object[] arguments, Callback[] callbacks) {
	setThreadCallbacks(callbacks);
	try {
		// Explicit reference equality is added here just in case Arrays.equals does not have one
		if (primaryConstructorArgTypes == argumentTypes ||
				Arrays.equals(primaryConstructorArgTypes, argumentTypes)) {
			// If we have relevant Constructor instance at hand, just call it
			// This skips "get constructors" machinery
			return ReflectUtils.newInstance(primaryConstructor, arguments);
		}
		// Take a slow path if observing unexpected argument types
		return ReflectUtils.newInstance(generatedClass, argumentTypes, arguments);
	}
	finally {
		// clear thread callbacks to allow them to be gc'd
		setThreadCallbacks(null);
	}

}
 
Example 7
Source Project: java-technology-stack   Source File: Enhancer.java    License: 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 8
Source Project: java-technology-stack   Source File: Enhancer.java    License: MIT License 6 votes vote down vote up
/**
 * Instantiates a proxy instance and assigns callback values.
 * Implementation detail: java.lang.reflect instances are not cached, so this method should not
 * be used on a hot path.
 * This method is used when {@link #setUseCache(boolean)} is set to {@code false}.
 * @param type class to instantiate
 * @return newly created instance
 */
private Object createUsingReflection(Class type) {
	setThreadCallbacks(type, callbacks);
	try {

		if (argumentTypes != null) {

			return ReflectUtils.newInstance(type, argumentTypes, arguments);

		}
		else {

			return ReflectUtils.newInstance(type);

		}
	}
	finally {
		// clear thread callbacks to allow them to be gc'd
		setThreadCallbacks(type, null);
	}
}
 
Example 9
Source Project: spring-analysis-note   Source File: Enhancer.java    License: MIT License 5 votes vote down vote up
private Object createHelper() {
	preValidate();
	Object key = KEY_FACTORY.newInstance((superclass != null) ? superclass.getName() : null,
			ReflectUtils.getNames(interfaces),
			filter == ALL_ZERO ? null : new WeakCacheKey<CallbackFilter>(filter),
			callbackTypes,
			useFactory,
			interceptDuringConstruction,
			serialVersionUID);
	this.currentKey = key;
	Object result = super.create(key);
	return result;
}
 
Example 10
Source Project: spring-analysis-note   Source File: Enhancer.java    License: MIT License 5 votes vote down vote up
@Override
protected Class generate(ClassLoaderData data) {
	validate();
	if (superclass != null) {
		setNamePrefix(superclass.getName());
	}
	else if (interfaces != null) {
		setNamePrefix(interfaces[ReflectUtils.findPackageProtected(interfaces)].getName());
	}
	return super.generate(data);
}
 
Example 11
Source Project: spring-analysis-note   Source File: Enhancer.java    License: MIT License 5 votes vote down vote up
protected ProtectionDomain getProtectionDomain() {
	if (superclass != null) {
		return ReflectUtils.getProtectionDomain(superclass);
	}
	else if (interfaces != null) {
		return ReflectUtils.getProtectionDomain(interfaces[0]);
	}
	else {
		return null;
	}
}
 
Example 12
Source Project: java-technology-stack   Source File: Enhancer.java    License: MIT License 5 votes vote down vote up
private Object createHelper() {
	preValidate();
	Object key = KEY_FACTORY.newInstance((superclass != null) ? superclass.getName() : null,
			ReflectUtils.getNames(interfaces),
			filter == ALL_ZERO ? null : new WeakCacheKey<CallbackFilter>(filter),
			callbackTypes,
			useFactory,
			interceptDuringConstruction,
			serialVersionUID);
	this.currentKey = key;
	Object result = super.create(key);
	return result;
}
 
Example 13
Source Project: java-technology-stack   Source File: Enhancer.java    License: MIT License 5 votes vote down vote up
@Override
protected Class generate(ClassLoaderData data) {
	validate();
	if (superclass != null) {
		setNamePrefix(superclass.getName());
	}
	else if (interfaces != null) {
		setNamePrefix(interfaces[ReflectUtils.findPackageProtected(interfaces)].getName());
	}
	return super.generate(data);
}
 
Example 14
Source Project: java-technology-stack   Source File: Enhancer.java    License: MIT License 5 votes vote down vote up
protected ProtectionDomain getProtectionDomain() {
	if (superclass != null) {
		return ReflectUtils.getProtectionDomain(superclass);
	}
	else if (interfaces != null) {
		return ReflectUtils.getProtectionDomain(interfaces[0]);
	}
	else {
		return null;
	}
}
 
Example 15
Source Project: rebuild   Source File: ApiGateway.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param apiName
 * @return
 */
protected BaseApi createApi(String apiName) {
	if (!API_CLASSES.containsKey(apiName)) {
		throw new ApiInvokeException(ApiInvokeException.ERR_BADAPI, "Unknown API : " + apiName);
	}
	return (BaseApi) ReflectUtils.newInstance(API_CLASSES.get(apiName));
}
 
Example 16
Source Project: rebuild   Source File: ApiGateway.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param clazz
 */
public static void registerApi(Class<? extends BaseApi> clazz) {
	BaseApi api = (BaseApi) ReflectUtils.newInstance(clazz);
	String apiName = api.getApiName();
	if (API_CLASSES.containsKey(apiName)) {
		LOG.error("Replaced API : " + apiName);
	}

	API_CLASSES.put(apiName, clazz);
	LOG.info("New API registered : " + apiName + " : " + clazz.getName());
}
 
Example 17
Source Project: mica   Source File: MicaBeanMap.java    License: GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected ProtectionDomain getProtectionDomain() {
	return ReflectUtils.getProtectionDomain(beanClass);
}
 
Example 18
Source Project: mica   Source File: MicaBeanMap.java    License: GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected Object firstInstance(Class type) {
	return ((BeanMap)ReflectUtils.newInstance(type)).newInstance(bean);
}
 
Example 19
Source Project: spring-analysis-note   Source File: Enhancer.java    License: 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 20
Source Project: java-technology-stack   Source File: Enhancer.java    License: 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();
}