• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

leeonky / test-charm-java / 135

28 Feb 2025 03:29PM UTC coverage: 74.297% (-0.007%) from 74.304%
135

push

circleci

leeonky
Refactor

7874 of 10598 relevant lines covered (74.3%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.28
/DAL-java/src/main/java/com/github/leeonky/dal/runtime/MetaData.java
1
package com.github.leeonky.dal.runtime;
2

3
import com.github.leeonky.dal.ast.node.DALNode;
4
import com.github.leeonky.dal.runtime.RuntimeContextBuilder.DALRuntimeContext;
5
import com.github.leeonky.util.InvocationException;
6

7
import java.util.ArrayList;
8
import java.util.List;
9
import java.util.Objects;
10
import java.util.function.Supplier;
11

12
import static com.github.leeonky.dal.runtime.ExpressionException.illegalOp2RuntimeException;
13
import static java.lang.String.format;
14

15
public class MetaData extends RuntimeData {
16
    private Throwable error;
17
    private RuntimeException originalException;
18
    private DALNode inputNode;
19
    private final Object name;
20
    protected Data data;
21

22
    public MetaData(DALNode inputNode, Object symbolName, DALRuntimeContext runtimeContext) {
23
        super(null, runtimeContext);
1✔
24
        this.inputNode = inputNode;
1✔
25
        name = symbolName;
1✔
26
        setData(() -> inputNode.evaluateData(runtimeContext()));
1✔
27
    }
1✔
28

29
    private MetaData(DALRuntimeContext runtimeContext,
30
                     Data data, Throwable error, RuntimeException originalException, String name) {
31
        super(null, runtimeContext);
1✔
32
        this.name = name;
1✔
33
        this.error = error;
1✔
34
        this.originalException = originalException;
1✔
35
        this.data = data;
1✔
36
    }
1✔
37

38
    private void setData(Supplier<Data> supplier) {
39
        try {
40
            data = supplier.get();
1✔
41
        } catch (RuntimeException e) {
1✔
42
            if (!(e.getCause() instanceof InvocationException))
1✔
43
                throw e;
1✔
44
            originalException = e;
1✔
45
            error = e.getCause().getCause();
1✔
46
            data = runtimeContext.wrap(null);
1✔
47
        }
1✔
48
    }
1✔
49

50
    public Throwable catchError() {
51
        Throwable throwable = error;
1✔
52
        error = null;
1✔
53
        return throwable;
1✔
54
    }
55

56
    private final List<Class<?>> callTypes = new ArrayList<>();
1✔
57

58
    public Object callSuper() {
59
        return runtimeContext().fetchSuperMetaFunction(this).orElseThrow(() -> illegalOp2RuntimeException(format(
1✔
60
                        "Local meta property `%s` has no super in type %s", name, callTypes.get(callTypes.size() - 1).getName())))
1✔
61
                .apply(this);
1✔
62
    }
63

64
    public Object callSuper(Supplier<Object> supplier) {
65
        setData(() -> {
1✔
66
            Object newData = supplier.get();
1✔
67
            checkType(newData);
1✔
68
            return runtimeContext.wrap(newData);
1✔
69
        });
70
        return callSuper();
1✔
71
    }
72

73
    public Object callGlobal() {
74
        return runtimeContext().fetchGlobalMetaFunction(this).apply(this);
1✔
75
    }
76

77
    public Object callGlobal(Supplier<Object> supplier) {
78
        setData(() -> runtimeContext.wrap(supplier.get()));
1✔
79
        return callGlobal();
1✔
80
    }
81

82
    private MetaData newMeta(String name) {
83
        return new MetaData(runtimeContext, data, error, originalException, name);
1✔
84
    }
85

86
    public Object callMeta(String another) {
87
        MetaData metaData = newMeta(another);
1✔
88
        return runtimeContext().fetchGlobalMetaFunction(metaData).apply(metaData);
1✔
89
    }
90

91
    public Object callMeta(String another, Supplier<Object> supplier) {
92
        MetaData metaData = newMeta(another);
1✔
93
        metaData.setData(() -> runtimeContext.wrap(supplier.get()));
1✔
94
        return runtimeContext().fetchGlobalMetaFunction(metaData).apply(metaData);
1✔
95
    }
96

97
    private void checkType(Object data) {
98
        Class<?> expect = this.data.instance().getClass();
1✔
99
        Class<?> actual = Objects.requireNonNull(data).getClass();
1✔
100
        if (actual.isAnonymousClass())
1✔
101
            actual = actual.getSuperclass();
1✔
102
        if (!actual.equals(expect))
1✔
103
            throw illegalOp2RuntimeException(format("Do not allow change data type in callSuper, expect %s but %s",
1✔
104
                    expect.getName(), actual.getName()));
1✔
105
    }
1✔
106

107
    public void addCallType(Class<?> callType) {
108
        callTypes.add(callType);
1✔
109
    }
1✔
110

111
    public boolean calledBy(Class<?> type) {
112
        return callTypes.contains(type);
1✔
113
    }
114

115
    public boolean isInstance(Class<?> type) {
116
        return type.isInstance(data.instance());
1✔
117
    }
118

119
    public Object name() {
120
        return name;
1✔
121
    }
122

123
    @Override
124
    public Data data() {
125
        if (error != null)
1✔
126
            throw originalException;
1✔
127
        return data;
1✔
128
    }
129

130
    public DALNode inputNode() {
131
        return inputNode;
×
132
    }
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc