ClassTransformer.java
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.transformers;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
import net.minecraft.launchwrapper.IClassTransformer;
/**
* Base class for transformers which work via ClassNode
*
* @author Adam Mummery-Smith
*/
public abstract class ClassTransformer implements IClassTransformer
{
public static final String HORIZONTAL_RULE =
"----------------------------------------------------------------------------------------------------";
private ClassReader classReader;
private ClassNode classNode;
/**
* @param basicClass
*/
protected final ClassNode readClass(byte[] basicClass, boolean cacheReader)
{
ClassReader classReader = new ClassReader(basicClass);
if (cacheReader) this.classReader = classReader;
ClassNode classNode = new ClassNode();
classReader.accept(classNode, ClassReader.EXPAND_FRAMES);
return classNode;
}
/**
* @param classNode
*/
protected final byte[] writeClass(ClassNode classNode)
{
// Use optimised writer for speed
if (this.classReader != null && this.classNode == classNode)
{
this.classNode = null;
IsolatedClassWriter writer = new IsolatedClassWriter(this.classReader, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
this.classReader = null;
classNode.accept(writer);
return writer.toByteArray();
}
this.classNode = null;
IsolatedClassWriter writer = new IsolatedClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
return writer.toByteArray();
}
protected static String getSimpleClassName(ClassNode classNode)
{
String className = classNode.name.replace('/', '.');
int dotPos = className.lastIndexOf('.');
return dotPos == -1 ? className : className.substring(dotPos + 1);
}
}