Search in sources :

Example 1 with LoaderClassPath

use of javassist.LoaderClassPath in project dubbo by alibaba.

the class JavassistCompiler method doCompile.

@Override
public Class<?> doCompile(String name, String source) throws Throwable {
    int i = name.lastIndexOf('.');
    String className = i < 0 ? name : name.substring(i + 1);
    ClassPool pool = new ClassPool(true);
    pool.appendClassPath(new LoaderClassPath(ClassHelper.getCallerClassLoader(getClass())));
    Matcher matcher = IMPORT_PATTERN.matcher(source);
    List<String> importPackages = new ArrayList<String>();
    Map<String, String> fullNames = new HashMap<String, String>();
    while (matcher.find()) {
        String pkg = matcher.group(1);
        if (pkg.endsWith(".*")) {
            String pkgName = pkg.substring(0, pkg.length() - 2);
            pool.importPackage(pkgName);
            importPackages.add(pkgName);
        } else {
            int pi = pkg.lastIndexOf('.');
            if (pi > 0) {
                String pkgName = pkg.substring(0, pi);
                pool.importPackage(pkgName);
                importPackages.add(pkgName);
                fullNames.put(pkg.substring(pi + 1), pkg);
            }
        }
    }
    String[] packages = importPackages.toArray(new String[0]);
    matcher = EXTENDS_PATTERN.matcher(source);
    CtClass cls;
    if (matcher.find()) {
        String extend = matcher.group(1).trim();
        String extendClass;
        if (extend.contains(".")) {
            extendClass = extend;
        } else if (fullNames.containsKey(extend)) {
            extendClass = fullNames.get(extend);
        } else {
            extendClass = ClassUtils.forName(packages, extend).getName();
        }
        cls = pool.makeClass(name, pool.get(extendClass));
    } else {
        cls = pool.makeClass(name);
    }
    matcher = IMPLEMENTS_PATTERN.matcher(source);
    if (matcher.find()) {
        String[] ifaces = matcher.group(1).trim().split("\\,");
        for (String iface : ifaces) {
            iface = iface.trim();
            String ifaceClass;
            if (iface.contains(".")) {
                ifaceClass = iface;
            } else if (fullNames.containsKey(iface)) {
                ifaceClass = fullNames.get(iface);
            } else {
                ifaceClass = ClassUtils.forName(packages, iface).getName();
            }
            cls.addInterface(pool.get(ifaceClass));
        }
    }
    String body = source.substring(source.indexOf("{") + 1, source.length() - 1);
    String[] methods = METHODS_PATTERN.split(body);
    for (String method : methods) {
        method = method.trim();
        if (method.length() > 0) {
            if (method.startsWith(className)) {
                cls.addConstructor(CtNewConstructor.make("public " + method, cls));
            } else if (FIELD_PATTERN.matcher(method).matches()) {
                cls.addField(CtField.make("private " + method, cls));
            } else {
                cls.addMethod(CtNewMethod.make("public " + method, cls));
            }
        }
    }
    return cls.toClass(ClassHelper.getCallerClassLoader(getClass()), JavassistCompiler.class.getProtectionDomain());
}
Also used : CtClass(javassist.CtClass) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) ClassPool(javassist.ClassPool) ArrayList(java.util.ArrayList) LoaderClassPath(javassist.LoaderClassPath)

Example 2 with LoaderClassPath

use of javassist.LoaderClassPath in project hibernate-orm by hibernate.

the class EnhancerTestUtils method enhanceAndDecompile.

/**
	 * method that performs the enhancement of a class
	 * also checks the signature of enhanced entities methods using 'javap' decompiler
	 */
public static Class<?> enhanceAndDecompile(Class<?> classToEnhance, ClassLoader cl) throws Exception {
    CtClass entityCtClass = generateCtClassForAnEntity(classToEnhance);
    byte[] original = entityCtClass.toBytecode();
    byte[] enhanced = Environment.getBytecodeProvider().getEnhancer(new EnhancerTestContext()).enhance(entityCtClass.getName(), original);
    assertFalse("entity was not enhanced", enhanced == null);
    log.infof("enhanced entity [%s]", entityCtClass.getName());
    ClassPool cp = new ClassPool(false);
    cp.appendClassPath(new LoaderClassPath(cl));
    CtClass enhancedCtClass = cp.makeClass(new ByteArrayInputStream(enhanced));
    enhancedCtClass.debugWriteFile(workingDir);
    DecompileUtils.decompileDumpedClass(workingDir, classToEnhance.getName());
    Class<?> enhancedClass = enhancedCtClass.toClass(cl, EnhancerTestUtils.class.getProtectionDomain());
    assertNotNull(enhancedClass);
    return enhancedClass;
}
Also used : CtClass(javassist.CtClass) ByteArrayInputStream(java.io.ByteArrayInputStream) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath)

Example 3 with LoaderClassPath

use of javassist.LoaderClassPath in project pinpoint by naver.

the class HierarchyMultipleClassPool method createClassPool.

private NamedClassPool createClassPool(ClassLoader classLoader, NamedClassPool parentClassPool) {
    String classLoaderName = classLoader.getClass().getName();
    NamedClassPool newClassPool = new NamedClassPool(parentClassPool, classLoaderName + "-" + ID.getAndIncrement());
    newClassPool.childFirstLookup = true;
    final ClassPath classPath = new LoaderClassPath(classLoader);
    newClassPool.appendClassPath(classPath);
    return newClassPool;
}
Also used : LoaderClassPath(javassist.LoaderClassPath) ClassPath(javassist.ClassPath) LoaderClassPath(javassist.LoaderClassPath)

Example 4 with LoaderClassPath

use of javassist.LoaderClassPath in project pinpoint by naver.

the class SingleClassPool method getClassPool.

@Override
public NamedClassPool getClassPool(ClassLoader classLoader) {
    synchronized (classPool) {
        final Object hit = this.checker.get(classLoader);
        if (hit != null) {
            return classPool;
        }
        this.checker.put(classLoader, EXIST);
        final ClassPath classPath = new LoaderClassPath(classLoader);
        this.classPool.appendClassPath(classPath);
        return classPool;
    }
}
Also used : LoaderClassPath(javassist.LoaderClassPath) ClassPath(javassist.ClassPath) LoaderClassPath(javassist.LoaderClassPath)

Example 5 with LoaderClassPath

use of javassist.LoaderClassPath in project pinpoint by naver.

the class TestBootstrapClass method test.

@Test
public void test() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException, CannotCompileException {
    URLClassLoader classLoader = new URLClassLoader(new URL[] {});
    LoaderClassPath loaderClassPath = new LoaderClassPath(classLoader);
    ClassPool cp = new ClassPool();
    cp.appendClassPath(loaderClassPath);
    CtClass ctClass = cp.makeClass(TEST_CLASS_NAME);
    byte[] bytes = ctClass.toBytecode();
    logger.debug(classLoader.getClass().getName());
    Class<?> aClass = BytecodeUtils.defineClass(classLoader, TEST_CLASS_NAME, bytes);
    logger.debug("{}", aClass.getName());
}
Also used : CtClass(javassist.CtClass) URLClassLoader(java.net.URLClassLoader) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath) Test(org.junit.Test)

Aggregations

LoaderClassPath (javassist.LoaderClassPath)13 ClassPool (javassist.ClassPool)9 CtClass (javassist.CtClass)5 ClassPath (javassist.ClassPath)3 NotFoundException (javassist.NotFoundException)2 Test (org.junit.Test)2 ASMBytecodeDisassembler (com.navercorp.pinpoint.profiler.instrument.ASMBytecodeDisassembler)1 ProxyCallableStatement (com.zaxxer.hikari.pool.ProxyCallableStatement)1 ProxyConnection (com.zaxxer.hikari.pool.ProxyConnection)1 ProxyPreparedStatement (com.zaxxer.hikari.pool.ProxyPreparedStatement)1 ProxyResultSet (com.zaxxer.hikari.pool.ProxyResultSet)1 ProxyStatement (com.zaxxer.hikari.pool.ProxyStatement)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Matcher (java.util.regex.Matcher)1 CannotCompileException (javassist.CannotCompileException)1 CtMethod (javassist.CtMethod)1 Ignore (org.junit.Ignore)1