summaryrefslogtreecommitdiffstats
path: root/check_java_packages
blob: fcc9e4f5b6c3b4b62bf9f116b14cfee98d73f919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#! /bin/bash

if [ $# -lt 1 ]; then
    echo "missing PATH argument"
    exit 1
fi

TOPDIR=$1
ERRORS=.check_package.err

RESET="\033[0m"
RED="\033[0;31m"
BROWN="\033[0;33m"
GREEN="\033[0;32m"

find $TOPDIR -name \*.java | while read file;
do
    relpath=${file#${TOPDIR}}
    CORRECT=$(echo $relpath | sed 's/\.\///; s/\/\+/\./g; s/\.\w\+.java//')
    N=$(cat $file | sed -n '/^\s*package /p ' | wc -l)
    if [ $N -gt 1 ]; then
        echo -e "$file : $BROWN more then 1 package directive$RESET";
        touch $ERRORS
        continue
    fi
    PACKAGE=$(cat $file | sed -n '/^\s*package /p ')
    if [ -z "$PACKAGE" ]; then
        echo -e "$file : $BROWN missing package directive$RESET";
        touch $ERRORS
        continue
    fi
    PACKAGE=$(echo "$PACKAGE" | sed 's/ *package *//; s/ *; *$//;')
    if [ "$PACKAGE" != "${CORRECT}" ]; then
        echo -e "$file : $RED$PACKAGE$RESET -> $GREEN$CORRECT$RESET";
        touch $ERRORS
    fi
    for IMPORT in $(cat $file | sed -n 's/import\s\+.*\.\(.*\);/\1/p');
    do
        cat $file | grep $IMPORT | grep -qv import
        if [ $? -ne 0 ]; then
            echo -e "$file : import $RED$IMPORT$RESET might be useless";
        fi
    done
done

if [ -r "$ERRORS" ]; then
    rm $ERRORS
    exit 1
fi