Created
April 13, 2015 11:58
-
-
Save commonquail/4a24a202fb619cb8a5d2 to your computer and use it in GitHub Desktop.
Find Java files with no declared constructors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These two commands both search the working directory | |
# for java class files that do not declare a constructor. | |
# | |
# The first command is marginally faster than the second; | |
# for truly large code bases, this may matter, | |
# but mostly it won't. | |
# | |
# The commands disregard package-info.java and tests. | |
# #1: Finds all files in the first pass, | |
# then greps them in a second pass. | |
for file in $(find . -type f -name '*.java' | grep -v 'Test\|package-info'); do | |
class="${file##*/}" && class="${class%*.java}" | |
echo "$(grep --count "\( \|private\|protected\|public\) $class(" "$file") $file"; | |
done | grep '^0' | |
# #2: Finds and greps files in the same pass. | |
find . -type f -name '*.java' \ | |
-a -not -iname 'package-info.java' \ | |
-a -not -iname '*test*' \ | |
-exec sh -c 'for file; do | |
class="${file##*/}" && class="${class%*.java}" | |
echo "$(grep --count "\( \|private\|protected\|public\) $class(" "$file") $file" | |
done' _ {} + | grep '^0' | |
# For both methods, | |
# the constructor examination can be moved into a function. | |
# This method is never faster than #2. | |
# NB: this worked for -exec bash but not -exec sh. | |
# Derives the Java class name from a Java file path. | |
ctors_in_file() | |
{ | |
file=$1 | |
class="${file##*/}" && class="${class%*.java}" | |
echo "$(grep --count "\( \|private\|protected\|public\) $class(" "$file") $file"; | |
} | |
export -f ctors_in_file | |
find . -type f -name '*.java' \ | |
-a -not -iname 'package-info.java' \ | |
-a -not -iname '*test*' \ | |
-exec bash -c 'for file; do ctors_in_file $file; done' _ {} + | grep '^0' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment