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();
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations