OSS-forge/CodeQualityEval
12
1#!/usr/bin/env bash2 3# Usage: run_PMD_analysis.sh <java_files_directory>4# Example: bash 3_Code_Defects_Analysis/run_PMD_analysis.sh java_human_temp5 6set -u # (no -e, we want to continue even if PMD exits non-zero)7 8# ---- Argument + directory checks ----9if [ -z "$1" ]; then10 echo "Usage: $0 <java_files_directory>"11 echo "Please provide the path to the Java files directory."12 exit 113fi14 15java_dir="$1"16 17if [ ! -d "$java_dir" ]; then18 echo "Error: Directory '$java_dir' does not exist."19 exit 120fi21 22# ---- Locate PMD binary (bundled in the repo) ----23SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"24ROOT_DIR="$(dirname "$SCRIPT_DIR")"25PMD_BIN="$ROOT_DIR/pmd-bin-7.16.0/bin/pmd"26 27if [ -f "$PMD_BIN" ]; then28 chmod +x "$PMD_BIN" || true29 PMD_CMD="$PMD_BIN"30else31 # Fallback: rely on PATH if needed32 PMD_CMD="pmd"33fi34 35# Ensure Java exists36if ! command -v java >/dev/null 2>&1; then37 echo "ERROR: 'java' command not found. Make sure OpenJDK is installed." >&238 exit 139fi40 41echo "Counted .java files:"42echo " In java_dir: $(find "$java_dir" -name '*.java' | wc -l)"43echo44 45# ---- Rulesets to run ----46rulesets=(47 "category/java/bestpractices.xml"48 # "category/java/codestyle.xml"49 "category/java/design.xml"50 # "category/java/documentation.xml"51 "category/java/errorprone.xml"52 "category/java/multithreading.xml"53 "category/java/performance.xml"54)55 56# ---- Run PMD for each ruleset ----57for ruleset in "${rulesets[@]}"; do58 base_name=$(basename "$ruleset" .xml)59 report_file="report_unique_${base_name}.json"60 error_file="errors_unique_${base_name}.json"61 62 echo "Running PMD with $ruleset..."63 echo "Command: $PMD_CMD check -d \"$java_dir\" -R \"$ruleset\" -f json -r \"$report_file\""64 65 # PMD 7 CLI:66 # pmd check -d <dir> -R <ruleset> -f json -r <report>67 # We ignore the exit code (|| true) and look at whether the report file is created.68 PMD_JAVA_OPTS="-Dpmd.error_recovery" \69 "$PMD_CMD" check \70 -d "$java_dir" \71 -R "$ruleset" \72 -f json \73 -r "$report_file" \74 --no-fail-on-error \75 --no-fail-on-violation \76 --verbose \77 2> "$error_file" || true78 79 if [ -s "$report_file" ]; then80 echo "PMD produced report: $report_file"81 else82 echo "PMD failed for $ruleset. See: $error_file"83 echo "---- First lines of $error_file ----"84 sed -n '1,20p' "$error_file" || true85 echo "-------------------------------------"86 fi87 88 echo "--------------------------------------------"89done90 