public void testReplaceAll() throws Exception { assertEquals("", StringHelper.replaceAll("", "", "")); assertEquals(null, StringHelper.replaceAll(null, "", "")); assertEquals("foobar", StringHelper.replaceAll("foobar", "###", "DOT")); assertEquals("foobar", StringHelper.replaceAll("foo.bar", ".", "")); assertEquals("fooDOTbar", StringHelper.replaceAll("foo.bar", ".", "DOT")); assertEquals("fooDOTbar", StringHelper.replaceAll("foo###bar", "###", "DOT")); assertEquals("foobar", StringHelper.replaceAll("foo###bar", "###", "")); assertEquals("fooDOTbarDOTbaz", StringHelper.replaceAll("foo.bar.baz", ".", "DOT")); assertEquals("fooDOTbarDOTbazDOT", StringHelper.replaceAll("foo.bar.baz.", ".", "DOT")); assertEquals("DOTfooDOTbarDOTbazDOT", StringHelper.replaceAll(".foo.bar.baz.", ".", "DOT")); assertEquals("fooDOT", StringHelper.replaceAll("foo.", ".", "DOT")); }
/** * This method supports two modes to set a property: * * <p>1. Setting a property that has already been resolved, this is the case when {@code context} * and {@code refName} are NULL and {@code value} is non-NULL. * * <p>2. Setting a property that has not yet been resolved, the property will be resolved based on * the suitable methods found matching the property name on the {@code target} bean. For this mode * to be triggered the parameters {@code context} and {@code refName} must NOT be NULL, and {@code * value} MUST be NULL. */ public static boolean setProperty( CamelContext context, TypeConverter typeConverter, Object target, String name, Object value, String refName, boolean allowBuilderPattern) throws Exception { Class<?> clazz = target.getClass(); Collection<Method> setters; // we need to lookup the value from the registry if (context != null && refName != null && value == null) { setters = findSetterMethodsOrderedByParameterType(clazz, name, allowBuilderPattern); } else { // find candidates of setter methods as there can be overloaded setters setters = findSetterMethods(clazz, name, value, allowBuilderPattern); } if (setters.isEmpty()) { return false; } // loop and execute the best setter method Exception typeConversionFailed = null; for (Method setter : setters) { Class<?> parameterType = setter.getParameterTypes()[0]; Object ref = value; // try and lookup the reference based on the method if (context != null && refName != null && ref == null) { String s = StringHelper.replaceAll(refName, "#", ""); ref = CamelContextHelper.lookup(context, s); if (ref == null) { // try the next method if nothing was found continue; } else { // setter method has not the correct type // (must use ObjectHelper.isAssignableFrom which takes primitive types into account) boolean assignable = isAssignableFrom(parameterType, ref.getClass()); if (!assignable) { continue; } } } try { try { // If the type is null or it matches the needed type, just use the value directly if (value == null || isAssignableFrom(parameterType, ref.getClass())) { // we may want to set options on classes that has package view visibility, so override // the accessible setter.setAccessible(true); setter.invoke(target, ref); if (LOG.isDebugEnabled()) { LOG.debug( "Configured property: {} on bean: {} with value: {}", new Object[] {name, target, ref}); } return true; } else { // We need to convert it Object convertedValue = convert(typeConverter, parameterType, ref); // we may want to set options on classes that has package view visibility, so override // the accessible setter.setAccessible(true); setter.invoke(target, convertedValue); if (LOG.isDebugEnabled()) { LOG.debug( "Configured property: {} on bean: {} with value: {}", new Object[] {name, target, ref}); } return true; } } catch (InvocationTargetException e) { // lets unwrap the exception Throwable throwable = e.getCause(); if (throwable instanceof Exception) { Exception exception = (Exception) throwable; throw exception; } else { Error error = (Error) throwable; throw error; } } // ignore exceptions as there could be another setter method where we could type convert // successfully } catch (SecurityException e) { typeConversionFailed = e; } catch (NoTypeConversionAvailableException e) { typeConversionFailed = e; } catch (IllegalArgumentException e) { typeConversionFailed = e; } if (LOG.isTraceEnabled()) { LOG.trace( "Setter \"{}\" with parameter type \"{}\" could not be used for type conversions of {}", new Object[] {setter, parameterType, ref}); } } if (typeConversionFailed != null) { // we did not find a setter method to use, and if we did try to use a type converter then // throw // this kind of exception as the caused by will hint this error throw new IllegalArgumentException( "Could not find a suitable setter for property: " + name + " as there isn't a setter method with same type: " + (value != null ? value.getClass().getCanonicalName() : "[null]") + " nor type conversion possible: " + typeConversionFailed.getMessage()); } else { return false; } }