This commit is contained in:
Redkale
2022-07-06 22:03:29 +08:00
parent 74009b38c4
commit dc285b6c2f

View File

@@ -8,6 +8,7 @@ package org.redkale.util;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.lang.reflect.*; import java.lang.reflect.*;
import java.math.*;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@@ -809,30 +810,33 @@ public final class ResourceFactory {
this.value = value; this.value = value;
this.elements = elements == null ? new CopyOnWriteArrayList<>() : elements; this.elements = elements == null ? new CopyOnWriteArrayList<>() : elements;
if (sync && elements != null && !elements.isEmpty()) { if (sync && elements != null && !elements.isEmpty()) {
for (ResourceElement element : elements) { for (ResourceElement element : elements) {
Object dest = element.dest.get(); Object dest = element.dest.get();
if (dest == null) continue; if (dest == null) continue; //依赖对象可能被销毁了
Object rs = value; Object newVal = value;
final Class classtype = element.fieldType; final Class classType = element.fieldType;
if (rs != null && !rs.getClass().isPrimitive() && classtype.isPrimitive()) { if (newVal != null && !newVal.getClass().isPrimitive() && (classType.isPrimitive() || Number.class.isAssignableFrom(classType))) {
if (classtype == int.class) { if (classType == int.class || classType == Integer.class) {
rs = Integer.decode(rs.toString()); newVal = Integer.decode(newVal.toString());
} else if (classtype == long.class) { } else if (classType == long.class || classType == Long.class) {
rs = Long.decode(rs.toString()); newVal = Long.decode(newVal.toString());
} else if (classtype == short.class) { } else if (classType == short.class || classType == Short.class) {
rs = Short.decode(rs.toString()); newVal = Short.decode(newVal.toString());
} else if (classtype == boolean.class) { } else if (classType == boolean.class || classType == Boolean.class) {
rs = "true".equalsIgnoreCase(rs.toString()); newVal = "true".equalsIgnoreCase(newVal.toString());
} else if (classtype == byte.class) { } else if (classType == byte.class || classType == Byte.class) {
rs = Byte.decode(rs.toString()); newVal = Byte.decode(newVal.toString());
} else if (classtype == float.class) { } else if (classType == float.class || classType == Float.class) {
rs = Float.parseFloat(rs.toString()); newVal = Float.parseFloat(newVal.toString());
} else if (classtype == double.class) { } else if (classType == double.class || classType == Double.class) {
rs = Double.parseDouble(rs.toString()); newVal = Double.parseDouble(newVal.toString());
} else if (classType == BigInteger.class) {
newVal = new BigInteger(newVal.toString());
} else if (classType == BigDecimal.class) {
newVal = new BigDecimal(newVal.toString());
} }
} }
if (rs == null && classtype.isPrimitive()) rs = Array.get(Array.newInstance(classtype, 1), 0); if (newVal == null && classType.isPrimitive()) newVal = Array.get(Array.newInstance(classType, 1), 0);
Object oldVal = null; Object oldVal = null;
if (element.listener != null) { if (element.listener != null) {
try { try {
@@ -842,14 +846,14 @@ public final class ResourceFactory {
} }
} }
try { try {
element.field.set(dest, rs); element.field.set(dest, newVal);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
if (element.listener != null) { if (element.listener != null) {
try { try {
if (!element.different || !Objects.equals(rs, oldVal)) { if (!element.different || !Objects.equals(newVal, oldVal)) {
element.listener.invoke(dest, name, rs, oldVal); element.listener.invoke(dest, name, newVal, oldVal);
} }
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.SEVERE, dest + " resource change listener error", e); logger.log(Level.SEVERE, dest + " resource change listener error", e);