use of org.objectweb.asm.ClassReader in project groovy by apache.
the class VerifyClass method readClass.
private boolean readClass(String clazz) throws IOException {
ClassReader cr = new ClassReader(new FileInputStream(clazz));
ClassNode ca = new ClassNode() {
public void visitEnd() {
//accept(cv);
}
};
cr.accept(new CheckClassAdapter(ca), ClassWriter.COMPUTE_MAXS);
boolean failed = false;
List methods = ca.methods;
for (int i = 0; i < methods.size(); ++i) {
MethodNode method = (MethodNode) methods.get(i);
if (method.instructions.size() > 0) {
Analyzer a = new Analyzer(new SimpleVerifier());
try {
a.analyze(ca.name, method);
continue;
} catch (Exception e) {
e.printStackTrace();
}
if (!failed) {
failed = true;
log("verifying of class " + clazz + " failed");
}
if (verbose)
log(method.name + method.desc);
TraceMethodVisitor mv = new TraceMethodVisitor(null);
/*= new TraceMethodVisitor(null) {
public void visitMaxs(int maxStack, int maxLocals) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < text.size(); ++i) {
String s = frames[i] == null ? "null" : frames[i].toString();
while (s.length() < maxStack + maxLocals + 1) {
s += " ";
}
buffer.append(Integer.toString(i + 100000).substring(1));
buffer.append(" ");
buffer.append(s);
buffer.append(" : ");
buffer.append(text.get(i));
}
if (verbose) log(buffer.toString());
}
};*/
for (int j = 0; j < method.instructions.size(); ++j) {
Object insn = method.instructions.get(j);
if (insn instanceof AbstractInsnNode) {
((AbstractInsnNode) insn).accept(mv);
} else {
mv.visitLabel((Label) insn);
}
}
mv.visitMaxs(method.maxStack, method.maxLocals);
}
}
return !failed;
}
use of org.objectweb.asm.ClassReader in project flink by apache.
the class JarFileCreator method addDependencies.
/**
* Add the dependencies within the given packages automatically.
* @throws IOException
* throw if an error occurs while read the class file.
*/
private synchronized void addDependencies() throws IOException {
List<String> dependencies = new ArrayList<String>();
for (Class clazz : classSet) {
dependencies.add(clazz.getName());
}
//Traverse the dependency tree using BFS.
int head = 0;
while (head != dependencies.size()) {
DependencyVisitor v = new DependencyVisitor(Opcodes.ASM5);
v.addNameSpace(this.packages);
InputStream classInputStream = null;
String name = dependencies.get(head);
try {
Class clazz = Class.forName(name);
int n = name.lastIndexOf('.');
String className = null;
if (n > -1) {
className = name.substring(n + 1, name.length());
}
classInputStream = clazz.getResourceAsStream(className + CLASS_EXTENSION);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage());
}
new ClassReader(classInputStream).accept(v, 0);
classInputStream.close();
//Update the BFS queue.
Set<String> classPackages = v.getPackages();
for (String s : classPackages) {
if (!dependencies.contains(s.replace('/', '.'))) {
dependencies.add(s.replace('/', '.'));
}
}
head++;
}
for (String dependency : dependencies) {
try {
this.classSet.add(Class.forName(dependency));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage());
}
}
}
use of org.objectweb.asm.ClassReader in project groovy by apache.
the class SunClassLoader method loadFromRes.
protected void loadFromRes(String name) throws IOException {
final InputStream asStream = SunClassLoader.class.getClassLoader().getResourceAsStream(resName(name));
ClassReader reader = new ClassReader(asStream);
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
reader.accept(cw, ClassWriter.COMPUTE_MAXS);
asStream.close();
define(cw.toByteArray(), name);
}
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);
}
}
Aggregations