use of javassist.CtMethod 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.CtMethod 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.CtMethod in project java-crud-api by kolchagov.
the class OrderedTestRunner method computeTestMethods.
/*
* (non-Javadoc)
*
* @see org.junit.runners.BlockJUnit4ClassRunner#computeTestMethods()
*/
@Override
protected List<FrameworkMethod> computeTestMethods() {
// get all methods to be tested
List<FrameworkMethod> toSort = super.computeTestMethods();
if (toSort.isEmpty())
return toSort;
// a map containing <line_number, method>
final Map<Integer, FrameworkMethod> testMethods = new TreeMap<>();
// check that all methods here are declared in the same class, we don't
// deal with test methods from superclasses that haven't been overridden
Class<?> clazz = getDeclaringClass(toSort);
if (clazz == null) {
// fail explicitly
System.err.println("OrderedTestRunner can only run test classes that" + " don't have test methods inherited from superclasses");
return Collections.emptyList();
}
// use Javassist to figure out line numbers for methods
ClassPool pool = ClassPool.getDefault();
try {
CtClass cc = pool.get(clazz.getName());
// all methods in toSort are declared in the same class, we checked
for (FrameworkMethod m : toSort) {
String methodName = m.getName();
CtMethod method = cc.getDeclaredMethod(methodName);
testMethods.put(method.getMethodInfo().getLineNumber(0), m);
}
} catch (NotFoundException e) {
e.printStackTrace();
}
return new ArrayList<>(testMethods.values());
}
use of javassist.CtMethod in project jPOS by jpos.
the class Slf4JDynamicBinder method applyMods.
public static void applyMods() throws Exception {
if (!bound && !bindingsExist()) {
final ProtectionDomain pd = Slf4JDynamicBinder.class.getProtectionDomain();
final ClassPool cp = ClassPool.getDefault();
CtClass clz = cp.getAndRename(StaticLoggerBinder.class.getName(), STATIC_BINDER_CLASS);
clz.toClass(null, pd);
CtClass clz2 = cp.get("org.slf4j.LoggerFactory");
CtMethod bindMethod = clz2.getDeclaredMethod("bind");
bindMethod.setBody("try " + "{" + " org.slf4j.impl.StaticLoggerBinder.getSingleton();" + " INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;" + " fixSubstituteLoggers();" + " replayEvents();" + " SUBST_FACTORY.clear();" + "} " + "catch(Exception e)" + "{" + " failedBinding(e); " + " throw new IllegalStateException(\"Unexpected initialization failure\", e);" + "}");
clz2.toClass(null, pd);
clz2.detach();
clz.detach();
bound = true;
}
}
use of javassist.CtMethod in project leopard by tanhaichao.
the class CtClassUtil method getMethod.
public static CtMethod getMethod(Class<?> clazz, Method method) throws NotFoundException {
CtClass ctClass = CtClassUtil.getClass(clazz);
Class<?>[] types = method.getParameterTypes();
CtMethod ctMethod;
if (types == null) {
ctMethod = ctClass.getDeclaredMethod(method.getName());
} else {
CtClass[] params = new CtClass[types.length];
for (int i = 0; i < params.length; i++) {
params[i] = CtClassUtil.getClass(types[i]);
}
ctMethod = ctClass.getDeclaredMethod(method.getName(), params);
}
return ctMethod;
}
Aggregations