use of javassist.ClassPool in project yyl_example by Relucent.
the class CreateSimpleBeanTest method create.
@SuppressWarnings("unchecked")
public static Class<? extends SimpleBean> create(String className, String... fields) throws CannotCompileException, NotFoundException {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass(className);
cc.setInterfaces(new CtClass[] { pool.get(SimpleBean.class.getName()) });
CtClass stringctClass = pool.get("java.lang.String");
{
for (String field : fields) {
cc.addField(new CtField(stringctClass, field, cc));
}
}
{
CtMethod setCtMethod = new CtMethod(CtClass.voidType, "set", new CtClass[] { stringctClass, stringctClass }, cc);
setCtMethod.setModifiers(Modifier.PUBLIC);
StringBuilder vSetBody = new StringBuilder();
vSetBody.append("if(null == $1){ throw new NullPointerException(\" key can not be NULL \"); }");
for (String field : fields) {
vSetBody.append("else if(\"").append(field).append("\".equals($1)){ $0.").append(field).append("=$2; }");
}
vSetBody.append("else { throw new IllegalArgumentException(\"Object does not exist in the \" + $1 + \" field\"); }");
setCtMethod.setBody(vSetBody.toString());
cc.addMethod(setCtMethod);
}
{
CtMethod getCtMethod = new CtMethod(stringctClass, "get", new CtClass[] { stringctClass }, cc);
getCtMethod.setModifiers(Modifier.PUBLIC);
StringBuilder vGetBody = new StringBuilder();
vGetBody.append("if(null == $1){ throw new NullPointerException(\" key can not be NULL \"); }");
for (String field : fields) {
vGetBody.append("else if(\"").append(field).append("\".equals($1)){ return $0.").append(field).append("; }");
}
vGetBody.append("else { throw new IllegalArgumentException(\"Object does not exist in the \" + $1 + \" field\"); }");
getCtMethod.setBody(vGetBody.toString());
cc.addMethod(getCtMethod);
}
{
CtConstructor cons = new CtConstructor(new CtClass[] {}, cc);
cons.setBody("{}");
cc.addConstructor(cons);
}
cc.detach();
return cc.toClass();
}
use of javassist.ClassPool in project incubator-systemml by apache.
the class GenerateClassesForMLContext method addPackageConvenienceMethodsToMLContext.
/**
* Add methods to MLContext to allow tab-completion to packages contained
* within the source directory (such as {@code ml.nn()}).
*
* @param dirPath
* path to source directory (typically, the scripts directory)
* @param ctMLContext
* javassist compile-time class representation of MLContext
*/
public static void addPackageConvenienceMethodsToMLContext(String dirPath, CtClass ctMLContext) {
try {
if (!SOURCE.equalsIgnoreCase(dirPath)) {
return;
}
File dir = new File(dirPath);
File[] subdirs = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory();
}
});
for (File subdir : subdirs) {
String subDirPath = dirPath + File.separator + subdir.getName();
if (skipDir(subdir, false)) {
continue;
}
String fullSubDirClassName = dirPathToFullDirClassName(subDirPath);
ClassPool pool = ClassPool.getDefault();
CtClass subDirClass = pool.get(fullSubDirClassName);
String subDirName = subdir.getName();
subDirName = subDirName.replaceAll("-", "_");
subDirName = subDirName.toLowerCase();
System.out.println("Adding " + subDirName + "() to " + ctMLContext.getName());
String methodBody = "{ " + fullSubDirClassName + " z = new " + fullSubDirClassName + "(); return z; }";
CtMethod ctMethod = CtNewMethod.make(Modifier.PUBLIC, subDirClass, subDirName, null, null, methodBody, ctMLContext);
ctMLContext.addMethod(ctMethod);
}
} catch (NotFoundException e) {
e.printStackTrace();
} catch (CannotCompileException e) {
e.printStackTrace();
}
}
use of javassist.ClassPool in project incubator-systemml by apache.
the class GenerateClassesForMLContext method makeCtClasses.
/**
* Create compile-time classes required for later class generation.
*/
public static void makeCtClasses() {
try {
ClassPool pool = ClassPool.getDefault();
pool.makeClass(new FileInputStream(new File(destination + File.separator + PATH_TO_MLCONTEXT_CLASS)));
pool.makeClass(new FileInputStream(new File(destination + File.separator + PATH_TO_MLRESULTS_CLASS)));
pool.makeClass(new FileInputStream(new File(destination + File.separator + PATH_TO_SCRIPT_CLASS)));
pool.makeClass(new FileInputStream(new File(destination + File.separator + PATH_TO_SCRIPTTYPE_CLASS)));
pool.makeClass(new FileInputStream(new File(destination + File.separator + PATH_TO_MATRIX_CLASS)));
pool.makeClass(new FileInputStream(new File(destination + File.separator + PATH_TO_FRAME_CLASS)));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
}
}
use of javassist.ClassPool in project incubator-systemml by apache.
the class GenerateClassesForMLContext method addConvenienceMethodsToMLContext.
/**
* Add methods to MLContext to allow tab-completion to folders/packages
* (such as {@code ml.scripts()} and {@code ml.nn()}).
*
* @param source
* path to source directory (typically, the scripts directory)
* @param fullDirClassName
* the full name of the class representing the source (scripts)
* directory
*/
public static void addConvenienceMethodsToMLContext(String source, String fullDirClassName) {
try {
ClassPool pool = ClassPool.getDefault();
CtClass ctMLContext = pool.get(MLContext.class.getName());
CtClass dirClass = pool.get(fullDirClassName);
String methodName = convertFullClassNameToConvenienceMethodName(fullDirClassName);
System.out.println("Adding " + methodName + "() to " + ctMLContext.getName());
String methodBody = "{ " + fullDirClassName + " z = new " + fullDirClassName + "(); return z; }";
CtMethod ctMethod = CtNewMethod.make(Modifier.PUBLIC, dirClass, methodName, null, null, methodBody, ctMLContext);
ctMLContext.addMethod(ctMethod);
addPackageConvenienceMethodsToMLContext(source, ctMLContext);
ctMLContext.writeFile(destination);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (CannotCompileException e) {
e.printStackTrace();
}
}
use of javassist.ClassPool in project hibernate-orm by hibernate.
the class EnhancerTestUtils method generateCtClassForAnEntity.
private static CtClass generateCtClassForAnEntity(Class<?> entityClassToEnhance) throws Exception {
ClassPool cp = new ClassPool(false);
ClassLoader cl = EnhancerTestUtils.class.getClassLoader();
return cp.makeClass(cl.getResourceAsStream(entityClassToEnhance.getName().replace('.', '/') + ".class"));
}
Aggregations