@Test @Config(interfaceClass = Greeting.class, filter = {"f1, f2"}, listener = {"l1, l2"}, parameters = {"k1", "v1", "k2", "v2"}) public void appendAnnotation() throws Exception { Config config = getClass().getMethod("appendAnnotation").getAnnotation(Config.class); AnnotationConfig annotationConfig = new AnnotationConfig(); annotationConfig.appendAnnotation(Config.class, config); TestCase.assertSame(Greeting.class, annotationConfig.getInterface()); TestCase.assertEquals("f1, f2", annotationConfig.getFilter()); TestCase.assertEquals("l1, l2", annotationConfig.getListener()); TestCase.assertEquals(2, annotationConfig.getParameters().size()); TestCase.assertEquals("v1", annotationConfig.getParameters().get("k1")); TestCase.assertEquals("v2", annotationConfig.getParameters().get("k2")); assertThat(annotationConfig.toString(), Matchers.containsString("filter=\"f1, f2\" ")); assertThat(annotationConfig.toString(), Matchers.containsString("listener=\"l1, l2\" ")); }
protected void appendAnnotation(Class<?> annotationClass, Object annotation) { Method[] methods = annotationClass.getMethods(); for (Method method : methods) { if (method.getDeclaringClass() != Object.class && method.getReturnType() != void.class && method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) { try { String property = method.getName(); if ("interfaceClass".equals(property) || "interfaceName".equals(property)) { property = "interface"; } String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); Object value = method.invoke(annotation); if (!isAnnotationArray(method.getReturnType()) && value != null && !value.equals(method.getDefaultValue())) { Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType()); if ("filter".equals(property) || "listener".equals(property)) { parameterType = String.class; value = StringUtils.join((String[]) value, ","); } else if ("parameters".equals(property)) { parameterType = Map.class; value = CollectionUtils.toStringMap((String[]) value); } try { Method setterMethod = getClass().getMethod(setter, parameterType); setterMethod.invoke(this, value); } catch (NoSuchMethodException e) { } } } catch (Throwable e) { logger.error(e.getMessage(), e); } } } }
AbstractConfig implements Serializable { protected void appendAnnotation(Class<?> annotationClass, Object annotation) { Method[] methods = annotationClass.getMethods(); for (Method method : methods) { if (method.getDeclaringClass() != Object.class && method.getReturnType() != void.class && method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) { try { String property = method.getName(); if ("interfaceClass".equals(property) || "interfaceName".equals(property)) { property = "interface"; } String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); Object value = method.invoke(annotation); if (!isAnnotationArray(method.getReturnType()) && value != null && !value.equals(method.getDefaultValue())) { Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType()); if ("filter".equals(property) || "listener".equals(property)) { parameterType = String.class; value = StringUtils.join((String[]) value, ","); } else if ("parameters".equals(property)) { parameterType = Map.class; value = CollectionUtils.toStringMap((String[]) value); } try { Method setterMethod = getClass().getMethod(setter, parameterType); setterMethod.invoke(this, value); } catch (NoSuchMethodException e) { } } } catch (Throwable e) { logger.error(e.getMessage(), e); } } } } }
AbstractConfig implements Serializable { protected void appendAnnotation(Class<?> annotationClass, Object annotation) { Method[] methods = annotationClass.getMethods(); for (Method method : methods) { if (method.getDeclaringClass() != Object.class && method.getReturnType() != void.class && method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) { try { String property = method.getName(); if ("interfaceClass".equals(property) || "interfaceName".equals(property)) { property = "interface"; } String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); Object value = method.invoke(annotation); if (!isAnnotationArray(method.getReturnType()) && value != null && !value.equals(method.getDefaultValue())) { Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType()); if ("filter".equals(property) || "listener".equals(property)) { parameterType = String.class; value = StringUtils.join((String[]) value, ","); } else if ("parameters".equals(property)) { parameterType = Map.class; value = CollectionUtils.toStringMap((String[]) value); } try { Method setterMethod = getClass().getMethod(setter, parameterType); setterMethod.invoke(this, value); } catch (NoSuchMethodException e) { } } } catch (Throwable e) { logger.error(e.getMessage(), e); } } } }  }
AbstractConfig implements Serializable { protected void appendAnnotation(Class<?> annotationClass, Object annotation) { Method[] methods = annotationClass.getMethods(); for (Method method : methods) { if (method.getDeclaringClass() != Object.class && method.getReturnType() != void.class && method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) { try { String property = method.getName(); if ("interfaceClass".equals(property) || "interfaceName".equals(property)) { property = "interface"; } String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); Object value = method.invoke(annotation); if (!isAnnotationArray(method.getReturnType()) && value != null && !value.equals(method.getDefaultValue())) { Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType()); if ("filter".equals(property) || "listener".equals(property)) { parameterType = String.class; value = StringUtils.join((String[]) value, ","); } else if ("parameters".equals(property)) { parameterType = Map.class; value = CollectionUtils.toStringMap((String[]) value); } try { Method setterMethod = getClass().getMethod(setter, parameterType); setterMethod.invoke(this, value); } catch (NoSuchMethodException e) { } } } catch (Throwable e) { logger.error(e.getMessage(), e); } } } }  @Parameter(excluded = true) String getId(); void setId(String id); @Override String toString(); }
AbstractConfig implements Serializable { protected void appendAnnotation(Class<?> annotationClass, Object annotation) { Method[] methods = annotationClass.getMethods(); for (Method method : methods) { if (method.getDeclaringClass() != Object.class && method.getReturnType() != void.class && method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) { try { String property = method.getName(); if ("interfaceClass".equals(property) || "interfaceName".equals(property)) { property = "interface"; } String setter = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); Object value = method.invoke(annotation); if (!isAnnotationArray(method.getReturnType()) && value != null && !value.equals(method.getDefaultValue())) { Class<?> parameterType = ReflectUtils.getBoxedClass(method.getReturnType()); if ("filter".equals(property) || "listener".equals(property)) { parameterType = String.class; value = StringUtils.join((String[]) value, ","); } else if ("parameters".equals(property)) { parameterType = Map.class; value = CollectionUtils.toStringMap((String[]) value); } try { Method setterMethod = getClass().getMethod(setter, parameterType); setterMethod.invoke(this, value); } catch (NoSuchMethodException e) { } } } catch (Throwable e) { logger.error(e.getMessage(), e); } } } }  @Parameter(excluded = true) String getId(); void setId(String id); @Override String toString();  }