Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull #5351: fixed RequireThisCheck and catch variable handling #5351

Merged
merged 2 commits into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1110,8 +1110,7 @@
<!-- generated classes, unfortunately use problematic api -->
<exclude>**/GeneratedJavaLexer.class</exclude>
<exclude>**/JavadocParser.class</exclude>
<!-- excluded till https://github.com/policeman-tools/forbidden-apis/issues/108-->
<exclude>**/checks/annotation/annotationlocation/InputAnnotationLocationDeprecatedAndCustom*</exclude>
<exclude>**/Input*</exclude>
</excludes>
</configuration>
<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ private static void collectDeclarations(Deque<AbstractFrame> frameStack, DetailA
break;
case TokenTypes.PARAMETER_DEF :
if (!CheckUtils.isReceiverParameter(ast)
&& !isLambdaParameter(ast)) {
&& !isLambdaParameter(ast)
&& ast.getParent().getType() != TokenTypes.LITERAL_CATCH) {
final DetailAST parameterIdent = ast.findFirstToken(TokenTypes.IDENT);
frame.addIdent(parameterIdent);
}
Expand Down Expand Up @@ -366,6 +367,12 @@ private static void collectDeclarations(Deque<AbstractFrame> frameStack, DetailA
final DetailAST ctorFrameNameIdent = ast.findFirstToken(TokenTypes.IDENT);
frameStack.addFirst(new ConstructorFrame(frame, ctorFrameNameIdent));
break;
case TokenTypes.LITERAL_CATCH:
final AbstractFrame catchFrame = new CatchFrame(frame, ast);
catchFrame.addIdent(ast.findFirstToken(TokenTypes.PARAMETER_DEF).findFirstToken(
TokenTypes.IDENT));
frameStack.addFirst(catchFrame);
break;
case TokenTypes.LITERAL_NEW:
if (isAnonymousClassDef(ast)) {
frameStack.addFirst(new AnonymousClassFrame(frame,
Expand Down Expand Up @@ -414,6 +421,7 @@ private void endCollectingDeclarations(Queue<AbstractFrame> frameStack, DetailAS
case TokenTypes.SLIST :
case TokenTypes.METHOD_DEF :
case TokenTypes.CTOR_DEF :
case TokenTypes.LITERAL_CATCH :
frames.put(ast, frameStack.poll());
break;
case TokenTypes.LITERAL_NEW :
Expand Down Expand Up @@ -952,6 +960,8 @@ private enum FrameType {
METHOD_FRAME,
/** Block frame type. */
BLOCK_FRAME,
/** Catch frame type. */
CATCH_FRAME,
}

/**
Expand Down Expand Up @@ -1357,4 +1367,24 @@ protected FrameType getType() {
return FrameType.BLOCK_FRAME;
}
}

/**
* A frame initiated on entering a catch block; holds local catch variable names.
* @author Richard Veach
*/
public static class CatchFrame extends AbstractFrame {
/**
* Creates catch frame.
* @param parent parent frame.
* @param ident ident frame name ident.
*/
protected CatchFrame(AbstractFrame parent, DetailAST ident) {
super(parent, ident);
}

@Override
public FrameType getType() {
return FrameType.CATCH_FRAME;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.MSG_METHOD;
import static com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck.MSG_VARIABLE;

import java.lang.reflect.Constructor;
import java.util.SortedSet;

import org.junit.Assert;
Expand All @@ -33,6 +34,7 @@
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;

public class RequireThisCheckTest extends AbstractModuleTestSupport {
Expand Down Expand Up @@ -295,6 +297,16 @@ public void testAllowLambdaParameters() throws Exception {
verify(checkConfig, getPath("InputRequireThisAllowLambdaParameters.java"), expected);
}

@Test
public void testCatchVariables() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
checkConfig.addAttribute("validateOnlyOverlapping", "false");
final String[] expected = {
"29:21: " + getCheckMessage(MSG_VARIABLE, "ex", ""),
};
verify(checkConfig, getPath("InputRequireThisCatchVariables.java"), expected);
}

@Test
public void test() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(RequireThisCheck.class);
Expand All @@ -309,4 +321,18 @@ public void testExtendedMethod() throws Exception {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputRequireThisExtendedMethod.java"), expected);
}

@Test
public void testUnusedMethod() throws Exception {
final DetailAST ident = new DetailAST();
ident.setText("testName");

final Class<?> cls = Class.forName(RequireThisCheck.class.getName() + "$CatchFrame");
final Constructor<?> constructor = cls.getDeclaredConstructors()[0];
constructor.setAccessible(true);
final Object o = constructor.newInstance(null, ident);

Assert.assertEquals("expected ident token", ident,
TestUtil.getClassDeclaredMethod(cls, "getFrameNameIdent").invoke(o));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.puppycrawl.tools.checkstyle.checks.coding.requirethis;

public class InputRequireThisCatchVariables extends Thread {
private Throwable ex;

public InputRequireThisCatchVariables(Throwable ex) {
this.ex = ex;
}

@Override
public void run() {
if (this.ex != null) {
try {
exceptional(this.ex);
}
catch (RuntimeException ex) {
if (ex == this.ex) {
debug("Expected exception thrown", ex);
}
else {
ex.printStackTrace();
}
}
catch (Error err) {
if (err == this.ex) {
debug("Expected exception thrown", err);
}
else {
ex.printStackTrace();
}
}
catch (Throwable ex) {
ex.printStackTrace();
}
}
}

private static void exceptional(Throwable ex) {}
private static void debug(String message, Throwable err) {}
}