Search in sources :

Example 11 with CannotCompileException

use of javassist.CannotCompileException 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 12 with CannotCompileException

use of javassist.CannotCompileException in project ignite by apache.

the class GridActivationCacheAbstractTestSuit method transform.

/**
 * @param c Class to transform.
 * @return Transformed class.
 */
private static Class transform(Class c) {
    try {
        ClassPool pool = ClassPool.getDefault();
        pool.insertClassPath(new ClassClassPath(GridActivationCacheAbstractTestSuit.class));
        String path = c.getProtectionDomain().getCodeSource().getLocation().getPath();
        CtClass ct = pool.get(c.getName());
        String name = c.getName() + SUFFIX;
        CtClass pr = pool.get(GridActivateExtensionTest.class.getName());
        pr.setName(name);
        pr.setSuperclass(ct);
        pr.writeFile(path);
        return Class.forName(name);
    } catch (IOException e) {
        System.out.println("Io exception: " + e.getMessage());
        throw new IgniteException(e);
    } catch (CannotCompileException e) {
        System.out.println("Cannot compile exception: " + e.getMessage());
        throw new IgniteException(e);
    } catch (NotFoundException e) {
        System.out.println("Not found exception: " + e.getMessage());
        throw new IgniteException(e);
    } catch (ClassNotFoundException e) {
        System.out.println("Class not found exception: " + e.getMessage());
        throw new IgniteException(e);
    }
}
Also used : CtClass(javassist.CtClass) IgniteException(org.apache.ignite.IgniteException) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) ClassClassPath(javassist.ClassClassPath)

Example 13 with CannotCompileException

use of javassist.CannotCompileException in project restfulie-java by caelum.

the class DefaultEnhancer method enhanceResource.

public <T> Class enhanceResource(Class<T> originalType) {
    ClassPool pool = ClassPool.getDefault();
    if (pool.find(DefaultEnhancer.class.getName()) == null) {
        pool.appendClassPath(new LoaderClassPath(getClass().getClassLoader()));
    }
    try {
        // TODO extract this enhancement to an interface and test it appart
        CtClass newType = pool.makeClass(generateNewUniqueClassName(originalType));
        newType.setSuperclass(pool.get(originalType.getName()));
        newType.addInterface(pool.get(Resource.class.getName()));
        enhanceLinks(newType);
        return newType.toClass();
    } catch (NotFoundException e) {
        throw new IllegalStateException("Unable to extend type " + originalType.getName(), e);
    } catch (CannotCompileException e) {
        throw new IllegalStateException("Unable to extend type " + originalType.getName(), e);
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException)

Example 14 with CannotCompileException

use of javassist.CannotCompileException in project incubator-systemml by apache.

the class GenerateClassesForMLContext method createScriptClass.

/**
 * Convert a script file to a Java class that extends the MLContext API's
 * Script class.
 *
 * @param scriptFilePath
 *            the path to a script file
 */
public static void createScriptClass(String scriptFilePath) {
    try {
        String fullScriptClassName = BASE_DEST_PACKAGE + "." + scriptFilePathToFullClassNameNoBase(scriptFilePath);
        System.out.println("Generating Class: " + fullScriptClassName);
        ClassPool pool = ClassPool.getDefault();
        CtClass ctNewScript = pool.makeClass(fullScriptClassName);
        CtClass ctScript = pool.get(Script.class.getName());
        ctNewScript.setSuperclass(ctScript);
        CtConstructor ctCon = new CtConstructor(null, ctNewScript);
        ctCon.setBody(scriptConstructorBody(scriptFilePath));
        ctNewScript.addConstructor(ctCon);
        addFunctionMethods(scriptFilePath, ctNewScript);
        ctNewScript.writeFile(destination);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (CannotCompileException e) {
        e.printStackTrace();
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
}
Also used : CtClass(javassist.CtClass) Script(org.apache.sysml.api.mlcontext.Script) DMLScript(org.apache.sysml.api.DMLScript) ClassPool(javassist.ClassPool) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) CtConstructor(javassist.CtConstructor)

Example 15 with CannotCompileException

use of javassist.CannotCompileException in project incubator-systemml by apache.

the class GenerateClassesForMLContext method createFunctionOutputClass.

/**
 * Create a class that encapsulates the outputs of a function.
 *
 * @param scriptFilePath
 *            the path to a script file
 * @param fs
 *            a SystemML function statement
 */
public static void createFunctionOutputClass(String scriptFilePath, FunctionStatement fs) {
    try {
        ArrayList<DataIdentifier> oparams = fs.getOutputParams();
        // than encapsulating it in a function output class
        if ((oparams.size() == 0) || (oparams.size() == 1)) {
            return;
        }
        String fullFunctionOutputClassName = getFullFunctionOutputClassName(scriptFilePath, fs);
        System.out.println("Generating Class: " + fullFunctionOutputClassName);
        ClassPool pool = ClassPool.getDefault();
        CtClass ctFuncOut = pool.makeClass(fullFunctionOutputClassName);
        // add fields
        for (int i = 0; i < oparams.size(); i++) {
            DataIdentifier oparam = oparams.get(i);
            String type = getParamTypeAsString(oparam);
            String name = oparam.getName();
            String fstring = "public " + type + " " + name + ";";
            CtField field = CtField.make(fstring, ctFuncOut);
            ctFuncOut.addField(field);
        }
        // add constructor
        String simpleFuncOutClassName = fullFunctionOutputClassName.substring(fullFunctionOutputClassName.lastIndexOf(".") + 1);
        StringBuilder con = new StringBuilder();
        con.append("public " + simpleFuncOutClassName + "(");
        for (int i = 0; i < oparams.size(); i++) {
            if (i > 0) {
                con.append(", ");
            }
            DataIdentifier oparam = oparams.get(i);
            String type = getParamTypeAsString(oparam);
            String name = oparam.getName();
            con.append(type + " " + name);
        }
        con.append(") {\n");
        for (int i = 0; i < oparams.size(); i++) {
            DataIdentifier oparam = oparams.get(i);
            String name = oparam.getName();
            con.append("this." + name + "=" + name + ";\n");
        }
        con.append("}\n");
        String cstring = con.toString();
        CtConstructor ctCon = CtNewConstructor.make(cstring, ctFuncOut);
        ctFuncOut.addConstructor(ctCon);
        // add toString
        StringBuilder s = new StringBuilder();
        s.append("public String toString(){\n");
        s.append("StringBuilder sb = new StringBuilder();\n");
        for (int i = 0; i < oparams.size(); i++) {
            DataIdentifier oparam = oparams.get(i);
            String name = oparam.getName();
            s.append("sb.append(\"" + name + " (" + getSimpleParamTypeAsString(oparam) + "): \" + " + name + " + \"\\n\");\n");
        }
        s.append("String str = sb.toString();\nreturn str;\n");
        s.append("}\n");
        String toStr = s.toString();
        CtMethod toStrMethod = CtNewMethod.make(toStr, ctFuncOut);
        ctFuncOut.addMethod(toStrMethod);
        ctFuncOut.writeFile(destination);
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (CannotCompileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) ClassPool(javassist.ClassPool) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) CtField(javassist.CtField) CtMethod(javassist.CtMethod)

Aggregations

CannotCompileException (javassist.CannotCompileException)29 NotFoundException (javassist.NotFoundException)20 CtClass (javassist.CtClass)18 CtMethod (javassist.CtMethod)16 ClassPool (javassist.ClassPool)10 IOException (java.io.IOException)8 CtField (javassist.CtField)8 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 FileNotFoundException (java.io.FileNotFoundException)4 File (java.io.File)3 CtConstructor (javassist.CtConstructor)3 BadBytecode (javassist.bytecode.BadBytecode)3 Bytecode (javassist.bytecode.Bytecode)3 CodeAttribute (javassist.bytecode.CodeAttribute)3 CodeIterator (javassist.bytecode.CodeIterator)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3 FileFilter (java.io.FileFilter)2 AsyncProvider (com.google.gwt.inject.client.AsyncProvider)1 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1