use of org.apache.aries.spifly.WeavingData in project aries by apache.
the class ClientWeavingHook method weave.
@Override
public void weave(WovenClass wovenClass) {
Bundle consumerBundle = wovenClass.getBundleWiring().getBundle();
Set<WeavingData> wd = activator.getWeavingData(consumerBundle);
if (wd != null) {
activator.log(Level.FINE, "Weaving class " + wovenClass.getClassName());
ClassReader cr = new ClassReader(wovenClass.getBytes());
ClassWriter cw = new OSGiFriendlyClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES, wovenClass.getBundleWiring().getClassLoader());
TCCLSetterVisitor tsv = new TCCLSetterVisitor(cw, wovenClass.getClassName(), wd);
cr.accept(tsv, ClassReader.SKIP_FRAMES);
if (tsv.isWoven()) {
wovenClass.setBytes(cw.toByteArray());
if (tsv.additionalImportRequired())
wovenClass.getDynamicImports().add(addedImport);
if (activator.isLogEnabled(Level.FINEST)) {
StringWriter stringWriter = new StringWriter();
ClassReader reader = new ClassReader(wovenClass.getBytes());
ClassVisitor tracer = new TraceClassVisitor(new PrintWriter(stringWriter));
ClassVisitor checker = new CheckClassAdapter(tracer, true);
reader.accept(checker, 0);
activator.log(Level.FINEST, "Woven class bytecode: \n" + stringWriter.toString());
}
}
}
}
use of org.apache.aries.spifly.WeavingData in project aries by apache.
the class TCCLSetterVisitor method visitEnd.
@Override
public void visitEnd() {
if (!woven) {
// if this class wasn't woven, then don't add the synthesized method either.
super.visitEnd();
return;
}
// Add generated static method
Set<String> methodNames = new HashSet<String>();
for (WeavingData wd : weavingData) {
String methodName = getGeneratedMethodName(wd);
if (methodNames.contains(methodName))
continue;
methodNames.add(methodName);
if (ServiceLoader.class.getName().equals(wd.getClassName())) {
continue;
}
/* Equivalent to:
* private static void $$FCCL$$<className>$<methodName>(Class<?> cls) {
* Util.fixContextClassLoader("java.util.ServiceLoader", "load", cls, WovenClass.class.getClassLoader());
* }
*/
Method method = new Method(methodName, Type.VOID_TYPE, new Type[] { CLASS_TYPE });
GeneratorAdapter mv = new GeneratorAdapter(cv.visitMethod(ACC_PRIVATE + ACC_STATIC, methodName, method.getDescriptor(), null, null), ACC_PRIVATE + ACC_STATIC, methodName, method.getDescriptor());
// Load the strings, method parameter and target
mv.visitLdcInsn(wd.getClassName());
mv.visitLdcInsn(wd.getMethodName());
mv.loadArg(0);
mv.visitLdcInsn(targetClass);
// Change the class on the stack into a classloader
mv.invokeVirtual(CLASS_TYPE, new Method("getClassLoader", CLASSLOADER_TYPE, new Type[0]));
// Call our util method
mv.invokeStatic(UTIL_CLASS, new Method("fixContextClassloader", Type.VOID_TYPE, new Type[] { String_TYPE, String_TYPE, CLASS_TYPE, CLASSLOADER_TYPE }));
mv.returnValue();
mv.endMethod();
}
super.visitEnd();
}
use of org.apache.aries.spifly.WeavingData in project aries by apache.
the class Main method weaveDir.
private static void weaveDir(File dir, String consumerHeaderKey, String consumerHeaderValue, String bundleClassPath) throws Exception {
Set<WeavingData> wd = ConsumerHeaderProcessor.processHeader(consumerHeaderKey, consumerHeaderValue);
URLClassLoader cl = new URLClassLoader(new URL[] { dir.toURI().toURL() }, Main.class.getClassLoader());
String dirName = dir.getAbsolutePath();
DirTree dt = new DirTree(dir);
for (File f : dt.getFiles()) {
if (!f.getName().endsWith(".class"))
continue;
String className = f.getAbsolutePath().substring(dirName.length());
if (className.startsWith(File.separator))
className = className.substring(1);
className = className.substring(0, className.length() - ".class".length());
className = className.replace(File.separator, ".");
InputStream is = new FileInputStream(f);
byte[] b;
try {
ClassReader cr = new ClassReader(is);
ClassWriter cw = new StaticToolClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES, cl);
TCCLSetterVisitor cv = new TCCLSetterVisitor(cw, className, wd);
cr.accept(cv, ClassReader.SKIP_FRAMES);
if (cv.isWoven()) {
b = cw.toByteArray();
} else {
// if not woven, store the original bytes
b = Streams.suck(new FileInputStream(f));
}
} finally {
is.close();
}
OutputStream os = new FileOutputStream(f);
try {
os.write(b);
} finally {
os.close();
}
}
if (bundleClassPath != null) {
for (String entry : bundleClassPath.split(",")) {
File jarFile = new File(dir, entry.trim());
if (jarFile.isFile()) {
weaveBCPJar(jarFile, consumerHeaderKey, consumerHeaderValue);
}
}
}
}
Aggregations