private static void addAllJavaFiles(File folder, List<String> result) throws IOException { for (File f : folder.listFiles()) { if (f.isDirectory()) { addAllJavaFiles(f, result); } else if (f.getName().endsWith(".java")) { result.add(f.toString()); System.out.println("Java file: " + f.getName()); System.out.println("------------------------------\n"); Path path = f.toPath(); Files.copy(path, System.out); System.out.println("\n------------------------------\n"); } } }
@After public void cleanup() { HollowCodeGenerationCompileUtil.cleanupFolder(new File(sourceFolder), null); HollowCodeGenerationCompileUtil.cleanupFolder(new File(clazzFolder), null); } }
/** * Compiles java source files in the provided source directory, to the provided class directory. * This also runs findbugs on the compiled classes, throwing an exception if findbugs fails. */ public static void compileSrcFiles(String sourceDirPath, String classDirPath) throws Exception { List<String> srcFiles = new ArrayList<>(); addAllJavaFiles(new File(sourceDirPath), srcFiles); File classDir = new File(classDirPath); classDir.mkdir(); List<String> argList = new ArrayList<>(); argList.add("-d"); argList.add(classDir.getAbsolutePath()); argList.add("-classpath"); argList.add(System.getProperty(PROPERTY_CLASSPATH) + System.getProperty("path.separator") + classDirPath); argList.addAll(srcFiles); // argList.toArray() for large size had trouble String[] args = new String[argList.size()]; for (int i = 0; i < argList.size(); i++) { args[i] = argList.get(i); } JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); int err = compiler.run(null, System.out, System.out, args); if (err != 0) throw new RuntimeException("compiler errors, see system.out"); runFindbugs(classDir); }
void runGenerator(String apiClassName, String packageName, Class<?> clazz, UnaryOperator<HollowAPIGenerator.Builder> generatorCustomizer) throws Exception { System.out.println(String.format("Folders (%s) : \n\tsource=%s \n\tclasses=%s", getClass().getSimpleName(), sourceFolder, clazzFolder)); // Setup Folders HollowCodeGenerationCompileUtil.cleanupFolder(new File(sourceFolder), null); HollowCodeGenerationCompileUtil.cleanupFolder(new File(clazzFolder), null); // Run Generator HollowAPIGenerator generator = generatorCustomizer.apply(new HollowAPIGenerator.Builder()) .withDataModel(clazz).withAPIClassname(apiClassName).withPackageName(packageName) .withDestination(sourceFolder).build(); generator.generateSourceFiles(); // Compile to validate generated files HollowCodeGenerationCompileUtil.compileSrcFiles(sourceFolder, clazzFolder); }
/** * Cleanup specified folder based on file older than specified timestamp * * @param folder - folder to be cleaned up * @param timestamp - specify timestamp to cleanup older files */ public static void cleanupFolder(File folder, Long timestamp) { System.out.println("Cleaning up folder: " + folder.getAbsolutePath()); if (folder.exists()) { for (File file : folder.listFiles()) { if (file.isDirectory()) { cleanupFolder(file, timestamp); file.delete(); } else if (timestamp == null || (timestamp.longValue() - file.lastModified() >= 5000)) { // cleanup file if it is older than specified timestamp with some buffer time System.out.println(String.format("\t deleting file: %s, lastModified=%s", file.getName(), new Date(file.lastModified()))); file.delete(); } } folder.delete(); } } }