use of org.objectweb.asm.ClassReader in project semantic-versioning by jeluard.
the class JarDiff method loadClasses.
/**
* Load all the classes from the specified URL and store information
* about them in the specified map.
* This currently only works for jar files, <b>not</b> directories
* which contain classes in subdirectories or in the current directory.
*
* @param infoMap the map to store the ClassInfo in.
* @param file the jarfile to load classes from.
* @throws IOException if there is an IOException reading info about a
* class.
*/
private void loadClasses(Map infoMap, File file) throws DiffException {
try {
JarFile jar = new JarFile(file);
Enumeration e = jar.entries();
while (e.hasMoreElements()) {
JarEntry entry = (JarEntry) e.nextElement();
String name = entry.getName();
if (!entry.isDirectory() && name.endsWith(".class")) {
ClassReader reader = new ClassReader(jar.getInputStream(entry));
ClassInfo ci = loadClassInfo(reader);
infoMap.put(ci.getName(), ci);
}
}
} catch (IOException ioe) {
throw new DiffException(ioe);
}
}
use of org.objectweb.asm.ClassReader in project jphp by jphp-compiler.
the class ClassStmtCompiler method writeCopiedMethod.
protected void writeCopiedMethod(ClassStmtToken.Alias alias, String methodName, ClassEntity trait) {
final MethodEntity methodEntity = fetchClassAndCheck(alias.getTrait()).findMethod(methodName.toLowerCase());
String name = alias.getName();
if (name == null)
name = methodName;
MethodEntity origin = entity.findMethod(name.toLowerCase());
if (origin != null) {
if (origin.getClazz() == entity) {
if (origin.getTrait() != null) {
compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_TRAIT_METHOD_COLLISION.fetch(methodName, trait.getName(), origin.getTrait().getName(), entity.getName()));
}
return;
}
}
if (methodEntity == null) {
compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_METHOD_NOT_FOUND.fetch(alias.getTrait(), methodName));
return;
}
MethodEntity dup = methodEntity.duplicateForInject();
dup.setClazz(entity);
dup.setTrait(trait);
if (alias.getName() != null)
dup.setName(alias.getName());
if (alias.getModifier() != null)
dup.setModifier(alias.getModifier());
MethodNodeImpl methodNode = MethodNodeImpl.duplicate(methodEntity.getAdditionalData("methodNode", MethodNode.class, new Function<MethodNode>() {
@Override
public MethodNode call() {
ClassNode classNode = methodEntity.getClazz().getAdditionalData("classNode", ClassNode.class, new Function<ClassNode>() {
@Override
public ClassNode call() {
ClassReader classReader;
if (methodEntity.getClazz().getData() != null)
classReader = new ClassReader(methodEntity.getClazz().getData());
else {
try {
classReader = new ClassReader(methodEntity.getClazz().getName());
} catch (IOException e) {
throw new CriticalException(e);
}
}
ClassNode classNode = new ClassNode();
classReader.accept(classNode, 0);
return classNode;
}
});
for (Object m : classNode.methods) {
MethodNode method = (MethodNode) m;
if (method.name.equals(methodEntity.getInternalName())) {
return method;
}
}
throw new CriticalException("Cannot find MethodNode for method - " + methodEntity.getName() + "(" + methodEntity.getSignatureString(true) + ")");
}
}));
if (origin != null) {
dup.setPrototype(origin);
}
dup.setInternalName(dup.getName() + "$" + entity.nextMethodIndex());
methodNode.name = dup.getInternalName();
ClassEntity.SignatureResult result = entity.addMethod(dup, null);
result.check(compiler.getEnvironment());
node.methods.add(methodNode);
}
use of org.objectweb.asm.ClassReader in project MinecraftForge by MinecraftForge.
the class AccessTransformer method processJar.
private static void processJar(File inFile, File outFile, AccessTransformer[] transformers) throws IOException {
ZipInputStream inJar = null;
ZipOutputStream outJar = null;
try {
try {
inJar = new ZipInputStream(new BufferedInputStream(new FileInputStream(inFile)));
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Could not open input file: " + e.getMessage());
}
try {
outJar = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));
} catch (FileNotFoundException e) {
throw new FileNotFoundException("Could not open output file: " + e.getMessage());
}
ZipEntry entry;
while ((entry = inJar.getNextEntry()) != null) {
if (entry.isDirectory()) {
outJar.putNextEntry(entry);
continue;
}
byte[] data = new byte[4096];
ByteArrayOutputStream entryBuffer = new ByteArrayOutputStream();
int len;
do {
len = inJar.read(data);
if (len > 0) {
entryBuffer.write(data, 0, len);
}
} while (len != -1);
byte[] entryData = entryBuffer.toByteArray();
String entryName = entry.getName();
if (entryName.endsWith(".class") && !entryName.startsWith(".")) {
ClassNode cls = new ClassNode();
ClassReader rdr = new ClassReader(entryData);
rdr.accept(cls, 0);
String name = cls.name.replace('/', '.').replace('\\', '.');
for (AccessTransformer trans : transformers) {
entryData = trans.transform(name, name, entryData);
}
}
ZipEntry newEntry = new ZipEntry(entryName);
outJar.putNextEntry(newEntry);
outJar.write(entryData);
}
} finally {
IOUtils.closeQuietly(outJar);
IOUtils.closeQuietly(inJar);
}
}
use of org.objectweb.asm.ClassReader in project MinecraftForge by MinecraftForge.
the class BlamingTransformer method transform.
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
if (bytes == null) {
return null;
}
ClassReader classReader = new ClassReader(bytes);
VersionVisitor visitor = new VersionVisitor();
classReader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG);
return bytes;
}
use of org.objectweb.asm.ClassReader in project MinecraftForge by MinecraftForge.
the class DeobfuscationTransformer method transform.
// COMPUTE_FRAMES causes classes to be loaded, which could cause issues if the classes do not exist.
// However in testing this has not happened. {As we run post SideTransformer}
// If reported we need to add a custom implementation of ClassWriter.getCommonSuperClass
// that does not cause class loading.
@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
if (bytes == null) {
return null;
}
ClassReader classReader = new ClassReader(bytes);
ClassWriter classWriter = new ClassWriter(WRITER_FLAGS);
RemappingClassAdapter remapAdapter = new FMLRemappingAdapter(classWriter);
classReader.accept(remapAdapter, READER_FLAGS);
return classWriter.toByteArray();
}
Aggregations