Return-Path: Delivered-To: apmail-ws-axis-dev-archive@ws.apache.org Received: (qmail 73413 invoked by uid 500); 1 Jul 2003 16:55:26 -0000 Mailing-List: contact axis-dev-help@ws.apache.org; run by ezmlm Precedence: bulk Reply-To: axis-dev@ws.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list axis-dev@ws.apache.org Delivered-To: moderator for axis-dev@ws.apache.org Received: (qmail 70643 invoked by uid 500); 1 Jul 2003 02:35:25 -0000 Delivered-To: apmail-xml-axis-cvs@apache.org Date: 1 Jul 2003 02:35:23 -0000 Message-ID: <20030701023523.50377.qmail@icarus.apache.org> From: ericf@apache.org To: xml-axis-cvs@apache.org Subject: cvs commit: xml-axis/java/src/org/apache/axis/utils JavaUtils.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N ericf 2003/06/30 19:35:23 Modified: java/src/org/apache/axis/utils JavaUtils.java Log: fix for bug 20666: made implementation of isEnumClass more performant by replacing exception-heavy code with a simpler algorithm and by removing a test for the existence of a toString() method which is always present on Java objects. Revision Changes Path 1.101 +41 -14 xml-axis/java/src/org/apache/axis/utils/JavaUtils.java Index: JavaUtils.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/utils/JavaUtils.java,v retrieving revision 1.100 retrieving revision 1.101 diff -u -r1.100 -r1.101 --- JavaUtils.java 29 Jun 2003 15:02:50 -0000 1.100 +++ JavaUtils.java 1 Jul 2003 02:35:23 -0000 1.101 @@ -982,25 +982,52 @@ */ public static boolean isEnumClass(Class cls) { try { - java.lang.reflect.Method m = cls.getMethod("getValue", null); - java.lang.reflect.Method m2 = cls.getMethod("toString", null); - java.lang.reflect.Method m3 = cls.getMethod("fromString", - new Class[] {java.lang.String.class}); + java.lang.reflect.Method[] methods = cls.getMethods(); + java.lang.reflect.Method getValueMethod = null, fromValueMethod = null, + setValueMethod = null, fromStringMethod = null; - if (m != null && m2 != null && m3 != null && - cls.getMethod("fromValue", new Class[] {m.getReturnType()}) != null) { - try { - if (cls.getMethod("setValue", new Class[] {m.getReturnType()}) == null) - return true; + // linear search: in practice, this is faster than + // sorting/searching a short array of methods. + for (int i = 0; i < methods.length; i++) { + String name = methods[i].getName(); + + if (name.equals("getValue") + && methods[i].getParameterTypes().length == 0) { // getValue() + getValueMethod = methods[i]; + } else if (name.equals("fromString")) { // fromString(String s) + Object[] params = methods[i].getParameterTypes(); + if (params.length == 1 + && params[0] == String.class) { + fromStringMethod = methods[i]; + } + } else if (name.equals("fromValue") + && methods[i].getParameterTypes().length == 1) { // fromValue(Something s) + fromValueMethod = methods[i]; + } else if (name.equals("setValue") + && methods[i].getParameterTypes().length == 1) { // setValue(Something s) + setValueMethod = methods[i]; + } + } + + // must have getValue and fromString, but not setValue + // must also have toString(), but every Object subclass has that, so + // no need to check for it. + if (null != getValueMethod && null != fromStringMethod) { + if (null != setValueMethod + && setValueMethod.getParameterTypes().length == 1 + && getValueMethod.getReturnType() == setValueMethod.getParameterTypes()[0]) { + // setValue exists: return false return false; - } catch (java.lang.NoSuchMethodException e) { - return true; // getValue & fromValue exist. setValue does not exist. Thus return true. + } else { + return true; } + } else { + return false; } - } catch (java.lang.NoSuchMethodException e) {} - return false; + } catch (java.lang.SecurityException e) { + return false; + } // end of catch } - public static String stackToString(Throwable e){ java.io.StringWriter sw= new java.io.StringWriter(1024);