From 423ad56f0c4771656f33758656a4e6d284626ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sun, 14 Oct 2018 20:35:12 +0800 Subject: [PATCH] =?UTF-8?q?TabooLib=20v4.56=20+=20=E4=BF=AE=E5=A4=8D=20Sim?= =?UTF-8?q?pleVersionControl=20=E7=9A=84=E4=B8=80=E4=BA=9B=E6=BC=8F?= =?UTF-8?q?=E6=B4=9E=EF=BC=8C=E5=A2=9E=E5=8A=A0=20useCache=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=B9=B6=E5=85=BC=E5=AE=B9=E7=83=AD=E9=87=8D=E8=BD=BD?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../versioncontrol/SimpleClassVisitor.java | 2 +- .../versioncontrol/SimpleMethodVisitor.java | 9 +++++-- .../versioncontrol/SimpleVersionControl.java | 25 ++++++++++++++++--- .../skymc/taboolib/fileutils/FileUtils.java | 9 +------ .../me/skymc/taboolib/string/ArrayUtils.java | 7 ++---- 6 files changed, 33 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 1e58f04..529ccd0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ me.skymc TabooLib - 4.55 + 4.56 UTF-8 diff --git a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleClassVisitor.java b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleClassVisitor.java index 3be025e..b58055c 100644 --- a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleClassVisitor.java +++ b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleClassVisitor.java @@ -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 diff --git a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleMethodVisitor.java b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleMethodVisitor.java index c9b6a8c..ab458db 100644 --- a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleMethodVisitor.java +++ b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleMethodVisitor.java @@ -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) { diff --git a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java index 1f020cb..8f4444d 100644 --- a/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java +++ b/src/main/java/me/skymc/taboolib/common/versioncontrol/SimpleVersionControl.java @@ -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> cacheClasses = new HashMap<>(); private String target; - private List from = Lists.newArrayList(); private String to; + private List 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; } -} + +} \ No newline at end of file diff --git a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java index 5c3ad6a..f9c4043 100644 --- a/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java +++ b/src/main/java/me/skymc/taboolib/fileutils/FileUtils.java @@ -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); } /** diff --git a/src/main/java/me/skymc/taboolib/string/ArrayUtils.java b/src/main/java/me/skymc/taboolib/string/ArrayUtils.java index 40fee02..b2da4df 100644 --- a/src/main/java/me/skymc/taboolib/string/ArrayUtils.java +++ b/src/main/java/me/skymc/taboolib/string/ArrayUtils.java @@ -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 cloneAsByte(T obj) throws Exception { + public static T cloneAsByte(T obj) throws IOException, ClassNotFoundException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(obj);