优化Array.newInstance

This commit is contained in:
redkale
2023-02-01 20:34:01 +08:00
parent 851c81e096
commit 1c1c54ea8a
4 changed files with 19 additions and 15 deletions

View File

@@ -7,7 +7,6 @@ package org.redkale.net.http;
import java.io.*; import java.io.*;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.net.*; import java.net.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.*; import java.nio.charset.*;
@@ -1248,9 +1247,9 @@ public class HttpRequest extends Request<HttpContext> {
*/ */
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
if (this.annotations == null) { if (this.annotations == null) {
return (T[]) Array.newInstance(annotationClass, 0); return Creator.arrayFunction(annotationClass).apply(0);
} }
T[] news = (T[]) Array.newInstance(annotationClass, this.annotations.length); T[] news = Creator.arrayFunction(annotationClass).apply(this.annotations.length);
int index = 0; int index = 0;
for (Annotation ann : this.annotations) { for (Annotation ann : this.annotations) {
if (ann.getClass() == annotationClass) { if (ann.getClass() == annotationClass) {
@@ -1258,7 +1257,7 @@ public class HttpRequest extends Request<HttpContext> {
} }
} }
if (index < 1) { if (index < 1) {
return (T[]) Array.newInstance(annotationClass, 0); return Creator.arrayFunction(annotationClass).apply(0);
} }
return Arrays.copyOf(news, index); return Arrays.copyOf(news, index);
} }

View File

@@ -6,8 +6,8 @@
package org.redkale.net.http; package org.redkale.net.http;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.Arrays; import java.util.Arrays;
import org.redkale.util.Creator;
/** /**
* *
@@ -27,22 +27,28 @@ public interface WebSocketParam {
default <T extends Annotation> T getAnnotation(Class<T> annotationClass) { default <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
for (Annotation ann : getAnnotations()) { for (Annotation ann : getAnnotations()) {
if (ann.getClass() == annotationClass) return (T) ann; if (ann.getClass() == annotationClass) {
return (T) ann;
}
} }
return null; return null;
} }
default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) { default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
Annotation[] annotations = getAnnotations(); Annotation[] annotations = getAnnotations();
if (annotations == null) return (T[]) Array.newInstance(annotationClass, 0); if (annotations == null) {
T[] news = (T[]) Array.newInstance(annotationClass, annotations.length); return Creator.arrayFunction(annotationClass).apply(0);
}
T[] news = Creator.arrayFunction(annotationClass).apply(annotations.length);
int index = 0; int index = 0;
for (Annotation ann : annotations) { for (Annotation ann : annotations) {
if (ann.getClass() == annotationClass) { if (ann.getClass() == annotationClass) {
news[index++] = (T) ann; news[index++] = (T) ann;
} }
} }
if (index < 1) return (T[]) Array.newInstance(annotationClass, 0); if (index < 1) {
return Creator.arrayFunction(annotationClass).apply(0);
}
return Arrays.copyOf(news, index); return Arrays.copyOf(news, index);
} }
} }

View File

@@ -6,7 +6,7 @@
package org.redkale.source; package org.redkale.source;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.*; import java.lang.reflect.Type;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import org.redkale.convert.Convert; import org.redkale.convert.Convert;
@@ -53,7 +53,7 @@ public interface CacheSource extends Resourcable {
public Map<String, byte[]> mgetBytes(final String... keys); public Map<String, byte[]> mgetBytes(final String... keys);
default <T> T[] mgets(final Type componentType, final String... keys) { default <T> T[] mgets(final Type componentType, final String... keys) {
T[] rs = (T[]) Array.newInstance(TypeToken.typeToClass(componentType), keys.length); T[] rs = (T[]) Creator.arrayFunction(TypeToken.typeToClass(componentType)).apply(keys.length);
Map<String, T> map = mget(componentType, keys); Map<String, T> map = mget(componentType, keys);
for (int i = 0; i < keys.length; i++) { for (int i = 0; i < keys.length; i++) {
rs[i] = map.get(keys[i]); rs[i] = map.get(keys[i]);
@@ -335,7 +335,7 @@ public interface CacheSource extends Resourcable {
default <T> CompletableFuture<T[]> mgetsAsync(final Type componentType, final String... keys) { default <T> CompletableFuture<T[]> mgetsAsync(final Type componentType, final String... keys) {
return mgetAsync(componentType, keys).thenApply(map -> { return mgetAsync(componentType, keys).thenApply(map -> {
T[] rs = (T[]) Array.newInstance(TypeToken.typeToClass(componentType), keys.length); T[] rs = (T[]) Creator.arrayFunction(TypeToken.typeToClass(componentType)).apply(keys.length);
for (int i = 0; i < keys.length; i++) { for (int i = 0; i < keys.length; i++) {
rs[i] = (T) map.get(keys[i]); rs[i] = (T) map.get(keys[i]);
} }

View File

@@ -802,9 +802,8 @@ public interface Creator<T> {
RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.'));
return (IntFunction<T[]>) resultClazz.getDeclaredConstructor().newInstance(); return (IntFunction<T[]>) resultClazz.getDeclaredConstructor().newInstance();
} catch (Exception ex) { } catch (Exception ex) {
//ex.printStackTrace(); //一般不会发生 //ex.printStackTrace(); //一般不会发生, native-image在没有预编译情况下会报错
//return t -> (T[]) Array.newInstance(clazz, t); return t -> (T[]) Array.newInstance(clazz, t);
throw new RuntimeException(ex);
} }
} }
} }