浏览代码

Merge pull request #18151 from rmuir/easy_win

painless: optimize/simplify dynamic field and method access
Robert Muir 9 年之前
父节点
当前提交
66e852922c
共有 1 个文件被更改,包括 16 次插入42 次删除
  1. 16 42
      modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java

+ 16 - 42
modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java

@@ -241,19 +241,18 @@ public class Def {
         }
         }
     }
     }
 
 
-    public static Method getMethod(final Object owner, final String name, final Definition definition) {
-        Struct struct = null;
+    /** Method lookup for owner.name(), returns null if no matching method was found */ 
+    private static Method getMethod(final Object owner, final String name, final Definition definition) {
         Class<?> clazz = owner.getClass();
         Class<?> clazz = owner.getClass();
-        Method method = null;
 
 
         while (clazz != null) {
         while (clazz != null) {
-            struct = definition.classes.get(clazz);
+            Struct struct = definition.classes.get(clazz);
 
 
             if (struct != null) {
             if (struct != null) {
-                method = struct.methods.get(name);
+                Method method = struct.methods.get(name);
 
 
                 if (method != null) {
                 if (method != null) {
-                    break;
+                    return method;
                 }
                 }
             }
             }
 
 
@@ -261,45 +260,32 @@ public class Def {
                 struct = definition.classes.get(iface);
                 struct = definition.classes.get(iface);
 
 
                 if (struct != null) {
                 if (struct != null) {
-                    method = struct.methods.get(name);
+                    Method method = struct.methods.get(name);
 
 
                     if (method != null) {
                     if (method != null) {
-                        break;
+                        return method;
                     }
                     }
                 }
                 }
             }
             }
 
 
-            if (struct != null) {
-                method = struct.methods.get(name);
-
-                if (method != null) {
-                    break;
-                }
-            }
-
             clazz = clazz.getSuperclass();
             clazz = clazz.getSuperclass();
         }
         }
 
 
-        if (struct == null) {
-            throw new IllegalArgumentException("Unable to find a dynamic struct for class [" + owner.getClass() + "].");
-        }
-
-        return method;
+        return null;
     }
     }
 
 
-    public static Field getField(final Object owner, final String name, final Definition definition) {
-        Struct struct = null;
+    /** Field lookup for owner.name, returns null if no matching field was found */ 
+    private static Field getField(final Object owner, final String name, final Definition definition) {
         Class<?> clazz = owner.getClass();
         Class<?> clazz = owner.getClass();
-        Field field = null;
 
 
         while (clazz != null) {
         while (clazz != null) {
-            struct = definition.classes.get(clazz);
+            Struct struct = definition.classes.get(clazz);
 
 
             if (struct != null) {
             if (struct != null) {
-                field = struct.members.get(name);
+                Field field = struct.members.get(name);
 
 
                 if (field != null) {
                 if (field != null) {
-                    break;
+                    return field;
                 }
                 }
             }
             }
 
 
@@ -307,30 +293,18 @@ public class Def {
                 struct = definition.classes.get(iface);
                 struct = definition.classes.get(iface);
 
 
                 if (struct != null) {
                 if (struct != null) {
-                    field = struct.members.get(name);
+                    Field field = struct.members.get(name);
 
 
                     if (field != null) {
                     if (field != null) {
-                        break;
+                        return field;
                     }
                     }
                 }
                 }
             }
             }
 
 
-            if (struct != null) {
-                field = struct.members.get(name);
-
-                if (field != null) {
-                    break;
-                }
-            }
-
             clazz = clazz.getSuperclass();
             clazz = clazz.getSuperclass();
         }
         }
 
 
-        if (struct == null) {
-            throw new IllegalArgumentException("Unable to find a dynamic struct for class [" + owner.getClass() + "].");
-        }
-
-        return field;
+        return null;
     }
     }
 
 
     public static Transform getTransform(Class<?> fromClass, Class<?> toClass, final Definition definition) {
     public static Transform getTransform(Class<?> fromClass, Class<?> toClass, final Definition definition) {