Search in sources :

Example 51 with NotFoundException

use of javassist.NotFoundException 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 52 with NotFoundException

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

the class GenerateClassesForMLContext method recurseDirectoriesForConvenienceClassGeneration.

/**
 * Generate convenience classes recursively. This allows for code such as
 * {@code ml.scripts.algorithms...}.
 *
 * @param dirPath
 *            path to directory
 * @return the full name of the class representing the dirPath directory
 */
public static String recurseDirectoriesForConvenienceClassGeneration(String dirPath) {
    try {
        File dir = new File(dirPath);
        String fullDirClassName = dirPathToFullDirClassName(dirPath);
        System.out.println("Generating Class: " + fullDirClassName);
        ClassPool pool = ClassPool.getDefault();
        CtClass ctDir = pool.makeClass(fullDirClassName);
        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 = recurseDirectoriesForConvenienceClassGeneration(subDirPath);
            CtClass subDirClass = pool.get(fullSubDirClassName);
            String subDirName = subdir.getName();
            subDirName = subDirName.replaceAll("-", "_");
            subDirName = subDirName.toLowerCase();
            System.out.println("Adding " + subDirName + "() to " + fullDirClassName);
            String methodBody = "{ " + fullSubDirClassName + " z = new " + fullSubDirClassName + "(); return z; }";
            CtMethod ctMethod = CtNewMethod.make(Modifier.PUBLIC, subDirClass, subDirName, null, null, methodBody, ctDir);
            ctDir.addMethod(ctMethod);
        }
        File[] scriptFiles = dir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return (name.toLowerCase().endsWith(".dml") || name.toLowerCase().endsWith(".pydml"));
            }
        });
        for (File scriptFile : scriptFiles) {
            String scriptFilePath = scriptFile.getPath();
            String fullScriptClassName = BASE_DEST_PACKAGE + "." + scriptFilePathToFullClassNameNoBase(scriptFilePath);
            CtClass scriptClass = pool.get(fullScriptClassName);
            String methodName = scriptFilePathToSimpleClassName(scriptFilePath);
            String methodBody = "{ " + fullScriptClassName + " z = new " + fullScriptClassName + "(); return z; }";
            CtMethod ctMethod = CtNewMethod.make(Modifier.PUBLIC, scriptClass, methodName, null, null, methodBody, ctDir);
            ctDir.addMethod(ctMethod);
        }
        ctDir.writeFile(destination);
        return fullDirClassName;
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (CannotCompileException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtClass(javassist.CtClass) FilenameFilter(java.io.FilenameFilter) FileFilter(java.io.FileFilter) File(java.io.File) CtMethod(javassist.CtMethod)

Example 53 with NotFoundException

use of javassist.NotFoundException in project leopard by tanhaichao.

the class CtClassUtil method getParameterNames.

public static String[] getParameterNames(Class<?> clazz, Method method) {
    CtMethod ctMethod;
    try {
        ctMethod = CtClassUtil.getMethod(clazz, method);
    } catch (NotFoundException e) {
        Throwable error = e.getCause();
        if (error instanceof RuntimeException) {
            throw (RuntimeException) error;
        }
        if (error instanceof Exception) {
            throw new RuntimeException(error.getMessage(), error);
        }
        throw new RuntimeException(e.getMessage(), e);
    }
    String[] names;
    try {
        names = getParameterNames(ctMethod);
    } catch (NotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    // System.err.println("getParameterNames methodName:" + method.toGenericString() + " names:" + StringUtils.join(names, ", "));
    return names;
}
Also used : NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod) NotFoundException(javassist.NotFoundException)

Example 54 with NotFoundException

use of javassist.NotFoundException in project leopard by tanhaichao.

the class CtClassUtil method getParameterNames.

public static String[] getParameterNames(Class<?> clazz, Method method) {
    CtMethod ctMethod;
    try {
        ctMethod = CtClassUtil.getMethod(clazz, method);
    } catch (NotFoundException e) {
        Throwable error = e.getCause();
        if (error instanceof RuntimeException) {
            throw (RuntimeException) error;
        }
        if (error instanceof Exception) {
            throw new RuntimeException(error.getMessage(), error);
        }
        throw new RuntimeException(e.getMessage(), e);
    }
    String[] names;
    try {
        names = getParameterNames(ctMethod);
    } catch (NotFoundException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    // System.err.println("getParameterNames methodName:" + method.toGenericString() + " names:" + StringUtils.join(names, ", "));
    return names;
}
Also used : NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod) NotFoundException(javassist.NotFoundException)

Example 55 with NotFoundException

use of javassist.NotFoundException in project duangframework by tcrct.

the class AutoBuildServiceInterface method getLocalVariableAttributeName.

/**
 * 反射取出方法里的参数名
 * @param cc                类对象
 * @param method        方法名
 * @return      方法名集合
 * @throws Exception
 */
private static List<String> getLocalVariableAttributeName(CtClass cc, Method method) throws Exception {
    List<String> paramNames = null;
    try {
        CtMethod cm = cc.getDeclaredMethod(method.getName());
        MethodInfo methodInfo = cm.getMethodInfo();
        CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
        LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
        if (attr != null) {
            int size = cm.getParameterTypes().length;
            paramNames = new ArrayList<>(size);
            int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
            for (int i = 0; i < size; i++) {
                paramNames.add(attr.variableName(i + pos));
            }
        }
    } catch (NotFoundException e) {
        throw new RpcException(e.getMessage(), e);
    }
    return paramNames;
}
Also used : CodeAttribute(javassist.bytecode.CodeAttribute) RpcException(com.duangframework.core.exceptions.RpcException) NotFoundException(javassist.NotFoundException) MethodInfo(javassist.bytecode.MethodInfo) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute) CtMethod(javassist.CtMethod)

Aggregations

NotFoundException (javassist.NotFoundException)88 CtClass (javassist.CtClass)64 CannotCompileException (javassist.CannotCompileException)42 CtMethod (javassist.CtMethod)36 ClassPool (javassist.ClassPool)32 IOException (java.io.IOException)19 CtField (javassist.CtField)16 FileNotFoundException (java.io.FileNotFoundException)9 CtConstructor (javassist.CtConstructor)9 File (java.io.File)7 Method (java.lang.reflect.Method)7 ClassFile (javassist.bytecode.ClassFile)7 ArrayList (java.util.ArrayList)6 Collectors (java.util.stream.Collectors)6 BadBytecode (javassist.bytecode.BadBytecode)6 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 MethodUsage (com.github.javaparser.resolution.MethodUsage)4 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)4 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)4 Context (com.github.javaparser.symbolsolver.core.resolution.Context)4