blob: 63d7daed6d01d8e1f3b6b4c1099ba3c7e4a1d01c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package ch.fhnw.thga.gradleplugins;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class FregeDTO {
public final String version;
public final String release;
public final String compilerDownloadDir;
public final String mainSourceDir;
public final String outputDir;
public FregeDTO(String version, String release, String compilerDownloadDir, String mainSourceDir,
String outputDir) {
this.version = version;
this.release = release;
this.compilerDownloadDir = compilerDownloadDir;
this.mainSourceDir = mainSourceDir;
this.outputDir = outputDir;
}
public String getVersion() {
return version;
}
public String getRelease() {
return release;
}
public String getCompilerDownloadDir() {
return compilerDownloadDir;
}
public String getMainSourceDir() {
return mainSourceDir;
}
public String getOutputDir() {
return outputDir;
}
private String getFieldValue(Field field) {
try {
return field.get(this).toString();
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
private Field getField(String fieldName) {
try {
return FregeDTO.class.getField(fieldName);
} catch (NoSuchFieldException e) {
throw new RuntimeException(
String.format("Field %s not found in class %s", e.getMessage(), FregeDTO.class.getName()),
e.getCause());
}
}
private boolean isEmpty(Field field) {
return getFieldValue(field).isEmpty();
}
private String toKeyValuePairs(Field field) {
return String.format("%s = %s", field.getName(), getFieldValue(field));
}
private boolean isGetterProperty(Method method) {
return method.getName().startsWith("get") && method.getReturnType().getName().contains("Property");
}
private String stripGetPrefixAndDecapitalize(String s) {
return Character.toLowerCase(s.charAt(3)) + s.substring(4);
}
public String toBuildFile() {
Stream<Method> methods = Arrays.stream(FregeExtension.class.getMethods());
Stream<Field> fields = methods.filter(m -> isGetterProperty(m))
.map(m -> stripGetPrefixAndDecapitalize(m.getName())).map(name -> getField(name));
return fields.filter(f -> !isEmpty(f)).map(f -> toKeyValuePairs(f)).collect(Collectors.joining("\n "));
}
}
|