package org.gap.jseed;
import java.util.Random;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
/**
* Responsible for instantiating a unique new subclass based
* on a given name and the parent class provided.
*
* @author gpelcha
*/
class SubClassFactory {
private ClassPool pool;
public SubClassFactory(ClassPool pool) {
this.pool = pool;
}
public <T> CtClass createSubClass(String name, CtClass template) throws CannotCompileException, SecurityException {
CtClass newClass = pool.makeClass(safeName(name + uniqueId()));
if (template.isInterface()) {
newClass.addInterface(template);
} else {
newClass.setSuperclass(template);
}
return newClass;
}
private String safeName(String string) {
return string.replace('$', '_').replace('-', '_');
}
public String uniqueId() {
return Long.toString(new Random().nextInt(1000));
}
}