Search in sources :

Example 31 with CtMethod

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();
    }
}
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 32 with CtMethod

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();
    }
}
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 33 with CtMethod

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());
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) FrameworkMethod(org.junit.runners.model.FrameworkMethod) CtMethod(javassist.CtMethod)

Example 34 with CtMethod

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

Example 35 with CtMethod

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

Aggregations

CtMethod (javassist.CtMethod)76 CtClass (javassist.CtClass)46 NotFoundException (javassist.NotFoundException)23 CannotCompileException (javassist.CannotCompileException)20 ClassPool (javassist.ClassPool)18 CtField (javassist.CtField)14 Test (org.junit.Test)12 IOException (java.io.IOException)10 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)6 Method (java.lang.reflect.Method)5 CtConstructor (javassist.CtConstructor)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2