|
1 | 1 | package de.loskutov.jpg; |
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.file.FileVisitResult; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.SimpleFileVisitor; |
| 9 | +import java.nio.file.attribute.BasicFileAttributes; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import java.util.stream.IntStream; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
3 | 16 | public class Main { |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + File rootDir = new File("./target/generated/"); |
| 20 | + System.out.println("Writing to " + rootDir.getAbsolutePath()); |
| 21 | + Path root = rootDir.toPath(); |
| 22 | + |
| 23 | + |
| 24 | + int depth = 10; |
| 25 | + int roots = 10; |
| 26 | + int classes = 100; |
| 27 | + |
| 28 | + new JavaBuilder(depth, roots, classes, root).build(); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class JavaBuilder { |
| 33 | + |
| 34 | + static List<String> namesList = IntStream.rangeClosed('a', 'z').mapToObj(x -> String.valueOf((char)x)) |
| 35 | + .collect(Collectors.toList()); |
| 36 | + |
| 37 | + int depth; |
| 38 | + int roots; |
| 39 | + private Ring<String> pnames; |
| 40 | + private Path root; |
| 41 | + |
| 42 | + private List<Clazz> classes; |
4 | 43 |
|
5 | | - public static void main(String[] args) { |
| 44 | + private List<Interface> interfaces; |
| 45 | + |
| 46 | + private int countClasses; |
| 47 | + |
| 48 | + public JavaBuilder(int depth, int roots, int countClasses, Path root) { |
| 49 | + this.depth = depth; |
| 50 | + this.roots = roots; |
| 51 | + this.countClasses = countClasses; |
| 52 | + this.root = root; |
| 53 | + pnames = new Ring<>(namesList); |
| 54 | + classes = new ArrayList<>(); |
| 55 | + interfaces = new ArrayList<>(); |
| 56 | + } |
| 57 | + |
| 58 | + void build() throws IOException { |
| 59 | + Files.walkFileTree(root, new SimpleFileVisitor<Path>() { |
| 60 | + @Override |
| 61 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 62 | + Files.delete(file); |
| 63 | + return FileVisitResult.CONTINUE; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 68 | + Files.delete(dir); |
| 69 | + return FileVisitResult.CONTINUE; |
| 70 | + } |
| 71 | + }); |
| 72 | + Files.createDirectories(root); |
| 73 | + |
| 74 | + List<Package> rootPackages = new ArrayList<>(); |
| 75 | + for (int i = 0; i < roots; i++) { |
| 76 | + Package p = createPackage(pnames.next(), null); |
| 77 | + rootPackages.add(p); |
| 78 | + } |
6 | 79 |
|
| 80 | + for (Package r : rootPackages) { |
| 81 | + Stream<Package> stream = Stream.iterate(r, x -> new Package(pnames.next(), x)).limit(depth); |
| 82 | + stream.forEach(p -> { |
| 83 | + createInterfaces(p); |
| 84 | + createClasses(p); |
| 85 | + }); |
| 86 | + } |
| 87 | + List<JavaElement> elements = new ArrayList<>(interfaces); |
| 88 | + elements.addAll(classes); |
| 89 | + elements.forEach(this::generateFile); |
| 90 | + System.out.println("Generated " + elements.size() + " classes"); |
7 | 91 | } |
8 | 92 |
|
| 93 | + private void generateFile(JavaElement e) { |
| 94 | + try { |
| 95 | + e.persist(root); |
| 96 | + } catch (IOException e1) { |
| 97 | + e1.printStackTrace(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + void createInterfaces(Package p) { |
| 102 | + Ring<Interface> toImplement = new Ring<>(interfaces, countClasses); |
| 103 | + if(interfaces.isEmpty()) { |
| 104 | + interfaces.add(new Interface("IFoo0", p.getFqn(), "java.util.concurrent.Callable")); |
| 105 | + } |
| 106 | + toImplement.stream().forEach(i -> { |
| 107 | + interfaces.add(new Interface("IFoo" + interfaces.size(), p.getFqn(), toImplement.next().fqn())); |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + void createClasses(Package p) { |
| 112 | + Ring<Interface> implement = new Ring<>(interfaces, countClasses); |
| 113 | + Ring<Clazz> toExtend = new Ring<>(classes, countClasses); |
| 114 | + if (classes.isEmpty()) { |
| 115 | + classes.add(new Clazz("Element0", p.getFqn(), implement.next().fqn(), "java.lang.Object")); |
| 116 | + } |
| 117 | + toExtend.stream().forEach(x -> { |
| 118 | + classes.add(new Clazz("Element" + classes.size(), p.getFqn(), implement.next().fqn(), toExtend.next().fqn())); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + Package createPackage(String name, Package parent) { |
| 123 | + return new Package(name, parent); |
| 124 | + } |
| 125 | + |
9 | 126 | } |
0 commit comments