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

feat: add UnnecessaryModifier rule #219

Merged
merged 3 commits into from
Oct 24, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public Changelog applyToGivenPath(String path) {
addProcessors(pm, changeListener);
pm.process(model.getAllTypes());
Collection<CtType<?>> newTypes = model.getAllTypes();
LOGGER.atInfo().log("Applying transformations to %s done", path);
LOGGER.atInfo().log(
"%s Changes found", changeListener.getChangelog().getChanges().size());
printing.printChangedTypes(changeListener, newTypes);
return changeListener.getChangelog();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import com.contrastsecurity.sarif.SarifSchema210;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.async.ResultCallbackTemplate;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.command.LogContainerCmd;
import com.github.dockerjava.api.command.WaitContainerResultCallback;
import com.github.dockerjava.api.model.AccessMode;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Frame;
import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Image;
import com.github.dockerjava.api.model.Volume;
Expand All @@ -19,6 +22,7 @@
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.transport.DockerHttpClient;
import com.google.common.flogger.FluentLogger;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
Expand Down Expand Up @@ -122,19 +126,37 @@ private void copyQodanaRules(Path sourceRoot) {

private List<AnalyzerResult> startQodanaContainer(DockerClient dockerClient, CreateContainerResponse container)
throws InterruptedException {
dockerClient.startContainerCmd(container.getId()).exec();
String containerId = container.getId();
if (containerId == null) {
logger.atSevere().log("Container id is null");
return List.of();
}
logger.atInfo().log("Starting qodana container %s", containerId);
dockerClient.startContainerCmd(containerId).exec();
WaitContainerResultCallback exec =
dockerClient.waitContainerCmd(container.getId()).exec(new WaitContainerResultCallback());
dockerClient.waitContainerCmd(containerId).exec(new WaitContainerResultCallback());
List<AnalyzerResult> results = new ArrayList<>();
dockerClient
.waitContainerCmd(container.getId())
.waitContainerCmd(containerId)
.exec(new ResultCallbackTemplate<WaitContainerResultCallback, WaitResponse>() {
@Override
public void onNext(WaitResponse object) {
try {
exec.awaitCompletion();

results.addAll(parseSarif(Paths.get(resultPathString)));
logger.atInfo().log("Qodana finished");
if (!Paths.get(resultPathString).toFile().exists()) {
StringBuilder sb = new StringBuilder();
LogContainerCmd logContainerCmd = dockerClient.logContainerCmd(containerId);
logContainerCmd
.withStdOut(true)
.withStdErr(true)
.withTailAll()
.exec(new ResultCallbackImplementation(sb));
logger.atSevere().log(sb.toString());
logger.atSevere().log("Qodana did not create result file");
} else {
results.addAll(parseSarif(Paths.get(resultPathString)));
}
} catch (Exception e) {
logger.atSevere().withCause(e).log("Could not parse sarif");
}
Expand Down Expand Up @@ -217,6 +239,33 @@ private List<AnalyzerResult> parseSarif(Path resultPath) throws IOException {
return results;
}

private final class ResultCallbackImplementation implements ResultCallback<Frame> {
private final StringBuilder sb;

private ResultCallbackImplementation(StringBuilder sb) {
this.sb = sb;
}

@Override
public void close() throws IOException {}

@Override
public void onStart(Closeable closeable) {}

@Override
public void onNext(Frame object) {
sb.append(new String(object.getPayload()));
}

@Override
public void onError(Throwable throwable) {
sb.append(throwable.getMessage());
}

@Override
public void onComplete() {}
}

public static class Builder {

private String resultFolder = "laughing-cache";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import xyz.keksdose.spoon.code_solver.analyzer.qodana.rules.SizeReplaceableByIsEmpty;
import xyz.keksdose.spoon.code_solver.analyzer.qodana.rules.UnnecessaryInterfaceModifier;
import xyz.keksdose.spoon.code_solver.analyzer.qodana.rules.UnnecessaryLocalVariable;
import xyz.keksdose.spoon.code_solver.analyzer.qodana.rules.UnnecessaryModifier;
import xyz.keksdose.spoon.code_solver.analyzer.qodana.rules.UnnecessaryReturn;
import xyz.keksdose.spoon.code_solver.analyzer.qodana.rules.UnnecessaryToStringCall;
import xyz.keksdose.spoon.code_solver.analyzer.qodana.rules.UnusedImport;
Expand All @@ -33,7 +34,9 @@ public enum QodanaRules implements AnalyzerRule {
UNNECESSARY_TO_STRING_CALL("UnnecessaryToStringCall", UnnecessaryToStringCall::new),
UNUSED_IMPORT("UnusedImport", UnusedImport::new),
PROTECTED_MEMBER_IN_FINAL_CLASS("ProtectedMemberInFinalClass", ProtectedMemberInFinalClass::new),
UNNECESSARY_MODIFIER("UnnecessaryModifier", UnnecessaryModifier::new),
POINTLESS_BOOLEAN_EXPRESSION("PointlessBooleanExpression", PointlessBooleanExpression::new);

private final String ruleId;
private final Function<AnalyzerResult, AbstractRefactoring> refactoring;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package xyz.keksdose.spoon.code_solver.analyzer.qodana.rules;

import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtModifiable;
import spoon.reflect.declaration.CtType;
import xyz.keksdose.spoon.code_solver.analyzer.PositionScanner;
import xyz.keksdose.spoon.code_solver.api.analyzer.AnalyzerResult;
import xyz.keksdose.spoon.code_solver.history.Change;
import xyz.keksdose.spoon.code_solver.history.ChangeListener;
import xyz.keksdose.spoon.code_solver.history.MarkdownString;
import xyz.keksdose.spoon.code_solver.transformations.BadSmell;

public class UnnecessaryModifier extends AbstractRefactoring {

public UnnecessaryModifier(AnalyzerResult result) {
super(result);
}

private static final BadSmell UNNECESSARY_MODIFIER = new BadSmell() {
@Override
public MarkdownString getName() {
return MarkdownString.fromRaw("Unnecessary-Modifier");
}

@Override
public MarkdownString getDescription() {
return MarkdownString.fromRaw(
"Some modifiers are not needed, because they are already the default and implicit. These modifiers can be removed.");
}
};

@Override
public void refactor(ChangeListener listener, CtType<?> type) {
if (type.isAnonymous() || !isSameType(type, Path.of(result.filePath()))) {
return;
}
String modifier = result.messageMarkdown().split("`")[1];
if (modifier == null || modifier.isEmpty()) {
return;
}
for (CtElement match : PositionScanner.findLineOnly(type, result.position())) {
if (match instanceof CtModifiable ctModifierHandler) {
var modifiers = new HashSet<>(ctModifierHandler.getModifiers());
modifiers.removeIf(v -> v.toString().equals(modifier.toLowerCase()));
ctModifierHandler.setModifiers(modifiers);
listener.setChanged(
type.getTopLevelType(),
new Change(
UNNECESSARY_MODIFIER,
MarkdownString.fromMarkdown(
"Unnecessary modifier " + modifier + " removed",
"Unnecessary modifier `" + modifier + "` removed"),
type.getTopLevelType(),
result));
}
}
}

@Override
public List<BadSmell> getHandledBadSmells() {
return List.of(UNNECESSARY_MODIFIER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static xyz.keksdose.spoon.code_solver.history.MarkdownString.fromMarkdown;

import com.google.common.flogger.FluentLogger;
import java.nio.file.Path;
import java.util.List;
import spoon.reflect.code.CtInvocation;
Expand All @@ -16,6 +17,7 @@

public class UnnecessaryToStringCall extends AbstractRefactoring {

private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final BadSmell UNNECESSARY_TO_STRING_CALL = new BadSmell() {
@Override
public MarkdownString getName() {
Expand All @@ -36,6 +38,7 @@ public UnnecessaryToStringCall(AnalyzerResult result) {
@Override
public void refactor(ChangeListener listener, CtType<?> type) {
if (type.isAnonymous() || !isSameType(type, Path.of(result.filePath()))) {
logger.atFinest().log("skipping %s", type.getQualifiedName());
return;
}
for (CtInvocation<?> toStringInvocation :
Expand Down