From eb1c0e15138d715a8d82e97dffbe4228d8db4bc7 Mon Sep 17 00:00:00 2001 From: redkale Date: Sat, 12 Aug 2023 18:50:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96util?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/net/http/HttpServer.java | 2 +- .../java/org/redkale/util/ByteBufferPool.java | 36 +++++++++------ .../redkale/util/ConstructorParameters.java | 1 + src/main/java/org/redkale/util/Copier.java | 45 +++++-------------- .../org/redkale/util/LogExcludeLevel.java | 1 + src/main/java/org/redkale/util/LogLevel.java | 1 + .../{ObjectReference.java => ObjectRef.java} | 6 +-- src/main/java/org/redkale/util/Utility.java | 4 +- 8 files changed, 42 insertions(+), 54 deletions(-) rename src/main/java/org/redkale/util/{ObjectReference.java => ObjectRef.java} (84%) diff --git a/src/main/java/org/redkale/net/http/HttpServer.java b/src/main/java/org/redkale/net/http/HttpServer.java index f083b3c64..16e1473d9 100644 --- a/src/main/java/org/redkale/net/http/HttpServer.java +++ b/src/main/java/org/redkale/net/http/HttpServer.java @@ -490,7 +490,7 @@ public class HttpServer extends Server dateRef = new ObjectReference<>(); + final ObjectRef dateRef = new ObjectRef<>(); final DateFormat gmtDateFormat = new SimpleDateFormat("EEE, d MMM y HH:mm:ss z", Locale.ENGLISH); gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); dateRef.set(("Date: " + gmtDateFormat.format(new Date()) + "\r\n").getBytes()); diff --git a/src/main/java/org/redkale/util/ByteBufferPool.java b/src/main/java/org/redkale/util/ByteBufferPool.java index c05b4efd1..b73ac8a09 100644 --- a/src/main/java/org/redkale/util/ByteBufferPool.java +++ b/src/main/java/org/redkale/util/ByteBufferPool.java @@ -20,14 +20,16 @@ public class ByteBufferPool extends ObjectPool { private final int bufferCapacity; - protected ByteBufferPool(ObjectPool parent, LongAdder creatCounter, LongAdder cycleCounter, Thread unsafeThread, int max, int bufferCapacity, Queue queue) { - super(parent, creatCounter, cycleCounter, unsafeThread, max, (Object... params) -> ByteBuffer.allocateDirect(bufferCapacity), null, (e) -> { - if (e == null || e.isReadOnly() || e.capacity() != bufferCapacity) { - return false; - } - e.clear(); - return true; - }, queue); + protected ByteBufferPool(ObjectPool parent, LongAdder creatCounter, + LongAdder cycleCounter, Thread unsafeThread, int max, int bufferCapacity, Queue queue) { + super(parent, creatCounter, cycleCounter, unsafeThread, max, + (Object... params) -> ByteBuffer.allocateDirect(bufferCapacity), null, (e) -> { + if (e == null || e.isReadOnly() || e.capacity() != bufferCapacity) { + return false; + } + e.clear(); + return true; + }, queue); this.bufferCapacity = bufferCapacity; } @@ -52,18 +54,23 @@ public class ByteBufferPool extends ObjectPool { } //非线程安全版 - public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter, LongAdder cycleCounter, int max, int bufferCapacity) { - return new ByteBufferPool(parent, creatCounter, cycleCounter, null, Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max))); + public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter, + LongAdder cycleCounter, int max, int bufferCapacity) { + return new ByteBufferPool(parent, creatCounter, cycleCounter, null, + Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max))); } //非线程安全版 public static ByteBufferPool createUnsafePool(Thread unsafeThread, int max, ByteBufferPool safePool) { - return createUnsafePool(safePool, safePool.getCreatCounter(), safePool.getCycleCounter(), unsafeThread, max, safePool.getBufferCapacity()); + return createUnsafePool(safePool, safePool.getCreatCounter(), + safePool.getCycleCounter(), unsafeThread, max, safePool.getBufferCapacity()); } //非线程安全版 - public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter, LongAdder cycleCounter, Thread unsafeThread, int max, int bufferCapacity) { - return new ByteBufferPool(parent, creatCounter, cycleCounter, unsafeThread, Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max))); + public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter, + LongAdder cycleCounter, Thread unsafeThread, int max, int bufferCapacity) { + return new ByteBufferPool(parent, creatCounter, cycleCounter, unsafeThread, + Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max))); } //线程安全版 @@ -78,7 +85,8 @@ public class ByteBufferPool extends ObjectPool { //线程安全版 public static ByteBufferPool createSafePool(LongAdder creatCounter, LongAdder cycleCounter, int max, int bufferCapacity) { - return new ByteBufferPool(null, creatCounter, cycleCounter, null, Math.max(Utility.cpus(), max), bufferCapacity, new LinkedBlockingQueue<>(Math.max(Utility.cpus(), max))); + return new ByteBufferPool(null, creatCounter, cycleCounter, null, + Math.max(Utility.cpus(), max), bufferCapacity, new LinkedBlockingQueue<>(Math.max(Utility.cpus(), max))); } public int getBufferCapacity() { diff --git a/src/main/java/org/redkale/util/ConstructorParameters.java b/src/main/java/org/redkale/util/ConstructorParameters.java index dde2e9d1c..892a7bd93 100644 --- a/src/main/java/org/redkale/util/ConstructorParameters.java +++ b/src/main/java/org/redkale/util/ConstructorParameters.java @@ -19,6 +19,7 @@ import static java.lang.annotation.ElementType.*; * * @author zhangjx * @deprecated replaced by org.redkale.annotation.ConstructorParameters + * @see org.redkale.annotation.ConstructorParameters */ @Deprecated(since = "2.8.0") @Documented diff --git a/src/main/java/org/redkale/util/Copier.java b/src/main/java/org/redkale/util/Copier.java index 8401c9acd..988d86183 100644 --- a/src/main/java/org/redkale/util/Copier.java +++ b/src/main/java/org/redkale/util/Copier.java @@ -9,32 +9,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.*; import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES; import org.redkale.asm.*; -import static org.redkale.asm.Opcodes.ACC_BRIDGE; -import static org.redkale.asm.Opcodes.ACC_FINAL; -import static org.redkale.asm.Opcodes.ACC_PRIVATE; -import static org.redkale.asm.Opcodes.ACC_PUBLIC; -import static org.redkale.asm.Opcodes.ACC_STATIC; -import static org.redkale.asm.Opcodes.ACC_SUPER; -import static org.redkale.asm.Opcodes.ACC_SYNTHETIC; -import static org.redkale.asm.Opcodes.ALOAD; -import static org.redkale.asm.Opcodes.ARETURN; -import static org.redkale.asm.Opcodes.ASTORE; -import static org.redkale.asm.Opcodes.CHECKCAST; -import static org.redkale.asm.Opcodes.GETFIELD; -import static org.redkale.asm.Opcodes.GOTO; -import static org.redkale.asm.Opcodes.IFEQ; -import static org.redkale.asm.Opcodes.IFLE; -import static org.redkale.asm.Opcodes.IFNONNULL; -import static org.redkale.asm.Opcodes.IFNULL; -import static org.redkale.asm.Opcodes.INSTANCEOF; -import static org.redkale.asm.Opcodes.INVOKEINTERFACE; -import static org.redkale.asm.Opcodes.INVOKESPECIAL; -import static org.redkale.asm.Opcodes.INVOKESTATIC; -import static org.redkale.asm.Opcodes.INVOKEVIRTUAL; -import static org.redkale.asm.Opcodes.POP; -import static org.redkale.asm.Opcodes.PUTFIELD; -import static org.redkale.asm.Opcodes.RETURN; -import static org.redkale.asm.Opcodes.V11; +import static org.redkale.asm.Opcodes.*; import org.redkale.asm.Type; /** @@ -166,7 +141,6 @@ public interface Copier extends BiFunction { * @param 源类泛型 * @param destClass 目标类名 * @param srcClass 源类名 - * @param options 可配项 * * @return 复制器 */ @@ -181,7 +155,6 @@ public interface Copier extends BiFunction { * @param 源类泛型 * @param destClass 目标类名 * @param srcClass 源类名 - * @param options 可配项 * * @return 复制器 */ @@ -196,7 +169,6 @@ public interface Copier extends BiFunction { * @param 源类泛型 * @param destClass 目标类名 * @param srcClass 源类名 - * @param options 可配项 * * @return 复制器 */ @@ -597,11 +569,14 @@ public interface Copier extends BiFunction { final String newDynName = "org/redkaledyn/copier/_Dyn" + Copier.class.getSimpleName() + "_" + options + "__" + srcClass.getName().replace('.', '_').replace('$', '_') + "__" + destClass.getName().replace('.', '_').replace('$', '_'); - try { - Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); - return (Copier) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz).getDeclaredConstructor().newInstance(); - } catch (Throwable ex) { + if (srcColumnPredicate == null && nameAlias == null) { + try { + Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); + return (Copier) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz).getDeclaredConstructor().newInstance(); + } catch (Throwable ex) { + } } + final Predicate> throwPredicate = e -> !RuntimeException.class.isAssignableFrom(e); // ------------------------------------------------------------------------------ ClassWriter cw = new ClassWriter(COMPUTE_FRAMES); @@ -1132,7 +1107,9 @@ public interface Copier extends BiFunction { return defineClass(name, b, 0, b.length); } }.loadClass(newDynName.replace('/', '.'), bytes); - RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); + if (srcColumnPredicate == null && nameAlias == null) { + RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); + } RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); try { return (Copier) newClazz.getDeclaredConstructor().newInstance(); diff --git a/src/main/java/org/redkale/util/LogExcludeLevel.java b/src/main/java/org/redkale/util/LogExcludeLevel.java index 58d134fdc..70e7b4f50 100644 --- a/src/main/java/org/redkale/util/LogExcludeLevel.java +++ b/src/main/java/org/redkale/util/LogExcludeLevel.java @@ -23,6 +23,7 @@ import java.lang.annotation.*; * *

* 详情见: https://redkale.org + * @see org.redkale.annotation.LogExcludeLevel * * @author zhangjx */ diff --git a/src/main/java/org/redkale/util/LogLevel.java b/src/main/java/org/redkale/util/LogLevel.java index 3dd388658..4ef2d1a9d 100644 --- a/src/main/java/org/redkale/util/LogLevel.java +++ b/src/main/java/org/redkale/util/LogLevel.java @@ -14,6 +14,7 @@ import java.lang.annotation.*; * *

* 详情见: https://redkale.org + * @see org.redkale.annotation.LogLevel * * @author zhangjx */ diff --git a/src/main/java/org/redkale/util/ObjectReference.java b/src/main/java/org/redkale/util/ObjectRef.java similarity index 84% rename from src/main/java/org/redkale/util/ObjectReference.java rename to src/main/java/org/redkale/util/ObjectRef.java index afe885d68..0fc353aa3 100644 --- a/src/main/java/org/redkale/util/ObjectReference.java +++ b/src/main/java/org/redkale/util/ObjectRef.java @@ -15,15 +15,15 @@ package org.redkale.util; * * @since 2.8.0 */ -public final class ObjectReference { +public final class ObjectRef { private V value; - public ObjectReference(V initialValue) { + public ObjectRef(V initialValue) { this.value = initialValue; } - public ObjectReference() { + public ObjectRef() { } public final V get() { diff --git a/src/main/java/org/redkale/util/Utility.java b/src/main/java/org/redkale/util/Utility.java index 21ae2db44..b72053709 100644 --- a/src/main/java/org/redkale/util/Utility.java +++ b/src/main/java/org/redkale/util/Utility.java @@ -373,9 +373,9 @@ public final class Utility { static class ObjectWriteStream extends ObjectOutputStream { - public final ObjectReference methodNameReference = new ObjectReference<>(); + public final ObjectRef methodNameReference = new ObjectRef<>(); - public final ObjectReference classNameReference = new ObjectReference<>(); + public final ObjectRef classNameReference = new ObjectRef<>(); public ObjectWriteStream(OutputStream out) throws IOException { super(out);