This is an automated email from the ASF dual-hosted git repository.

lihan pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new a9fa36f198 Refactor MbeansDescriptorsIntrospectionSource. Simplify 
code.
a9fa36f198 is described below

commit a9fa36f1983729267f64240fe2c412dab7c3402a
Author: lihan <li...@apache.org>
AuthorDate: Sat Oct 8 16:39:52 2022 +0800

    Refactor MbeansDescriptorsIntrospectionSource. Simplify code.
---
 .../MbeansDescriptorsIntrospectionSource.java      | 76 ++++++++++------------
 1 file changed, 35 insertions(+), 41 deletions(-)

diff --git 
a/java/org/apache/tomcat/util/modeler/modules/MbeansDescriptorsIntrospectionSource.java
 
b/java/org/apache/tomcat/util/modeler/modules/MbeansDescriptorsIntrospectionSource.java
index ffb0937066..0bd28eb6a6 100644
--- 
a/java/org/apache/tomcat/util/modeler/modules/MbeansDescriptorsIntrospectionSource.java
+++ 
b/java/org/apache/tomcat/util/modeler/modules/MbeansDescriptorsIntrospectionSource.java
@@ -22,7 +22,9 @@ import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.Map;
 import java.util.Map.Entry;
 
@@ -188,16 +190,16 @@ public class MbeansDescriptorsIntrospectionSource extends 
ModelerSource
      * Process the methods and extract 'attributes', methods, etc.
      *
      * @param realClass The class to process
-     * @param methods The methods to process
-     * @param attMap The attribute map (complete)
+     * @param attNames The attribute name (complete)
      * @param getAttMap The readable attributes map
      * @param setAttMap The settable attributes map
      * @param invokeAttMap The invokable attributes map
      */
-    private void initMethods(Class<?> realClass, Method methods[], 
Map<String,Method> attMap,
+    private void initMethods(Class<?> realClass, Set<String> attNames,
             Map<String,Method> getAttMap, Map<String,Method> setAttMap,
             Map<String,Method> invokeAttMap) {
 
+        Method[] methods = realClass.getMethods();
         for (Method method : methods) {
             String name = method.getName();
 
@@ -213,7 +215,7 @@ public class MbeansDescriptorsIntrospectionSource extends 
ModelerSource
             if (method.getDeclaringClass() == Object.class) {
                 continue;
             }
-            Class<?> params[] = method.getParameterTypes();
+            Class<?>[] params = method.getParameterTypes();
 
             if (name.startsWith("get") && params.length == 0) {
                 Class<?> ret = method.getReturnType();
@@ -226,8 +228,7 @@ public class MbeansDescriptorsIntrospectionSource extends 
ModelerSource
                 name = unCapitalize(name.substring(3));
 
                 getAttMap.put(name, method);
-                // just a marker, we don't use the value
-                attMap.put(name, method);
+                attNames.add(name);
             } else if (name.startsWith("is") && params.length == 0) {
                 Class<?> ret = method.getReturnType();
                 if (Boolean.TYPE != ret) {
@@ -239,8 +240,7 @@ public class MbeansDescriptorsIntrospectionSource extends 
ModelerSource
                 name = unCapitalize(name.substring(2));
 
                 getAttMap.put(name, method);
-                // just a marker, we don't use the value
-                attMap.put(name, method);
+                attNames.add(name);
 
             } else if (name.startsWith("set") && params.length == 1) {
                 if (!supportedType(params[0])) {
@@ -251,7 +251,7 @@ public class MbeansDescriptorsIntrospectionSource extends 
ModelerSource
                 }
                 name = unCapitalize(name.substring(3));
                 setAttMap.put(name, method);
-                attMap.put(name, method);
+                attNames.add(name);
             } else {
                 if (params.length == 0) {
                     if (specialMethods.get(method.getName()) != null) {
@@ -290,25 +290,21 @@ public class MbeansDescriptorsIntrospectionSource extends 
ModelerSource
     public ManagedBean createManagedBean(Registry registry, String domain,
                                          Class<?> realClass, String type)
     {
-        ManagedBean mbean= new ManagedBean();
+        ManagedBean mbean = new ManagedBean();
 
-        Method methods[]=null;
 
-        Map<String,Method> attMap = new HashMap<>();
+        Set<String> attrNames = new HashSet<>();
         // key: attribute val: getter method
-        Map<String,Method> getAttMap = new HashMap<>();
+        Map<String, Method> getAttMap = new HashMap<>();
         // key: attribute val: setter method
-        Map<String,Method> setAttMap = new HashMap<>();
+        Map<String, Method> setAttMap = new HashMap<>();
         // key: operation val: invoke method
-        Map<String,Method> invokeAttMap = new HashMap<>();
+        Map<String, Method> invokeAttMap = new HashMap<>();
 
-        methods = realClass.getMethods();
-
-        initMethods(realClass, methods, attMap, getAttMap, setAttMap, 
invokeAttMap );
+        initMethods(realClass, attrNames, getAttMap, setAttMap, invokeAttMap);
 
         try {
-            for (Entry<String,Method> attEntry : attMap.entrySet()) {
-                String name = attEntry.getKey();
+            for (String name : attrNames) {
                 AttributeInfo ai = new AttributeInfo();
                 ai.setName(name);
                 Method gm = getAttMap.get(name);
@@ -320,26 +316,24 @@ public class MbeansDescriptorsIntrospectionSource extends 
ModelerSource
                     }
                 }
                 Method sm = setAttMap.get(name);
-                if( sm!=null ) {
-                    //ai.setSetMethodObj(sm);
+                if (sm != null) {
                     Class<?> t = sm.getParameterTypes()[0];
-                    if( t!=null ) {
-                        ai.setType( t.getName());
+                    if (t != null) {
+                        ai.setType(t.getName());
                     }
-                    ai.setSetMethod( sm.getName());
+                    ai.setSetMethod(sm.getName());
                 }
                 ai.setDescription("Introspected attribute " + name);
-                if( log.isDebugEnabled()) {
-                    log.debug("Introspected attribute " +
-                            name + " " + gm + " " + sm);
+                if (log.isDebugEnabled()) {
+                    log.debug("Introspected attribute " + name + " " + gm + " 
" + sm);
                 }
-                if( gm==null ) {
+                if (gm == null) {
                     ai.setReadable(false);
                 }
-                if( sm==null ) {
+                if (sm == null) {
                     ai.setWriteable(false);
                 }
-                if( sm!=null || gm!=null ) {
+                if (sm != null || gm != null) {
                     mbean.addAttribute(ai);
                 }
             }
@@ -347,18 +341,18 @@ public class MbeansDescriptorsIntrospectionSource extends 
ModelerSource
             // This map is populated by iterating the methods (which end up as
             // values in the Map) and obtaining the key from the value. It is
             // impossible for a key to be associated with a null value.
-            for (Entry<String,Method> entry : invokeAttMap.entrySet()) {
+            for (Entry<String, Method> entry : invokeAttMap.entrySet()) {
                 String name = entry.getKey();
                 Method m = entry.getValue();
 
-                OperationInfo op=new OperationInfo();
+                OperationInfo op = new OperationInfo();
                 op.setName(name);
                 op.setReturnType(m.getReturnType().getName());
                 op.setDescription("Introspected operation " + name);
-                Class<?> parms[] = m.getParameterTypes();
-                for(int i=0; i<parms.length; i++ ) {
-                    ParameterInfo pi=new ParameterInfo();
-                    pi.setType(parms[i].getName());
+                Class<?>[] params = m.getParameterTypes();
+                for (int i = 0; i < params.length; i++) {
+                    ParameterInfo pi = new ParameterInfo();
+                    pi.setType(params[i].getName());
                     pi.setName(("param" + i).intern());
                     pi.setDescription(("Introspected parameter param" + 
i).intern());
                     op.addParameter(pi);
@@ -366,13 +360,13 @@ public class MbeansDescriptorsIntrospectionSource extends 
ModelerSource
                 mbean.addOperation(op);
             }
 
-            if( log.isDebugEnabled()) {
-                log.debug("Setting name: " + type );
+            if (log.isDebugEnabled()) {
+                log.debug("Setting name: " + type);
             }
-            mbean.setName( type );
+            mbean.setName(type);
 
             return mbean;
-        } catch( Exception ex ) {
+        } catch (Exception ex) {
             ex.printStackTrace();
             return null;
         }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to