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