Fix bug in ReflectionUtil in recognizing ReflectionConfigurable nested objects

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@418964 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian T. Co 2006-07-04 09:38:58 +00:00
parent 76d2eccf7c
commit 9f3fcde566
2 changed files with 101 additions and 2 deletions

View File

@ -44,25 +44,38 @@ public final class ReflectionUtil {
debugInfo = "Invoking: " + targetClass.getName();
StringTokenizer tokenizer = new StringTokenizer(key, ".");
String keySubString = key;
int tokenCount = tokenizer.countTokens();
// For nested settings, get the object first. -1, do not count the last token
for (int j=0; j<tokenCount-1; j++) {
// Find getter method first
String name = tokenizer.nextToken();
// Check if the target object will accept the settings
if (target instanceof ReflectionConfigurable && !((ReflectionConfigurable)target).acceptConfig(keySubString, val)) {
return;
} else {
// This will reduce the key, so that it will be recognize by the next object. i.e.
// Property name: factory.prefetchPolicy.queuePrefetch
// Calling order: this.getFactory().prefetchPolicy().queuePrefetch();
// If factory does not accept the config, it should be given prefetchPolicy.queuePrefetch as the key
keySubString = keySubString.substring(name.length() + 1); // +1 to account for the '.'
}
String getMethod = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
Method method = targetClass.getMethod(getMethod, new Class[] {});
target = method.invoke(target, null);
targetClass = target.getClass();
debugInfo += ("." + getMethod + "()");
}
// Property name
String property = tokenizer.nextToken();
// Check if the target object will accept the settings
if (obj instanceof ReflectionConfigurable && !((ReflectionConfigurable)target).acceptConfig(property, val)) {
if (target instanceof ReflectionConfigurable && !((ReflectionConfigurable)target).acceptConfig(property, val)) {
return;
}

View File

@ -22,8 +22,45 @@ import java.util.Properties;
import java.io.File;
import org.apache.activemq.tool.properties.ReflectionUtil;
import org.apache.activemq.tool.properties.ReflectionConfigurable;
public class ReflectionUtilTest extends TestCase {
public void testConfigurableOption() {
TestClass5 data = new TestClass5();
data.willIntercept = true;
ReflectionUtil.configureClass(data, "this-should-not-matter", "this-should-not-matter");
assertTrue(data.intercepted);
data.willIntercept = false;
data.nest = new TestClass5();
data.nest.willIntercept = true;
ReflectionUtil.configureClass(data, "nest.this-should-not-matter", "this-should-not-matter");
assertTrue(data.intercepted);
assertTrue(data.nest.intercepted);
data.willIntercept = false;
data.nest = new TestClass5();
data.nest.willIntercept = false;
data.nest.nest = new TestClass5();
data.nest.nest.willIntercept = true;
ReflectionUtil.configureClass(data, "nest.nest.this-should-not-matter", "this-should-not-matter");
assertTrue(data.intercepted);
assertTrue(data.nest.intercepted);
assertTrue(data.nest.nest.intercepted);
TestClass6 data2 = new TestClass6();
data2.nestConfig = new TestClass5();
data2.nestConfig.willIntercept = true;
ReflectionUtil.configureClass(data2, "nestConfig.this-should-not-matter", "this-should-not-matter");
assertTrue(data2.nestConfig.intercepted);
data2.nestNotConfig = new TestClass6();
data2.nestNotConfig.nestConfig = new TestClass5();
data2.nestNotConfig.nestConfig.willIntercept = true;
ReflectionUtil.configureClass(data2, "nestNotConfig.nestConfig.this-should-not-matter", "this-should-not-matter");
assertTrue(data2.nestNotConfig.nestConfig.intercepted);
}
public void testDataTypeConfig() {
TestClass3 targetObj = new TestClass3();
@ -257,4 +294,53 @@ public class ReflectionUtilTest extends TestCase {
this.testFile = new File(testFile);
}
}
public class TestClass5 implements ReflectionConfigurable {
public boolean intercepted = false;
public boolean willIntercept = true;
public TestClass5 nest = null;
public void configureProperties(Properties props) {
// Do nothing
}
public Properties retrieveProperties(Properties props) {
return null;
}
public boolean acceptConfig(String key, String val) {
intercepted = true;
return !willIntercept;
}
public TestClass5 getNest() {
return nest;
}
public void setNest(TestClass5 nest) {
this.nest = nest;
}
}
public class TestClass6 {
public TestClass6 nestNotConfig = null;
public TestClass5 nestConfig = null;
public TestClass6 getNestNotConfig() {
return nestNotConfig;
}
public void setNestNotConfig(TestClass6 nestNotConfig) {
this.nestNotConfig = nestNotConfig;
}
public TestClass5 getNestConfig() {
return nestConfig;
}
public void setNestConfig(TestClass5 nestConfig) {
this.nestConfig = nestConfig;
}
}
}