use of org.objectweb.asm.ClassReader in project presto by prestodb.
the class PluginDiscovery method classInterfaces.
private static List<String> classInterfaces(String name, ClassLoader classLoader) {
ImmutableList.Builder<String> list = ImmutableList.builder();
ClassReader reader = readClass(name, classLoader);
for (String binaryName : reader.getInterfaces()) {
list.add(javaName(binaryName));
}
if (reader.getSuperName() != null) {
list.addAll(classInterfaces(javaName(reader.getSuperName()), classLoader));
}
return list.build();
}
use of org.objectweb.asm.ClassReader in project robovm by robovm.
the class InterfaceBuilderClassesPlugin method getCustomClass.
private String getCustomClass(URL url, Map<URL, String> customClassValuesCache) throws IOException {
if (customClassValuesCache.containsKey(url)) {
return customClassValuesCache.get(url);
}
class Visitor extends ClassVisitor {
String customClass;
Visitor() {
super(Opcodes.ASM4);
}
@Override
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
if (CUSTOM_CLASS.equals(desc)) {
return new AnnotationVisitor(Opcodes.ASM4) {
public void visit(String name, Object value) {
customClass = (String) value;
}
};
}
return super.visitAnnotation(desc, visible);
}
}
Visitor visitor = new Visitor();
new ClassReader(IOUtils.toByteArray(url)).accept(visitor, 0);
customClassValuesCache.put(url, visitor.customClass);
return visitor.customClass;
}
use of org.objectweb.asm.ClassReader in project storm by apache.
the class DefaultShader method addRemappedClass.
private void addRemappedClass(RelocatorRemapper remapper, JarOutputStream jos, String name, InputStream is) throws IOException {
LOG.debug("Remapping class... " + name);
if (!remapper.hasRelocators()) {
try {
LOG.debug("Just copy class...");
jos.putNextEntry(new JarEntry(name));
IOUtil.copy(is, jos);
} catch (ZipException e) {
LOG.info("zip exception ", e);
}
return;
}
ClassReader cr = new ClassReader(is);
// We don't pass the ClassReader here. This forces the ClassWriter to rebuild the constant pool.
// Copying the original constant pool should be avoided because it would keep references
// to the original class names. This is not a problem at runtime (because these entries in the
// constant pool are never used), but confuses some tools such as Felix' maven-bundle-plugin
// that use the constant pool to determine the dependencies of a class.
ClassWriter cw = new ClassWriter(0);
final String pkg = name.substring(0, name.lastIndexOf('/') + 1);
ClassVisitor cv = new RemappingClassAdapter(cw, remapper) {
@Override
public void visitSource(final String source, final String debug) {
LOG.debug("visitSource " + source);
if (source == null) {
super.visitSource(source, debug);
} else {
final String fqSource = pkg + source;
final String mappedSource = remapper.map(fqSource);
final String filename = mappedSource.substring(mappedSource.lastIndexOf('/') + 1);
LOG.debug("Remapped to " + filename);
super.visitSource(filename, debug);
}
}
};
try {
cr.accept(cv, ClassReader.EXPAND_FRAMES);
} catch (Throwable ise) {
throw new IOException("Error in ASM processing class " + name, ise);
}
byte[] renamedClass = cw.toByteArray();
// Need to take the .class off for remapping evaluation
String mappedName = remapper.map(name.substring(0, name.indexOf('.')));
LOG.debug("Remapped class name to " + mappedName);
try {
// Now we put it back on so the class file is written out with the right extension.
jos.putNextEntry(new JarEntry(mappedName + ".class"));
jos.write(renamedClass);
} catch (ZipException e) {
LOG.info("zip exception ", e);
}
}
use of org.objectweb.asm.ClassReader in project cglib by cglib.
the class AbstractClassGenerator method generate.
protected Class generate(ClassLoaderData data) {
Class gen;
Object save = CURRENT.get();
CURRENT.set(this);
try {
ClassLoader classLoader = data.getClassLoader();
if (classLoader == null) {
throw new IllegalStateException("ClassLoader is null while trying to define class " + getClassName() + ". It seems that the loader has been expired from a weak reference somehow. " + "Please file an issue at cglib's issue tracker.");
}
synchronized (classLoader) {
String name = generateClassName(data.getUniqueNamePredicate());
data.reserveName(name);
this.setClassName(name);
}
if (attemptLoad) {
try {
gen = classLoader.loadClass(getClassName());
return gen;
} catch (ClassNotFoundException e) {
// ignore
}
}
byte[] b = strategy.generate(this);
String className = ClassNameReader.getClassName(new ClassReader(b));
ProtectionDomain protectionDomain = getProtectionDomain();
synchronized (classLoader) {
// just in case
if (protectionDomain == null) {
gen = ReflectUtils.defineClass(className, b, classLoader);
} else {
gen = ReflectUtils.defineClass(className, b, classLoader, protectionDomain);
}
}
return gen;
} catch (RuntimeException e) {
throw e;
} catch (Error e) {
throw e;
} catch (Exception e) {
throw new CodeGenerationException(e);
} finally {
CURRENT.set(save);
}
}
use of org.objectweb.asm.ClassReader in project cglib by cglib.
the class DebuggingClassWriter method toByteArray.
public byte[] toByteArray() {
return (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {
public Object run() {
byte[] b = ((ClassWriter) DebuggingClassWriter.super.cv).toByteArray();
if (debugLocation != null) {
String dirs = className.replace('.', File.separatorChar);
try {
new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs();
File file = new File(new File(debugLocation), dirs + ".class");
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
try {
out.write(b);
} finally {
out.close();
}
if (traceCtor != null) {
file = new File(new File(debugLocation), dirs + ".asm");
out = new BufferedOutputStream(new FileOutputStream(file));
try {
ClassReader cr = new ClassReader(b);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
ClassVisitor tcv = (ClassVisitor) traceCtor.newInstance(new Object[] { null, pw });
cr.accept(tcv, 0);
pw.flush();
} finally {
out.close();
}
}
} catch (Exception e) {
throw new CodeGenerationException(e);
}
}
return b;
}
});
}
Aggregations