TabooLib v4.56

+ 修复 SimpleVersionControl 的一些漏洞,增加 useCache 方法并兼容热重载。
This commit is contained in:
坏黑 2018-10-14 20:35:12 +08:00
parent d793444dd5
commit 423ad56f0c
6 changed files with 33 additions and 21 deletions

View File

@ -6,7 +6,7 @@
<groupId>me.skymc</groupId> <groupId>me.skymc</groupId>
<artifactId>TabooLib</artifactId> <artifactId>TabooLib</artifactId>
<version>4.55</version> <version>4.56</version>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -24,7 +24,7 @@ public class SimpleClassVisitor extends ClassVisitor {
@Override @Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { 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 @Override

View File

@ -21,9 +21,14 @@ public class SimpleMethodVisitor extends MethodVisitor {
this.simpleVersionControl = simpleVersionControl; 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 @Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { 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 @Override
@ -43,7 +48,7 @@ public class SimpleMethodVisitor extends MethodVisitor {
@Override @Override
public void visitLocalVariable(String name, String descriptor, String signature, Label start, Label end, int index) { 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) { private String translate(String target) {

View File

@ -11,8 +11,9 @@ import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* @Author sky * @Author sky
@ -20,12 +21,15 @@ import java.util.List;
*/ */
public class SimpleVersionControl { public class SimpleVersionControl {
private static Map<String, Class<?>> cacheClasses = new HashMap<>();
private String target; private String target;
private List<String> from = Lists.newArrayList();
private String to; private String to;
private List<String> from = Lists.newArrayList();
private Plugin plugin; private Plugin plugin;
private boolean useCache;
SimpleVersionControl() { SimpleVersionControl() {
useCache = false;
} }
public static SimpleVersionControl create() { public static SimpleVersionControl create() {
@ -61,18 +65,30 @@ public class SimpleVersionControl {
return this; return this;
} }
public SimpleVersionControl useCache() {
this.useCache = true;
return this;
}
public Class<?> translate() throws IOException { public Class<?> translate() throws IOException {
return translate(plugin); return translate(plugin);
} }
public Class<?> translate(Plugin plugin) throws IOException { 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")); ClassReader classReader = new ClassReader(FileUtils.getResource(plugin, target.replace(".", "/") + ".class"));
ClassWriter classWriter = new ClassWriter(0); ClassWriter classWriter = new ClassWriter(0);
ClassVisitor classVisitor = new SimpleClassVisitor(this, classWriter); ClassVisitor classVisitor = new SimpleClassVisitor(this, classWriter);
classReader.accept(classVisitor, 0); classReader.accept(classVisitor, 0);
classWriter.visitEnd(); classWriter.visitEnd();
classVisitor.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; return origin;
} }
}
}

View File

@ -134,14 +134,7 @@ public class FileUtils {
* @return {@link InputStream} * @return {@link InputStream}
*/ */
public static InputStream getResource(Plugin plugin, String filename) { public static InputStream getResource(Plugin plugin, String filename) {
try { return plugin.getClass().getClassLoader().getResourceAsStream(filename);
URL url = plugin.getClass().getClassLoader().getResource(filename);
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
return connection.getInputStream();
} catch (IOException ignored) {
return null;
}
} }
/** /**

View File

@ -2,10 +2,7 @@ package me.skymc.taboolib.string;
import com.ilummc.tlib.util.Strings; import com.ilummc.tlib.util.Strings;
import java.io.ByteArrayInputStream; import java.io.*;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -64,7 +61,7 @@ public class ArrayUtils {
return (T) newArray; 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(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj); objectOutputStream.writeObject(obj);