diff --git a/core/pom.xml b/core/pom.xml
index 5b0813aef..4a9a9ba9b 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -134,6 +134,10 @@
com.taobao.middlewarelogger.api
+
+ com.alibaba.arthas
+ arthas-repackage-logger
+ log4j
diff --git a/core/src/main/java/com/taobao/arthas/core/config/BinderUtils.java b/core/src/main/java/com/taobao/arthas/core/config/BinderUtils.java
new file mode 100644
index 000000000..fe52eb441
--- /dev/null
+++ b/core/src/main/java/com/taobao/arthas/core/config/BinderUtils.java
@@ -0,0 +1,110 @@
+package com.taobao.arthas.core.config;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import com.taobao.arthas.core.env.Environment;
+
+/**
+ *
+ * @author hengyunabc 2020-01-10
+ *
+ */
+public class BinderUtils {
+
+ public static void inject(Environment environment, Object instance) {
+ inject(environment, null, null, instance);
+ }
+
+ public static void inject(Environment environment, String prefix, Object instance) {
+ inject(environment, null, prefix, instance);
+ }
+
+ public static void inject(Environment environment, String parentPrefix, String prefix, Object instance) {
+ Class extends Object> type = instance.getClass();
+ try {
+ Config annotation = type.getAnnotation(Config.class);
+
+ if (prefix == null) {
+ prefix = "";
+ }
+
+ if (annotation == null) {
+ prefix = parentPrefix + '.' + prefix;
+ } else {
+ prefix = annotation.prefix();
+ if (prefix != null) {
+ if (parentPrefix != null && parentPrefix.length() > 0) {
+ prefix = parentPrefix + '.' + prefix;
+ }
+ }
+ }
+
+ Method[] declaredMethods = type.getDeclaredMethods();
+ if (declaredMethods != null) {
+ // 获取到所有setter方法,再提取出field。根据前缀从 properties里取出值,再尝试用setter方法注入到对象里
+ for (Method method : declaredMethods) {
+ String methodName = method.getName();
+ Class>[] parameterTypes = method.getParameterTypes();
+
+ if (parameterTypes != null && parameterTypes.length == 1 && methodName.startsWith("set")
+ && methodName.length() > "set".length()) {
+
+ String field = getFieldNameFromSetterMethod(methodName);
+ Object reslovedValue = environment.getProperty(prefix + '.' + field, parameterTypes[0]);
+ if (reslovedValue != null) {
+ method.invoke(instance, new Object[] { reslovedValue });
+ }
+ }
+ }
+ }
+
+ } catch (Exception e) {
+ throw new RuntimeException("inject error. prefix: " + prefix + ", instance: " + instance, e);
+ }
+
+ // process @NestedConfig
+ Field[] fields = type.getDeclaredFields();
+ if (fields != null) {
+ for (Field field : fields) {
+ NestedConfig nestedConfig = field.getAnnotation(NestedConfig.class);
+ if (nestedConfig != null) {
+ String prefixForField = field.getName();
+ if (parentPrefix != null && prefix.length() > 0) {
+ prefixForField = prefix + '.' + prefixForField;
+ }
+
+ field.setAccessible(true);
+ try {
+ Object fieldValue = field.get(instance);
+ if (fieldValue == null) {
+ fieldValue = field.getType().newInstance();
+ }
+ inject(environment, prefix, prefixForField, fieldValue);
+
+ field.set(instance, fieldValue);
+ } catch (Exception e) {
+ throw new RuntimeException("process @NestedConfig error, field: " + field + ", prefix: "
+ + prefix + ", instance: " + instance, e);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * 从setter方法获取到field的String。比如 setHost, 则获取到的是host。
+ *
+ * @param methodName
+ * @return
+ */
+ private static String getFieldNameFromSetterMethod(String methodName) {
+ String field = methodName.substring("set".length());
+ String startPart = field.substring(0, 1).toLowerCase();
+ String endPart = field.substring(1);
+
+ field = startPart + endPart;
+ return field;
+ }
+
+}
diff --git a/core/src/main/java/com/taobao/arthas/core/config/Config.java b/core/src/main/java/com/taobao/arthas/core/config/Config.java
new file mode 100644
index 000000000..666b46ce0
--- /dev/null
+++ b/core/src/main/java/com/taobao/arthas/core/config/Config.java
@@ -0,0 +1,19 @@
+package com.taobao.arthas.core.config;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ *
+ * @author hengyunabc 2019-08-05
+ *
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface Config {
+
+ String prefix() default "";
+
+}
\ No newline at end of file
diff --git a/core/src/main/java/com/taobao/arthas/core/config/Configure.java b/core/src/main/java/com/taobao/arthas/core/config/Configure.java
index d2f91a9fe..17a62d868 100644
--- a/core/src/main/java/com/taobao/arthas/core/config/Configure.java
+++ b/core/src/main/java/com/taobao/arthas/core/config/Configure.java
@@ -15,6 +15,7 @@ import static java.lang.reflect.Modifier.isStatic;
* @author vlinux
* @author hengyunabc 2018-11-12
*/
+@Config(prefix = "arthas")
public class Configure {
public static final long DEFAULT_SESSION_TIMEOUT_SECONDS = ShellServerOptions.DEFAULT_SESSION_TIMEOUT/1000;
private String ip;
@@ -117,9 +118,6 @@ public class Configure {
this.statUrl = statUrl;
}
- // 对象的编码解码器
- private final static FeatureCodec codec = new FeatureCodec(';', '=');
-
/**
* 序列化成字符串
*
@@ -148,7 +146,7 @@ public class Configure {
}
- return codec.toString(map);
+ return FeatureCodec.DEFAULT_COMMANDLINE_CODEC.toString(map);
}
/**
@@ -159,7 +157,7 @@ public class Configure {
*/
public static Configure toConfigure(String toString) throws IllegalAccessException {
final Configure configure = new Configure();
- final Map map = codec.toMap(toString);
+ final Map map = FeatureCodec.DEFAULT_COMMANDLINE_CODEC.toMap(toString);
for (Map.Entry entry : map.entrySet()) {
final Field field = ArthasReflectUtils.getField(Configure.class, entry.getKey());
diff --git a/core/src/main/java/com/taobao/arthas/core/config/FeatureCodec.java b/core/src/main/java/com/taobao/arthas/core/config/FeatureCodec.java
index fa9c2e354..8d5cec807 100644
--- a/core/src/main/java/com/taobao/arthas/core/config/FeatureCodec.java
+++ b/core/src/main/java/com/taobao/arthas/core/config/FeatureCodec.java
@@ -16,6 +16,8 @@ import static com.taobao.arthas.core.util.StringUtils.isBlank;
* Created by dukun on 15/3/31.
*/
public class FeatureCodec {
+ // 对象的编码解码器
+ public final static FeatureCodec DEFAULT_COMMANDLINE_CODEC = new FeatureCodec(';', '=');
/**
* KV片段分割符
diff --git a/core/src/main/java/com/taobao/arthas/core/config/NestedConfig.java b/core/src/main/java/com/taobao/arthas/core/config/NestedConfig.java
new file mode 100644
index 000000000..1c48590ef
--- /dev/null
+++ b/core/src/main/java/com/taobao/arthas/core/config/NestedConfig.java
@@ -0,0 +1,17 @@
+package com.taobao.arthas.core.config;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ *
+ * @author hengyunabc 2019-08-05
+ *
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+public @interface NestedConfig {
+
+}
\ No newline at end of file
diff --git a/core/src/main/java/com/taobao/arthas/core/config/SecondConfig.java b/core/src/main/java/com/taobao/arthas/core/config/SecondConfig.java
new file mode 100644
index 000000000..013a45790
--- /dev/null
+++ b/core/src/main/java/com/taobao/arthas/core/config/SecondConfig.java
@@ -0,0 +1,5 @@
+package com.taobao.arthas.core.config;
+
+public class SecondConfig {
+
+}
diff --git a/core/src/main/java/com/taobao/arthas/core/config/TestConfig.java b/core/src/main/java/com/taobao/arthas/core/config/TestConfig.java
new file mode 100644
index 000000000..2145f47ca
--- /dev/null
+++ b/core/src/main/java/com/taobao/arthas/core/config/TestConfig.java
@@ -0,0 +1,9 @@
+package com.taobao.arthas.core.config;
+
+
+@Config
+public class TestConfig {
+
+ @NestedConfig
+ SecondConfig secondConfig;
+}
diff --git a/core/src/main/java/com/taobao/arthas/core/env/AbstractPropertyResolver.java b/core/src/main/java/com/taobao/arthas/core/env/AbstractPropertyResolver.java
index 9cc715aa8..5ccda39c3 100644
--- a/core/src/main/java/com/taobao/arthas/core/env/AbstractPropertyResolver.java
+++ b/core/src/main/java/com/taobao/arthas/core/env/AbstractPropertyResolver.java
@@ -19,6 +19,9 @@ package com.taobao.arthas.core.env;
import java.util.LinkedHashSet;
import java.util.Set;
+import com.taobao.arthas.core.env.convert.ConfigurableConversionService;
+import com.taobao.arthas.core.env.convert.DefaultConversionService;
+
/**
* Abstract base class for resolving properties against any underlying source.
*
diff --git a/core/src/main/java/com/taobao/arthas/core/env/ArthasEnvironment.java b/core/src/main/java/com/taobao/arthas/core/env/ArthasEnvironment.java
index d7bfccdf5..72186b8cb 100644
--- a/core/src/main/java/com/taobao/arthas/core/env/ArthasEnvironment.java
+++ b/core/src/main/java/com/taobao/arthas/core/env/ArthasEnvironment.java
@@ -27,6 +27,13 @@ public class ArthasEnvironment implements Environment {
new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}
+ /**
+ * Add the given property source object with highest precedence.
+ */
+ public void addFirst(PropertySource> propertySource) {
+ this.propertySources.addFirst(propertySource);
+ }
+
/**
* Add the given property source object with lowest precedence.
*/
diff --git a/core/src/main/java/com/taobao/arthas/core/env/ConfigurablePropertyResolver.java b/core/src/main/java/com/taobao/arthas/core/env/ConfigurablePropertyResolver.java
index c0f63ae68..8960ea094 100644
--- a/core/src/main/java/com/taobao/arthas/core/env/ConfigurablePropertyResolver.java
+++ b/core/src/main/java/com/taobao/arthas/core/env/ConfigurablePropertyResolver.java
@@ -16,6 +16,8 @@
package com.taobao.arthas.core.env;
+import com.taobao.arthas.core.env.convert.ConfigurableConversionService;
+
/**
* Configuration interface to be implemented by most if not all
* {@link PropertyResolver} types. Provides facilities for accessing and
diff --git a/core/src/main/java/com/taobao/arthas/core/env/convert/ConfigurableConversionService.java b/core/src/main/java/com/taobao/arthas/core/env/convert/ConfigurableConversionService.java
index 28ff54841..9ba27ee44 100644
--- a/core/src/main/java/com/taobao/arthas/core/env/convert/ConfigurableConversionService.java
+++ b/core/src/main/java/com/taobao/arthas/core/env/convert/ConfigurableConversionService.java
@@ -14,7 +14,9 @@
* limitations under the License.
*/
-package com.taobao.arthas.core.env;
+package com.taobao.arthas.core.env.convert;
+
+import com.taobao.arthas.core.env.ConversionService;
/**
* Configuration interface to be implemented by most if not all
@@ -28,7 +30,7 @@ package com.taobao.arthas.core.env;
*
* @author Chris Beams
* @since 3.1
- * @see org.springframework.core.env.ConfigurablePropertyResolver#getConversionService()
+ * @see com.taobao.arthas.core.env.springframework.core.env.ConfigurablePropertyResolver#getConversionService()
* @see org.springframework.core.env.ConfigurableEnvironment
* @see org.springframework.context.ConfigurableApplicationContext#getEnvironment()
*/
diff --git a/core/src/main/java/com/taobao/arthas/core/env/convert/Converter.java b/core/src/main/java/com/taobao/arthas/core/env/convert/Converter.java
new file mode 100644
index 000000000..2ab514626
--- /dev/null
+++ b/core/src/main/java/com/taobao/arthas/core/env/convert/Converter.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2002-2015 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.taobao.arthas.core.env.convert;
+
+/**
+ * A converter converts a source object of type S to a target of type T.
+ * Implementations of this interface are thread-safe and can be shared.
+ *
+ *
Implementations may additionally implement {@link ConditionalConverter}.
+ *
+ * @author Keith Donald
+ * @since 3.0
+ * @param The source type
+ * @param The target type
+ */
+public interface Converter {
+
+ /**
+ * Convert the source of type S to target type T.
+ * @param source the source object to convert, which must be an instance of S (never {@code null})
+ * @return the converted object, which must be an instance of T (potentially {@code null})
+ * @throws IllegalArgumentException if the source could not be converted to the desired target type
+ */
+ T convert(S source, Class targetType);
+
+}
diff --git a/core/src/main/java/com/taobao/arthas/core/env/convert/ConvertiblePair.java b/core/src/main/java/com/taobao/arthas/core/env/convert/ConvertiblePair.java
new file mode 100644
index 000000000..d792db621
--- /dev/null
+++ b/core/src/main/java/com/taobao/arthas/core/env/convert/ConvertiblePair.java
@@ -0,0 +1,52 @@
+package com.taobao.arthas.core.env.convert;
+
+/**
+ * Holder for a source-to-target class pair.
+ */
+public final class ConvertiblePair {
+
+ private final Class> sourceType;
+
+ private final Class> targetType;
+
+ /**
+ * Create a new source-to-target pair.
+ *
+ * @param sourceType the source type
+ * @param targetType the target type
+ */
+ public ConvertiblePair(Class> sourceType, Class> targetType) {
+ this.sourceType = sourceType;
+ this.targetType = targetType;
+ }
+
+ public Class> getSourceType() {
+ return this.sourceType;
+ }
+
+ public Class> getTargetType() {
+ return this.targetType;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || obj.getClass() != ConvertiblePair.class) {
+ return false;
+ }
+ ConvertiblePair other = (ConvertiblePair) obj;
+ return this.sourceType.equals(other.sourceType) && this.targetType.equals(other.targetType);
+ }
+
+ @Override
+ public int hashCode() {
+ return this.sourceType.hashCode() * 31 + this.targetType.hashCode();
+ }
+
+ @Override
+ public String toString() {
+ return this.sourceType.getName() + " -> " + this.targetType.getName();
+ }
+}
\ No newline at end of file
diff --git a/core/src/main/java/com/taobao/arthas/core/env/convert/DefaultConversionService.java b/core/src/main/java/com/taobao/arthas/core/env/convert/DefaultConversionService.java
index 462ce4bd3..cc31e5417 100644
--- a/core/src/main/java/com/taobao/arthas/core/env/convert/DefaultConversionService.java
+++ b/core/src/main/java/com/taobao/arthas/core/env/convert/DefaultConversionService.java
@@ -1,17 +1,123 @@
-package com.taobao.arthas.core.env;
+package com.taobao.arthas.core.env.convert;
+
+import java.lang.reflect.Array;
+import java.net.InetAddress;
+import java.util.Arrays;
+import java.util.concurrent.ConcurrentHashMap;
public class DefaultConversionService implements ConfigurableConversionService {
+ private static ConcurrentHashMap converters = new ConcurrentHashMap();
+
+ public DefaultConversionService() {
+ addDefaultConverter();
+
+ }
+
+ private void addDefaultConverter() {
+ converters.put(new ConvertiblePair(String.class, Integer.class), new StringToIntegerConverter());
+ converters.put(new ConvertiblePair(String.class, Long.class), new StringToLongConverter());
+
+ converters.put(new ConvertiblePair(String.class, Boolean.class), new StringToBooleanConverter());
+
+ converters.put(new ConvertiblePair(String.class, InetAddress.class), new StringToInetAddressConverter());
+
+ converters.put(new ConvertiblePair(String.class, Enum.class), new StringToEnumConverter());
+
+ converters.put(new ConvertiblePair(String.class, Arrays.class), new StringToArrayConverter(this));
+
+ }
+
@Override
public boolean canConvert(Class> sourceType, Class> targetType) {
- // TODO Auto-generated method stub
- return true;
+ if (sourceType == targetType) {
+ return true;
+ }
+
+ if (targetType.isPrimitive()) {
+ targetType = objectiveClass(targetType);
+ }
+
+ if (converters.containsKey(new ConvertiblePair(sourceType, targetType))) {
+ return true;
+ }
+ if (targetType.isEnum()) {
+ if (converters.containsKey(new ConvertiblePair(sourceType, Enum.class))) {
+ return true;
+ }
+ }
+
+ if (targetType.isArray()) {
+ return true;
+ }
+ return false;
}
@Override
public T convert(Object source, Class targetType) {
- // TODO Auto-generated method stub
+
+ if (targetType.isPrimitive()) {
+ targetType = (Class) objectiveClass(targetType);
+ }
+
+ Converter converter = converters.get(new ConvertiblePair(source.getClass(), targetType));
+
+ if (converter == null && targetType.isArray()) {
+ converter = converters.get(new ConvertiblePair(source.getClass(), Arrays.class));
+ }
+
+ if (converter == null && targetType.isEnum()) {
+ converter = converters.get(new ConvertiblePair(source.getClass(), Enum.class));
+ }
+ if (converter != null) {
+ return (T) converter.convert(source, targetType);
+ }
+
return (T) source;
}
+ /**
+ * Get an array class of the given class.
+ *
+ * @param klass to get an array class of
+ * @param the targeted class
+ * @return an array class of the given class
+ */
+ public static Class arrayClass(Class klass) {
+ return (Class) Array.newInstance(klass, 0).getClass();
+ }
+
+ /**
+ * Get the class that extends {@link Object} that represent the given class.
+ *
+ * @param klass to get the object class of
+ * @return the class that extends Object class and represent the given class
+ */
+ public static Class> objectiveClass(Class> klass) {
+ Class> component = klass.getComponentType();
+ if (component != null) {
+ if (component.isPrimitive() || component.isArray())
+ return arrayClass(objectiveClass(component));
+ } else if (klass.isPrimitive()) {
+ if (klass == char.class)
+ return Character.class;
+ if (klass == int.class)
+ return Integer.class;
+ if (klass == boolean.class)
+ return Boolean.class;
+ if (klass == byte.class)
+ return Byte.class;
+ if (klass == double.class)
+ return Double.class;
+ if (klass == float.class)
+ return Float.class;
+ if (klass == long.class)
+ return Long.class;
+ if (klass == short.class)
+ return Short.class;
+ }
+
+ return klass;
+ }
+
}
diff --git a/core/src/main/java/com/taobao/arthas/core/env/convert/ObjectToStringConverter.java b/core/src/main/java/com/taobao/arthas/core/env/convert/ObjectToStringConverter.java
new file mode 100644
index 000000000..cb5f4059a
--- /dev/null
+++ b/core/src/main/java/com/taobao/arthas/core/env/convert/ObjectToStringConverter.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2002-2014 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.taobao.arthas.core.env.convert;
+
+/**
+ * Simply calls {@link Object#toString()} to convert a source Object to a String.
+ *
+ * @author Keith Donald
+ * @since 3.0
+ */
+final class ObjectToStringConverter implements Converter