groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Minor refactoring: remove duplicated code of `Inspector`
Date Wed, 18 Apr 2018 11:06:19 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 264985f7f -> ca44609c4


Minor refactoring: remove duplicated code of `Inspector`


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ca44609c
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ca44609c
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ca44609c

Branch: refs/heads/master
Commit: ca44609c4dfa33b17a974689fe7abcbc2dad8bcc
Parents: 264985f
Author: sunlan <sunlan@apache.org>
Authored: Wed Apr 18 19:06:14 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Wed Apr 18 19:06:14 2018 +0800

----------------------------------------------------------------------
 src/main/groovy/groovy/inspect/Inspector.java | 61 ++++++++++------------
 1 file changed, 29 insertions(+), 32 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ca44609c/src/main/groovy/groovy/inspect/Inspector.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/inspect/Inspector.java b/src/main/groovy/groovy/inspect/Inspector.java
index f0c94a7..b8ad561 100644
--- a/src/main/groovy/groovy/inspect/Inspector.java
+++ b/src/main/groovy/groovy/inspect/Inspector.java
@@ -228,53 +228,47 @@ public class Inspector {
         return className;
     }
 
+    private static String makeTypesInfo(Class[] types) {
+        StringBuilder sb = new StringBuilder(32);
+        for (int k = 0; k < types.length; k++) {
+            sb.append(shortName(types[k]));
+            if (k < (types.length - 1)) sb.append(", ");
+        }
+
+        return sb.toString();
+    }
+
+    private static String makeParamsInfo(Class[] params) {
+        return makeTypesInfo(params);
+    }
+
+    private static String makeExceptionInfo(Class[] exceptions) {
+        return makeTypesInfo(exceptions);
+    }
+
     protected String[] methodInfo(Method method) {
         String[] result = new String[MEMBER_EXCEPTIONS_IDX + 1];
-        int mod = method.getModifiers();
         result[MEMBER_ORIGIN_IDX] = JAVA;
         result[MEMBER_DECLARER_IDX] = shortName(method.getDeclaringClass());
-        result[MEMBER_MODIFIER_IDX] = Modifier.toString(mod);
+        result[MEMBER_MODIFIER_IDX] = Modifier.toString(method.getModifiers());
         result[MEMBER_NAME_IDX] = method.getName();
         result[MEMBER_TYPE_IDX] = shortName(method.getReturnType());
-        Class[] params = method.getParameterTypes();
-        StringBuilder sb = new StringBuilder();
-        for (int j = 0; j < params.length; j++) {
-            sb.append(shortName(params[j]));
-            if (j < (params.length - 1)) sb.append(", ");
-        }
-        result[MEMBER_PARAMS_IDX] = sb.toString();
-        sb.setLength(0);
-        Class[] exceptions = method.getExceptionTypes();
-        for (int k = 0; k < exceptions.length; k++) {
-            sb.append(shortName(exceptions[k]));
-            if (k < (exceptions.length - 1)) sb.append(", ");
-        }
-        result[MEMBER_EXCEPTIONS_IDX] = sb.toString();
+        result[MEMBER_PARAMS_IDX] = makeParamsInfo(method.getParameterTypes());
+        result[MEMBER_EXCEPTIONS_IDX] = makeExceptionInfo(method.getExceptionTypes());
+
         return withoutNulls(result);
     }
 
     protected String[] methodInfo(Constructor ctor) {
         String[] result = new String[MEMBER_EXCEPTIONS_IDX + 1];
-        int mod = ctor.getModifiers();
         result[MEMBER_ORIGIN_IDX] = JAVA;
-        result[MEMBER_MODIFIER_IDX] = Modifier.toString(mod);
+        result[MEMBER_MODIFIER_IDX] = Modifier.toString(ctor.getModifiers());
         result[MEMBER_DECLARER_IDX] = shortName(ctor.getDeclaringClass());
         result[MEMBER_TYPE_IDX] = shortName(ctor.getDeclaringClass());
         result[MEMBER_NAME_IDX] = ctor.getName();
-        Class[] params = ctor.getParameterTypes();
-        StringBuilder sb = new StringBuilder();
-        for (int j = 0; j < params.length; j++) {
-            sb.append(shortName(params[j]));
-            if (j < (params.length - 1)) sb.append(", ");
-        }
-        result[MEMBER_PARAMS_IDX] = sb.toString();
-        sb.setLength(0);
-        Class[] exceptions = ctor.getExceptionTypes();
-        for (int k = 0; k < exceptions.length; k++) {
-            sb.append(shortName(exceptions[k]));
-            if (k < (exceptions.length - 1)) sb.append(", ");
-        }
-        result[MEMBER_EXCEPTIONS_IDX] = sb.toString();
+        result[MEMBER_PARAMS_IDX] = makeParamsInfo(ctor.getParameterTypes());
+        result[MEMBER_EXCEPTIONS_IDX] = makeExceptionInfo(ctor.getExceptionTypes());
+
         return withoutNulls(result);
     }
 
@@ -287,11 +281,14 @@ public class Inspector {
         result[MEMBER_TYPE_IDX] = shortName(method.getReturnType());
         result[MEMBER_NAME_IDX] = method.getName();
         CachedClass[] params = method.getParameterTypes();
+
+        // TODO reuse `makeParamsInfo`
         StringBuilder sb = new StringBuilder();
         for (int j = 0; j < params.length; j++) {
             sb.append(shortName(params[j].getTheClass()));
             if (j < (params.length - 1)) sb.append(", ");
         }
+
         result[MEMBER_PARAMS_IDX] = sb.toString();
         result[MEMBER_EXCEPTIONS_IDX] = NOT_APPLICABLE; // no exception info for Groovy MetaMethods
         return withoutNulls(result);


Mime
View raw message