Skip to content

Commit eaf8b03

Browse files
committed
Fix NPE Exception in ReplaceLambdaWithMethodReference
1 parent bd797ca commit eaf8b03

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

rewrite-java/src/main/java/org/openrewrite/java/cleanup/RenamePrivateFieldsToCamelCase.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,19 @@ public JavaSourceFile visitJavaSourceFile(JavaSourceFile cu, ExecutionContext ct
9797
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) {
9898
Cursor parentScope = getCursorToParentScope(getCursor());
9999

100-
// Does support renaming private fields in a J.ClassDeclaration.
101-
if ((parentScope.getParent() != null && parentScope.getParent().getValue() instanceof J.ClassDeclaration && variable.getVariableType().hasFlags(Flag.Private) &&
102-
//do not rename constants
103-
!(variable.getVariableType().hasFlags(Flag.Static, Flag.Final)) &&
104-
// Does not apply for instance variables of inner classes
105-
!((J.ClassDeclaration) parentScope.getParent().getValue()).getType().getFullyQualifiedName().contains("$") &&
106-
// Name does not match camelCase pattern.
107-
!LOWER_CAMEL.matches(variable.getSimpleName()))) {
100+
// Does support renaming fields in a J.ClassDeclaration.
101+
// We must have a variable type to make safe changes.
102+
// Only make changes to private fields that are not constants.
103+
// Does not apply for instance variables of inner classes
104+
// Only make a change if the variable does not conform to lower camelcase format.
105+
if (parentScope.getParent() != null
106+
&& parentScope.getParent().getValue() instanceof J.ClassDeclaration
107+
&& variable.getVariableType() != null
108+
&& variable.getVariableType().hasFlags(Flag.Private)
109+
&& !(variable.getVariableType().hasFlags(Flag.Static, Flag.Final))
110+
&& !((J.ClassDeclaration) parentScope.getParent().getValue()).getType().getFullyQualifiedName().contains("$")
111+
&& !LOWER_CAMEL.matches(variable.getSimpleName())) {
112+
108113
String toName = LOWER_CAMEL.format(variable.getSimpleName());
109114
((Map<J.VariableDeclarations.NamedVariable, String>) getCursor().getNearestMessage("RENAME_VARIABLES_KEY")).put(variable, toName);
110115
} else {

0 commit comments

Comments
 (0)