Search in sources :

Example 21 with ClassPool

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();
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod) CtConstructor(javassist.CtConstructor)

Example 22 with ClassPool

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();
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) CannotCompileException(javassist.CannotCompileException) FileFilter(java.io.FileFilter) File(java.io.File) CtMethod(javassist.CtMethod)

Example 23 with ClassPool

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();
    }
}
Also used : ClassPool(javassist.ClassPool) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 24 with ClassPool

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();
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) MLContext(org.apache.sysml.api.mlcontext.MLContext) CtMethod(javassist.CtMethod)

Example 25 with ClassPool

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"));
}
Also used : ClassPool(javassist.ClassPool)

Aggregations

ClassPool (javassist.ClassPool)44 CtClass (javassist.CtClass)32 CtMethod (javassist.CtMethod)16 NotFoundException (javassist.NotFoundException)15 CannotCompileException (javassist.CannotCompileException)10 LoaderClassPath (javassist.LoaderClassPath)9 CtField (javassist.CtField)8 IOException (java.io.IOException)7 Test (org.junit.Test)7 File (java.io.File)6 CtConstructor (javassist.CtConstructor)6 FileNotFoundException (java.io.FileNotFoundException)5 MethodInfo (javassist.bytecode.MethodInfo)4 Method (java.lang.reflect.Method)3 ConstPool (javassist.bytecode.ConstPool)3 DirectoryInput (com.android.build.api.transform.DirectoryInput)2 JarInput (com.android.build.api.transform.JarInput)2 InjectParam (com.taobao.android.builder.tools.classinject.InjectParam)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2