Issue #3144: fixed NPE in FinalLocalVariable for lambda in field (#3146)

This commit is contained in:
rnveach 2016-05-01 08:37:07 -04:00 committed by Roman Ivanov
parent 27537e9a0a
commit dfef8ec6d8
3 changed files with 16 additions and 3 deletions

View File

@ -562,7 +562,7 @@ public class FinalLocalVariableCheck extends AbstractCheck {
}
/**
* Find the Class, Constructor, Enum or Method in which it is defined.
* Find the Class, Constructor, Enum, Method, or Field in which it is defined.
* @param ast Variable for which we want to find the scope in which it is defined
* @return ast The Class or Constructor or Method in which it is defined.
*/
@ -571,7 +571,9 @@ public class FinalLocalVariableCheck extends AbstractCheck {
while (astTraverse.getType() != TokenTypes.METHOD_DEF
&& astTraverse.getType() != TokenTypes.CLASS_DEF
&& astTraverse.getType() != TokenTypes.ENUM_DEF
&& astTraverse.getType() != TokenTypes.CTOR_DEF) {
&& astTraverse.getType() != TokenTypes.CTOR_DEF
&& (astTraverse.getType() != TokenTypes.VARIABLE_DEF
|| !ScopeUtils.isClassFieldDef(astTraverse))) {
astTraverse = astTraverse.getParent();
}
return astTraverse;

View File

@ -160,7 +160,9 @@ public class FinalLocalVariableCheckTest
final DefaultConfiguration checkConfig =
createCheckConfig(FinalLocalVariableCheck.class);
checkConfig.addAttribute("tokens", "PARAMETER_DEF,VARIABLE_DEF");
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
final String[] expected = {
"32:16: " + "Variable 'result' should be declared final.",
};
verify(checkConfig, getNonCompilablePath("InputFinalLocalVariableNameLambda.java"),
expected);
}

View File

@ -25,3 +25,12 @@ public class InputFinalLocalVariableNameLambda {
(t, u) -> t.add(u.getAmount()));
}
}
interface Operation {
public Object apply();
public static final Operation OPERATION = () -> {
Object result;
result = null;
return result;
};
}