Skip to content

Commit

Permalink
Refactored data clumps with the help of LLMs (research project) (#960)
Browse files Browse the repository at this point in the history
Hello maintainers,

I am conducting a master thesis project focused on enhancing code
quality through automated refactoring of data clumps, assisted by Large
Language Models (LLMs).


<details>
  <summary>Data clump definition</summary>
  
A data clump exists if
1. two methods (in the same or in different classes) have at least 3
common parameters and one of those methods does not override the other,
or
2. At least three fields in a class are common with the parameters of a
method (in the same or in a different class), or
3. Two different classes have at least three common fields
  
See also the following UML diagram as an example
![Example data
clump](https://raw.githubusercontent.com/compf/data_clump_eval_assets/main/data_clump_explained.svg)
</details>


I believe these refactoring can contribute to the project by reducing
complexity and enhancing readability of your source code.

Pursuant to the EU AI Act, I fully disclose the use of LLMs in
generating these refactorings, emphasizing that all changes have
undergone human review for quality assurance.


Even if you decide not to integrate my changes to your codebase (which
is perfectly fine), I ask you to fill out a feedback survey, which will
be scientifically evaluated to determine the acceptance of AI-supported
refactorings. You can find the feedback survey under
https://campus.lamapoll.de/Data-clump-refactoring/en


Thank you for considering my contribution. I look forward to your
feedback. If you have any other questions or comments, feel free to
write a comment, or email me under tschoemaker@uni-osnabrueck.de .


Best regards,
Timo Schoemaker
Department of Computer Science
University of Osnabrück

---------

Co-authored-by: Manu Sridharan <msridhar@gmail.com>
  • Loading branch information
compf and msridhar authored Jun 17, 2024
1 parent c4aed81 commit a4ce249
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 127 deletions.
14 changes: 8 additions & 6 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import com.uber.nullaway.generics.GenericsChecks;
import com.uber.nullaway.handlers.Handler;
import com.uber.nullaway.handlers.Handlers;
import com.uber.nullaway.handlers.MethodAnalysisContext;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -384,7 +385,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
return Description.NO_MATCH;
}
Symbol.MethodSymbol methodSymbol = getSymbolForMethodInvocation(tree);
handler.onMatchMethodInvocation(this, tree, state, methodSymbol);
handler.onMatchMethodInvocation(tree, new MethodAnalysisContext(this, state, methodSymbol));
// assuming this list does not include the receiver
List<? extends ExpressionTree> actualParams = tree.getArguments();
return handleInvocation(tree, state, methodSymbol, actualParams);
Expand Down Expand Up @@ -644,7 +645,7 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
// overridden method (if overridden method is in an annotated
// package)
Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(tree);
handler.onMatchMethod(this, tree, state, methodSymbol);
handler.onMatchMethod(tree, new MethodAnalysisContext(this, state, methodSymbol));
boolean isOverriding = ASTHelpers.hasAnnotation(methodSymbol, "java.lang.Override", state);
boolean exhaustiveOverride = config.exhaustiveOverride();
if (isOverriding || !exhaustiveOverride) {
Expand Down Expand Up @@ -957,7 +958,8 @@ public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState
// we need to update environment mapping before running the handler, as some handlers
// (like Rx nullability) run dataflow analysis
updateEnvironmentMapping(state.getPath(), state);
handler.onMatchLambdaExpression(this, tree, state, funcInterfaceMethod);
handler.onMatchLambdaExpression(
tree, new MethodAnalysisContext(this, state, funcInterfaceMethod));
if (codeAnnotationInfo.isSymbolUnannotated(funcInterfaceMethod, config, handler)) {
return Description.NO_MATCH;
}
Expand Down Expand Up @@ -1001,7 +1003,7 @@ public Description matchMemberReference(MemberReferenceTree tree, VisitorState s
Symbol.MethodSymbol referencedMethod = ASTHelpers.getSymbol(tree);
Symbol.MethodSymbol funcInterfaceSymbol =
NullabilityUtil.getFunctionalInterfaceMethod(tree, state.getTypes());
handler.onMatchMethodReference(this, tree, state, referencedMethod);
handler.onMatchMethodReference(tree, new MethodAnalysisContext(this, state, referencedMethod));
return checkOverriding(funcInterfaceSymbol, referencedMethod, tree, state);
}

Expand Down Expand Up @@ -1445,7 +1447,7 @@ private boolean okToReadBeforeInitialized(TreePath path, VisitorState state) {
} else {
castToNonNullArg =
handler.castToNonNullArgumentPositionsForMethod(
this, state, methodSymbol, arguments, null);
arguments, null, new MethodAnalysisContext(this, state, methodSymbol));
}
if (castToNonNullArg != null && leaf.equals(arguments.get(castToNonNullArg))) {
return true;
Expand Down Expand Up @@ -1818,7 +1820,7 @@ private Description checkCastToNonNullTakesNullable(
} else {
castToNonNullPosition =
handler.castToNonNullArgumentPositionsForMethod(
this, state, methodSymbol, actualParams, null);
actualParams, null, new MethodAnalysisContext(this, state, methodSymbol));
}
if (castToNonNullPosition != null) {
ExpressionTree actual = actualParams.get(castToNonNullPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ protected AbstractFieldContractHandler(String annotName) {
/**
* Verifies that the method being processed adheres to the annotation specifications.
*
* @param analysis NullAway instance.
* @param tree Method tree under processing.
* @param state Error Prone {@link VisitorState}.
* @param methodSymbol Processing method symbol.
* @param methodAnalysisContext The MethodAnalysisContext object
*/
@Override
public void onMatchMethod(
NullAway analysis, MethodTree tree, VisitorState state, Symbol.MethodSymbol methodSymbol) {
public void onMatchMethod(MethodTree tree, MethodAnalysisContext methodAnalysisContext) {

Symbol.MethodSymbol methodSymbol = methodAnalysisContext.methodSymbol();
VisitorState state = methodAnalysisContext.state();
Set<String> annotationContent =
NullabilityUtil.getAnnotationValueArray(methodSymbol, annotName, false);
boolean isAnnotated = annotationContent != null;
boolean isValid =
isAnnotated
&& validateAnnotationSyntax(
castToNonNull(annotationContent), analysis, tree, state, methodSymbol)
&& validateAnnotationSemantics(analysis, state, tree, methodSymbol);
castToNonNull(annotationContent), tree, methodAnalysisContext)
&& validateAnnotationSemantics(tree, methodAnalysisContext);
if (isAnnotated && !isValid) {
return;
}
Expand All @@ -90,8 +90,9 @@ && validateAnnotationSyntax(
} else {
fieldNames = Collections.emptySet();
}
validateOverridingRules(fieldNames, analysis, state, tree, closestOverriddenMethod);
super.onMatchMethod(analysis, tree, state, methodSymbol);
validateOverridingRules(
fieldNames, methodAnalysisContext.analysis(), state, tree, closestOverriddenMethod);
super.onMatchMethod(tree, methodAnalysisContext);
}

/**
Expand All @@ -117,9 +118,10 @@ protected abstract void validateOverridingRules(
* Validates that a method implementation matches the semantics of the annotation.
*
* @return Returns true, if the annotation conforms to the semantic rules.
* @param methodAnalysisContext The MethodAnalysisContext object
*/
protected abstract boolean validateAnnotationSemantics(
NullAway analysis, VisitorState state, MethodTree tree, Symbol.MethodSymbol methodSymbol);
MethodTree tree, MethodAnalysisContext methodAnalysisContext);

/**
* Validates whether the parameter inside annotation conforms to the syntax rules. Parameters must
Expand All @@ -137,14 +139,13 @@ protected abstract boolean validateAnnotationSemantics(
* <p>
*
* @return Returns true, if the annotation conforms to the syntax rules.
* @param methodAnalysisContext The MethodAnalysisContext object
*/
protected boolean validateAnnotationSyntax(
Set<String> content,
NullAway analysis,
MethodTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
Set<String> content, MethodTree tree, MethodAnalysisContext methodAnalysisContext) {
String message;
VisitorState state = methodAnalysisContext.state();
NullAway analysis = methodAnalysisContext.analysis();
if (content.isEmpty()) {
// we should not allow useless annotations.
message =
Expand Down Expand Up @@ -187,7 +188,8 @@ protected boolean validateAnnotationSyntax(
fieldName = fieldName.substring(fieldName.lastIndexOf(".") + 1);
}
}
Symbol.ClassSymbol classSymbol = castToNonNull(ASTHelpers.enclosingClass(methodSymbol));
Symbol.ClassSymbol classSymbol =
castToNonNull(ASTHelpers.enclosingClass(methodAnalysisContext.methodSymbol()));
VariableElement field = getInstanceFieldOfClass(classSymbol, fieldName);
if (field == null) {
message =
Expand All @@ -197,6 +199,7 @@ protected boolean validateAnnotationSyntax(
+ fieldName
+ " in class "
+ classSymbol.getSimpleName();

state.reportMatch(
analysis
.getErrorBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,25 @@ public void onMatchTopLevelClass(
}

@Override
public void onMatchMethod(
NullAway analysis, MethodTree tree, VisitorState state, Symbol.MethodSymbol methodSymbol) {
public void onMatchMethod(MethodTree tree, MethodAnalysisContext methodAnalysisContext) {
// NoOp
}

@Override
public void onMatchMethodInvocation(
NullAway analysis,
MethodInvocationTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
MethodInvocationTree tree, MethodAnalysisContext methodAnalysisContext) {
// NoOp
}

@Override
public void onMatchLambdaExpression(
NullAway analysis,
LambdaExpressionTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
LambdaExpressionTree tree, MethodAnalysisContext methodAnalysisContext) {
// NoOp
}

@Override
public void onMatchMethodReference(
NullAway analysis,
MemberReferenceTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
MemberReferenceTree tree, MethodAnalysisContext methodAnalysisContext) {
// NoOp
}

Expand Down Expand Up @@ -238,11 +228,9 @@ public MethodInvocationNode onCFGBuildPhase1AfterVisitMethodInvocation(
@Override
@Nullable
public Integer castToNonNullArgumentPositionsForMethod(
NullAway analysis,
VisitorState state,
Symbol.MethodSymbol methodSymbol,
List<? extends ExpressionTree> actualParams,
@Nullable Integer previousArgumentPosition) {
@Nullable Integer previousArgumentPosition,
MethodAnalysisContext methodAnalysisContext) {
// NoOp
return previousArgumentPosition;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,43 +80,33 @@ public void onMatchTopLevelClass(
}

@Override
public void onMatchMethod(
NullAway analysis, MethodTree tree, VisitorState state, Symbol.MethodSymbol methodSymbol) {
public void onMatchMethod(MethodTree tree, MethodAnalysisContext methodAnalysisContext) {
for (Handler h : handlers) {
h.onMatchMethod(analysis, tree, state, methodSymbol);
h.onMatchMethod(tree, methodAnalysisContext);
}
}

@Override
public void onMatchLambdaExpression(
NullAway analysis,
LambdaExpressionTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
LambdaExpressionTree tree, MethodAnalysisContext methodAnalysisContext) {
for (Handler h : handlers) {
h.onMatchLambdaExpression(analysis, tree, state, methodSymbol);
h.onMatchLambdaExpression(tree, methodAnalysisContext);
}
}

@Override
public void onMatchMethodReference(
NullAway analysis,
MemberReferenceTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
MemberReferenceTree tree, MethodAnalysisContext methodAnalysisContext) {
for (Handler h : handlers) {
h.onMatchMethodReference(analysis, tree, state, methodSymbol);
h.onMatchMethodReference(tree, methodAnalysisContext);
}
}

@Override
public void onMatchMethodInvocation(
NullAway analysis,
MethodInvocationTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol) {
MethodInvocationTree tree, MethodAnalysisContext methodAnalysisContext) {
for (Handler h : handlers) {
h.onMatchMethodInvocation(analysis, tree, state, methodSymbol);
h.onMatchMethodInvocation(tree, methodAnalysisContext);
}
}

Expand Down Expand Up @@ -310,15 +300,13 @@ public MethodInvocationNode onCFGBuildPhase1AfterVisitMethodInvocation(
@Override
@Nullable
public Integer castToNonNullArgumentPositionsForMethod(
NullAway analysis,
VisitorState state,
Symbol.MethodSymbol methodSymbol,
List<? extends ExpressionTree> actualParams,
@Nullable Integer previousArgumentPosition) {
@Nullable Integer previousArgumentPosition,
MethodAnalysisContext methodAnalysisContext) {
for (Handler h : handlers) {
previousArgumentPosition =
h.castToNonNullArgumentPositionsForMethod(
analysis, state, methodSymbol, actualParams, previousArgumentPosition);
actualParams, previousArgumentPosition, methodAnalysisContext);
}
return previousArgumentPosition;
}
Expand Down
44 changes: 11 additions & 33 deletions nullaway/src/main/java/com/uber/nullaway/handlers/Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,55 +78,37 @@ void onMatchTopLevelClass(
/**
* Called when NullAway first matches a particular method node.
*
* @param analysis A reference to the running NullAway analysis.
* @param tree The AST node for the method being matched.
* @param state The current visitor state.
* @param methodSymbol The method symbol for the method being matched.
* @param methodAnalysisContext The MethodAnalysisContext object
*/
void onMatchMethod(
NullAway analysis, MethodTree tree, VisitorState state, Symbol.MethodSymbol methodSymbol);
void onMatchMethod(MethodTree tree, MethodAnalysisContext methodAnalysisContext);

/**
* Called when NullAway first matches a particular method call-site.
*
* @param analysis A reference to the running NullAway analysis.
* @param tree The AST node for the method invocation (call-site) being matched.
* @param state The current visitor state.
* @param methodSymbol The method symbol for the method being called.
* @param methodAnalysisContext The MethodAnalysisContext object
*/
void onMatchMethodInvocation(
NullAway analysis,
MethodInvocationTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol);
MethodInvocationTree tree, MethodAnalysisContext methodAnalysisContext);

/**
* Called when NullAway first matches a particular lambda expression.
*
* @param analysis A reference to the running NullAway analysis.
* @param tree The AST node for the lambda expression being matched.
* @param state The current visitor state.
* @param methodSymbol The method symbol for the functional interface of the lambda being matched.
* @param methodAnalysisContext The MethodAnalysisContext object
*/
void onMatchLambdaExpression(
NullAway analysis,
LambdaExpressionTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol);
LambdaExpressionTree tree, MethodAnalysisContext methodAnalysisContext);

/**
* Called when NullAway first matches a particular method reference expression
*
* @param analysis A reference to the running NullAway analysis.
* @param tree The AST node for the method reference expression being matched.
* @param state The current visitor state.
* @param methodSymbol The method symbol for the reference being matched.
* @param methodAnalysisContext The MethodAnalysisContext object
*/
void onMatchMethodReference(
NullAway analysis,
MemberReferenceTree tree,
VisitorState state,
Symbol.MethodSymbol methodSymbol);
MemberReferenceTree tree, MethodAnalysisContext methodAnalysisContext);

/**
* Called when NullAway first matches a return statement.
Expand Down Expand Up @@ -386,24 +368,20 @@ MethodInvocationNode onCFGBuildPhase1AfterVisitMethodInvocation(
* <p>See {@link LibraryModels#castToNonNullMethods()} for more information about general
* configuration of <code>castToNonNull</code> methods.
*
* @param analysis A reference to the running NullAway analysis.
* @param state The current visitor state.
* @param methodSymbol The method symbol for the potential castToNonNull method.
* @param actualParams The actual parameters from the invocation node
* @param previousArgumentPosition The result computed by the previous handler in the chain, if
* any.
* @return The index of the parameter for which the method should act as a cast (if any). This
* value can be set only once through the full chain of handlers, with each handler deciding
* whether to propagate or override the value previousArgumentPosition passed by the previous
* handler in the chain.
* @param methodAnalysisContext The MethodAnalysisContext object
*/
@Nullable
Integer castToNonNullArgumentPositionsForMethod(
NullAway analysis,
VisitorState state,
Symbol.MethodSymbol methodSymbol,
List<? extends ExpressionTree> actualParams,
@Nullable Integer previousArgumentPosition);
@Nullable Integer previousArgumentPosition,
MethodAnalysisContext methodAnalysisContext);

/**
* Method to override the nullability of the upper bound for a generic type variable on a class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ public boolean onOverrideMayBeNullExpr(
@Override
@Nullable
public Integer castToNonNullArgumentPositionsForMethod(
NullAway analysis,
VisitorState state,
Symbol.MethodSymbol methodSymbol,
List<? extends ExpressionTree> actualParams,
@Nullable Integer previousArgumentPosition) {
OptimizedLibraryModels optLibraryModels = getOptLibraryModels(state.context);
@Nullable Integer previousArgumentPosition,
MethodAnalysisContext methodAnalysisContext) {
Symbol.MethodSymbol methodSymbol = methodAnalysisContext.methodSymbol();
OptimizedLibraryModels optLibraryModels =
getOptLibraryModels(methodAnalysisContext.state().context);
ImmutableSet<Integer> newPositions = optLibraryModels.castToNonNullMethod(methodSymbol);
if (newPositions.size() > 1) {
// Library models sanity check
Expand Down
Loading

0 comments on commit a4ce249

Please sign in to comment.