IntrospectionUtils.java

Index Score
org.apache.tomcat.util
Apache Tomcat 6

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
JAVA0034JAVA0034 Missing braces in if statement
COMPARISONSNumber of comparison operators
CYCLOMATICCyclomatic complexity
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
LOOPSNumber of loops
JAVA0067JAVA0067 Array descriptor on identifier name
OPERATORSNumber of operators
INTERFACE_COMPLEXITYInterface complexity
PROGRAM_LENGTHHalstead program length
BLOCKSNumber of blocks
RETURNSNumber of return points from functions
OPERANDSNumber of operands
EXEC_COMMENTSComments in executable code
PARAMSNumber of formal parameter declarations
SIZESize of the file in bytes
ELOCEffective lines of code
LOGICAL_LINESNumber of statements
LOCLines of code
LINE_COMMENTNumber of line comments
JAVA0265JAVA0265 Use of Throwable.printStackTrace()
JAVA0270JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
UNIQUE_OPERANDSNumber of unique operands
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
JAVA0049JAVA0049 Nested block at depth N (maximum: M)
LINESNumber of lines in the source file
JAVA0285JAVA0285 Dereference of potentially null variable
PROGRAM_VOCABHalstead program vocabulary
JAVA0117JAVA0117 Missing javadoc: method 'method'
EXITSProcedure exits
JAVA0166JAVA0166 Generic exception caught
DECL_COMMENTSComments in declarations
JAVA0115JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
FUNCTIONSNumber of function declarations
JAVA0119JAVA0119 Control variable changed within body of for loop
JAVA0177JAVA0177 Variable declaration missing initializer
JAVA0278JAVA0278 Unnecessary use of Boolean constructor
JAVA0171JAVA0171 Unused local variable
UNIQUE_OPERATORSNumber of unique operators
NEST_DEPTHMaximum nesting depth
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0160JAVA0160 Method does not throw specified exception
JAVA0126JAVA0126 Method declares unchecked exception in throws
COMMENTSComment lines
JAVA0145JAVA0145 Tab character used in source file
/* * 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.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; import java.util.Hashtable; import java.util.StringTokenizer; import java.util.Vector; // Depends: JDK1.1 /** * Utils for introspection and reflection */ public final class IntrospectionUtils { private static org.apache.juli.logging.Log log= org.apache.juli.logging.LogFactory.getLog( IntrospectionUtils.class ); /** * Call execute() - any ant-like task should work */ public static void execute(Object proxy, String method) throws Exception { Method executeM = null; Class c = proxy.getClass(); Class params[] = new Class[0]; // params[0]=args.getClass(); executeM = findMethod(c, method, params); if (executeM == null) { throw new RuntimeException("No execute in " + proxy.getClass()); } executeM.invoke(proxy, (Object[]) null);//new Object[] { args }); } /** * Call void setAttribute( String ,Object ) */ public static void setAttribute(Object proxy, String n, Object v) throws Exception { if (proxy instanceof AttributeHolder) { ((AttributeHolder) proxy).setAttribute(n, v); return; } Method executeM = null; Class c = proxy.getClass(); Class params[] = new Class[2]; params[0] = String.class; params[1] = Object.class; executeM = findMethod(c, "setAttribute", params); if (executeM == null) { if (log.isDebugEnabled()) log.debug("No setAttribute in " + proxy.getClass()); return; } if (false) if (log.isDebugEnabled()) log.debug("Setting " + n + "=" + v + " in " + proxy); executeM.invoke(proxy, new Object[] { n, v }); return; } /** * Call void getAttribute( String ) */ public static Object getAttribute(Object proxy, String n) throws Exception { Method executeM = null; Class c = proxy.getClass(); Class params[] = new Class[1]; params[0] = String.class; executeM = findMethod(c, "getAttribute", params); if (executeM == null) { if (log.isDebugEnabled()) log.debug("No getAttribute in " + proxy.getClass()); return null; } return executeM.invoke(proxy, new Object[] { n }); } /** * Construct a URLClassLoader. Will compile and work in JDK1.1 too. */ public static ClassLoader getURLClassLoader(URL urls[], ClassLoader parent) { try { Class urlCL = Class.forName("java.net.URLClassLoader"); Class paramT[] = new Class[2]; paramT[0] = urls.getClass(); paramT[1] = ClassLoader.class; Method m = findMethod(urlCL, "newInstance", paramT); if (m == null) return null; ClassLoader cl = (ClassLoader) m.invoke(urlCL, new Object[] { urls, parent }); return cl; } catch (ClassNotFoundException ex) { // jdk1.1 return null; } catch (Exception ex) { ex.printStackTrace(); return null; } } public static String guessInstall(String installSysProp, String homeSysProp, String jarName) { return guessInstall(installSysProp, homeSysProp, jarName, null); } /** * Guess a product install/home by analyzing the class path. It works for * product using the pattern: lib/executable.jar or if executable.jar is * included in classpath by a shell script. ( java -jar also works ) * * Insures both "install" and "home" System properties are set. If either or * both System properties are unset, "install" and "home" will be set to the * same value. This value will be the other System property that is set, or * the guessed value if neither is set. */ public static String guessInstall(String installSysProp, String homeSysProp, String jarName, String classFile) { String install = null; String home = null; if (installSysProp != null) install = System.getProperty(installSysProp); if (homeSysProp != null) home = System.getProperty(homeSysProp); if (install != null) { if (home == null) System.getProperties().put(homeSysProp, install); return install; } // Find the directory where jarName.jar is located String cpath = System.getProperty("java.class.path"); String pathSep = System.getProperty("path.separator"); StringTokenizer st = new StringTokenizer(cpath, pathSep); while (st.hasMoreTokens()) { String path = st.nextToken(); // log( "path " + path ); if (path.endsWith(jarName)) { home = path.substring(0, path.length() - jarName.length()); try { if ("".equals(home)) { home = new File("./").getCanonicalPath(); } else if (home.endsWith(File.separator)) { home = home.substring(0, home.length() - 1); } File f = new File(home); String parentDir = f.getParent(); if (parentDir == null) parentDir = home; // unix style File f1 = new File(parentDir); install = f1.getCanonicalPath(); if (installSysProp != null) System.getProperties().put(installSysProp, install); if (home == null && homeSysProp != null) System.getProperties().put(homeSysProp, install); return install; } catch (Exception ex) { ex.printStackTrace(); } } else { String fname = path + (path.endsWith("/") ? "" : "/") + classFile; if (new File(fname).exists()) { try { File f = new File(path); String parentDir = f.getParent(); if (parentDir == null) parentDir = path; // unix style File f1 = new File(parentDir); install = f1.getCanonicalPath(); if (installSysProp != null) System.getProperties().put(installSysProp, install); if (home == null && homeSysProp != null) System.getProperties().put(homeSysProp, install); return install; } catch (Exception ex) { ex.printStackTrace(); } } } } // if install directory can't be found, use home as the default if (home != null) { System.getProperties().put(installSysProp, home); return home; } return null; } /** * Debug method, display the classpath */ public static void displayClassPath(String msg, URL[] cp) { if (log.isDebugEnabled()) { log.debug(msg); for (int i = 0; i < cp.length; i++) { log.debug(cp[i].getFile()); } } } public static String PATH_SEPARATOR = System.getProperty("path.separator"); /** * Adds classpath entries from a vector of URL's to the "tc_path_add" System * property. This System property lists the classpath entries common to web * applications. This System property is currently used by Jasper when its * JSP servlet compiles the Java file for a JSP. */ public static String classPathAdd(URL urls[], String cp) { if (urls == null) return cp; for (int i = 0; i < urls.length; i++) { if (cp != null) cp += PATH_SEPARATOR + urls[i].getFile(); else cp = urls[i].getFile(); } return cp; } /** * 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). */ public static boolean setProperty(Object o, String name, String value) { if (dbg > 1) d("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] = new Integer(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] = new Long(value); } catch (NumberFormatException ex) { ok = false; } // Try a setFoo ( boolean ) } else if ("java.lang.Boolean".equals(paramType.getName()) || "boolean".equals(paramType.getName())) { params[0] = new Boolean(value); // Try a setFoo ( InetAddress ) } else if ("java.net.InetAddress".equals(paramType .getName())) { try { params[0] = InetAddress.getByName(value); } catch (UnknownHostException exc) { d("Unable to resolve host name:" + value); ok = false; } // Unknown type } else { d("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 (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); }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 (dbg > 0) d("SecurityException for " + o.getClass() + " " + name + "=" + value + ")"); if (dbg > 1) ex1.printStackTrace(); } catch (IllegalAccessException iae) { if (dbg > 0) d("IllegalAccessException for " + o.getClass() + " " + name + "=" + value + ")"); if (dbg > 1) iae.printStackTrace(); } catch (InvocationTargetException ie) { if (dbg > 0) d("InvocationTargetException for " + o.getClass() + " " + name + "=" + value + ")"); if (dbg > 1) ie.printStackTrace(); } 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 ex2) { log.warn("IAE " + o + " " + name, ex2); } catch (SecurityException ex1) { if (dbg > 0) d("SecurityException for " + o.getClass() + " " + name + ")"); if (dbg > 1) ex1.printStackTrace(); } catch (IllegalAccessException iae) { if (dbg > 0) d("IllegalAccessException for " + o.getClass() + " " + name + ")"); if (dbg > 1) iae.printStackTrace(); } catch (InvocationTargetException ie) { if (dbg > 0) d("InvocationTargetException for " + o.getClass() + " " + name + ")"); if (dbg > 1) ie.printStackTrace(); } return null; } /** */ public static void setProperty(Object o, String name) { String setter = "set" + capitalize(name); try { Method methods[] = findMethods(o.getClass()); Method setPropertyMethod = null; // find setFoo() method for (int i = 0; i < methods.length; i++) { Class paramT[] = methods[i].getParameterTypes(); if (setter.equals(methods[i].getName()) && paramT.length == 0) { methods[i].invoke(o, new Object[] {}); return; } } } catch (Exception ex1) { if (dbg > 0) d("Exception for " + o.getClass() + " " + name); if (dbg > 1) ex1.printStackTrace(); } } /** * Replace ${NAME} with the property value * * @deprecated Use the explicit method */ public static String replaceProperties(String value, Object getter) { if (getter instanceof Hashtable) return replaceProperties(value, (Hashtable) getter, null); if (getter instanceof PropertySource) { PropertySource src[] = new PropertySource[] { (PropertySource) getter }; return replaceProperties(value, null, src); } return value; } /** * Replace ${NAME} with the property value */ public static String replaceProperties(String value, Hashtable staticProp, PropertySource dynamicProp[]) { StringBuffer sb = new StringBuffer(); int prev = 0; // assert value!=nil int pos; while ((pos = value.indexOf("$", prev)) >= 0) { if (pos > 0) { sb.append(value.substring(prev, pos)); } if (pos == (value.length() - 1)) { sb.append('$'); prev = pos + 1; } else if (value.charAt(pos + 1) != '{') { sb.append('$'); prev = pos + 1; // XXX } else { int endName = value.indexOf('}', pos); if (endName < 0) { sb.append(value.substring(pos)); prev = value.length(); continue; } String n = value.substring(pos + 2, endName); String v = null; if (staticProp != null) { v = (String) ((Hashtable) staticProp).get(n); } if (v == null && dynamicProp != null) { for (int i = 0; i < dynamicProp.length; i++) { v = dynamicProp[i].getProperty(n); if (v != null) { break; } } } if (v == null) v = "${" + n + "}"; sb.append(v); prev = endName + 1; } } if (prev < value.length()) sb.append(value.substring(prev)); return sb.toString(); } /** * 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); } public static String unCapitalize(String name) { if (name == null || name.length() == 0) { return name; } char chars[] = name.toCharArray(); chars[0] = Character.toLowerCase(chars[0]); return new String(chars); } // -------------------- Class path tools -------------------- /** * Add all the jar files in a dir to the classpath, represented as a Vector * of URLs. */ public static void addToClassPath(Vector cpV, String dir) { try { String cpComp[] = getFilesByExt(dir, ".jar"); if (cpComp != null) { int jarCount = cpComp.length; for (int i = 0; i < jarCount; i++) { URL url = getURL(dir, cpComp[i]); if (url != null) cpV.addElement(url); } } } catch (Exception ex) { ex.printStackTrace(); } } public static void addToolsJar(Vector v) { try { // Add tools.jar in any case File f = new File(System.getProperty("java.home") + "/../lib/tools.jar"); if (!f.exists()) { // On some systems java.home gets set to the root of jdk. // That's a bug, but we can work around and be nice. f = new File(System.getProperty("java.home") + "/lib/tools.jar"); if (f.exists()) { if (log.isDebugEnabled()) log.debug("Detected strange java.home value " + System.getProperty("java.home") + ", it should point to jre"); } } URL url = new URL("file", "", f.getAbsolutePath()); v.addElement(url); } catch (MalformedURLException ex) { ex.printStackTrace(); } } /** * Return all files with a given extension in a dir */ public static String[] getFilesByExt(String ld, String ext) { File dir = new File(ld); String[] names = null; final String lext = ext; if (dir.isDirectory()) { names = dir.list(new FilenameFilter() { public boolean accept(File d, String name) { if (name.endsWith(lext)) { return true; } return false; } }); } return names; } /** * Construct a file url from a file, using a base dir */ public static URL getURL(String base, String file) { try { File baseF = new File(base); File f = new File(baseF, file); String path = f.getCanonicalPath(); if (f.isDirectory()) { path += "/"; } if (!f.exists()) return null; return new URL("file", "", path); } catch (Exception ex) { ex.printStackTrace(); return null; } } /** * Add elements from the classpath <i>cp </i> to a Vector <i>jars </i> as * file URLs (We use Vector for JDK 1.1 compat). * <p> * * @param jars The jar list * @param cp a String classpath of directory or jar file elements * separated by path.separator delimiters. * @throws IOException If an I/O error occurs * @throws MalformedURLException Doh ;) */ public static void addJarsFromClassPath(Vector jars, String cp) throws IOException, MalformedURLException { String sep = System.getProperty("path.separator"); String token; StringTokenizer st; if (cp != null) { st = new StringTokenizer(cp, sep); while (st.hasMoreTokens()) { File f = new File(st.nextToken()); String path = f.getCanonicalPath(); if (f.isDirectory()) { path += "/"; } URL url = new URL("file", "", path); if (!jars.contains(url)) { jars.addElement(url); } } } } /** * Return a URL[] that can be used to construct a class loader */ public static URL[] getClassPath(Vector v) { URL[] urls = new URL[v.size()]; for (int i = 0; i < v.size(); i++) { urls[i] = (URL) v.elementAt(i); } return urls; } /** * Construct a URL classpath from files in a directory, a cpath property, * and tools.jar. */ public static URL[] getClassPath(String dir, String cpath, String cpathProp, boolean addTools) throws IOException, MalformedURLException { Vector jarsV = new Vector(); if (dir != null) { // Add dir/classes first, if it exists URL url = getURL(dir, "classes"); if (url != null) jarsV.addElement(url); addToClassPath(jarsV, dir); } if (cpath != null) addJarsFromClassPath(jarsV, cpath); if (cpathProp != null) { String cpath1 = System.getProperty(cpathProp); addJarsFromClassPath(jarsV, cpath1); } if (addTools) addToolsJar(jarsV); return getClassPath(jarsV); } // -------------------- Mapping command line params to setters public static boolean processArgs(Object proxy, String args[]) throws Exception { String args0[] = null; if (null != findMethod(proxy.getClass(), "getOptions1", new Class[] {})) { args0 = (String[]) callMethod0(proxy, "getOptions1"); } if (args0 == null) { //args0=findVoidSetters(proxy.getClass()); args0 = findBooleanSetters(proxy.getClass()); } Hashtable h = null; if (null != findMethod(proxy.getClass(), "getOptionAliases", new Class[] {})) { h = (Hashtable) callMethod0(proxy, "getOptionAliases"); } return processArgs(proxy, args, args0, null, h); } public static boolean processArgs(Object proxy, String args[], String args0[], String args1[], Hashtable aliases) throws Exception { for (int i = 0; i < args.length; i++) { String arg = args[i]; if (arg.startsWith("-")) arg = arg.substring(1); if (aliases != null && aliases.get(arg) != null) arg = (String) aliases.get(arg); if (args0 != null) { boolean set = false; for (int j = 0; j < args0.length; j++) { if (args0[j].equalsIgnoreCase(arg)) { setProperty(proxy, args0[j], "true"); set = true; break; } } if (set) continue; } if (args1 != null) { for (int j = 0; j < args1.length; j++) { if (args1[j].equalsIgnoreCase(arg)) { i++; if (i >= args.length) return false; setProperty(proxy, arg, args[i]); break; } } } else { // if args1 is not specified,assume all other options have param i++; if (i >= args.length) return false; setProperty(proxy, arg, args[i]); } } return true; } // -------------------- other utils -------------------- public static void clear() { objectMethods.clear(); } public static String[] findVoidSetters(Class c) { Method m[] = findMethods(c); if (m == null) return null; Vector v = new Vector(); for (int i = 0; i < m.length; i++) { if (m[i].getName().startsWith("set") && m[i].getParameterTypes().length == 0) { String arg = m[i].getName().substring(3); v.addElement(unCapitalize(arg)); } } String s[] = new String[v.size()]; for (int i = 0; i < s.length; i++) { s[i] = (String) v.elementAt(i); } return s; } public static String[] findBooleanSetters(Class c) { Method m[] = findMethods(c); if (m == null) return null; Vector v = new Vector(); for (int i = 0; i < m.length; i++) { if (m[i].getName().startsWith("set") && m[i].getParameterTypes().length == 1 && "boolean".equalsIgnoreCase(m[i].getParameterTypes()[0] .getName())) { String arg = m[i].getName().substring(3); v.addElement(unCapitalize(arg)); } } String s[] = new String[v.size()]; for (int i = 0; i < s.length; i++) { s[i] = (String) v.elementAt(i); } return s; } static Hashtable objectMethods = new Hashtable(); public static Method[] findMethods(Class c) { Method methods[] = (Method[]) objectMethods.get(c); if (methods != null) return methods; methods = c.getMethods(); objectMethods.put(c, methods); return methods; } public static Method findMethod(Class c, String name, Class params[]) { Method methods[] = findMethods(c); if (methods == null) return null; for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equals(name)) { Class methodParams[] = methods[i].getParameterTypes(); if (methodParams == null) if (params == null || params.length == 0) return methods[i]; if (params == null) if (methodParams == null || methodParams.length == 0) return methods[i]; if (params.length != methodParams.length) continue; boolean found = true; for (int j = 0; j < params.length; j++) { if (params[j] != methodParams[j]) { found = false; break; } } if (found) return methods[i]; } } return null; } /** Test if the object implements a particular * method */ public static boolean hasHook(Object obj, String methodN) { try { Method myMethods[] = findMethods(obj.getClass()); for (int i = 0; i < myMethods.length; i++) { if (methodN.equals(myMethods[i].getName())) { // check if it's overriden Class declaring = myMethods[i].getDeclaringClass(); Class parentOfDeclaring = declaring.getSuperclass(); // this works only if the base class doesn't extend // another class. // if the method is declared in a top level class // like BaseInterceptor parent is Object, otherwise // parent is BaseInterceptor or an intermediate class if (!"java.lang.Object".equals(parentOfDeclaring.getName())) { return true; } } } } catch (Exception ex) { ex.printStackTrace(); } return false; } public static void callMain(Class c, String args[]) throws Exception { Class p[] = new Class[1]; p[0] = args.getClass(); Method m = c.getMethod("main", p); m.invoke(c, new Object[] { args }); } public static Object callMethod1(Object target, String methodN, Object param1, String typeParam1, ClassLoader cl) throws Exception { if (target == null || param1 == null) { d("Assert: Illegal params " + target + " " + param1); } if (dbg > 0) d("callMethod1 " + target.getClass().getName() + " " + param1.getClass().getName() + " " + typeParam1); Class params[] = new Class[1]; if (typeParam1 == null) params[0] = param1.getClass(); else params[0] = cl.loadClass(typeParam1); Method m = findMethod(target.getClass(), methodN, params); if (m == null) throw new NoSuchMethodException(target.getClass().getName() + " " + methodN); return m.invoke(target, new Object[] { param1 }); } public static Object callMethod0(Object target, String methodN) throws Exception { if (target == null) { d("Assert: Illegal params " + target); return null; } if (dbg > 0) d("callMethod0 " + target.getClass().getName() + "." + methodN); Class params[] = new Class[0]; Method m = findMethod(target.getClass(), methodN, params); if (m == null) throw new NoSuchMethodException(target.getClass().getName() + " " + methodN); return m.invoke(target, emptyArray); } static Object[] emptyArray = new Object[] {}; public static Object callMethodN(Object target, String methodN, Object params[], Class typeParams[]) throws Exception { Method m = null; m = findMethod(target.getClass(), methodN, typeParams); if (m == null) { d("Can't find method " + methodN + " in " + target + " CLASS " + target.getClass()); return null; } Object o = m.invoke(target, params); if (dbg > 0) { // debug StringBuffer sb = new StringBuffer(); sb.append("" + target.getClass().getName() + "." + methodN + "( "); for (int i = 0; i < params.length; i++) { if (i > 0) sb.append(", "); sb.append(params[i]); } sb.append(")"); d(sb.toString()); } return o; } public static Object convert(String object, Class paramType) { Object result = null; if ("java.lang.String".equals(paramType.getName())) { result = object; } else if ("java.lang.Integer".equals(paramType.getName()) || "int".equals(paramType.getName())) { try { result = new Integer(object); } catch (NumberFormatException ex) { } // Try a setFoo ( boolean ) } else if ("java.lang.Boolean".equals(paramType.getName()) || "boolean".equals(paramType.getName())) { result = new Boolean(object); // Try a setFoo ( InetAddress ) } else if ("java.net.InetAddress".equals(paramType .getName())) { try { result = InetAddress.getByName(object); } catch (UnknownHostException exc) { d("Unable to resolve host name:" + object); } // Unknown type } else { d("Unknown type " + paramType.getName()); } if (result == null) { throw new IllegalArgumentException("Can't convert argument: " + object); } return result; } // -------------------- Get property -------------------- // This provides a layer of abstraction public static interface PropertySource { public String getProperty(String key); } public static interface AttributeHolder { public void setAttribute(String key, Object o); } // debug -------------------- static final int dbg = 0; static void d(String s) { if (log.isDebugEnabled()) log.debug("IntrospectionUtils: " + s); } }

The table below shows all metrics for IntrospectionUtils.java.

MetricValueDescription
BLOCKS178.00Number of blocks
BLOCK_COMMENT16.00Number of block comment lines
COMMENTS143.00Comment lines
COMMENT_DENSITY 0.22Comment density
COMPARISONS205.00Number of comparison operators
CYCLOMATIC243.00Cyclomatic complexity
DECL_COMMENTS27.00Comments in declarations
DOC_COMMENT83.00Number of javadoc comment lines
ELOC638.00Effective lines of code
EXEC_COMMENTS32.00Comments in executable code
EXITS82.00Procedure exits
FUNCTIONS39.00Number of function declarations
HALSTEAD_DIFFICULTY113.67Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY198.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 0.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 0.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 1.00JAVA0007 Should not declare public field
JAVA0008 1.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 1.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA003464.00JAVA0034 Missing braces in if statement
JAVA0035 0.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 9.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 0.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA006729.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA0076 0.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 0.00JAVA0081 Boolean literal in comparison
JAVA0082 1.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 0.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 0.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA010841.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA011013.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 1.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 1.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 5.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 1.00JAVA0116 Missing javadoc: field 'field'
JAVA011718.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 2.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 2.00JAVA0119 Control variable changed within body of for loop
JAVA0123 0.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 0.00JAVA0128 Public constructor in non-public class
JAVA0130 0.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 0.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 2.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 0.00JAVA0143 Synchronized method
JAVA0144 0.00JAVA0144 Line exceeds maximum M characters
JAVA0145 2.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 1.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA0166 7.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 2.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 3.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 0.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 0.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA026514.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA027013.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 2.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 9.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES1026.00Number of lines in the source file
LINE_COMMENT44.00Number of line comments
LOC780.00Lines of code
LOGICAL_LINES391.00Number of statements
LOOPS23.00Number of loops
NEST_DEPTH 6.00Maximum nesting depth
OPERANDS2011.00Number of operands
OPERATORS3896.00Number of operators
PARAMS84.00Number of formal parameter declarations
PROGRAM_LENGTH5907.00Halstead program length
PROGRAM_VOCAB512.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS114.00Number of return points from functions
SIZE36874.00Size of the file in bytes
UNIQUE_OPERANDS460.00Number of unique operands
UNIQUE_OPERATORS52.00Number of unique operators
WHITESPACE103.00Number of whitespace lines