TabooLib v4.56
+ 修复 SimpleVersionControl 的一些漏洞,增加 useCache 方法并兼容热重载。
This commit is contained in:
parent
d793444dd5
commit
423ad56f0c
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.skymc</groupId>
|
||||
<artifactId>TabooLib</artifactId>
|
||||
<version>4.55</version>
|
||||
<version>4.56</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
@ -24,7 +24,7 @@ public class SimpleClassVisitor extends ClassVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
|
||||
super.visit(version, access, name, signature, translate(superName), translate(interfaces));
|
||||
super.visit(version, access, name, translate(signature), translate(superName), translate(interfaces));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,9 +21,14 @@ public class SimpleMethodVisitor extends MethodVisitor {
|
||||
this.simpleVersionControl = simpleVersionControl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitMethodInsn(int opcode, String owner, String name, String descriptor) {
|
||||
super.visitMethodInsn(opcode, translate(owner), translate(name), translate(descriptor));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
|
||||
super.visitMethodInsn(opcode, translate(owner), name, translate(descriptor), isInterface);
|
||||
super.visitMethodInsn(opcode, translate(owner), translate(name), translate(descriptor), isInterface);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -43,7 +48,7 @@ public class SimpleMethodVisitor extends MethodVisitor {
|
||||
|
||||
@Override
|
||||
public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) {
|
||||
super.visitLocalVariable(name, translate(descriptor), translate(signature), start, end, index);
|
||||
super.visitLocalVariable(translate(name), translate(descriptor), translate(signature), start, end, index);
|
||||
}
|
||||
|
||||
private String translate(String target) {
|
||||
|
@ -11,8 +11,9 @@ import org.objectweb.asm.ClassVisitor;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
@ -20,12 +21,15 @@ import java.util.List;
|
||||
*/
|
||||
public class SimpleVersionControl {
|
||||
|
||||
private static Map<String, Class<?>> cacheClasses = new HashMap<>();
|
||||
private String target;
|
||||
private List<String> from = Lists.newArrayList();
|
||||
private String to;
|
||||
private List<String> from = Lists.newArrayList();
|
||||
private Plugin plugin;
|
||||
private boolean useCache;
|
||||
|
||||
SimpleVersionControl() {
|
||||
useCache = false;
|
||||
}
|
||||
|
||||
public static SimpleVersionControl create() {
|
||||
@ -61,18 +65,30 @@ public class SimpleVersionControl {
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleVersionControl useCache() {
|
||||
this.useCache = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Class<?> translate() throws IOException {
|
||||
return translate(plugin);
|
||||
}
|
||||
|
||||
public Class<?> translate(Plugin plugin) throws IOException {
|
||||
if (useCache && cacheClasses.containsKey(target)) {
|
||||
return cacheClasses.get(target);
|
||||
}
|
||||
ClassReader classReader = new ClassReader(FileUtils.getResource(plugin, target.replace(".", "/") + ".class"));
|
||||
ClassWriter classWriter = new ClassWriter(0);
|
||||
ClassVisitor classVisitor = new SimpleClassVisitor(this, classWriter);
|
||||
classReader.accept(classVisitor, 0);
|
||||
classWriter.visitEnd();
|
||||
classVisitor.visitEnd();
|
||||
return AsmClassLoader.createNewClass(target, classWriter.toByteArray());
|
||||
Class<?> newClass = AsmClassLoader.createNewClass(target, classWriter.toByteArray());
|
||||
if (useCache) {
|
||||
cacheClasses.put(target, newClass);
|
||||
}
|
||||
return newClass;
|
||||
}
|
||||
|
||||
// *********************************
|
||||
@ -99,4 +115,5 @@ public class SimpleVersionControl {
|
||||
}
|
||||
return origin;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -134,14 +134,7 @@ public class FileUtils {
|
||||
* @return {@link InputStream}
|
||||
*/
|
||||
public static InputStream getResource(Plugin plugin, String filename) {
|
||||
try {
|
||||
URL url = plugin.getClass().getClassLoader().getResource(filename);
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.setUseCaches(false);
|
||||
return connection.getInputStream();
|
||||
} catch (IOException ignored) {
|
||||
return null;
|
||||
}
|
||||
return plugin.getClass().getClassLoader().getResourceAsStream(filename);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,10 +2,7 @@ package me.skymc.taboolib.string;
|
||||
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
@ -64,7 +61,7 @@ public class ArrayUtils {
|
||||
return (T) newArray;
|
||||
}
|
||||
|
||||
public static <T> T cloneAsByte(T obj) throws Exception {
|
||||
public static <T> T cloneAsByte(T obj) throws IOException, ClassNotFoundException {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
|
||||
objectOutputStream.writeObject(obj);
|
||||
|
Loading…
Reference in New Issue
Block a user