Search in sources :

Example 1 with Parameter

use of org.apache.dubbo.config.support.Parameter in project dubbo by alibaba.

the class AbstractConfig method getMetaData.

/**
 * Should be called after Config was fully initialized.
 * // FIXME: this method should be completely replaced by appendParameters
 *
 * @return
 * @see AbstractConfig#appendParameters(Map, Object, String)
 * <p>
 * Notice! This method should include all properties in the returning map, treat @Parameter differently compared to appendParameters.
 */
public Map<String, String> getMetaData() {
    Map<String, String> metaData = new HashMap<>();
    Method[] methods = this.getClass().getMethods();
    for (Method method : methods) {
        try {
            String name = method.getName();
            if (MethodUtils.isMetaMethod(method)) {
                String key;
                Parameter parameter = method.getAnnotation(Parameter.class);
                if (parameter != null && parameter.key().length() > 0 && parameter.useKeyAsProperty()) {
                    key = parameter.key();
                } else {
                    key = calculateAttributeFromGetter(name);
                }
                // if (method.getReturnType() == Object.class || parameter != null && parameter.excluded()) {
                if (method.getReturnType() == Object.class) {
                    metaData.put(key, null);
                    continue;
                }
                /**
                 * Attributes annotated as deprecated should not override newly added replacement.
                 */
                if (MethodUtils.isDeprecated(method) && metaData.get(key) != null) {
                    continue;
                }
                Object value = method.invoke(this);
                String str = String.valueOf(value).trim();
                if (value != null && str.length() > 0) {
                    metaData.put(key, str);
                } else {
                    metaData.put(key, null);
                }
            } else if (isParametersGetter(method)) {
                Map<String, String> map = (Map<String, String>) method.invoke(this, new Object[0]);
                metaData.putAll(convert(map, ""));
            }
        } catch (Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
    return metaData;
}
Also used : HashMap(java.util.HashMap) Parameter(org.apache.dubbo.config.support.Parameter) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Parameter

use of org.apache.dubbo.config.support.Parameter in project dubbo by alibaba.

the class AbstractConfig method appendParameters.

@SuppressWarnings("unchecked")
public static void appendParameters(Map<String, String> parameters, Object config, String prefix) {
    if (config == null) {
        return;
    }
    Method[] methods = config.getClass().getMethods();
    for (Method method : methods) {
        try {
            String name = method.getName();
            if (MethodUtils.isGetter(method)) {
                Parameter parameter = method.getAnnotation(Parameter.class);
                if (method.getReturnType() == Object.class || parameter != null && parameter.excluded()) {
                    continue;
                }
                String key;
                if (parameter != null && parameter.key().length() > 0) {
                    key = parameter.key();
                } else {
                    key = calculatePropertyFromGetter(name);
                }
                Object value = method.invoke(config);
                String str = String.valueOf(value).trim();
                if (value != null && str.length() > 0) {
                    if (parameter != null && parameter.escaped()) {
                        str = URL.encode(str);
                    }
                    if (parameter != null && parameter.append()) {
                        String pre = parameters.get(key);
                        if (pre != null && pre.length() > 0) {
                            str = pre + "," + str;
                        }
                    }
                    if (prefix != null && prefix.length() > 0) {
                        key = prefix + "." + key;
                    }
                    parameters.put(key, str);
                } else if (parameter != null && parameter.required()) {
                    throw new IllegalStateException(config.getClass().getSimpleName() + "." + key + " == null");
                }
            } else if (isParametersGetter(method)) {
                Map<String, String> map = (Map<String, String>) method.invoke(config, new Object[0]);
                parameters.putAll(convert(map, prefix));
            }
        } catch (Exception e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
}
Also used : Parameter(org.apache.dubbo.config.support.Parameter) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Parameter

use of org.apache.dubbo.config.support.Parameter in project dubbo by alibaba.

the class AbstractConfig method extractPropertyName.

private static String extractPropertyName(Class<?> clazz, Method setter) throws Exception {
    String propertyName = setter.getName().substring("set".length());
    Method getter = null;
    try {
        getter = clazz.getMethod("get" + propertyName);
    } catch (NoSuchMethodException e) {
        getter = clazz.getMethod("is" + propertyName);
    }
    Parameter parameter = getter.getAnnotation(Parameter.class);
    if (parameter != null && StringUtils.isNotEmpty(parameter.key()) && parameter.useKeyAsProperty()) {
        propertyName = parameter.key();
    } else {
        propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
    }
    return propertyName;
}
Also used : Parameter(org.apache.dubbo.config.support.Parameter) Method(java.lang.reflect.Method)

Example 4 with Parameter

use of org.apache.dubbo.config.support.Parameter in project dubbo by alibaba.

the class AbstractConfig method equals.

@Override
public boolean equals(Object obj) {
    if (obj == null || !(obj.getClass().getName().equals(this.getClass().getName()))) {
        return false;
    }
    Method[] methods = this.getClass().getMethods();
    for (Method method1 : methods) {
        if (MethodUtils.isGetter(method1)) {
            Parameter parameter = method1.getAnnotation(Parameter.class);
            if (parameter != null && parameter.excluded()) {
                continue;
            }
            try {
                Method method2 = obj.getClass().getMethod(method1.getName(), method1.getParameterTypes());
                Object value1 = method1.invoke(this, new Object[] {});
                Object value2 = method2.invoke(obj, new Object[] {});
                if (!Objects.equals(value1, value2)) {
                    return false;
                }
            } catch (Exception e) {
                return true;
            }
        }
    }
    return true;
}
Also used : Parameter(org.apache.dubbo.config.support.Parameter) Method(java.lang.reflect.Method)

Example 5 with Parameter

use of org.apache.dubbo.config.support.Parameter in project dubbo by alibaba.

the class AbstractConfig method hashCode.

@Override
public int hashCode() {
    int hashCode = 1;
    Method[] methods = this.getClass().getMethods();
    for (Method method : methods) {
        if (MethodUtils.isGetter(method)) {
            Parameter parameter = method.getAnnotation(Parameter.class);
            if (parameter != null && parameter.excluded()) {
                continue;
            }
            try {
                Object value = method.invoke(this, new Object[] {});
                hashCode = 31 * hashCode + value.hashCode();
            } catch (Exception ignored) {
            // ignored
            }
        }
    }
    if (hashCode == 0) {
        hashCode = 1;
    }
    return hashCode;
}
Also used : Parameter(org.apache.dubbo.config.support.Parameter) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)6 Parameter (org.apache.dubbo.config.support.Parameter)6 HashMap (java.util.HashMap)2 Map (java.util.Map)2