aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/ch/fhnw/thga/gradleplugins/FregeDTO.java
blob: e092eac94f97aac005cf5546082d24e224c506f3 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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 final String mainModule;
    public final String compilerFlags;
    public final String replSource;

    public FregeDTO(
        String version,
        String release,
        String compilerDownloadDir,
        String mainSourceDir,
        String outputDir,
        String mainModule,
        String compilerFlags,
        String replSource) {
        this.version = version;
        this.release = release;
        this.compilerDownloadDir = compilerDownloadDir;
        this.mainSourceDir = mainSourceDir;
        this.outputDir = outputDir;
        this.mainModule = mainModule;
        this.compilerFlags = compilerFlags;
        this.replSource = replSource;
    }

    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;
    }

    public String getMainModule() {
        return mainModule;
    }

    public String getCompilerFlags() {
        return compilerFlags;
    }

    public String getReplSource() {
        return replSource;
    }

    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<Field> fields =
            Arrays.stream(FregeExtension.class.getMethods())
            .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  "));
    }
}