org.apache.activemq.tool.properties.ReflectionUtil Maven / Gradle / Ivy
The newest version!
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.activemq.tool.properties;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ReflectionUtil {
private static final Logger LOG = LoggerFactory.getLogger(ReflectionUtil.class);
private ReflectionUtil() {
}
public static void configureClass(Object obj, String key, String val) {
try {
String debugInfo;
Object target = obj;
Class> targetClass = obj.getClass();
// DEBUG: Debugging Info
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
// +1 to account for the '.'
keySubString = keySubString.substring(name.length() + 1);
}
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 (target instanceof ReflectionConfigurable && !((ReflectionConfigurable)target).acceptConfig(property, val)) {
return;
}
// Find setter method
Method setterMethod = findSetterMethod(targetClass, property);
// Get the first parameter type. This assumes that there is only one
// parameter.
if (setterMethod == null) {
throw new IllegalAccessException("Unable to find appropriate setter method signature for property: " + property);
}
Class> paramType = setterMethod.getParameterTypes()[0];
// Set primitive type
debugInfo += "." + setterMethod + "(" + paramType.getName() + ": " + val + ")";
if (paramType.isPrimitive()) {
if (paramType == Boolean.TYPE) {
setterMethod.invoke(target, new Object[] {
Boolean.valueOf(val)
});
} else if (paramType == Integer.TYPE) {
setterMethod.invoke(target, new Object[] {
Integer.valueOf(val)
});
} else if (paramType == Long.TYPE) {
setterMethod.invoke(target, new Object[] {
Long.valueOf(val)
});
} else if (paramType == Double.TYPE) {
setterMethod.invoke(target, new Object[] {
Double.valueOf(val)
});
} else if (paramType == Float.TYPE) {
setterMethod.invoke(target, new Object[] {
Float.valueOf(val)
});
} else if (paramType == Short.TYPE) {
setterMethod.invoke(target, new Object[] {
Short.valueOf(val)
});
} else if (paramType == Byte.TYPE) {
setterMethod.invoke(target, new Object[] {
Byte.valueOf(val)
});
} else if (paramType == Character.TYPE) {
setterMethod.invoke(target, new Object[] {
val.charAt(0)
});
}
} else {
// Set String type
if (paramType == String.class) {
setterMethod.invoke(target, new Object[] {
val
});
// For unknown object type, try to create an instance of the
// object using a String constructor
} else {
Constructor c = paramType.getConstructor(new Class[] {
String.class
});
Object paramObject = c.newInstance(new Object[] {
val
});
setterMethod.invoke(target, new Object[] {
paramObject
});
}
}
LOG.debug(debugInfo);
} catch (Exception e) {
LOG.warn(e.toString());
}
}
public static void configureClass(Object obj, Properties props) {
for (Iterator