Skip to content

Commit

Permalink
Issue checkstyle#7977: Resolve Pitest Issues - JavadocMethodCheck (5)
Browse files Browse the repository at this point in the history
  • Loading branch information
HuGanghui committed Apr 18, 2020
1 parent 729d735 commit bca8af1
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 146 deletions.
6 changes: 0 additions & 6 deletions .ci/pitest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ pitest-javadoc)
"AbstractJavadocCheck.java.html:<td class='covered'><pre><span class='survived'> beginJavadocTree(root);</span></pre></td></tr>"
"AbstractJavadocCheck.java.html:<td class='covered'><pre><span class='survived'> finishJavadocTree(root);</span></pre></td></tr>"
"AbstractJavadocCheck.java.html:<td class='covered'><pre><span class='survived'> javadocTokens.clear();</span></pre></td></tr>"
"JavadocMethodCheck.java.html:<td class='covered'><pre><span class='survived'> if (classInfo == null) {</span></pre></td></tr>"
"JavadocMethodCheck.java.html:<td class='covered'><pre><span class='survived'> child != null;</span></pre></td></tr>"
"JavadocMethodCheck.java.html:<td class='covered'><pre><span class='survived'> if (child.getType() == TokenTypes.TYPE_PARAMETER) {</span></pre></td></tr>"
"JavadocMethodCheck.java.html:<td class='covered'><pre><span class='survived'> if (classInfo != null) {</span></pre></td></tr>"
"JavadocMethodCheck.java.html:<td class='covered'><pre><span class='survived'> currentTypeParams.clear();</span></pre></td></tr>"
"JavadocMethodCheck.java.html:<td class='covered'><pre><span class='survived'> while (iterator.hasNext()) {</span></pre></td></tr>"
"JavadocTagInfo.java.html:<td class='covered'><pre><span class='survived'> .collect(Collectors.toMap(JavadocTagInfo::getName, tagName -&#62; tagName)));</span></pre></td></tr>"
"JavadocTagInfo.java.html:<td class='covered'><pre><span class='survived'> .collect(Collectors.toMap(JavadocTagInfo::getText, tagText -&#62; tagText)));</span></pre></td></tr>"
"TagParser.java.html:<td class='covered'><pre><span class='survived'> while (column &#60; currentLine.length()</span></pre></td></tr>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -263,9 +259,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;

Expand Down Expand Up @@ -380,7 +373,6 @@ public int[] getAcceptableTokens() {
@Override
public void beginTree(DetailAST rootAST) {
currentClassName = "";
currentTypeParams.clear();
}

@Override
Expand All @@ -391,9 +383,6 @@ public final void visitToken(DetailAST ast) {
processClass(ast);
}
else {
if (ast.getType() == TokenTypes.METHOD_DEF) {
processTypeParams(ast);
}
processAST(ast);
}
}
Expand All @@ -406,10 +395,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();
}
}

Expand Down Expand Up @@ -999,39 +984,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.
*
Expand All @@ -1043,7 +995,6 @@ private void processClass(DetailAST ast) {

innerClass = "$" + innerClass;
currentClassName += innerClass;
processTypeParams(ast);
}

/**
Expand All @@ -1055,36 +1006,7 @@ private void processClass(DetailAST ast) {
*/
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;
return new RegularClass(name, surroundingClass, this);
}

/**
Expand Down Expand Up @@ -1153,30 +1075,6 @@ public String toString() {

}

/** 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,43 +537,6 @@ public void testClassRegularClass() throws Exception {
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);
Expand Down

0 comments on commit bca8af1

Please sign in to comment.