org.springframework.cglib.core.Constants Java Examples

The following examples show how to use org.springframework.cglib.core.Constants. 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 void emitNewInstanceCallback(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, SINGLE_NEW_INSTANCE, null);
	switch (callbackTypes.length) {
		case 0:
			// TODO: make sure Callback is null
			break;
		case 1:
			// for now just make a new array; TODO: optimize
			e.push(1);
			e.newarray(CALLBACK);
			e.dup();
			e.push(0);
			e.load_arg(0);
			e.aastore();
			e.invoke_static(getThisType(e), SET_THREAD_CALLBACKS);
			break;
		default:
			e.throw_exception(ILLEGAL_STATE_EXCEPTION, "More than one callback object required");
	}
	emitCommonNewInstance(e);
}
 
Example #3
Source File: Enhancer.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void emitGetCallbacks(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, GET_CALLBACKS, null);
	e.load_this();
	e.invoke_static_this(BIND_CALLBACKS);
	e.load_this();
	e.push(callbackTypes.length);
	e.newarray(CALLBACK);
	for (int i = 0; i < callbackTypes.length; i++) {
		e.dup();
		e.push(i);
		e.load_this();
		e.getfield(getCallbackField(i));
		e.aastore();
	}
	e.return_value();
	e.end_method();
}
 
Example #4
Source File: Enhancer.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void emitSetCallback(ClassEmitter ce, int[] keys) {
	final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, SET_CALLBACK, null);
	e.load_arg(0);
	e.process_switch(keys, new ProcessSwitchCallback() {
		public void processCase(int key, Label end) {
			e.load_this();
			e.load_arg(1);
			e.checkcast(callbackTypes[key]);
			e.putfield(getCallbackField(key));
			e.goTo(end);
		}

		public void processDefault() {
			// TODO: error?
		}
	});
	e.return_value();
	e.end_method();
}
 
Example #5
Source File: Enhancer.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void emitGetCallback(ClassEmitter ce, int[] keys) {
	final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, GET_CALLBACK, null);
	e.load_this();
	e.invoke_static_this(BIND_CALLBACKS);
	e.load_this();
	e.load_arg(0);
	e.process_switch(keys, new ProcessSwitchCallback() {
		public void processCase(int key, Label end) {
			e.getfield(getCallbackField(key));
			e.goTo(end);
		}

		public void processDefault() {
			e.pop(); // stack height
			e.aconst_null();
		}
	});
	e.return_value();
	e.end_method();
}
 
Example #6
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 #7
Source File: Enhancer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void emitNewInstanceCallback(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, SINGLE_NEW_INSTANCE, null);
	switch (callbackTypes.length) {
		case 0:
			// TODO: make sure Callback is null
			break;
		case 1:
			// for now just make a new array; TODO: optimize
			e.push(1);
			e.newarray(CALLBACK);
			e.dup();
			e.push(0);
			e.load_arg(0);
			e.aastore();
			e.invoke_static(getThisType(e), SET_THREAD_CALLBACKS);
			break;
		default:
			e.throw_exception(ILLEGAL_STATE_EXCEPTION, "More than one callback object required");
	}
	emitCommonNewInstance(e);
}
 
Example #8
Source File: Enhancer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void emitGetCallbacks(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, GET_CALLBACKS, null);
	e.load_this();
	e.invoke_static_this(BIND_CALLBACKS);
	e.load_this();
	e.push(callbackTypes.length);
	e.newarray(CALLBACK);
	for (int i = 0; i < callbackTypes.length; i++) {
		e.dup();
		e.push(i);
		e.load_this();
		e.getfield(getCallbackField(i));
		e.aastore();
	}
	e.return_value();
	e.end_method();
}
 
Example #9
Source File: Enhancer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void emitSetCallback(ClassEmitter ce, int[] keys) {
	final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, SET_CALLBACK, null);
	e.load_arg(0);
	e.process_switch(keys, new ProcessSwitchCallback() {
		public void processCase(int key, Label end) {
			e.load_this();
			e.load_arg(1);
			e.checkcast(callbackTypes[key]);
			e.putfield(getCallbackField(key));
			e.goTo(end);
		}

		public void processDefault() {
			// TODO: error?
		}
	});
	e.return_value();
	e.end_method();
}
 
Example #10
Source File: Enhancer.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void emitGetCallback(ClassEmitter ce, int[] keys) {
	final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, GET_CALLBACK, null);
	e.load_this();
	e.invoke_static_this(BIND_CALLBACKS);
	e.load_this();
	e.load_arg(0);
	e.process_switch(keys, new ProcessSwitchCallback() {
		public void processCase(int key, Label end) {
			e.getfield(getCallbackField(key));
			e.goTo(end);
		}

		public void processDefault() {
			e.pop(); // stack height
			e.aconst_null();
		}
	});
	e.return_value();
	e.end_method();
}
 
Example #11
Source File: Enhancer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void emitConstructors(ClassEmitter ce, List constructors) {
	boolean seenNull = false;
	for (Iterator it = constructors.iterator(); it.hasNext(); ) {
		MethodInfo constructor = (MethodInfo) it.next();
		if (currentData != null && !"()V".equals(constructor.getSignature().getDescriptor())) {
			continue;
		}
		CodeEmitter e = EmitUtils.begin_method(ce, constructor, Constants.ACC_PUBLIC);
		e.load_this();
		e.dup();
		e.load_args();
		Signature sig = constructor.getSignature();
		seenNull = seenNull || sig.getDescriptor().equals("()V");
		e.super_invoke_constructor(sig);
		if (currentData == null) {
			e.invoke_static_this(BIND_CALLBACKS);
			if (!interceptDuringConstruction) {
				e.load_this();
				e.push(1);
				e.putfield(CONSTRUCTED_FIELD);
			}
		}
		e.return_value();
		e.end_method();
	}
	if (!classOnly && !seenNull && arguments == null)
		throw new IllegalArgumentException("Superclass has no null constructors but no arguments were given");
}
 
Example #12
Source File: ConfigurationClassEnhancer.java    From spring4-understanding with Apache License 2.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 #13
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 #14
Source File: Enhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void emitSetStaticCallbacks(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_STATIC,
			SET_STATIC_CALLBACKS,
			null);
	e.load_arg(0);
	e.putfield(STATIC_CALLBACKS_FIELD);
	e.return_value();
	e.end_method();
}
 
Example #15
Source File: Enhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void emitSetThreadCallbacks(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_STATIC,
			SET_THREAD_CALLBACKS,
			null);
	e.getfield(THREAD_CALLBACKS_FIELD);
	e.load_arg(0);
	e.invoke_virtual(THREAD_LOCAL, THREAD_LOCAL_SET);
	e.return_value();
	e.end_method();
}
 
Example #16
Source File: Enhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void emitNewInstanceMultiarg(ClassEmitter ce, List constructors) {
	final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, MULTIARG_NEW_INSTANCE, null);
	final Type thisType = getThisType(e);
	e.load_arg(2);
	e.invoke_static(thisType, SET_THREAD_CALLBACKS);
	e.new_instance(thisType);
	e.dup();
	e.load_arg(0);
	EmitUtils.constructor_switch(e, constructors, new ObjectSwitchCallback() {
		public void processCase(Object key, Label end) {
			MethodInfo constructor = (MethodInfo) key;
			Type types[] = constructor.getSignature().getArgumentTypes();
			for (int i = 0; i < types.length; i++) {
				e.load_arg(1);
				e.push(i);
				e.aaload();
				e.unbox(types[i]);
			}
			e.invoke_constructor(thisType, constructor.getSignature());
			e.goTo(end);
		}

		public void processDefault() {
			e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Constructor not found");
		}
	});
	e.aconst_null();
	e.invoke_static(thisType, SET_THREAD_CALLBACKS);
	e.return_value();
	e.end_method();
}
 
Example #17
Source File: ConfigurationClassEnhancer.java    From spring-analysis-note 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 #18
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 #19
Source File: Enhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void emitSetCallbacks(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, SET_CALLBACKS, null);
	e.load_this();
	e.load_arg(0);
	for (int i = 0; i < callbackTypes.length; i++) {
		e.dup2();
		e.aaload(i);
		e.checkcast(callbackTypes[i]);
		e.putfield(getCallbackField(i));
	}
	e.return_value();
	e.end_method();
}
 
Example #20
Source File: Enhancer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
protected Object nextInstance(Object instance) {
	EnhancerFactoryData data = (EnhancerFactoryData) instance;

	if (classOnly) {
		return data.generatedClass;
	}

	Class[] argumentTypes = this.argumentTypes;
	Object[] arguments = this.arguments;
	if (argumentTypes == null) {
		argumentTypes = Constants.EMPTY_CLASS_ARRAY;
		arguments = null;
	}
	return data.newInstance(argumentTypes, arguments, callbacks);
}
 
Example #21
Source File: Enhancer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void emitSetCallbacks(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, SET_CALLBACKS, null);
	e.load_this();
	e.load_arg(0);
	for (int i = 0; i < callbackTypes.length; i++) {
		e.dup2();
		e.aaload(i);
		e.checkcast(callbackTypes[i]);
		e.putfield(getCallbackField(i));
	}
	e.return_value();
	e.end_method();
}
 
Example #22
Source File: Enhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void emitConstructors(ClassEmitter ce, List constructors) {
	boolean seenNull = false;
	for (Iterator it = constructors.iterator(); it.hasNext(); ) {
		MethodInfo constructor = (MethodInfo) it.next();
		if (currentData != null && !"()V".equals(constructor.getSignature().getDescriptor())) {
			continue;
		}
		CodeEmitter e = EmitUtils.begin_method(ce, constructor, Constants.ACC_PUBLIC);
		e.load_this();
		e.dup();
		e.load_args();
		Signature sig = constructor.getSignature();
		seenNull = seenNull || sig.getDescriptor().equals("()V");
		e.super_invoke_constructor(sig);
		if (currentData == null) {
			e.invoke_static_this(BIND_CALLBACKS);
			if (!interceptDuringConstruction) {
				e.load_this();
				e.push(1);
				e.putfield(CONSTRUCTED_FIELD);
			}
		}
		e.return_value();
		e.end_method();
	}
	if (!classOnly && !seenNull && arguments == null)
		throw new IllegalArgumentException("Superclass has no null constructors but no arguments were given");
}
 
Example #23
Source File: Enhancer.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected Object nextInstance(Object instance) {
	EnhancerFactoryData data = (EnhancerFactoryData) instance;

	if (classOnly) {
		return data.generatedClass;
	}

	Class[] argumentTypes = this.argumentTypes;
	Object[] arguments = this.arguments;
	if (argumentTypes == null) {
		argumentTypes = Constants.EMPTY_CLASS_ARRAY;
		arguments = null;
	}
	return data.newInstance(argumentTypes, arguments, callbacks);
}
 
Example #24
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 #25
Source File: Enhancer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void emitSetStaticCallbacks(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_STATIC,
			SET_STATIC_CALLBACKS,
			null);
	e.load_arg(0);
	e.putfield(STATIC_CALLBACKS_FIELD);
	e.return_value();
	e.end_method();
}
 
Example #26
Source File: Enhancer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void emitSetThreadCallbacks(ClassEmitter ce) {
	CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC | Constants.ACC_STATIC,
			SET_THREAD_CALLBACKS,
			null);
	e.getfield(THREAD_CALLBACKS_FIELD);
	e.load_arg(0);
	e.invoke_virtual(THREAD_LOCAL, THREAD_LOCAL_SET);
	e.return_value();
	e.end_method();
}
 
Example #27
Source File: Enhancer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void emitNewInstanceMultiarg(ClassEmitter ce, List constructors) {
	final CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, MULTIARG_NEW_INSTANCE, null);
	final Type thisType = getThisType(e);
	e.load_arg(2);
	e.invoke_static(thisType, SET_THREAD_CALLBACKS);
	e.new_instance(thisType);
	e.dup();
	e.load_arg(0);
	EmitUtils.constructor_switch(e, constructors, new ObjectSwitchCallback() {
		public void processCase(Object key, Label end) {
			MethodInfo constructor = (MethodInfo) key;
			Type types[] = constructor.getSignature().getArgumentTypes();
			for (int i = 0; i < types.length; i++) {
				e.load_arg(1);
				e.push(i);
				e.aaload();
				e.unbox(types[i]);
			}
			e.invoke_constructor(thisType, constructor.getSignature());
			e.goTo(end);
		}

		public void processDefault() {
			e.throw_exception(ILLEGAL_ARGUMENT_EXCEPTION, "Constructor not found");
		}
	});
	e.aconst_null();
	e.invoke_static(thisType, SET_THREAD_CALLBACKS);
	e.return_value();
	e.end_method();
}
 
Example #28
Source File: Enhancer.java    From spring-analysis-note 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 #29
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 #30
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();
}