Package org.gap.jseed

Source Code of org.gap.jseed.SubClassFactory

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));
  }
}
TOP

Related Classes of org.gap.jseed.SubClassFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.