typetools / annotation-tools

Tools for type annotations in Java
https://checkerframework.org/annotation-file-utilities/
MIT License
38 stars 34 forks source link

insert-annotation-to-source: process whole directory #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Add a way to process an entire directory of .java files rather than having
to specify each one on the command line

Original issue reported on code.google.com by michael.ernst@gmail.com on 28 Jul 2009 at 4:15

GoogleCodeExporter commented 9 years ago
The simple workaround (Unix shell syntax) is:
`find . -type f -name '*.java' -print`

Original comment by michael.ernst@gmail.com on 29 Jul 2009 at 6:32

GoogleCodeExporter commented 9 years ago
Even with the workaround, it is very inefficient to run 
insert-annotation-to-source with one source file at a time. When processing a 
JAIF, the AFU looks for classes specified in the JAIF even if they are not 
found in the Java source file. This time is wasted on each run of 
insert-annotation-to-source. For the Google Guava libraries, this resulted in 
the annotation insertions taking a couple of hours.

insert-annotation-to-source does support wildcards, but it does not recurse 
into subdirectories. It would be useful if insert-annotation-to-source could 
follow the package structure on disk (e.g. org/checkerframework/checker/linear) 
when given a root directory to start from.

Original comment by jtha...@cs.washington.edu on 9 Apr 2014 at 6:15

GoogleCodeExporter commented 9 years ago
At one point David had a script to split JAIF files by package and/or class so 
there would be parallel directory structures for source and index files.  That 
provides a good interim solution, though it would be better simply to read and 
process the index for one class at a time.

Original comment by d...@cs.washington.edu on 10 Apr 2014 at 7:15

GoogleCodeExporter commented 9 years ago
Can we add the scripts to split jaif files to the repository so that everybody 
can use the interim solution?

Original comment by wdi...@gmail.com on 11 Apr 2014 at 7:56

the1derer commented 5 years ago

@mernst

Even with the workaround, it is very inefficient to run insert-annotation-to-source with one source file at a time. When processing a JAIF, the AFU looks for classes specified in the JAIF even if they are not found in the Java source file. This time is wasted on each run of insert-annotation-to-source. For the Google Guava libraries, this resulted in the annotation insertions taking a couple of hours.

I have a tried and tested script to achieve this without having to check every JAIF file with every other JAVA file. But we have to assume the user has named JAVA file same as the Class name which is a very realistic assumption. This script also takes a very small amount of execution time because of this assumption.

Note: This is not a proposal. I just want to put out information to public on how to reduce time while processing whole directory.
IMHO we should just write this in the manual as an example.

#! /bin/bash

JDK12BASE=/mnt/Work/Checker_Framework/openjdk-jdk12u/src #Directory Containing Java Source files
JAIFJDK8BASE=JAIF/jdk8   # Directory Containing JAIF files
export CLASSPATH=checker-qual.jar: # Jar containing required Annotation Classes

# Copy Outer Class JAIF parallel to Java Source files
for i in `find ${JAIFJDK8BASE} -name '*.jaif'` ; do
   JAVAFILE=`basename $i .jaif`.java
   for j in `find ${JDK12BASE} -name ${JAVAFILE}` ; do 
     cp ${i}  "`dirname ${j}`/"
     echo "${i} has been copied to `dirname ${j}`" 
   done
done

# Copy Inner Class JAIF parallel to Java Source files
 for i in `find ${JAIFJDK8BASE} -type f -name '*$*.jaif'` ; do
   FILENAME=`basename $i .jaif`
   searchstring="$"
   OUTERCLASS=${FILENAME%$searchstring*}  
   JAVAFILE=${OUTERCLASS}.java
   # echo $JAVAFILE
   for j in `find ${JDK12BASE} -name ${JAVAFILE}` ; do 
     cp ${i}  "`dirname ${j}`/"
     echo "${i} has been copied to `dirname ${j}`" 
   done
 done

# Now inserting annotation using both Inner and Outerlass C JAIFs

for i in `find ${JDK12BASE}  -name '*.jaif'` ; do

  JAIFs=${i} # List of JAIFs for a single java file
  FILENAME=`basename ${i} .jaif` # Extract file name without extension

  if [[ $FILENAME =~ "\$" ]]; then # Not Performing below operation on Inner Class JAIFs
    continue;
  fi

  OUTERCLASSLOC=`dirname $i` #Define Outer Class Location

  OUTERCLASS=${FILENAME}

  INNERCLASS=${OUTERCLASS}"\$*.jaif"  # Pattern to find Inner Class of a given Outer file

  for j in `find ${OUTERCLASSLOC} -name $INNERCLASS` ; do  # Cnsturct a list of JAIF files containg Outer and Inner JAIFs
    # echo $j
    JAIFs="$JAIFs $j"  #Concat name of all Inner Classs to JAIF list.
    # echo "${JAIFS} ${j}"
  done
  echo ${JAIFs}
  insert-annotations-to-source -i=true ${JAIFs}  "`dirname ${i}`/`basename $i .jaif`.java"
  echo "Annotations has been added successfully to `dirname ${i}`/`basename $i .jaif`.java"
done

Thanks.