优化Array.newInstance
This commit is contained in:
@@ -7,7 +7,6 @@ package org.redkale.net.http;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Array;
|
||||
import java.net.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.*;
|
||||
@@ -1248,9 +1247,9 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
*/
|
||||
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
|
||||
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;
|
||||
for (Annotation ann : this.annotations) {
|
||||
if (ann.getClass() == annotationClass) {
|
||||
@@ -1258,7 +1257,7 @@ public class HttpRequest extends Request<HttpContext> {
|
||||
}
|
||||
}
|
||||
if (index < 1) {
|
||||
return (T[]) Array.newInstance(annotationClass, 0);
|
||||
return Creator.arrayFunction(annotationClass).apply(0);
|
||||
}
|
||||
return Arrays.copyOf(news, index);
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
package org.redkale.net.http;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Array;
|
||||
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) {
|
||||
for (Annotation ann : getAnnotations()) {
|
||||
if (ann.getClass() == annotationClass) return (T) ann;
|
||||
if (ann.getClass() == annotationClass) {
|
||||
return (T) ann;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
|
||||
Annotation[] annotations = getAnnotations();
|
||||
if (annotations == null) return (T[]) Array.newInstance(annotationClass, 0);
|
||||
T[] news = (T[]) Array.newInstance(annotationClass, annotations.length);
|
||||
if (annotations == null) {
|
||||
return Creator.arrayFunction(annotationClass).apply(0);
|
||||
}
|
||||
T[] news = Creator.arrayFunction(annotationClass).apply(annotations.length);
|
||||
int index = 0;
|
||||
for (Annotation ann : annotations) {
|
||||
if (ann.getClass() == annotationClass) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
package org.redkale.source;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.redkale.convert.Convert;
|
||||
@@ -53,7 +53,7 @@ public interface CacheSource extends Resourcable {
|
||||
public Map<String, byte[]> mgetBytes(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);
|
||||
for (int i = 0; i < keys.length; 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) {
|
||||
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++) {
|
||||
rs[i] = (T) map.get(keys[i]);
|
||||
}
|
||||
|
||||
@@ -802,9 +802,8 @@ public interface Creator<T> {
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(resultClazz, newDynName.replace('/', '.'));
|
||||
return (IntFunction<T[]>) resultClazz.getDeclaredConstructor().newInstance();
|
||||
} catch (Exception ex) {
|
||||
//ex.printStackTrace(); //一般不会发生
|
||||
//return t -> (T[]) Array.newInstance(clazz, t);
|
||||
throw new RuntimeException(ex);
|
||||
//ex.printStackTrace(); //一般不会发生, native-image在没有预编译情况下会报错
|
||||
return t -> (T[]) Array.newInstance(clazz, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user