From 7f5d579dcf68f8f778d57528251b2ae1a08f2507 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 1 Feb 2024 14:45:23 +0100 Subject: [PATCH] Bump com.github.javaparser:javaparser-core from 3.25.7 to 3.25.8 (#117) * Bump com.github.javaparser:javaparser-core from 3.25.7 to 3.25.8 Bumps [com.github.javaparser:javaparser-core](https://github.com/javaparser/javaparser) from 3.25.7 to 3.25.8. - [Release notes](https://github.com/javaparser/javaparser/releases) - [Changelog](https://github.com/javaparser/javaparser/blob/master/changelog.md) - [Commits](https://github.com/javaparser/javaparser/compare/javaparser-parent-3.25.7...javaparser-parent-3.25.8) --- updated-dependencies: - dependency-name: com.github.javaparser:javaparser-core dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] * Remove usage of guava in HammingWalker Guava is a transitive dependency and shouldn't be used in the analyzer --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Sander Ploegsma --- build.gradle | 2 +- .../exercises/hamming/HammingWalker.java | 130 ++++++++---------- 2 files changed, 62 insertions(+), 70 deletions(-) diff --git a/build.gradle b/build.gradle index aebe4224..2e71ce4f 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ repositories { dependencies { implementation "org.json:json:20231013" - implementation "com.github.javaparser:javaparser-core:3.25.7" + implementation "com.github.javaparser:javaparser-core:3.25.8" testImplementation platform("org.junit:junit-bom:5.10.1") testImplementation "org.junit.jupiter:junit-jupiter" diff --git a/src/main/java/analyzer/exercises/hamming/HammingWalker.java b/src/main/java/analyzer/exercises/hamming/HammingWalker.java index 5d01ca18..70423131 100644 --- a/src/main/java/analyzer/exercises/hamming/HammingWalker.java +++ b/src/main/java/analyzer/exercises/hamming/HammingWalker.java @@ -9,26 +9,17 @@ import com.github.javaparser.ast.expr.MethodCallExpr; import com.github.javaparser.ast.nodeTypes.NodeWithRange; import com.github.javaparser.ast.stmt.*; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableListMultimap; -import com.google.common.collect.ListMultimap; -import com.google.common.collect.Multimaps; - -import java.util.HashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; + +import java.util.*; import java.util.function.Consumer; +import java.util.stream.Collectors; import java.util.stream.Stream; -import static com.google.common.collect.ImmutableSet.toImmutableSet; -import static com.google.common.collect.MoreCollectors.toOptional; - class HammingWalker implements Consumer { private ClassOrInterfaceDeclaration hammingClass; - private List constructors = ImmutableList.of(); + private final Set constructors = new HashSet<>(); private ConstructorDeclaration constructor; - private ListMultimap methods = ImmutableListMultimap.of(); + private final Map> methods = new HashMap<>(); private final Set methodsCalledByConstructor = new HashSet<>(); private boolean constructorHasIfStatements; private boolean constructorThrowsIllegalArgumentDirectly; @@ -45,8 +36,8 @@ public void accept(ClassOrInterfaceDeclaration node) { } private void walkHammingClass() { - methods = getMethodsByName(); - constructors = hammingClass.getConstructors(); + methods.putAll(getMethodsByName()); + constructors.addAll(hammingClass.getConstructors()); findConstructor().ifPresent(this::walkConstructor); @@ -54,10 +45,10 @@ private void walkHammingClass() { } - private ListMultimap getMethodsByName() { - return Multimaps.index( - hammingClass.findAll(MethodDeclaration.class), - MethodDeclaration::getNameAsString); + private Map> getMethodsByName() { + return hammingClass.findAll(MethodDeclaration.class) + .stream() + .collect(Collectors.groupingBy(MethodDeclaration::getNameAsString)); } private Optional findConstructor() { @@ -74,28 +65,28 @@ private void walkConstructorStatement(Statement statement) { if (statement.isIfStmt()) { constructorHasIfStatements = true; } - + if (isThrowNewIllegalArgument(statement)) { constructorThrowsIllegalArgumentDirectly = true; } - + if (statementMayCalculateHammingDistance(statement)) { constructorMayCalculateDistanceDirectly = true; } getMethodCallNames(statement) - .forEach(methodName -> - recursivelyAddMethodsCalled(methodName, methodsCalledByConstructor)); + .forEach(methodName -> + recursivelyAddMethodsCalled(methodName, methodsCalledByConstructor)); } private boolean isThrowNewIllegalArgument(Statement statement) { return statement.findAll(ThrowStmt.class).stream() - .anyMatch(this::isCreatingIllegalArgumentException); + .anyMatch(this::isCreatingIllegalArgumentException); } private boolean isCreatingIllegalArgumentException(ThrowStmt throwStmt) { return throwStmt.getExpression().isObjectCreationExpr() - && throwStmt + && throwStmt .getExpression() .asObjectCreationExpr() .getType() @@ -109,15 +100,15 @@ private boolean isMethodCall(Statement statement) { private Stream getMethodCallNames(Statement statement) { return statement.findAll(MethodCallExpr.class).stream() - .map(MethodCallExpr::getNameAsString) - .distinct(); + .map(MethodCallExpr::getNameAsString) + .distinct(); } private Optional findGetHammingDistanceMethod() { return methods.get("getHammingDistance").stream() - // we only care about the one with no parameters - .filter(method -> method.getParameters().isEmpty()) - .collect(toOptional()); + // we only care about the one with no parameters + .filter(method -> method.getParameters().isEmpty()) + .findFirst(); } private void walkGetHammingDistanceMethod(MethodDeclaration getHammingDistanceMethod) { @@ -134,39 +125,39 @@ private void walkGetHammingDistanceStatement(Statement statement) { } getMethodCallNames(statement) - .forEach(methodName -> - recursivelyAddMethodsCalled(methodName, methodsCalledByGetHammingDistance)); + .forEach(methodName -> + recursivelyAddMethodsCalled(methodName, methodsCalledByGetHammingDistance)); } private void recursivelyAddMethodsCalled( - String methodName, - Set methodsCalled) { + String methodName, + Set methodsCalled) { if (methodsCalled.contains(methodName)) { return; } methodsCalled.add(methodName); getMethodsCalledBy(methodName) - .distinct() - .forEach(calledMethod -> - recursivelyAddMethodsCalled(calledMethod, methodsCalled)); + .distinct() + .forEach(calledMethod -> + recursivelyAddMethodsCalled(calledMethod, methodsCalled)); } private Stream getMethodsCalledBy(String methodName) { - return methods.get(methodName).stream() - .flatMap(this::getMethodsCalledBy); + return methods.getOrDefault(methodName, List.of()).stream() + .flatMap(this::getMethodsCalledBy); } private Stream getMethodsCalledBy(MethodDeclaration method) { return method.getBody() - .map(this::getMethodsCalledBy) - .orElse(Stream.of()); + .map(this::getMethodsCalledBy) + .orElse(Stream.of()); } private Stream getMethodsCalledBy(BlockStmt body) { return body.getStatements().stream() - .filter(this::isMethodCall) - .flatMap(this::getMethodCallNames); + .filter(this::isMethodCall) + .flatMap(this::getMethodCallNames); } public boolean hasConstructor() { @@ -183,64 +174,64 @@ public boolean constructorHasMethodCalls() { public boolean constructorThrowsIllegalArgument() { return constructorThrowsIllegalArgumentDirectly - || constructorThrowsIllegarArgumentIndirectly(); + || constructorThrowsIllegarArgumentIndirectly(); } private boolean constructorThrowsIllegarArgumentIndirectly() { return methodsCalledByConstructor.stream() - .anyMatch(this::methodThrowsIllegalArgumentException); + .anyMatch(this::methodThrowsIllegalArgumentException); } private boolean methodThrowsIllegalArgumentException(String methodName) { - return methods.get(methodName).stream() - .anyMatch(this::methodThrowsIllegalArgumentException); + return methods.getOrDefault(methodName, List.of()).stream() + .anyMatch(this::methodThrowsIllegalArgumentException); } private boolean methodThrowsIllegalArgumentException(MethodDeclaration method) { return method.getBody() - .map(this::methodBodyThrowsIllegalArgumentException) - .orElse(false); + .map(this::methodBodyThrowsIllegalArgumentException) + .orElse(false); } private boolean methodBodyThrowsIllegalArgumentException(BlockStmt body) { return body.getStatements().stream() - .anyMatch(this::isThrowNewIllegalArgument); + .anyMatch(this::isThrowNewIllegalArgument); } public boolean constructorMayCalculateDistance() { return constructorMayCalculateDistanceDirectly - || constructorCallsMethodThatMayCalculateDistance(); + || constructorCallsMethodThatMayCalculateDistance(); } private boolean constructorCallsMethodThatMayCalculateDistance() { return methodsCalledByConstructor.stream() - .anyMatch(this::methodMayCalculateHammingDistance); + .anyMatch(this::methodMayCalculateHammingDistance); } public boolean getHammingDistanceMethodMayCalculateDistance() { return getHammingDistanceMayCalculateDistanceDirectly - || getHammingDistanceCallsMethodThatMayCalculateDistance(); + || getHammingDistanceCallsMethodThatMayCalculateDistance(); } private boolean getHammingDistanceCallsMethodThatMayCalculateDistance() { return methodsCalledByGetHammingDistance.stream() - .anyMatch(this::methodMayCalculateHammingDistance); + .anyMatch(this::methodMayCalculateHammingDistance); } private boolean methodMayCalculateHammingDistance(String methodName) { - return methods.get(methodName).stream() - .anyMatch(this::methodMayCalculateHammingDistance); + return methods.getOrDefault(methodName, List.of()).stream() + .anyMatch(this::methodMayCalculateHammingDistance); } private boolean methodMayCalculateHammingDistance(MethodDeclaration method) { return method.getBody() - .map(this::methodBodyMayCalculateHammingDistance) - .orElse(false); + .map(this::methodBodyMayCalculateHammingDistance) + .orElse(false); } private boolean methodBodyMayCalculateHammingDistance(BlockStmt body) { return body.getStatements().stream() - .anyMatch(this::statementMayCalculateHammingDistance); + .anyMatch(this::statementMayCalculateHammingDistance); } private boolean statementMayCalculateHammingDistance(Statement statement) { @@ -249,8 +240,8 @@ private boolean statementMayCalculateHammingDistance(Statement statement) { private boolean hasLoopStatement(Statement statement) { return !statement.findAll(ForStmt.class).isEmpty() - || !statement.findAll(ForEachStmt.class).isEmpty() - || !statement.findAll(WhileStmt.class).isEmpty(); + || !statement.findAll(ForEachStmt.class).isEmpty() + || !statement.findAll(WhileStmt.class).isEmpty(); } private boolean hasLambdaExpression(Statement statement) { @@ -259,16 +250,17 @@ private boolean hasLambdaExpression(Statement statement) { public Set getLongConstructors() { return constructors.stream() - .filter(this::isLongNode) - .map(ConstructorDeclaration::getNameAsString) - .collect(toImmutableSet()); + .filter(this::isLongNode) + .map(ConstructorDeclaration::getNameAsString) + .collect(Collectors.toUnmodifiableSet()); } public Set getLongMethods() { return methods.values().stream() - .filter(this::isLongNode) - .map(MethodDeclaration::getNameAsString) - .collect(toImmutableSet()); + .flatMap(Collection::stream) + .filter(this::isLongNode) + .map(MethodDeclaration::getNameAsString) + .collect(Collectors.toUnmodifiableSet()); } private boolean isLongNode(NodeWithRange node) { @@ -289,6 +281,6 @@ public boolean shouldUseStreamFilterAndCount() { private boolean usesMethod(String methodName) { return methodsCalledByConstructor.contains(methodName) - || methodsCalledByGetHammingDistance.contains(methodName); + || methodsCalledByGetHammingDistance.contains(methodName); } } \ No newline at end of file