You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by er...@apache.org on 2003/07/01 04:35:23 UTC
cvs commit: xml-axis/java/src/org/apache/axis/utils JavaUtils.java
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);