This commit is contained in:
@@ -51,7 +51,7 @@ public interface Creator<T> {
|
||||
}
|
||||
Constructor<T> constructor = null;
|
||||
for (Constructor c : clazz.getConstructors()) {
|
||||
if (c.getParameterCount() == 0) {
|
||||
if (c.getParameterTypes().length == 0) { //为了兼容android 而不使用 getParameterCount()
|
||||
constructor = c;
|
||||
break;
|
||||
}
|
||||
@@ -95,8 +95,8 @@ public interface Creator<T> {
|
||||
mv.visitTypeInsn(NEW, interName);
|
||||
mv.visitInsn(DUP);
|
||||
//---------------------------------------
|
||||
Class[] params = constructor.getParameterTypes();
|
||||
{
|
||||
Parameter[] params = constructor.getParameters();
|
||||
final int[] iconsts = {ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5};
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
mv.visitVarInsn(ALOAD, 1);
|
||||
@@ -106,7 +106,7 @@ public interface Creator<T> {
|
||||
mv.visitIntInsn(BIPUSH, i);
|
||||
}
|
||||
mv.visitInsn(AALOAD);
|
||||
final Class ct = params[i].getType();
|
||||
final Class ct = params[i];
|
||||
if (ct.isPrimitive()) {
|
||||
final Class bigct = Array.get(Array.newInstance(ct, 1), 0).getClass();
|
||||
mv.visitTypeInsn(CHECKCAST, bigct.getName().replace('.', '/'));
|
||||
@@ -124,7 +124,7 @@ public interface Creator<T> {
|
||||
//---------------------------------------
|
||||
mv.visitMethodInsn(INVOKESPECIAL, interName, "<init>", Type.getConstructorDescriptor(constructor), false);
|
||||
mv.visitInsn(ARETURN);
|
||||
mv.visitMaxs((constructor.getParameterCount() > 0 ? (constructor.getParameterCount() + 3) : 2), 2);
|
||||
mv.visitMaxs((params.length > 0 ? (params.length + 3) : 2), 2);
|
||||
mv.visitEnd();
|
||||
}
|
||||
{ //虚拟 create 方法
|
||||
|
||||
@@ -74,7 +74,7 @@ public interface Reproduce<D, S> {
|
||||
|
||||
for (java.lang.reflect.Method getter : srcClass.getMethods()) {
|
||||
if (Modifier.isStatic(getter.getModifiers())) continue;
|
||||
if (getter.getParameterCount() > 0) continue;
|
||||
if (getter.getParameterTypes().length > 0) continue;
|
||||
if ("getClass".equals(getter.getName())) continue;
|
||||
if (!getter.getName().startsWith("get") && !getter.getName().startsWith("is")) continue;
|
||||
java.lang.reflect.Method setter;
|
||||
|
||||
@@ -119,7 +119,7 @@ public abstract class TypeToken<T> {
|
||||
boolean first = true;
|
||||
for (Type t : actualTypeArguments) {
|
||||
if (!first) sb.append(", ");
|
||||
sb.append(t.getTypeName());
|
||||
sb.append(t);
|
||||
first = false;
|
||||
}
|
||||
sb.append(">");
|
||||
|
||||
@@ -5,11 +5,9 @@
|
||||
package org.redkale.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.*;
|
||||
import java.time.*;
|
||||
import java.util.*;
|
||||
import javax.net.ssl.*;
|
||||
|
||||
@@ -28,30 +26,9 @@ public final class Utility {
|
||||
|
||||
private static final char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
|
||||
private static final sun.misc.Unsafe UNSAFE;
|
||||
|
||||
private static final long strvaloffset;
|
||||
|
||||
private static final long sbvaloffset;
|
||||
|
||||
private static final javax.net.ssl.SSLContext DEFAULTSSL_CONTEXT;
|
||||
|
||||
static {
|
||||
sun.misc.Unsafe usafe = null;
|
||||
long fd1 = 0L;
|
||||
long fd2 = 0L;
|
||||
try {
|
||||
Field safeField = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
|
||||
safeField.setAccessible(true);
|
||||
usafe = (sun.misc.Unsafe) safeField.get(null);
|
||||
fd1 = usafe.objectFieldOffset(String.class.getDeclaredField("value"));
|
||||
fd2 = usafe.objectFieldOffset(StringBuilder.class.getSuperclass().getDeclaredField("value"));
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e); //不可能会发生
|
||||
}
|
||||
UNSAFE = usafe;
|
||||
strvaloffset = fd1;
|
||||
sbvaloffset = fd2;
|
||||
|
||||
try {
|
||||
DEFAULTSSL_CONTEXT = javax.net.ssl.SSLContext.getInstance("SSL");
|
||||
@@ -156,8 +133,8 @@ public final class Utility {
|
||||
* @return
|
||||
*/
|
||||
public static int today() {
|
||||
java.time.LocalDate today = java.time.LocalDate.now();
|
||||
return today.getYear() * 10000 + today.getMonthValue() * 100 + today.getDayOfMonth();
|
||||
final Calendar cal = Calendar.getInstance();
|
||||
return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,11 +144,14 @@ public final class Utility {
|
||||
* @return
|
||||
*/
|
||||
public static long monday(long time) {
|
||||
ZoneId zid = ZoneId.systemDefault();
|
||||
Instant instant = Instant.ofEpochMilli(time);
|
||||
LocalDate ld = instant.atZone(zid).toLocalDate();
|
||||
ld = ld.minusDays(ld.getDayOfWeek().getValue() - 1);
|
||||
return ld.atStartOfDay(zid).toInstant().toEpochMilli();
|
||||
final Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeInMillis(time);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 0);
|
||||
cal.set(Calendar.MINUTE, 0);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
cal.set(Calendar.MILLISECOND, 0);
|
||||
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
||||
return cal.getTimeInMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,11 +161,14 @@ public final class Utility {
|
||||
* @return
|
||||
*/
|
||||
public static long sunday(long time) {
|
||||
ZoneId zid = ZoneId.systemDefault();
|
||||
Instant instant = Instant.ofEpochMilli(time);
|
||||
LocalDate ld = instant.atZone(zid).toLocalDate();
|
||||
ld = ld.plusDays(7 - ld.getDayOfWeek().getValue());
|
||||
return ld.atStartOfDay(zid).toInstant().toEpochMilli();
|
||||
final Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeInMillis(time);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 0);
|
||||
cal.set(Calendar.MINUTE, 0);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
cal.set(Calendar.MILLISECOND, 0);
|
||||
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
||||
return cal.getTimeInMillis();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -195,10 +178,14 @@ public final class Utility {
|
||||
* @return
|
||||
*/
|
||||
public static long monthFirstDay(long time) {
|
||||
ZoneId zid = ZoneId.systemDefault();
|
||||
Instant instant = Instant.ofEpochMilli(time);
|
||||
LocalDate ld = instant.atZone(zid).toLocalDate().withDayOfMonth(1);
|
||||
return ld.atStartOfDay(zid).toInstant().toEpochMilli();
|
||||
final Calendar cal = Calendar.getInstance();
|
||||
cal.setTimeInMillis(time);
|
||||
cal.set(Calendar.HOUR_OF_DAY, 0);
|
||||
cal.set(Calendar.MINUTE, 0);
|
||||
cal.set(Calendar.SECOND, 0);
|
||||
cal.set(Calendar.MILLISECOND, 0);
|
||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||
return cal.getTimeInMillis();
|
||||
}
|
||||
|
||||
public static String binToHexString(byte[] bytes) {
|
||||
@@ -311,7 +298,7 @@ public final class Utility {
|
||||
|
||||
public static byte[] encodeUTF8(final String value) {
|
||||
if (value == null) return new byte[0];
|
||||
return encodeUTF8((char[]) UNSAFE.getObject(value, strvaloffset));
|
||||
return encodeUTF8(value.toCharArray());
|
||||
}
|
||||
|
||||
public static byte[] encodeUTF8(final char[] array) {
|
||||
@@ -351,12 +338,12 @@ public final class Utility {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static char[] charArray(String value) {
|
||||
return value == null ? null : (char[]) UNSAFE.getObject(value, strvaloffset);
|
||||
public static char[] charArray(String value) { //与JDK 8 的实现版本不一样
|
||||
return value == null ? null : value.toCharArray();
|
||||
}
|
||||
|
||||
public static char[] charArray(StringBuilder value) {
|
||||
return value == null ? null : (char[]) UNSAFE.getObject(value, sbvaloffset);
|
||||
public static char[] charArray(StringBuilder value) { //与JDK 8 的实现版本不一样
|
||||
return value == null ? null : value.toString().toCharArray();
|
||||
}
|
||||
|
||||
public static ByteBuffer encodeUTF8(final ByteBuffer buffer, final char[] array) {
|
||||
@@ -369,7 +356,7 @@ public final class Utility {
|
||||
|
||||
public static int encodeUTF8Length(String value) {
|
||||
if (value == null) return -1;
|
||||
return encodeUTF8Length((char[]) UNSAFE.getObject(value, strvaloffset));
|
||||
return encodeUTF8Length(value.toCharArray());
|
||||
}
|
||||
|
||||
public static int encodeUTF8Length(final char[] text) {
|
||||
|
||||
Reference in New Issue
Block a user