Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save commonquail/4a24a202fb619cb8a5d2 to your computer and use it in GitHub Desktop.
Save commonquail/4a24a202fb619cb8a5d2 to your computer and use it in GitHub Desktop.
Find Java files with no declared constructors
# 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