Snippets

Jonathan Hult Java - decompile JAR and remove comments

Created by Jonathan Hult last modified
#!/bin/bash
# Source: https://bitbucket.org/jhult/workspace/snippets/7eg4r4
# This script attempts to follow this style guide: https://google.github.io/styleguide/shellguide.html#s7-naming-conventions

readonly JAR="$1"
readonly DIR_TMP="/tmp"
readonly DIR_SRC="$DIR_TMP/src_$JAR"
readonly DIR_NO_COMMENTS="$DIR_TMP/src_no_comm_$JAR"
readonly FILE_CFR="$DIR_TMP/cfr.jar"
readonly FILE_JCP="$DIR_TMP/jcp.jar"
readonly VERSION_CFR=0.152
readonly VERSION_JCP=7.1.2
readonly URL_CFR="https://github.com/leibnitz27/cfr/releases/download/$VERSION_CFR/cfr-$VERSION_CFR.jar"
readonly URL_JCP="https://github.com/raydac/java-comment-preprocessor/releases/download/$VERSION_JCP/jcp-$VERSION_JCP.jar"

download() {
    echo "Downloading: $2 ..."
	curl -fsSL "$1" -o "$2"
}

check_param() {
    if [ "$#" == "" ]; then
        echo "You must enter a JAR file as the first and only parameter."
        exit 1
    fi
}

exists_or_download() {
    # If JAR file does not exist, download it
    if ! test -f "$1"; then

        # Default to CFR
        local URL="$URL_CFR"

        if [ "$1" == "$FILE_JCP" ]; then
            URL="$URL_JCP"
        fi
        download "$URL" "$1"
    fi
}

main() {
    check_param "$@"
    echo "This script will decompile $JAR and remove any comments."
    exists_or_download "$FILE_CFR"
    exists_or_download "$FILE_JCP"
    java -jar "$FILE_CFR" --outputdir "$DIR_SRC" "$JAR"
    java -jar "$FILE_JCP" --i:"$DIR_SRC" --o:"$DIR_NO_COMMENTS" -ef:none --r
    rm -rf "$DIR_SRC"
    echo "JAR decompiled here: $DIR_NO_COMMENTS"
}

main "$@"

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.