Repository: checkstyle/checkstyle

Problem statement:
Resolve Pitest Issues - JavadocMethodCheck (4)
Child issue of https://github.com/checkstyle/checkstyle/issues/7797 ,

https://github.com/checkstyle/checkstyle/blob/f9131f9536c71d963c83aab1550b7a0371aa9ff5/.ci/pitest.sh#L185


Hints:

Patch:
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.java
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheck.java
@@ -19,17 +19,13 @@
 
 package com.puppycrawl.tools.checkstyle.checks.javadoc;
 
-import java.util.ArrayDeque;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.Deque;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
-import java.util.Map;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -332,9 +328,6 @@ public class JavadocMethodCheck extends AbstractCheck {
     private static final Pattern MATCH_JAVADOC_NOARG_CURLY =
             CommonUtil.createPattern("\\{\\s*@(inheritDoc)\\s*\\}");
 
-    /** Stack of maps for type params. */
-    private final Deque<Map<String, ClassInfo>> currentTypeParams = new ArrayDeque<>();
-
     /** Name of current class. */
     private String currentClassName;
 
@@ -459,9 +452,6 @@ public final void visitToken(DetailAST ast) {
             processClass(ast);
         }
         else {
-            if (ast.getType() == TokenTypes.METHOD_DEF) {
-                processTypeParams(ast);
-            }
             processAST(ast);
         }
     }
@@ -474,10 +464,6 @@ public final void leaveToken(DetailAST ast) {
             // perhaps it was inner class
             final int dotIdx = currentClassName.lastIndexOf('$');
             currentClassName = currentClassName.substring(0, dotIdx);
-            currentTypeParams.pop();
-        }
-        else if (ast.getType() == TokenTypes.METHOD_DEF) {
-            currentTypeParams.pop();
         }
     }
 
@@ -730,7 +716,7 @@ private static List<DetailAST> getParameters(DetailAST ast) {
      * @param ast the method node.
      * @return the list of exception nodes for ast.
      */
-    private List<ExceptionInfo> getThrows(DetailAST ast) {
+    private static List<ExceptionInfo> getThrows(DetailAST ast) {
         final List<ExceptionInfo> returnValue = new ArrayList<>();
         final DetailAST throwsAST = ast
                 .findFirstToken(TokenTypes.LITERAL_THROWS);
@@ -741,7 +727,7 @@ private List<ExceptionInfo> getThrows(DetailAST ast) {
                         || child.getType() == TokenTypes.DOT) {
                     final FullIdent ident = FullIdent.createFullIdent(child);
                     final ExceptionInfo exceptionInfo = new ExceptionInfo(
-                            createClassInfo(new Token(ident), currentClassName));
+                            new ClassInfo(new Token(ident)));
                     returnValue.add(exceptionInfo);
                 }
                 child = child.getNextSibling();
@@ -756,7 +742,7 @@ private List<ExceptionInfo> getThrows(DetailAST ast) {
      * @param methodAst method DetailAST object where to find exceptions
      * @return list of ExceptionInfo
      */
-    private List<ExceptionInfo> getThrowed(DetailAST methodAst) {
+    private static List<ExceptionInfo> getThrowed(DetailAST methodAst) {
         final List<ExceptionInfo> returnValue = new ArrayList<>();
         final DetailAST blockAst = methodAst.findFirstToken(TokenTypes.SLIST);
         if (blockAst != null) {
@@ -768,7 +754,7 @@ private List<ExceptionInfo> getThrowed(DetailAST methodAst) {
                     if (newAst.getType() == TokenTypes.LITERAL_NEW) {
                         final FullIdent ident = FullIdent.createFullIdent(newAst.getFirstChild());
                         final ExceptionInfo exceptionInfo = new ExceptionInfo(
-                                createClassInfo(new Token(ident), currentClassName));
+                                new ClassInfo(new Token(ident)));
                         returnValue.add(exceptionInfo);
                     }
                 }
@@ -1018,8 +1004,7 @@ private void checkThrowsTags(List<JavadocTag> tags,
             // Loop looking for matching throw
             final Token token = new Token(tag.getFirstArg(), tag.getLineNo(), tag
                     .getColumnNo());
-            final ClassInfo documentedClassInfo = createClassInfo(token,
-                    currentClassName);
+            final ClassInfo documentedClassInfo = new ClassInfo(token);
             processThrows(throwsList, documentedClassInfo, foundThrows);
         }
         // Now dump out all throws without tags :- unless
@@ -1099,39 +1084,6 @@ private static boolean isClassNamesSame(String class1, String class2) {
         return result;
     }
 
-    /**
-     * Process type params (if any) for given class, enum or method.
-     *
-     * @param ast class, enum or method to process.
-     */
-    private void processTypeParams(DetailAST ast) {
-        final DetailAST params =
-            ast.findFirstToken(TokenTypes.TYPE_PARAMETERS);
-
-        final Map<String, ClassInfo> paramsMap = new HashMap<>();
-        currentTypeParams.push(paramsMap);
-
-        if (params != null) {
-            for (DetailAST child = params.getFirstChild();
-                 child != null;
-                 child = child.getNextSibling()) {
-                if (child.getType() == TokenTypes.TYPE_PARAMETER) {
-                    final DetailAST bounds =
-                        child.findFirstToken(TokenTypes.TYPE_UPPER_BOUNDS);
-                    if (bounds != null) {
-                        final FullIdent name =
-                            FullIdent.createFullIdentBelow(bounds);
-                        final ClassInfo classInfo =
-                            createClassInfo(new Token(name), currentClassName);
-                        final String alias =
-                                child.findFirstToken(TokenTypes.IDENT).getText();
-                        paramsMap.put(alias, classInfo);
-                    }
-                }
-            }
-        }
-    }
-
     /**
      * Processes class definition.
      *
@@ -1143,48 +1095,6 @@ private void processClass(DetailAST ast) {
 
         innerClass = "$" + innerClass;
         currentClassName += innerClass;
-        processTypeParams(ast);
-    }
-
-    /**
-     * Creates class info for given name.
-     *
-     * @param name name of type.
-     * @param surroundingClass name of surrounding class.
-     * @return class info for given name.
-     */
-    private ClassInfo createClassInfo(final Token name,
-                                      final String surroundingClass) {
-        final ClassInfo result;
-        final ClassInfo classInfo = findClassAlias(name.getText());
-        if (classInfo == null) {
-            result = new RegularClass(name, surroundingClass, this);
-        }
-        else {
-            result = new ClassAlias(name, classInfo);
-        }
-        return result;
-    }
-
-    /**
-     * Looking if a given name is alias.
-     *
-     * @param name given name
-     * @return ClassInfo for alias if it exists, null otherwise
-     * @noinspection WeakerAccess
-     */
-    private ClassInfo findClassAlias(final String name) {
-        ClassInfo classInfo = null;
-        final Iterator<Map<String, ClassInfo>> iterator = currentTypeParams
-                .descendingIterator();
-        while (iterator.hasNext()) {
-            final Map<String, ClassInfo> paramMap = iterator.next();
-            classInfo = paramMap.get(name);
-            if (classInfo != null) {
-                break;
-            }
-        }
-        return classInfo;
     }
 
     /**
@@ -1202,10 +1112,6 @@ private static class ClassInfo {
          * @throws IllegalArgumentException when className is nulls
          */
         protected ClassInfo(final Token className) {
-            if (className == null) {
-                throw new IllegalArgumentException(
-                    "ClassInfo's name should be non-null");
-            }
             name = className;
         }
 
@@ -1220,63 +1126,6 @@ public final Token getName() {
 
     }
 
-    /** Represents regular classes/enums. */
-    private static final class RegularClass extends ClassInfo {
-
-        /** Name of surrounding class. */
-        private final String surroundingClass;
-        /** The check we use to resolve classes. */
-        private final JavadocMethodCheck check;
-
-        /**
-         * Creates new instance of of class information object.
-         *
-         * @param name {@code FullIdent} associated with new object.
-         * @param surroundingClass name of current surrounding class.
-         * @param check the check we use to load class.
-         */
-        /* package */ RegularClass(final Token name,
-                             final String surroundingClass,
-                             final JavadocMethodCheck check) {
-            super(name);
-            this.surroundingClass = surroundingClass;
-            this.check = check;
-        }
-
-        @Override
-        public String toString() {
-            return "RegularClass[name=" + getName()
-                    + ", in class='" + surroundingClass + '\''
-                    + ", check=" + check.hashCode()
-                    + ']';
-        }
-
-    }
-
-    /** Represents type param which is "alias" for real type. */
-    private static class ClassAlias extends ClassInfo {
-
-        /** Class information associated with the alias. */
-        private final ClassInfo classInfo;
-
-        /**
-         * Creates new instance of the class.
-         *
-         * @param name token which represents name of class alias.
-         * @param classInfo class information associated with the alias.
-         */
-        /* package */ ClassAlias(final Token name, ClassInfo classInfo) {
-            super(name);
-            this.classInfo = classInfo;
-        }
-
-        @Override
-        public String toString() {
-            return "ClassAlias[alias " + getName() + " for " + classInfo.getName() + "]";
-        }
-
-    }
-
     /**
      * Represents text element with location in the text.
      */


Test patch:
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheckTest.java
--- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheckTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/javadoc/JavadocMethodCheckTest.java
@@ -27,11 +27,8 @@
 import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.MSG_UNUSED_TAG_GENERAL;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 
 import org.junit.jupiter.api.Test;
@@ -511,83 +508,6 @@ public void testTokenToString() throws Exception {
         assertEquals("Token[blablabla(1x1)]", result, "Invalid toString result");
     }
 
-    @Test
-    public void testClassRegularClass() throws Exception {
-        final Class<?> tokenType = Class.forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
-                + "JavadocMethodCheck$Token");
-
-        final Class<?> regularClassType = Class
-                .forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
-                        + "JavadocMethodCheck$RegularClass");
-        final Constructor<?> regularClassConstructor = regularClassType.getDeclaredConstructor(
-                tokenType, String.class, JavadocMethodCheck.class);
-        regularClassConstructor.setAccessible(true);
-
-        try {
-            regularClassConstructor.newInstance(null, "", new JavadocMethodCheck());
-            fail("Exception is expected");
-        }
-        catch (InvocationTargetException ex) {
-            assertTrue(ex.getCause() instanceof IllegalArgumentException,
-                    "Invalid exception class, expected: IllegalArgumentException.class");
-            assertEquals("ClassInfo's name should be non-null", ex.getCause().getMessage(),
-                    "Invalid exception message");
-        }
-
-        final Constructor<?> tokenConstructor = tokenType.getDeclaredConstructor(String.class,
-                int.class, int.class);
-        final Object token = tokenConstructor.newInstance("blablabla", 1, 1);
-
-        final JavadocMethodCheck methodCheck = new JavadocMethodCheck();
-        final Object regularClass = regularClassConstructor.newInstance(token, "sur",
-                methodCheck);
-
-        final Method toString = regularClass.getClass().getDeclaredMethod("toString");
-        toString.setAccessible(true);
-        final String result = (String) toString.invoke(regularClass);
-        final String expected = "RegularClass[name=Token[blablabla(1x1)], in class='sur', check="
-                + methodCheck.hashCode() + "]";
-
-        assertEquals(expected, result, "Invalid toString result");
-    }
-
-    @Test
-    public void testClassAliasToString() throws Exception {
-        final Class<?> tokenType = Class.forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
-                + "JavadocMethodCheck$Token");
-
-        final Class<?> regularClassType = Class
-                .forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
-                        + "JavadocMethodCheck$RegularClass");
-        final Constructor<?> regularClassConstructor = regularClassType.getDeclaredConstructor(
-                tokenType, String.class, JavadocMethodCheck.class);
-        regularClassConstructor.setAccessible(true);
-
-        final Constructor<?> tokenConstructor = tokenType.getDeclaredConstructor(String.class,
-                int.class, int.class);
-        final Object token = tokenConstructor.newInstance("blablabla", 1, 1);
-
-        final Object regularClass = regularClassConstructor.newInstance(token, "sur",
-                new JavadocMethodCheck());
-
-        final Class<?> classAliasType = Class.forName(
-                "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck$ClassAlias");
-        final Class<?> abstractTypeInfoType = Class
-                .forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
-                        + "JavadocMethodCheck$ClassInfo");
-
-        final Constructor<?> classAliasConstructor = classAliasType
-                .getDeclaredConstructor(tokenType, abstractTypeInfoType);
-        classAliasConstructor.setAccessible(true);
-
-        final Object classAlias = classAliasConstructor.newInstance(token, regularClass);
-        final Method toString = classAlias.getClass().getDeclaredMethod("toString");
-        toString.setAccessible(true);
-        final String result = (String) toString.invoke(classAlias);
-        assertEquals("ClassAlias[alias Token[blablabla(1x1)] for Token[blablabla(1x1)]]", result,
-                "Invalid toString result");
-    }
-
     @Test
     public void testWithoutLogErrors() throws Exception {
         final DefaultConfiguration config = createModuleConfig(JavadocMethodCheck.class);