Skip to content

Commit

Permalink
Merge branch '3.3' into fix/aotUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyHZM committed Feb 4, 2024
2 parents 32f4bde + 2dde7ac commit 806aba4
Show file tree
Hide file tree
Showing 286 changed files with 23,403 additions and 2,567 deletions.
4 changes: 4 additions & 0 deletions .artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,7 @@ dubbo-tracing
dubbo-xds
dubbo-plugin-proxy-bytebuddy
dubbo-plugin-loom
dubbo-rest-jaxrs
dubbo-rest-servlet
dubbo-rest-spring

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.dubbo.common.extension;

import java.util.Collections;
import java.util.List;

/**
* Uniform accessor for extension
*/
Expand All @@ -24,7 +27,7 @@ public interface ExtensionAccessor {
ExtensionDirector getExtensionDirector();

default <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
return this.getExtensionDirector().getExtensionLoader(type);
return getExtensionDirector().getExtensionLoader(type);
}

default <T> T getExtension(Class<T> type, String name) {
Expand All @@ -41,4 +44,21 @@ default <T> T getDefaultExtension(Class<T> type) {
ExtensionLoader<T> extensionLoader = getExtensionLoader(type);
return extensionLoader != null ? extensionLoader.getDefaultExtension() : null;
}

default <T> List<T> getActivateExtensions(Class<T> type) {
ExtensionLoader<T> extensionLoader = getExtensionLoader(type);
return extensionLoader != null ? extensionLoader.getActivateExtensions() : Collections.emptyList();
}

default <T> T getFirstActivateExtension(Class<T> type) {
ExtensionLoader<T> extensionLoader = getExtensionLoader(type);
if (extensionLoader == null) {
throw new IllegalArgumentException("ExtensionLoader for [" + type + "] is not found");
}
List<T> extensions = extensionLoader.getActivateExtensions();
if (extensions.isEmpty()) {
throw new IllegalArgumentException("No activate extensions for [" + type + "] found");
}
return extensions.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
*/
package org.apache.dubbo.common.io;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Stream utils.
Expand Down Expand Up @@ -229,4 +234,51 @@ public static void skipUnusedStream(InputStream is) throws IOException {
is.skip(is.available());
}
}

public static void copy(InputStream in, OutputStream out) throws IOException {
if (in.getClass() == ByteArrayInputStream.class) {
copy((ByteArrayInputStream) in, out);
return;
}
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}

public static void copy(ByteArrayInputStream in, OutputStream out) throws IOException {
int len = in.available();
byte[] buffer = new byte[len];
in.read(buffer, 0, len);
out.write(buffer, 0, len);
}

public static byte[] readBytes(InputStream in) throws IOException {
if (in.getClass() == ByteArrayInputStream.class) {
return readBytes((ByteArrayInputStream) in);
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
return out.toByteArray();
}

public static byte[] readBytes(ByteArrayInputStream in) throws IOException {
int len = in.available();
byte[] bytes = new byte[len];
in.read(bytes, 0, len);
return bytes;
}

public static String toString(InputStream in) throws IOException {
return new String(readBytes(in), StandardCharsets.UTF_8);
}

public static String toString(InputStream in, Charset charset) throws IOException {
return new String(readBytes(in), charset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public interface JsonUtil {

Map<String, ?> getObject(Map<String, ?> obj, String key);

Object convertObject(Object obj, Type type);

Object convertObject(Object obj, Class<?> clazz);

Double getNumberAsDouble(Map<String, ?> obj, String key);

Integer getNumberAsInteger(Map<String, ?> obj, String key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,14 @@ public <T> List<T> toJavaList(String json, Class<T> clazz) {
public String toJson(Object obj) {
return com.alibaba.fastjson2.JSON.toJSONString(obj, JSONWriter.Feature.WriteEnumsUsingName);
}

@Override
public Object convertObject(Object obj, Type type) {
return com.alibaba.fastjson2.util.TypeUtils.cast(obj, type);
}

@Override
public Object convertObject(Object obj, Class<?> clazz) {
return com.alibaba.fastjson2.util.TypeUtils.cast(obj, clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.reflect.Type;
import java.util.List;

import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FastJsonImpl extends AbstractJsonUtilImpl {
Expand All @@ -37,4 +38,14 @@ public <T> List<T> toJavaList(String json, Class<T> clazz) {
public String toJson(Object obj) {
return com.alibaba.fastjson.JSON.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect);
}

@Override
public Object convertObject(Object obj, Type type) {
return com.alibaba.fastjson.util.TypeUtils.cast(obj, type, ParserConfig.getGlobalInstance());
}

@Override
public Object convertObject(Object obj, Class<?> clazz) {
return com.alibaba.fastjson.util.TypeUtils.cast(obj, clazz, ParserConfig.getGlobalInstance());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public String toJson(Object obj) {
return getGson().toJson(obj);
}

@Override
public Object convertObject(Object obj, Type type) {
Gson gson = getGson();
return gson.fromJson(gson.toJsonTree(obj), type);
}

@Override
public Object convertObject(Object obj, Class<?> clazz) {
Gson gson = getGson();
return gson.fromJson(gson.toJsonTree(obj), clazz);
}

private Gson getGson() {
if (gsonCache == null || !(gsonCache instanceof Gson)) {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public String toJson(Object obj) {
}
}

@Override
public Object convertObject(Object obj, Type type) {
JsonMapper mapper = getJackson();
return mapper.convertValue(obj, mapper.constructType(type));
}

@Override
public Object convertObject(Object obj, Class<?> clazz) {
return getJackson().convertValue(obj, clazz);
}

private JsonMapper getJackson() {
if (jacksonCache == null || !(jacksonCache instanceof JsonMapper)) {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ public static int indexOf(String[] array, String valueToFind, int startIndex) {
public static <T> T[] of(T... values) {
return values;
}

public static <T> T first(T[] data) {
return isEmpty(data) ? null : data[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public static void notNull(Object obj, String message) {
}
}

public static void notNull(Object obj, String format, Object... args) {
if (obj == null) {
throw new IllegalArgumentException(String.format(format, args));
}
}

public static void notEmptyString(String str, String message) {
if (StringUtils.isEmpty(str)) {
throw new IllegalArgumentException(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ public static boolean isPrimitive(Class<?> type) {
return type != null && (type.isPrimitive() || isSimpleType(type));
}

public static boolean isPrimitiveWrapper(Class<?> type) {
return PRIMITIVE_WRAPPER_TYPE_MAP.containsKey(type);
}

/**
* The specified type is simple type or not
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.Collections.emptySet;
Expand Down Expand Up @@ -404,13 +407,19 @@ public static <T> T first(Collection<T> values) {
return null;
}
if (values instanceof List) {
List<T> list = (List<T>) values;
return list.get(0);
return ((List<T>) values).get(0);
} else {
return values.iterator().next();
}
}

public static <T> T first(List<T> values) {
if (isEmpty(values)) {
return null;
}
return values.get(0);
}

public static <T> Set<T> toTreeSet(Set<T> set) {
if (isEmpty(set)) {
return set;
Expand All @@ -420,4 +429,78 @@ public static <T> Set<T> toTreeSet(Set<T> set) {
}
return set;
}

public static <T> Set<T> newHashSet(int expectedSize) {
return new HashSet<>(capacity(expectedSize));
}

public static <T> Set<T> newLinkedHashSet(int expectedSize) {
return new LinkedHashSet<>(capacity(expectedSize));
}

public static <T, K> Map<K, T> newHashMap(int expectedSize) {
return new HashMap<>(capacity(expectedSize));
}

public static <T, K> Map<K, T> newLinkedHashMap(int expectedSize) {
return new LinkedHashMap<>(capacity(expectedSize));
}

public static <T, K> Map<K, T> newConcurrentHashMap(int expectedSize) {
if (JRE.JAVA_8.isCurrentVersion()) {
return new SafeConcurrentHashMap<>(capacity(expectedSize));
}
return new ConcurrentHashMap<>(capacity(expectedSize));
}

public static <T, K> Map<K, T> newConcurrentHashMap() {
if (JRE.JAVA_8.isCurrentVersion()) {
return new SafeConcurrentHashMap<>();
}
return new ConcurrentHashMap<>();
}

public static int capacity(int expectedSize) {
if (expectedSize < 3) {
if (expectedSize < 0) {
throw new IllegalArgumentException("expectedSize cannot be negative but was: " + expectedSize);
}
return expectedSize + 1;
}
if (expectedSize < 1 << (Integer.SIZE - 2)) {
return (int) (expectedSize / 0.75F + 1.0F);
}
return Integer.MAX_VALUE;
}

public static class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
private static final long serialVersionUID = 1L;

public SafeConcurrentHashMap() {}

public SafeConcurrentHashMap(int initialCapacity) {
super(initialCapacity);
}

public SafeConcurrentHashMap(Map<? extends K, ? extends V> m) {
super(m);
}

@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
V value = get(key);
if (value != null) {
return value;
}
value = mappingFunction.apply(key);
if (value == null) {
return null;
}
V exists = putIfAbsent(key, value);
if (exists != null) {
return exists;
}
return value;
}
}
}
Loading

0 comments on commit 806aba4

Please sign in to comment.