ArchUnit icon indicating copy to clipboard operation
ArchUnit copied to clipboard

If origin file could be deleted when importLocations called

Open zhangchunzhao12 opened this issue 2 years ago • 4 comments

Normally I don't need to delete origin jars when call ClassFileImporter().importLocations(androidJarLocation); But there is an occation, when input is aar file, I would unzip it before feed to importLocations; So there would generate temp jars when unzip aar file, and I have no wish to keep it when check is over; But I try many way to delete those temp jars when checking process is over So my question is: is there any way to detete those temp jars; Hoping for your reply , thanks very much !

zhangchunzhao12 avatar Nov 14 '22 08:11 zhangchunzhao12

Maybe you can use Runtime.getRuntime().addShutdownHook(deleteTempJars); with a suitable Thread deleteTempJars?

hankem avatar Nov 16 '22 20:11 hankem

For example: JavaClasses dependencyClasses = new ClassFileImporter().importLocations(dependencyLocation); This is how I import jars, is there any way to get the Thread object of the process? Thanks very much!

zhangchunzhao12 avatar Nov 18 '22 06:11 zhangchunzhao12

The idea of the shutdown hook is to define your own Thread that does the necessary cleanup. It will be started when the JVM begins its shutdown sequence. (But note that all of this is entirely unrelated to ArchUnit.)

hankem avatar Nov 18 '22 07:11 hankem

I'm not sure I fully understand your constraints, but what about just creating your own utility around the ClassFileImporter? Like

class ImportUtils {
  static JavaClasses importAar(aarFile) {
    try {
      Path location = unpack(aarFile);
      return new ClassFileImporter().importPath(location);
    } finally {
      delete(aarFile);
    }
  }
}

codecholeric avatar Nov 21 '22 17:11 codecholeric