![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.tomcat.util.IntrospectionUtils 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.tomcat.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Hashtable;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.res.StringManager;
/**
* Utils for introspection and reflection
*/
public final class IntrospectionUtils {
private static final Log log = LogFactory.getLog(IntrospectionUtils.class);
private static final StringManager sm = StringManager.getManager(IntrospectionUtils.class);
/**
* Find a method with the right name If found, call the method ( if param is
* int or boolean we'll convert value to the right type before) - that means
* you can have setDebug(1).
* @param o The object to set a property on
* @param name The property name
* @param value The property value
* @return true
if operation was successful
*/
public static boolean setProperty(Object o, String name, String value) {
return setProperty(o,name,value,true);
}
@SuppressWarnings("null") // setPropertyMethodVoid is not null when used
public static boolean setProperty(Object o, String name, String value,
boolean invokeSetProperty) {
if (log.isDebugEnabled())
log.debug("IntrospectionUtils: setProperty(" +
o.getClass() + " " + name + "=" + value + ")");
String setter = "set" + capitalize(name);
try {
Method methods[] = findMethods(o.getClass());
Method setPropertyMethodVoid = null;
Method setPropertyMethodBool = null;
// First, the ideal case - a setFoo( String ) method
for (int i = 0; i < methods.length; i++) {
Class> paramT[] = methods[i].getParameterTypes();
if (setter.equals(methods[i].getName()) && paramT.length == 1
&& "java.lang.String".equals(paramT[0].getName())) {
methods[i].invoke(o, new Object[] { value });
return true;
}
}
// Try a setFoo ( int ) or ( boolean )
for (int i = 0; i < methods.length; i++) {
boolean ok = true;
if (setter.equals(methods[i].getName())
&& methods[i].getParameterTypes().length == 1) {
// match - find the type and invoke it
Class> paramType = methods[i].getParameterTypes()[0];
Object params[] = new Object[1];
// Try a setFoo ( int )
if ("java.lang.Integer".equals(paramType.getName())
|| "int".equals(paramType.getName())) {
try {
params[0] = Integer.valueOf(value);
} catch (NumberFormatException ex) {
ok = false;
}
// Try a setFoo ( long )
}else if ("java.lang.Long".equals(paramType.getName())
|| "long".equals(paramType.getName())) {
try {
params[0] = Long.valueOf(value);
} catch (NumberFormatException ex) {
ok = false;
}
// Try a setFoo ( boolean )
} else if ("java.lang.Boolean".equals(paramType.getName())
|| "boolean".equals(paramType.getName())) {
params[0] = Boolean.valueOf(value);
// Try a setFoo ( InetAddress )
} else if ("java.net.InetAddress".equals(paramType
.getName())) {
try {
params[0] = InetAddress.getByName(value);
} catch (UnknownHostException exc) {
if (log.isDebugEnabled())
log.debug("IntrospectionUtils: Unable to resolve host name:" + value);
ok = false;
}
// Unknown type
} else {
if (log.isDebugEnabled())
log.debug("IntrospectionUtils: Unknown type " +
paramType.getName());
}
if (ok) {
methods[i].invoke(o, params);
return true;
}
}
// save "setProperty" for later
if ("setProperty".equals(methods[i].getName())) {
if (methods[i].getReturnType()==Boolean.TYPE){
setPropertyMethodBool = methods[i];
}else {
setPropertyMethodVoid = methods[i];
}
}
}
// Ok, no setXXX found, try a setProperty("name", "value")
if (invokeSetProperty && (setPropertyMethodBool != null ||
setPropertyMethodVoid != null)) {
Object params[] = new Object[2];
params[0] = name;
params[1] = value;
if (setPropertyMethodBool != null) {
try {
return ((Boolean) setPropertyMethodBool.invoke(o,
params)).booleanValue();
}catch (IllegalArgumentException biae) {
//the boolean method had the wrong
//parameter types. lets try the other
if (setPropertyMethodVoid!=null) {
setPropertyMethodVoid.invoke(o, params);
return true;
}else {
throw biae;
}
}
} else {
setPropertyMethodVoid.invoke(o, params);
return true;
}
}
} catch (IllegalArgumentException | SecurityException | IllegalAccessException e) {
log.warn(sm.getString("introspectionUtils.setPropertyError", name, value, o.getClass()), e);
} catch (InvocationTargetException e) {
ExceptionUtils.handleThrowable(e.getCause());
log.warn(sm.getString("introspectionUtils.setPropertyError", name, value, o.getClass()), e);
}
return false;
}
public static Object getProperty(Object o, String name) {
String getter = "get" + capitalize(name);
String isGetter = "is" + capitalize(name);
try {
Method methods[] = findMethods(o.getClass());
Method getPropertyMethod = null;
// First, the ideal case - a getFoo() method
for (int i = 0; i < methods.length; i++) {
Class> paramT[] = methods[i].getParameterTypes();
if (getter.equals(methods[i].getName()) && paramT.length == 0) {
return methods[i].invoke(o, (Object[]) null);
}
if (isGetter.equals(methods[i].getName()) && paramT.length == 0) {
return methods[i].invoke(o, (Object[]) null);
}
if ("getProperty".equals(methods[i].getName())) {
getPropertyMethod = methods[i];
}
}
// Ok, no setXXX found, try a getProperty("name")
if (getPropertyMethod != null) {
Object params[] = new Object[1];
params[0] = name;
return getPropertyMethod.invoke(o, params);
}
} catch (IllegalArgumentException | SecurityException | IllegalAccessException e) {
log.warn(sm.getString("introspectionUtils.getPropertyError", name, o.getClass()), e);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof NullPointerException) {
// Assume the underlying object uses a storage to represent an unset property
return null;
}
ExceptionUtils.handleThrowable(e.getCause());
log.warn(sm.getString("introspectionUtils.getPropertyError", name, o.getClass()), e);
}
return null;
}
/**
* Replace ${NAME} with the property value.
* @param value The value
* @param staticProp Replacement properties
* @param dynamicProp Replacement properties
* @return the replacement value
*/
public static String replaceProperties(String value,
Hashtable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy