mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -6,6 +6,7 @@ install: true
|
|||||||
|
|
||||||
jdk:
|
jdk:
|
||||||
- openjdk8
|
- openjdk8
|
||||||
|
- openjdk17
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
email: false
|
email: false
|
||||||
|
@@ -83,7 +83,8 @@ public class AnnotationProxy<T extends Annotation> implements Annotation, Invoca
|
|||||||
* @return 属性(方法结果)映射
|
* @return 属性(方法结果)映射
|
||||||
*/
|
*/
|
||||||
private Map<String, Object> initAttributes() {
|
private Map<String, Object> initAttributes() {
|
||||||
final Method[] methods = MethodUtil.getMethods(this.type);
|
// 只缓存注解定义的方法
|
||||||
|
final Method[] methods = MethodUtil.getDeclaredMethods(this.type);
|
||||||
final Map<String, Object> attributes = new HashMap<>(methods.length, 1);
|
final Map<String, Object> attributes = new HashMap<>(methods.length, 1);
|
||||||
|
|
||||||
for (final Method method : methods) {
|
for (final Method method : methods) {
|
||||||
|
@@ -324,7 +324,8 @@ public class AnnotationUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置新的注解的属性(字段)值
|
* 设置新的注解的属性(字段)值<br>
|
||||||
|
* 注意此方法在jdk9+中抛出异常,须添加`--add-opens=java.base/java.lang=ALL-UNNAMED`启动参数
|
||||||
*
|
*
|
||||||
* @param annotation 注解对象
|
* @param annotation 注解对象
|
||||||
* @param annotationField 注解属性(字段)名称
|
* @param annotationField 注解属性(字段)名称
|
||||||
|
@@ -37,6 +37,24 @@ import java.lang.reflect.*;
|
|||||||
*/
|
*/
|
||||||
public class ReflectUtil {
|
public class ReflectUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置方法为可访问(私有方法可以被外部调用),静默调用,抛出异常则跳过<br>
|
||||||
|
* 注意此方法在jdk9+中抛出异常,须添加`--add-opens=java.base/java.lang=ALL-UNNAMED`启动参数
|
||||||
|
*
|
||||||
|
* @param <T> AccessibleObject的子类,比如Class、Method、Field等
|
||||||
|
* @param accessibleObject 可设置访问权限的对象,比如Class、Method、Field等
|
||||||
|
* @return 被设置可访问的对象
|
||||||
|
* @throws SecurityException 访问被禁止抛出此异常
|
||||||
|
*/
|
||||||
|
public static <T extends AccessibleObject> T setAccessibleQuietly(final T accessibleObject) {
|
||||||
|
try{
|
||||||
|
setAccessible(accessibleObject);
|
||||||
|
} catch (final RuntimeException ignore){
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
return accessibleObject;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置方法为可访问(私有方法可以被外部调用)<br>
|
* 设置方法为可访问(私有方法可以被外部调用)<br>
|
||||||
* 注意此方法在jdk9+中抛出异常,须添加`--add-opens=java.base/java.lang=ALL-UNNAMED`启动参数
|
* 注意此方法在jdk9+中抛出异常,须添加`--add-opens=java.base/java.lang=ALL-UNNAMED`启动参数
|
||||||
@@ -50,7 +68,6 @@ public class ReflectUtil {
|
|||||||
public static <T extends AccessibleObject> T setAccessible(final T accessibleObject) throws SecurityException {
|
public static <T extends AccessibleObject> T setAccessible(final T accessibleObject) throws SecurityException {
|
||||||
if (null != accessibleObject && !accessibleObject.isAccessible()) {
|
if (null != accessibleObject && !accessibleObject.isAccessible()) {
|
||||||
accessibleObject.setAccessible(true);
|
accessibleObject.setAccessible(true);
|
||||||
return accessibleObject;
|
|
||||||
}
|
}
|
||||||
return accessibleObject;
|
return accessibleObject;
|
||||||
}
|
}
|
||||||
|
@@ -361,17 +361,28 @@ public class ObjUtil {
|
|||||||
* @see #cloneByStream(Object)
|
* @see #cloneByStream(Object)
|
||||||
*/
|
*/
|
||||||
public static <T> T clone(final T obj) {
|
public static <T> T clone(final T obj) {
|
||||||
T result = ArrayUtil.clone(obj);
|
final T result = ArrayUtil.clone(obj);
|
||||||
if (null == result) {
|
if(null != result){
|
||||||
if (obj instanceof Cloneable) {
|
// 数组
|
||||||
result = MethodUtil.invoke(obj, "clone");
|
|
||||||
} else {
|
|
||||||
result = cloneByStream(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (obj instanceof Cloneable) {
|
||||||
|
try{
|
||||||
|
return MethodUtil.invoke(obj, "clone");
|
||||||
|
} catch (final HutoolException e){
|
||||||
|
if(e.getCause() instanceof IllegalAccessException){
|
||||||
|
// JDK9+下可能无权限
|
||||||
|
return cloneByStream(obj);
|
||||||
|
}else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return cloneByStream(obj);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回克隆后的对象,如果克隆失败,返回原对象
|
* 返回克隆后的对象,如果克隆失败,返回原对象
|
||||||
*
|
*
|
||||||
|
@@ -5,6 +5,8 @@ import org.dromara.hutool.core.util.ObjUtil;
|
|||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.condition.EnabledForJreRange;
|
||||||
|
import org.junit.jupiter.api.condition.JRE;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@@ -113,7 +115,9 @@ public class AnnotationUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@EnabledForJreRange(max = JRE.JAVA_8)
|
||||||
public void testSetValue() {
|
public void testSetValue() {
|
||||||
|
// jdk9+中抛出异常,须添加`--add-opens=java.base/java.lang=ALL-UNNAMED`启动参数
|
||||||
final AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
final AnnotationForTest annotation = ClassForTest.class.getAnnotation(AnnotationForTest.class);
|
||||||
final String newValue = "is a new value";
|
final String newValue = "is a new value";
|
||||||
Assertions.assertNotEquals(newValue, annotation.value());
|
Assertions.assertNotEquals(newValue, annotation.value());
|
||||||
@@ -124,6 +128,7 @@ public class AnnotationUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetAnnotationAlias() {
|
public void testGetAnnotationAlias() {
|
||||||
final MetaAnnotationForTest annotation = AnnotationUtil.getAnnotationAlias(AnnotationForTest.class, MetaAnnotationForTest.class);
|
final MetaAnnotationForTest annotation = AnnotationUtil.getAnnotationAlias(AnnotationForTest.class, MetaAnnotationForTest.class);
|
||||||
|
Assertions.assertNotNull(annotation);
|
||||||
Assertions.assertEquals(annotation.value(), annotation.alias());
|
Assertions.assertEquals(annotation.value(), annotation.alias());
|
||||||
Assertions.assertEquals(MetaAnnotationForTest.class, annotation.annotationType());
|
Assertions.assertEquals(MetaAnnotationForTest.class, annotation.annotationType());
|
||||||
}
|
}
|
||||||
|
@@ -20,6 +20,8 @@ import org.dromara.hutool.core.lang.tuple.Tuple;
|
|||||||
import org.dromara.hutool.core.reflect.MethodUtil;
|
import org.dromara.hutool.core.reflect.MethodUtil;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.condition.EnabledForJreRange;
|
||||||
|
import org.junit.jupiter.api.condition.JRE;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
@@ -181,17 +183,16 @@ public class LambdaUtilTest {
|
|||||||
|
|
||||||
final BiConsumer<Bean, Long> setId = LambdaUtil.buildSetter(MethodUtil.getMethod(Bean.class, "setId", Long.class));
|
final BiConsumer<Bean, Long> setId = LambdaUtil.buildSetter(MethodUtil.getMethod(Bean.class, "setId", Long.class));
|
||||||
final BiConsumer<Bean, Long> setId2 = LambdaUtil.buildSetter(Bean.class, Bean.Fields.id);
|
final BiConsumer<Bean, Long> setId2 = LambdaUtil.buildSetter(Bean.class, Bean.Fields.id);
|
||||||
final BiConsumer<Bean, Boolean> setFlag = LambdaUtil.buildSetter(Bean.class, Bean.Fields.flag);
|
|
||||||
Assertions.assertEquals(setId, setId2);
|
Assertions.assertEquals(setId, setId2);
|
||||||
|
|
||||||
setId.accept(bean, 3L);
|
setId.accept(bean, 3L);
|
||||||
setFlag.accept(bean, true);
|
|
||||||
Assertions.assertEquals(3L, (long) bean.getId());
|
Assertions.assertEquals(3L, (long) bean.getId());
|
||||||
Assertions.assertTrue(bean.isFlag());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void buildSetterTest() {
|
@EnabledForJreRange(max = JRE.JAVA_8)
|
||||||
|
void buildSetterWithPrimitiveTest() {
|
||||||
|
// 原始类型参数在jdk9+中构建setter异常
|
||||||
final Bean bean = new Bean();
|
final Bean bean = new Bean();
|
||||||
bean.setId(2L);
|
bean.setId(2L);
|
||||||
bean.setFlag(false);
|
bean.setFlag(false);
|
||||||
|
@@ -16,10 +16,13 @@ import lombok.Data;
|
|||||||
import org.dromara.hutool.core.convert.Convert;
|
import org.dromara.hutool.core.convert.Convert;
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.condition.EnabledForJreRange;
|
||||||
|
import org.junit.jupiter.api.condition.JRE;
|
||||||
|
|
||||||
public class CglibUtilTest {
|
public class CglibUtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@EnabledForJreRange(max = JRE.JAVA_8)
|
||||||
public void copyTest() {
|
public void copyTest() {
|
||||||
final SampleBean bean = new SampleBean();
|
final SampleBean bean = new SampleBean();
|
||||||
OtherSampleBean otherBean = new OtherSampleBean();
|
OtherSampleBean otherBean = new OtherSampleBean();
|
||||||
|
@@ -12,10 +12,10 @@
|
|||||||
|
|
||||||
package org.dromara.hutool.http.client.engine.jdk;
|
package org.dromara.hutool.http.client.engine.jdk;
|
||||||
|
|
||||||
|
import org.dromara.hutool.core.array.ArrayUtil;
|
||||||
import org.dromara.hutool.core.io.IORuntimeException;
|
import org.dromara.hutool.core.io.IORuntimeException;
|
||||||
import org.dromara.hutool.core.reflect.FieldUtil;
|
import org.dromara.hutool.core.reflect.FieldUtil;
|
||||||
import org.dromara.hutool.core.reflect.ModifierUtil;
|
import org.dromara.hutool.core.reflect.ModifierUtil;
|
||||||
import org.dromara.hutool.core.array.ArrayUtil;
|
|
||||||
import org.dromara.hutool.core.util.SystemUtil;
|
import org.dromara.hutool.core.util.SystemUtil;
|
||||||
import org.dromara.hutool.http.HttpException;
|
import org.dromara.hutool.http.HttpException;
|
||||||
|
|
||||||
@@ -26,8 +26,6 @@ import java.net.HttpURLConnection;
|
|||||||
import java.net.Proxy;
|
import java.net.Proxy;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.security.AccessController;
|
|
||||||
import java.security.PrivilegedAction;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 针对{@link HttpURLConnection}相关工具
|
* 针对{@link HttpURLConnection}相关工具
|
||||||
@@ -44,23 +42,34 @@ public class HttpUrlConnectionUtil {
|
|||||||
/**
|
/**
|
||||||
* 增加支持的METHOD方法<br>
|
* 增加支持的METHOD方法<br>
|
||||||
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法<br>
|
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法<br>
|
||||||
* see: <a href="https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch">https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch</a>
|
* see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch
|
||||||
*/
|
*/
|
||||||
public static void allowPatch() {
|
public static void allowPatchQuietly() {
|
||||||
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
|
try{
|
||||||
doAllowPatch();
|
allowPatch();
|
||||||
return null;
|
} catch (final Exception ignore){
|
||||||
});
|
// ignore
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 增加支持的METHOD方法<br>
|
* 增加支持的METHOD方法<br>
|
||||||
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法<br>
|
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法<br>
|
||||||
* see: <a href="https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch">https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch</a>
|
* see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch
|
||||||
|
*/
|
||||||
|
public static void allowPatch() {
|
||||||
|
doAllowPatch();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 增加支持的METHOD方法<br>
|
||||||
|
* 此方法通过注入方式修改{@link HttpURLConnection}中的methods静态属性,增加PATCH方法<br>
|
||||||
|
* see: https://stackoverflow.com/questions/25163131/httpurlconnection-invalid-http-method-patch
|
||||||
*
|
*
|
||||||
* @since 5.7.4
|
* @since 5.7.4
|
||||||
*/
|
*/
|
||||||
synchronized private static void doAllowPatch() {
|
synchronized private static void doAllowPatch() {
|
||||||
|
// 注意此方法在jdk9+中抛出异常,须添加`--add-opens=java.base/java.lang=ALL-UNNAMED`启动参数
|
||||||
final Field methodsField = FieldUtil.getField(HttpURLConnection.class, "methods");
|
final Field methodsField = FieldUtil.getField(HttpURLConnection.class, "methods");
|
||||||
if (null == methodsField) {
|
if (null == methodsField) {
|
||||||
throw new HttpException("None static field [methods] with Java version: [{}]", SystemUtil.get("java.version"));
|
throw new HttpException("None static field [methods] with Java version: [{}]", SystemUtil.get("java.version"));
|
||||||
|
@@ -6,6 +6,6 @@ import org.junit.jupiter.api.Test;
|
|||||||
public class HttpUrlConnectionUtilTest {
|
public class HttpUrlConnectionUtilTest {
|
||||||
@Test
|
@Test
|
||||||
public void allowPatchTest() {
|
public void allowPatchTest() {
|
||||||
HttpUrlConnectionUtil.allowPatch();
|
HttpUrlConnectionUtil.allowPatchQuietly();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1295,13 +1295,15 @@ public class ImgUtil {
|
|||||||
// issue#1821@Github
|
// issue#1821@Github
|
||||||
img = new ImageIcon(img).getImage();
|
img = new ImageIcon(img).getImage();
|
||||||
|
|
||||||
final BufferedImage bimage = new BufferedImage(
|
final BufferedImage bImage = new BufferedImage(
|
||||||
img.getWidth(null), img.getHeight(null), imageType);
|
img.getWidth(null),
|
||||||
final Graphics2D bGr = GraphicsUtil.createGraphics(bimage, backgroundColor);
|
img.getHeight(null),
|
||||||
|
imageType);
|
||||||
|
final Graphics2D bGr = GraphicsUtil.createGraphics(bImage, backgroundColor);
|
||||||
bGr.drawImage(img, 0, 0, null);
|
bGr.drawImage(img, 0, 0, null);
|
||||||
bGr.dispose();
|
bGr.dispose();
|
||||||
|
|
||||||
return bimage;
|
return bImage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1571,48 +1573,34 @@ public class ImgUtil {
|
|||||||
write(image, imageType, destImageStream, 1);
|
write(image, imageType, destImageStream, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 写出图像为指定格式
|
|
||||||
*
|
|
||||||
* @param image {@link Image}
|
|
||||||
* @param imageType 图片类型(图片扩展名)
|
|
||||||
* @param targetImageStream 写出到的目标流
|
|
||||||
* @param quality 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
|
|
||||||
* @throws IORuntimeException IO异常
|
|
||||||
* @since 4.3.2
|
|
||||||
*/
|
|
||||||
public static void write(final Image image, final String imageType, final ImageOutputStream targetImageStream,
|
|
||||||
final float quality) throws IORuntimeException {
|
|
||||||
write(image, imageType, targetImageStream, quality, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 写出图像为指定格式
|
* 写出图像为指定格式
|
||||||
*
|
*
|
||||||
* @param image {@link Image}
|
* @param image {@link Image}
|
||||||
* @param imageType 图片类型(图片扩展名),{@code null}表示使用RGB模式(JPG)
|
* @param imageType 图片类型(图片扩展名),{@code null}表示使用RGB模式(JPG)
|
||||||
* @param destImageStream 写出到的目标流
|
* @param out 写出到的目标流
|
||||||
* @param quality 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
|
* @param quality 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
|
||||||
* @param backgroundColor 背景色{@link Color}
|
* @param backgroundColor 背景色{@link Color}
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
* @since 4.3.2
|
* @since 4.3.2
|
||||||
*/
|
*/
|
||||||
public static void write(final Image image, final String imageType, final ImageOutputStream destImageStream,
|
public static void write(final Image image, final String imageType, final ImageOutputStream out,
|
||||||
final float quality, final Color backgroundColor) throws IORuntimeException {
|
final float quality, final Color backgroundColor) throws IORuntimeException {
|
||||||
final BufferedImage bufferedImage = toBufferedImage(image, imageType, backgroundColor);
|
final BufferedImage bufferedImage = toBufferedImage(image, imageType, backgroundColor);
|
||||||
write(bufferedImage, destImageStream, quality);
|
write(bufferedImage, imageType, out, quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过{@link ImageWriter}写出图片到输出流
|
* 通过{@link ImageWriter}写出图片到输出流
|
||||||
*
|
*
|
||||||
* @param image 图片
|
* @param image 图片
|
||||||
|
* @param imageType 图片类型
|
||||||
* @param output 输出的Image流{@link ImageOutputStream}
|
* @param output 输出的Image流{@link ImageOutputStream}
|
||||||
* @param quality 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
|
* @param quality 质量,数字为0~1(不包括0和1)表示质量压缩比,除此数字外设置表示不压缩
|
||||||
* @since 4.3.2
|
* @since 4.3.2
|
||||||
*/
|
*/
|
||||||
public static void write(final Image image, final ImageOutputStream output, final float quality) {
|
public static void write(final Image image, final String imageType, final ImageOutputStream output, final float quality) {
|
||||||
ImgWriter.of(image, null)
|
ImgWriter.of(image, imageType)
|
||||||
.setQuality(quality)
|
.setQuality(quality)
|
||||||
.write(output);
|
.write(output);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user