All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pm.pride.ObjectFactory Maven / Gradle / Ivy

There is a newer version: 3.4.10
Show newest version
package pm.pride;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class ObjectFactory {
	Method cloneMethod;
	Constructor defaultConstructor;
	Constructor copyConstructor;

	public ObjectFactory(Class objectType) {
		try {
			cloneMethod = objectType.getMethod("clone");
		}
		catch(NoSuchMethodException nsmx1) {
			try {
				defaultConstructor = objectType.getConstructor();
			}
			catch(NoSuchMethodException nsmx2) {
				try {
					copyConstructor = objectType.getConstructor(objectType);
				}
				catch(NoSuchMethodException nsmx3) {
					// There is no way to duplicate objects of that type.
					// Well, this is no reason to report a problem
					// Maybe there is no need to create any duplicates at all
				}
			}
		}
	}

	public Object instanciate(Object referenceObject) throws ReflectiveOperationException {
		if (cloneMethod != null) {
			return cloneMethod.invoke(referenceObject);
		}
		if (defaultConstructor != null) {
			return defaultConstructor.newInstance();
		}
		if (copyConstructor != null) {
			return copyConstructor.newInstance(referenceObject);
		}
		throw new NoSuchMethodException();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy