ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/tribes/demos/IntrospectionUtils.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/test/org/apache/catalina/tribes/demos/IntrospectionUtils.java
Status
scanned
Type
file
Name
IntrospectionUtils.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
8449 (8.3 KB)
MD5
77b3bb45bad66ef6555f0bd65eaa0af9
SHA1
0b8fd73585a077589c1d3458700155f0fbf242f7
SHA256
9ee4730acd7f26308eec8861e29a0660f70418b2603df5b2027bd5a9bdca807b
SHA512

      
    
SHA1_git
64276c12362ff2b9968447c6e11874b65510b4ce
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
IntrospectionUtils.java | 8.3 KB |

/* * 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.catalina.tribes.demos; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; /** * Utils for introspection and reflection */ public final class IntrospectionUtils { private static final Log log = LogFactory.getLog(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). */ @SuppressWarnings("null") public static boolean setProperty(Object o, String name, String value) { 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 (Method item : methods) { Class<?> paramT[] = item.getParameterTypes(); if (setter.equals(item.getName()) && paramT.length == 1 && "java.lang.String".equals(paramT[0].getName())) { item.invoke(o, new Object[] { value }); return true; } } // Try a setFoo ( int ) or ( boolean ) for (Method method : methods) { boolean ok = true; if (setter.equals(method.getName()) && method.getParameterTypes().length == 1) { // match - find the type and invoke it Class<?> paramType = method.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) { method.invoke(o, params); return true; } } // save "setProperty" for later if ("setProperty".equals(method.getName())) { if (method.getReturnType() == Boolean.TYPE) { setPropertyMethodBool = method; } else { setPropertyMethodVoid = method; } } } // Ok, no setXXX found, try a setProperty("name", "value") if (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 ex2) { log.warn("IAE " + o + " " + name + " " + value, ex2); } catch (SecurityException ex1) { if (log.isDebugEnabled()) { log.debug("IntrospectionUtils: SecurityException for " + o.getClass() + " " + name + "=" + value + ")", ex1); } } catch (IllegalAccessException iae) { if (log.isDebugEnabled()) { log.debug("IntrospectionUtils: IllegalAccessException for " + o.getClass() + " " + name + "=" + value + ")", iae); } } catch (InvocationTargetException ie) { Throwable cause = ie.getCause(); if (cause instanceof VirtualMachineError) { throw (VirtualMachineError) cause; } if (log.isDebugEnabled()) { log.debug("IntrospectionUtils: InvocationTargetException for " + o.getClass() + " " + name + "=" + value + ")", ie); } } return false; } /* * Reverse of Introspector.decapitalize */ public static String capitalize(String name) { if (name == null || name.length() == 0) { return name; } char chars[] = name.toCharArray(); chars[0] = Character.toUpperCase(chars[0]); return new String(chars); } // -------------------- other utils -------------------- public static void clear() { objectMethods.clear(); } static Map<Class<?>, Method[]> objectMethods = new ConcurrentHashMap<>(); public static Method[] findMethods(Class<?> c) { Method methods[] = objectMethods.get(c); if (methods != null) { return methods; } methods = c.getMethods(); objectMethods.put(c, methods); return methods; } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
17.45
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9