chore: add openrewrite gradle plugin to improve code quality

This commit is contained in:
Skylot
2024-08-11 20:55:36 +01:00
parent 8c6ec3bccc
commit a2bfe9bbe8
19 changed files with 62 additions and 21 deletions
+2
View File
@@ -4,6 +4,8 @@ plugins {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:2.0.10")
implementation("org.openrewrite:plugin:6.19.1")
}
repositories {
@@ -3,6 +3,8 @@ import org.gradle.api.tasks.testing.logging.TestExceptionFormat
plugins {
java
checkstyle
id("jadx-rewrite")
}
val jadxVersion: String by rootProject.extra
@@ -11,7 +13,7 @@ group = "io.github.skylot"
version = jadxVersion
dependencies {
implementation("org.slf4j:slf4j-api:2.0.14")
implementation("org.slf4j:slf4j-api:2.0.16")
compileOnly("org.jetbrains:annotations:24.1.0")
testImplementation("ch.qos.logback:logback-classic:1.5.6")
@@ -0,0 +1,37 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
plugins {
id("org.openrewrite.rewrite")
}
repositories {
mavenCentral()
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:2.15.0")
rewrite("org.openrewrite.recipe:rewrite-logging-frameworks:2.12.0")
rewrite("org.openrewrite.recipe:rewrite-migrate-java:2.21.0")
rewrite("org.openrewrite.recipe:rewrite-static-analysis:1.13.0")
}
tasks {
rewrite {
// exclusion("src/test/java/jadx/tests/integration")
// activeRecipe("org.openrewrite.java.migrate.Java8toJava11")
// checkstyle auto fix
// activeRecipe("org.openrewrite.staticanalysis.CodeCleanup")
// setCheckstyleConfigFile(file("$rootDir/config/checkstyle/checkstyle.xml"))
// logging
// activeRecipe("org.openrewrite.java.logging.slf4j.Slf4jBestPractices")
// activeRecipe("org.openrewrite.java.logging.slf4j.LoggersNamedForEnclosingClass")
// activeRecipe("org.openrewrite.java.logging.slf4j.ParameterizedLogging")
// activeRecipe("org.openrewrite.java.logging.PrintStackTraceToLogError")
// testing
activeRecipe("org.openrewrite.java.testing.assertj.Assertj")
}
}
@@ -62,7 +62,7 @@ public class AnnotatedCodeWriter extends SimpleCodeWriter implements ICodeWriter
buf.append(cw.getCodeStr());
return this;
}
AnnotatedCodeWriter code = ((AnnotatedCodeWriter) cw);
AnnotatedCodeWriter code = (AnnotatedCodeWriter) cw;
line--;
int startPos = getLength();
for (Map.Entry<Integer, ICodeAnnotation> entry : code.annotations.entrySet()) {
@@ -24,7 +24,7 @@ public class ClspClass {
private Map<String, ClspMethod> methodsMap = Collections.emptyMap();
private List<ArgType> typeParameters = Collections.emptyList();
private ClspClassSource source;
private final ClspClassSource source;
public ClspClass(ArgType clsType, int id, int accFlags, ClspClassSource source) {
this.clsType = clsType;
@@ -33,7 +33,7 @@ public class JadxCommentsAttr implements IJadxAttribute {
private final Map<CommentsLevel, List<String>> comments = new EnumMap<>(CommentsLevel.class);
public void add(CommentsLevel level, String comment) {
comments.computeIfAbsent(level, (l) -> new ArrayList<>()).add(comment);
comments.computeIfAbsent(level, l -> new ArrayList<>()).add(comment);
}
public List<String> formatAndFilter(CommentsLevel level) {
@@ -14,14 +14,14 @@ public class MethodOverrideAttr extends PinnedAttribute {
/**
* All methods overridden by current method. Current method excluded, empty for base method.
*/
private List<IMethodDetails> overrideList;
private final List<IMethodDetails> overrideList;
/**
* All method nodes from override hierarchy. Current method included.
*/
private SortedSet<MethodNode> relatedMthNodes;
private Set<IMethodDetails> baseMethods;
private final Set<IMethodDetails> baseMethods;
public MethodOverrideAttr(List<IMethodDetails> overrideList, SortedSet<MethodNode> relatedMthNodes, Set<IMethodDetails> baseMethods) {
this.overrideList = overrideList;
@@ -51,7 +51,7 @@ public class MethodUtils {
public MethodNode resolveMethod(BaseInvokeNode invokeNode) {
IMethodDetails methodDetails = getMethodDetails(invokeNode);
if (methodDetails instanceof MethodNode) {
return ((MethodNode) methodDetails);
return (MethodNode) methodDetails;
}
return null;
}
@@ -69,7 +69,7 @@ public class ConstructorVisitor extends AbstractVisitor {
}
co.inheritMetadata(inv);
RegisterArg instanceArg = ((RegisterArg) inv.getArg(0));
RegisterArg instanceArg = (RegisterArg) inv.getArg(0);
instanceArg.getSVar().removeUse(instanceArg);
if (co.isNewInstance()) {
InsnNode assignInsn = instanceArg.getAssignInsn();
@@ -187,7 +187,7 @@ public class MethodInvokeVisitor extends AbstractVisitor {
arg.setType(castType);
arg.add(AFlag.EXPLICIT_PRIMITIVE_TYPE);
} else if (InsnUtils.isWrapped(arg, InsnType.CHECK_CAST)) {
IndexInsnNode wrapInsn = ((IndexInsnNode) ((InsnWrapArg) arg).getWrapInsn());
IndexInsnNode wrapInsn = (IndexInsnNode) ((InsnWrapArg) arg).getWrapInsn();
wrapInsn.updateIndex(castType);
} else {
if (Consts.DEBUG_TYPE_INFERENCE) {
@@ -165,7 +165,7 @@ public class ShadowFieldVisitor extends AbstractVisitor {
switch (insn.getType()) {
case IPUT:
case IGET:
return ((FieldInfo) ((IndexInsnNode) insn).getIndex());
return (FieldInfo) ((IndexInsnNode) insn).getIndex();
default:
return null;
}
@@ -233,7 +233,7 @@ public class RegionMaker {
BlockNode out;
if (loopRegion.isConditionAtEnd()) {
BlockNode thenBlock = condInfo.getThenBlock();
out = (thenBlock == loop.getEnd() || thenBlock == loopStart) ? condInfo.getElseBlock() : thenBlock;
out = thenBlock == loop.getEnd() || thenBlock == loopStart ? condInfo.getElseBlock() : thenBlock;
out = BlockUtils.followEmptyPath(out);
loopStart.remove(AType.LOOP);
loop.getEnd().add(AFlag.ADDED_TO_REGION);
@@ -351,7 +351,7 @@ public class RegionMaker {
return true;
}
Optional<Edge> mainEdgeOpt = exitEdges.stream().filter(edge -> edge.getSource() == mainExitBlock).findFirst();
if (!mainEdgeOpt.isPresent()) {
if (mainEdgeOpt.isEmpty()) {
throw new JadxRuntimeException("Not found exit edge by exit block: " + mainExitBlock);
}
Edge mainExitEdge = mainEdgeOpt.get();
@@ -20,7 +20,7 @@ public final class TypeBoundFieldGetAssign implements ITypeBoundDynamic {
public TypeBoundFieldGetAssign(RootNode root, IndexInsnNode getNode, ArgType initType) {
this.root = root;
this.getNode = getNode;
this.fieldInfo = ((FieldInfo) getNode.getIndex());
this.fieldInfo = (FieldInfo) getNode.getIndex();
this.initType = initType;
}
@@ -142,7 +142,7 @@ public class UsageInfoVisitor extends AbstractVisitor {
IMethodRef mthRef;
ICustomPayload payload = insnData.getPayload();
if (payload != null) {
mthRef = ((IMethodRef) payload);
mthRef = (IMethodRef) payload;
} else {
mthRef = insnData.getIndexAsMethod();
}
@@ -22,7 +22,7 @@ public class InsnDataUtils {
}
ICustomPayload payload = insnData.getPayload();
if (payload != null) {
return ((ICallSite) payload);
return (ICallSite) payload;
}
return insnData.getIndexAsCallSite();
}
@@ -34,7 +34,7 @@ public class InsnDataUtils {
}
ICustomPayload payload = insnData.getPayload();
if (payload != null) {
return ((IMethodRef) payload);
return (IMethodRef) payload;
}
return insnData.getIndexAsMethod();
}
@@ -273,7 +273,7 @@ public class ResTableBinaryParser extends CommonBinaryParser implements IResTabl
// to the value of the type bits in a resource identifier). 0 is invalid.
int id = is.readInt8();
int flags = is.readInt8(); // 0 or 1
boolean flagSparse = (flags == 1);
boolean flagSparse = flags == 1;
is.checkInt16(0, "type chunk, reserved");
int entryCount = is.readInt32();
@@ -10,7 +10,7 @@ import org.junit.jupiter.api.extension.TestExecutionExceptionHandler;
public class NotYetImplementedExtension implements AfterTestExecutionCallback, TestExecutionExceptionHandler {
private Set<Method> knownFailedMethods = new HashSet<>();
private final Set<Method> knownFailedMethods = new HashSet<>();
@Override
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable {
@@ -24,7 +24,7 @@ public class NotYetImplementedExtension implements AfterTestExecutionCallback, T
public void afterTestExecution(ExtensionContext context) throws Exception {
if (!knownFailedMethods.contains(context.getTestMethod().get())
&& isNotYetImplemented(context)
&& !context.getExecutionException().isPresent()) {
&& context.getExecutionException().isEmpty()) {
throw new AssertionError("Test "
+ context.getTestClass().get().getName() + '.' + context.getTestMethod().get().getName()
+ " is marked as @NotYetImplemented, but passes!");
@@ -95,7 +95,7 @@ public abstract class DexInsnFormat {
regs[0] = byte1(opcodeUnit);
long literal = in.readShort();
literal <<= (byte0(opcodeUnit) == DexOpcodes.CONST_HIGH16) ? 16 : 48;
literal <<= byte0(opcodeUnit) == DexOpcodes.CONST_HIGH16 ? 16 : 48;
insn.setLiteral(literal);
}
};
@@ -99,7 +99,7 @@ public class SmaliInsnFormat {
}
private InsnFormatter noArgsInsn(String name) {
return (fi) -> fi.getCodeWriter().add(name);
return fi -> fi.getCodeWriter().add(name);
}
private String literal(InsnFormatterInfo fi) {