diff --git a/redisson/src/main/java/org/redisson/config/Config.java b/redisson/src/main/java/org/redisson/config/Config.java index 18ebd7b87..bb478349a 100644 --- a/redisson/src/main/java/org/redisson/config/Config.java +++ b/redisson/src/main/java/org/redisson/config/Config.java @@ -30,6 +30,7 @@ import org.redisson.connection.ConnectionManager; import org.redisson.connection.DnsAddressResolverGroupFactory; import org.redisson.connection.AddressResolverGroupFactory; import org.redisson.connection.ReplicatedConnectionManager; +import org.redisson.misc.URIBuilder; import io.netty.channel.EventLoopGroup; @@ -95,6 +96,10 @@ public class Config { public Config() { } + + static { + URIBuilder.patchUriObject(); + } public Config(Config oldConf) { setUseLinuxNativeEpoll(oldConf.isUseLinuxNativeEpoll()); diff --git a/redisson/src/main/java/org/redisson/config/ConfigSupport.java b/redisson/src/main/java/org/redisson/config/ConfigSupport.java index 804c63709..3a1544d49 100644 --- a/redisson/src/main/java/org/redisson/config/ConfigSupport.java +++ b/redisson/src/main/java/org/redisson/config/ConfigSupport.java @@ -19,9 +19,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Reader; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; import java.net.URI; import java.net.URL; import java.util.List; @@ -124,102 +121,63 @@ public class ConfigSupport { private ObjectMapper jsonMapper = createMapper(null, null); private ObjectMapper yamlMapper = createMapper(new YAMLFactory(), null); - - private void patchUriObject() throws IOException { - patchUriField("lowMask", "L_DASH"); - patchUriField("highMask", "H_DASH"); - } - - private void patchUriField(String methodName, String fieldName) - throws IOException { - try { - Method lowMask = URI.class.getDeclaredMethod(methodName, String.class); - lowMask.setAccessible(true); - Long lowMaskValue = (Long) lowMask.invoke(null, "-_"); - - Field lowDash = URI.class.getDeclaredField(fieldName); - - Field modifiers = Field.class.getDeclaredField("modifiers"); - modifiers.setAccessible(true); - modifiers.setInt(lowDash, lowDash.getModifiers() & ~Modifier.FINAL); - - lowDash.setAccessible(true); - lowDash.setLong(null, lowMaskValue); - } catch (Exception e) { - throw new IOException(e); - } - } public T fromJSON(String content, Class configType) throws IOException { - patchUriObject(); return jsonMapper.readValue(content, configType); } public T fromJSON(File file, Class configType) throws IOException { - patchUriObject(); return fromJSON(file, configType, null); } public T fromJSON(File file, Class configType, ClassLoader classLoader) throws IOException { - patchUriObject(); jsonMapper = createMapper(null, classLoader); return jsonMapper.readValue(file, configType); } public T fromJSON(URL url, Class configType) throws IOException { - patchUriObject(); return jsonMapper.readValue(url, configType); } public T fromJSON(Reader reader, Class configType) throws IOException { - patchUriObject(); return jsonMapper.readValue(reader, configType); } public T fromJSON(InputStream inputStream, Class configType) throws IOException { - patchUriObject(); return jsonMapper.readValue(inputStream, configType); } public String toJSON(Config config) throws IOException { - patchUriObject(); return jsonMapper.writeValueAsString(config); } public T fromYAML(String content, Class configType) throws IOException { - patchUriObject(); return yamlMapper.readValue(content, configType); } public T fromYAML(File file, Class configType) throws IOException { - patchUriObject(); return yamlMapper.readValue(file, configType); } public T fromYAML(File file, Class configType, ClassLoader classLoader) throws IOException { - patchUriObject(); yamlMapper = createMapper(new YAMLFactory(), classLoader); return yamlMapper.readValue(file, configType); } public T fromYAML(URL url, Class configType) throws IOException { - patchUriObject(); return yamlMapper.readValue(url, configType); } public T fromYAML(Reader reader, Class configType) throws IOException { - patchUriObject(); return yamlMapper.readValue(reader, configType); } public T fromYAML(InputStream inputStream, Class configType) throws IOException { - patchUriObject(); return yamlMapper.readValue(inputStream, configType); } public String toYAML(Config config) throws IOException { - patchUriObject(); return yamlMapper.writeValueAsString(config); } diff --git a/redisson/src/main/java/org/redisson/misc/URIBuilder.java b/redisson/src/main/java/org/redisson/misc/URIBuilder.java index 3a9476d2f..65ee49438 100644 --- a/redisson/src/main/java/org/redisson/misc/URIBuilder.java +++ b/redisson/src/main/java/org/redisson/misc/URIBuilder.java @@ -15,6 +15,9 @@ */ package org.redisson.misc; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.net.InetSocketAddress; import java.net.URI; import java.util.regex.Pattern; @@ -40,6 +43,31 @@ public class URIBuilder { return URI.create(uri.replace(s, "[" + s + "]")); } + public static void patchUriObject() { + try { + patchUriField(35184372088832L, "L_DASH"); + patchUriField(2147483648L, "H_DASH"); + } catch (IOException e) { + throw new IllegalStateException(e); + } + } + + private static void patchUriField(Long maskValue, String fieldName) + throws IOException { + try { + Field field = URI.class.getDeclaredField(fieldName); + + Field modifiers = Field.class.getDeclaredField("modifiers"); + modifiers.setAccessible(true); + modifiers.setInt(field, field.getModifiers() & ~Modifier.FINAL); + + field.setAccessible(true); + field.setLong(null, maskValue); + } catch (Exception e) { + throw new IOException(e); + } + } + public static boolean isValidIP(String host) { if (ipv4Pattern.matcher(host).matches()) { return true;