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

Refactored data clumps with the help of LLMs (research project) #4447

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -333,36 +333,36 @@ private void fileReaderFix(
Description.Builder description, VisitorState state, Tree arg, Tree toReplace) {
for (CharsetFix charset : CharsetFix.values()) {
if (shouldUseGuava(state)) {
description.addFix(guavaFileReaderFix(state, arg, toReplace, charset));
description.addFix(guavaFileReaderFix(arg, new FileCharsetReplacementContext(state, toReplace, charset)));
} else {
description.addFix(nioFileReaderFix(state, arg, toReplace, charset));
description.addFix(nioFileReaderFix(arg, new FileCharsetReplacementContext(state, toReplace, charset)));
}
}
}

private static Fix nioFileReaderFix(
VisitorState state, Tree arg, Tree toReplace, CharsetFix charset) {
Tree arg, FileCharsetReplacementContext fileCharsetReplacementContext) {
SuggestedFix.Builder fix = SuggestedFix.builder();
fix.replace(
toReplace,
fileCharsetReplacementContext.getToReplace(),
String.format(
"Files.newBufferedReader(%s, %s)", toPath(state, arg, fix), charset.replacement()));
"Files.newBufferedReader(%s, %s)", toPath(fileCharsetReplacementContext.getState(), arg, fix), fileCharsetReplacementContext.getCharset().replacement()));
fix.addImport("java.nio.file.Files");
charset.addImport(fix);
variableTypeFix(fix, state, FileReader.class, Reader.class);
fileCharsetReplacementContext.getCharset().addImport(fix);
variableTypeFix(fix, fileCharsetReplacementContext.getState(), FileReader.class, Reader.class);
return fix.build();
}

private static Fix guavaFileReaderFix(
VisitorState state, Tree fileArg, Tree toReplace, CharsetFix charset) {
Tree fileArg, FileCharsetReplacementContext fileCharsetReplacementContext) {
SuggestedFix.Builder fix = SuggestedFix.builder();
fix.replace(
toReplace,
fileCharsetReplacementContext.getToReplace(),
String.format(
"Files.newReader(%s, %s)", toFile(state, fileArg, fix), charset.replacement()));
"Files.newReader(%s, %s)", toFile(fileCharsetReplacementContext.getState(), fileArg, fix), fileCharsetReplacementContext.getCharset().replacement()));
fix.addImport("com.google.common.io.Files");
charset.addImport(fix);
variableTypeFix(fix, state, FileReader.class, Reader.class);
fileCharsetReplacementContext.getCharset().addImport(fix);
variableTypeFix(fix, fileCharsetReplacementContext.getState(), FileReader.class, Reader.class);
return fix.build();
}

Expand Down Expand Up @@ -411,35 +411,35 @@ private Description handleFileWriter(NewClassTree tree, VisitorState state) {
boolean useGuava = shouldUseGuava(state);
for (CharsetFix charset : CharsetFix.values()) {
if (appendMode == null && useGuava) {
description.addFix(guavaFileWriterFix(state, fileArg, toReplace, charset));
description.addFix(guavaFileWriterFix(fileArg, new FileCharsetReplacementContext(state, toReplace, charset)));
} else {
description.addFix(
nioFileWriterFix(state, appendMode, fileArg, toReplace, charset, useGuava));
nioFileWriterFix(appendMode, fileArg, useGuava, new FileCharsetReplacementContext(
state,
toReplace,
charset)));
}
}
return description.build();
}

private static Fix guavaFileWriterFix(
VisitorState state, Tree fileArg, Tree toReplace, CharsetFix charset) {
Tree fileArg, FileCharsetReplacementContext fileCharsetReplacementContext) {
SuggestedFix.Builder fix = SuggestedFix.builder();
fix.replace(
toReplace,
fileCharsetReplacementContext.getToReplace(),
String.format(
"Files.newWriter(%s, %s)", toFile(state, fileArg, fix), charset.replacement()));
"Files.newWriter(%s, %s)", toFile(fileCharsetReplacementContext.getState(), fileArg, fix), fileCharsetReplacementContext.getCharset().replacement()));
fix.addImport("com.google.common.io.Files");
charset.addImport(fix);
variableTypeFix(fix, state, FileWriter.class, Writer.class);
fileCharsetReplacementContext.getCharset().addImport(fix);
variableTypeFix(fix, fileCharsetReplacementContext.getState(), FileWriter.class, Writer.class);
return fix.build();
}

private static Fix nioFileWriterFix(
VisitorState state,
Tree appendTree,
Tree fileArg,
Tree toReplace,
CharsetFix charset,
boolean qualify) {
Tree appendTree,
Tree fileArg,
boolean qualify, FileCharsetReplacementContext fileCharsetReplacementContext) {
SuggestedFix.Builder fix = SuggestedFix.builder();
StringBuilder sb = new StringBuilder();
if (qualify) {
Expand All @@ -449,15 +449,15 @@ private static Fix nioFileWriterFix(
fix.addImport("java.nio.file.Files");
}
sb.append(".newBufferedWriter(");
sb.append(toPath(state, fileArg, fix));
sb.append(", ").append(charset.replacement());
charset.addImport(fix);
sb.append(toPath(fileCharsetReplacementContext.getState(), fileArg, fix));
sb.append(", ").append(fileCharsetReplacementContext.getCharset().replacement());
fileCharsetReplacementContext.getCharset().addImport(fix);
if (appendTree != null) {
sb.append(toAppendMode(fix, appendTree, state));
sb.append(toAppendMode(fix, appendTree, fileCharsetReplacementContext.getState()));
}
sb.append(")");
fix.replace(toReplace, sb.toString());
variableTypeFix(fix, state, FileWriter.class, Writer.class);
fix.replace(fileCharsetReplacementContext.getToReplace(), sb.toString());
variableTypeFix(fix, fileCharsetReplacementContext.getState(), FileWriter.class, Writer.class);
return fix.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2016 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.errorprone.bugpatterns;

import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.DefaultCharset.CharsetFix;
import com.sun.source.tree.Tree;

public class FileCharsetReplacementContext {

private VisitorState state;

private Tree toReplace;

private CharsetFix charset;

public VisitorState getState() {
return state;
}

public Tree getToReplace() {
return toReplace;
}

public CharsetFix getCharset() {
return charset;
}

public void setState(VisitorState state) {
this.state = state;
}

public void setToReplace(Tree toReplace) {
this.toReplace = toReplace;
}

public void setCharset(CharsetFix charset) {
this.charset = charset;
}

public FileCharsetReplacementContext(VisitorState state, Tree toReplace, CharsetFix charset) {
this.state = state;
this.toReplace = toReplace;
this.charset = charset;
}
}