Skip to content

Commit b65f06d

Browse files
committed
Allow to generate unlimited number of comments, fields, methods, etc
- additionally print overall count of generated lines of code By default generate 20002 classes with 870084 lines of code and some reasonable number of comments, fields, imports & methods.
1 parent 7809d87 commit b65f06d

File tree

5 files changed

+196
-74
lines changed

5 files changed

+196
-74
lines changed

JavaProjectGenerator/src/de/loskutov/jpg/Clazz.java

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,81 +15,114 @@ public class Clazz extends JavaElement {
1515
String generateCode() {
1616
boolean object = "java.lang.Object".equals(extend);
1717
if(object) {
18-
return generateObject();
18+
return generateFirstObject();
1919
}
2020
String type = genTypes.next();
2121
String s = "package " + packageName + ";\n\n" +
2222
generateImports() +
23+
generateComments() +
2324
"@SuppressWarnings(\"all\")\n" +
2425
"public abstract class " + name + "<"+type+"> extends " + extend + "<"+type+"> implements " + implement + "<"+type+"> {\n\n" +
25-
"\t public "+type+" element;\n\n" +
2626
generateFields() +
27-
"\t public static " + name + " instance;\n\n" +
28-
"\t public static " + name + " getInstance() {\n" +
29-
"\t \t return instance;\n" +
30-
"\t }\n\n" +
31-
"\t public static <T> T create(java.util.List<T> input) {\n" +
32-
"\t \t return " + extend + ".create(input);\n" +
33-
"\t }\n\n" +
34-
"\t public String getName() {\n" +
35-
"\t \t return " + extend + ".getInstance().getName();\n" +
36-
"\t }\n\n" +
37-
"\t public void setName(String string) {\n" +
38-
"\t \t " + extend + ".getInstance().setName(getName());\n" +
39-
"\t \t return;\n" +
40-
"\t }\n\n" +
41-
"\t public "+type+" get() {\n" +
42-
"\t \t return ("+type+")" + extend + ".getInstance().get();\n" +
43-
"\t }\n\n" +
44-
"\t public void set(Object element) {\n" +
45-
"\t \t this.element = ("+type+")element;\n" +
46-
"\t \t " + extend + ".getInstance().set(this.element);\n" +
47-
"\t }\n\n" +
48-
"\t public "+type+" call() throws Exception {\n" +
49-
"\t \t return ("+type+")" + extend + ".getInstance().call();\n" +
50-
"\t }\n" +
27+
generateClassFields(type) +
28+
generateMethods(type) +
5129
"}\n";
5230
return s;
5331
}
5432

55-
String generateObject() {
33+
String generateClassFields(String type) {
34+
if(methodCounts == 0) {
35+
return "";
36+
}
37+
String result =
38+
"\t public "+type+" element;\n\n" +
39+
"\t public static " + name + " instance;\n\n";
40+
return result;
41+
}
42+
43+
String generateMethods(String type) {
44+
if(methodCounts == 0) {
45+
return "";
46+
}
47+
StringBuilder sb = new StringBuilder();
48+
for (int i = 0; i < methodCounts; i++) {
49+
String suffix = i == 0? "" : "" + i;
50+
String result = "\t public static " + name + " getInstance" + suffix + "() {\n" +
51+
"\t \t return instance;\n" +
52+
"\t }\n\n" +
53+
"\t public static <T> T create" + suffix + "(java.util.List<T> input) {\n" +
54+
"\t \t return " + extend + ".create(input);\n" +
55+
"\t }\n\n" +
56+
"\t public String getName" + suffix + "() {\n" +
57+
"\t \t return " + extend + ".getInstance" + suffix + "().getName" + suffix + "();\n" +
58+
"\t }\n\n" +
59+
"\t public void setName" + suffix + "(String string) {\n" +
60+
"\t \t " + extend + ".getInstance" + suffix + "().setName" + suffix + "(getName" + suffix + "());\n" +
61+
"\t \t return;\n" +
62+
"\t }\n\n" +
63+
"\t public "+type+" get" + suffix + "() {\n" +
64+
"\t \t return ("+type+")" + extend + ".getInstance" + suffix + "().get" + suffix + "();\n" +
65+
"\t }\n\n" +
66+
"\t public void set" + suffix + "(Object element) {\n" +
67+
"\t \t this.element = ("+type+")element;\n" +
68+
"\t \t " + extend + ".getInstance" + suffix + "().set" + suffix + "(this.element);\n" +
69+
"\t }\n\n" +
70+
"\t public "+type+" call" + suffix + "() throws Exception {\n" +
71+
"\t \t return ("+type+")" + extend + ".getInstance" + suffix + "().call" + suffix + "();\n" +
72+
"\t }\n";
73+
sb.append(result);
74+
}
75+
return sb.toString();
76+
}
77+
78+
String generateFirstObject() {
5679
String type = genTypes.next();
5780
String s = "package " + packageName + ";\n\n" +
5881
generateImports() +
82+
generateComments() +
5983
"@SuppressWarnings(\"all\")\n" +
6084
"public abstract class " + name + "<"+type+"> implements " + implement + "<"+type+"> {\n\n" +
61-
"public "+type+" element;\n" +
6285
generateFields() +
86+
"public "+type+" element;\n" +
6387
"public static " + name + " instance;\n\n" +
64-
"public static " + name + " getInstance() {\n" +
65-
"\t return instance;\n" +
66-
"}\n" +
6788

68-
"public static <T> T create(java.util.List<T> input) {\n" +
69-
"\t return null;\n" +
70-
"}\n" +
71-
72-
"public String getName() {\n" +
73-
"\t return element.toString();\n" +
74-
"}\n" +
75-
76-
"public void setName(String string) {\n" +
77-
"\t return;\n" +
78-
"}\n" +
79-
80-
"public "+type+" get() {\n" +
81-
"\t return element;\n" +
82-
"}\n" +
83-
84-
"public void set(Object element) {\n" +
85-
"\t this.element = ("+type+")element;\n" +
86-
"}\n" +
87-
88-
"public "+type+" call() throws Exception {\n" +
89-
"\t return ("+type+")getInstance().call();\n" +
90-
"}\n" +
89+
generateObjectMethods(type) +
9190
"}\n";
9291
return s;
9392
}
9493

94+
String generateObjectMethods(String type) {
95+
if(methodCounts == 0) {
96+
return "";
97+
}
98+
StringBuilder sb = new StringBuilder();
99+
for (int i = 0; i < methodCounts; i++) {
100+
String suffix = i == 0? "" : "" + i;
101+
String result =
102+
"\t public static " + name + " getInstance" + suffix + "() {\n" +
103+
"\t \t return instance;\n" +
104+
"\t }\n\n" +
105+
"\t public static <T> T create" + suffix + "(java.util.List<T> input) {\n" +
106+
"\t \t return null;\n" +
107+
"\t }\n\n" +
108+
"\t public String getName" + suffix + "() {\n" +
109+
"\t \t return element.toString();\n" +
110+
"\t }\n\n" +
111+
"\t public void setName" + suffix + "(String string) {\n" +
112+
"\t \t return;\n" +
113+
"\t }\n\n" +
114+
"\t public "+type+" get" + suffix + "() {\n" +
115+
"\t \t return element;\n" +
116+
"\t }\n\n" +
117+
"\t public void set" + suffix + "(Object element) {\n" +
118+
"\t \t this.element = ("+type+")element;\n" +
119+
"\t }\n\n" +
120+
"\t public "+type+" call" + suffix + "() throws Exception {\n" +
121+
"\t \t return ("+type+")getInstance" + suffix + "().call" + suffix + "();\n" +
122+
"\t }\n";
123+
sb.append(result);
124+
}
125+
return sb.toString();
126+
}
127+
95128
}

JavaProjectGenerator/src/de/loskutov/jpg/Interface.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,28 @@ String generateCode() {
1414
String type = genTypes.next();
1515
String s = "package " + packageName + ";\n\n" +
1616
generateImports() +
17+
generateComments() +
1718
"@SuppressWarnings(\"all\")\n" +
1819
"public interface " + name + "<"+type+"> extends " + extend + "<"+type+"> {\n\n" +
19-
2020
generateFields() +
21-
22-
"\t String getName();\n\n" +
23-
24-
"\t void setName(String s);\n\n" +
25-
26-
"\t "+type+" get();\n\n" +
27-
28-
"\t void set("+type+" e);\n\n" +
21+
generateMethods(type) +
2922
"}\n";
3023
return s;
3124
}
3225

26+
String generateMethods(String type) {
27+
if(methodCounts == 0) {
28+
return "";
29+
}
30+
StringBuilder sb = new StringBuilder();
31+
for (int i = 0; i < methodCounts; i++) {
32+
String suffix = i == 0? "" : "" + i;
33+
sb.append("\t String getName" + suffix + "();\n\n");
34+
sb.append("\t void setName" + suffix + "(String s);\n\n");
35+
sb.append("\t " + type + " get" + suffix + "();\n\n");
36+
sb.append("\t void set" + suffix + "(" + type + " e);\n\n");
37+
}
38+
return sb.toString();
39+
}
40+
3341
}

JavaProjectGenerator/src/de/loskutov/jpg/JavaElement.java

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.BufferedWriter;
44
import java.io.File;
55
import java.io.IOException;
6+
import java.io.LineNumberReader;
7+
import java.io.StringReader;
68
import java.nio.charset.StandardCharsets;
79
import java.nio.file.Files;
810
import java.nio.file.Path;
@@ -15,6 +17,9 @@ public abstract class JavaElement {
1517

1618
static int fieldsCount = 3;
1719
static int importsCount = 3;
20+
static int commentsCount = 3;
21+
static int seeCount = 3;
22+
static int methodCounts = 1;
1823

1924
static final List<String> IMPORTS = Arrays.asList(
2025
"java.awt.datatransfer.*",
@@ -50,14 +55,44 @@ public abstract class JavaElement {
5055
"javax.rmi.ssl.SslRMIClientSocketFactory"
5156
);
5257

58+
static final List<String> LOREM = Arrays.asList(
59+
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut ",
60+
"labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. ",
61+
"Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. ",
62+
"Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum ",
63+
"dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent ",
64+
"luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, ",
65+
"consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. ",
66+
"Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ",
67+
"ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, ",
68+
"vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit ",
69+
"praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. ",
70+
"Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat ",
71+
"facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh ",
72+
"euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis ",
73+
"nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. ",
74+
"Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum ",
75+
"dolore eu feugiat nulla facilisis. ",
76+
"At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata ",
77+
"sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, ",
78+
"sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. ",
79+
"At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata ",
80+
"sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, ",
81+
"At accusam aliquyam diam diam dolore dolores duo eirmod eos erat, et nonumy sed tempor et et invidunt ",
82+
"justo labore Stet clita ea et gubergren, kasd magna no rebum. sanctus sea sed takimata ut vero voluptua. "
83+
);
84+
5385
static final List<String> LETTERS = IntStream.rangeClosed('A', 'Z').mapToObj(x -> String.valueOf((char)x))
5486
.collect(Collectors.toList());
5587

88+
89+
5690
String name;
5791
String packageName;
5892
static final Ring<String> imports = new Ring<>(IMPORTS);
5993
static final Ring<String> fields = new Ring<>(FIELDS);
6094
static final Ring<String> genTypes = new Ring<>(LETTERS);
95+
final Ring<String> loremIpsum = new Ring<>(LOREM);
6196

6297
JavaElement(String name, String packageName){
6398
this.name = name;
@@ -69,25 +104,48 @@ public abstract class JavaElement {
69104
String generateImports() {
70105
StringBuilder sb = new StringBuilder();
71106
for (int i = 0; i < importsCount; i++) {
72-
sb.append("import ").append(imports.next()).append(";\n\n");
107+
sb.append("import ").append(imports.next()).append(";\n");
108+
}
109+
sb.append("\n");
110+
return sb.toString();
111+
}
112+
113+
String generateComments() {
114+
StringBuilder sb = new StringBuilder();
115+
if(commentsCount > 0) {
116+
sb.append("/**\n");
117+
for (int i = 0; i < commentsCount; i++) {
118+
sb.append(" * ").append(loremIpsum.next()).append("\n");
119+
}
120+
sb.append(" *\n");
121+
for (int i = 0; i < seeCount; i++) {
122+
sb.append(" * @see ").append(fields.next()).append("\n");
123+
}
124+
sb.append(" */\n");
73125
}
74126
return sb.toString();
75127
}
76128

77129
String generateFields() {
78130
StringBuilder sb = new StringBuilder();
79131
for (int i = 0; i < fieldsCount; i++) {
80-
sb.append("\t ").append(fields.next()).append(" f").append(i).append(" = null;\n\n");
132+
sb.append("\t ").append(fields.next()).append(" f").append(i).append(" = null;\n");
81133
}
134+
sb.append("\n");
82135
return sb.toString();
83136
}
84137

85-
void persist(Path root) throws IOException {
138+
int persist(Path root) throws IOException {
139+
int lines = 0;
86140
try(BufferedWriter writer = createWriter(root)){
87141
String code = generateCode();
142+
LineNumberReader lineNumberReader = new LineNumberReader(new StringReader(code));
143+
lineNumberReader.skip(Long.MAX_VALUE);
144+
lines = lineNumberReader.getLineNumber();
88145
writer.append(code);
89146
writer.flush();
90147
}
148+
return lines;
91149
}
92150

93151
String fqn() {

0 commit comments

Comments
 (0)