1 | package com.github.dakusui.osynth.core; | |
2 | ||
3 | import com.github.dakusui.osynth.annotations.BuiltInHandlerFactory; | |
4 | import com.github.dakusui.osynth.annotations.ReservedByOSynth; | |
5 | import com.github.dakusui.osynth.core.utils.AssertionUtils; | |
6 | ||
7 | import java.lang.annotation.Annotation; | |
8 | import java.lang.reflect.Method; | |
9 | import java.util.*; | |
10 | ||
11 | import static com.github.dakusui.osynth.core.AbstractObjectSynthesizer.DEFAULT_FALLBACK_OBJECT; | |
12 | import static com.github.dakusui.osynth.core.MethodHandlerDecorator.chainMethodHandlerDecorators; | |
13 | import static com.github.dakusui.osynth.core.SynthesizedObject.InternalUtils.builtIndMethodSignatures; | |
14 | import static com.github.dakusui.osynth.core.SynthesizedObject.InternalUtils.reservedMethodSignatures; | |
15 | import static com.github.dakusui.osynth.core.utils.MessageUtils.messageForAttemptToCastToUnavailableInterface; | |
16 | import static com.github.dakusui.pcond.forms.Predicates.*; | |
17 | import static com.github.dakusui.valid8j.Requires.require; | |
18 | import static java.lang.String.format; | |
19 | import static java.util.Collections.unmodifiableList; | |
20 | import static java.util.stream.Collectors.toList; | |
21 | import static java.util.stream.Collectors.toSet; | |
22 | ||
23 | public interface SynthesizedObject { | |
24 | Set<Method> RESERVED_METHODS = reservedMethodSignatures(); | |
25 | Set<Method> BUILT_IN_METHODS = builtIndMethodSignatures(); | |
26 | ||
27 | @BuiltInHandlerFactory(BuiltInHandlerFactory.ForDescriptor.class) | |
28 | @ReservedByOSynth | |
29 | Descriptor descriptor(); | |
30 | ||
31 | @ReservedByOSynth | |
32 | default <T> T castTo(Class<T> classInUse) { | |
33 | require(classInUse, and( | |
34 | isNotNull(), | |
35 | or(AssertionUtils.classIsInterface(), isEqualTo(Object.class)))); | |
36 |
1
1. castTo : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject::castTo → KILLED |
return descriptor().interfaces().stream() |
37 | .filter(classInUse::isAssignableFrom) | |
38 | .findFirst() | |
39 |
1
1. lambda$castTo$0 : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject::lambda$castTo$0 → KILLED |
.map(each -> classInUse.cast(this)) |
40 |
1
1. lambda$castTo$1 : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject::lambda$castTo$1 → KILLED |
.orElseThrow(() -> new ClassCastException(messageForAttemptToCastToUnavailableInterface(classInUse, descriptor().interfaces()))); |
41 | } | |
42 | ||
43 | @BuiltInHandlerFactory(BuiltInHandlerFactory.ForEquals.class) | |
44 | @Override | |
45 | boolean equals(Object object); | |
46 | ||
47 | @BuiltInHandlerFactory(BuiltInHandlerFactory.ForHashCode.class) | |
48 | @Override | |
49 | int hashCode(); | |
50 | ||
51 | @BuiltInHandlerFactory(BuiltInHandlerFactory.ForToString.class) | |
52 | @Override | |
53 | String toString(); | |
54 | ||
55 | enum InternalUtils { | |
56 | ; | |
57 | ||
58 | static Set<Method> reservedMethodSignatures() { | |
59 |
1
1. reservedMethodSignatures : replaced return value with Collections.emptySet for com/github/dakusui/osynth/core/SynthesizedObject$InternalUtils::reservedMethodSignatures → SURVIVED |
return methodsAnnotatedBy(ReservedByOSynth.class); |
60 | } | |
61 | ||
62 | static Set<Method> builtIndMethodSignatures() { | |
63 |
1
1. builtIndMethodSignatures : replaced return value with Collections.emptySet for com/github/dakusui/osynth/core/SynthesizedObject$InternalUtils::builtIndMethodSignatures → SURVIVED |
return methodsAnnotatedBy(BuiltInHandlerFactory.class); |
64 | } | |
65 | ||
66 | private static Set<Method> methodsAnnotatedBy(Class<? extends Annotation> annotationClass) { | |
67 |
1
1. methodsAnnotatedBy : replaced return value with Collections.emptySet for com/github/dakusui/osynth/core/SynthesizedObject$InternalUtils::methodsAnnotatedBy → SURVIVED |
return Arrays.stream(SynthesizedObject.class.getMethods()) |
68 |
2
1. lambda$methodsAnnotatedBy$0 : replaced boolean return with false for com/github/dakusui/osynth/core/SynthesizedObject$InternalUtils::lambda$methodsAnnotatedBy$0 → SURVIVED 2. lambda$methodsAnnotatedBy$0 : replaced boolean return with true for com/github/dakusui/osynth/core/SynthesizedObject$InternalUtils::lambda$methodsAnnotatedBy$0 → SURVIVED |
.filter(each -> each.isAnnotationPresent(annotationClass)) |
69 | .collect(toSet()); | |
70 | } | |
71 | } | |
72 | ||
73 | /** | |
74 | * A class to describe attributes of a synthesized object. | |
75 | */ | |
76 | final class Descriptor { | |
77 | final List<MethodHandlerEntry> methodHandlers; | |
78 | final List<Class<?>> interfaces; | |
79 | final MethodHandlerDecorator methodHandlerDecorator; | |
80 | final Object fallbackObject; | |
81 | ||
82 | public Descriptor( | |
83 | List<Class<?>> interfaces, | |
84 | List<MethodHandlerEntry> methodHandlers, | |
85 | MethodHandlerDecorator methodHandlerDecorator, | |
86 | Object fallbackObject) { | |
87 | // Not using pcond library to avoid unintentional `toString` call back on failure. | |
88 | this.methodHandlers = new LinkedList<>(Objects.requireNonNull(unmodifiableList(methodHandlers))); | |
89 | this.interfaces = new LinkedList<>(Objects.requireNonNull(interfaces)); | |
90 | this.fallbackObject = Objects.requireNonNull(fallbackObject); | |
91 | this.methodHandlerDecorator = Objects.requireNonNull(methodHandlerDecorator); | |
92 | } | |
93 | ||
94 | public static List<MethodHandlerEntry> nonBuiltInMethodHandlerEntriesOf(Descriptor b) { | |
95 |
3
1. lambda$nonBuiltInMethodHandlerEntriesOf$0 : negated conditional → KILLED 2. lambda$nonBuiltInMethodHandlerEntriesOf$0 : replaced boolean return with true for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::lambda$nonBuiltInMethodHandlerEntriesOf$0 → KILLED 3. nonBuiltInMethodHandlerEntriesOf : replaced return value with Collections.emptyList for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::nonBuiltInMethodHandlerEntriesOf → KILLED |
return b.methodHandlerEntries().stream().filter(methodHandlerEntry -> !methodHandlerEntry.isBuiltIn()).collect(toList()); |
96 | } | |
97 | ||
98 | /** | |
99 | * Returns a new descriptor merging two descriptors. | |
100 | * The merge happens in a manner, where "a overrides b". | |
101 | * | |
102 | * @param a A descriptor to merge. | |
103 | * @param b Another descriptor to merge. | |
104 | * @return A merged new descriptor object. | |
105 | */ | |
106 | public static Descriptor merge(Descriptor a, Descriptor b) { | |
107 | Builder builder = a.toBuilder(); | |
108 |
1
1. merge : negated conditional → KILLED |
if (a.fallbackObject() == DEFAULT_FALLBACK_OBJECT) |
109 | builder.fallbackObject(b.fallbackObject()); | |
110 |
1
1. merge : removed call to java/util/List::forEach → SURVIVED |
nonBuiltInMethodHandlerEntriesOf(b).forEach(builder::addMethodHandler); |
111 |
1
1. merge : removed call to java/util/List::forEach → KILLED |
b.interfaces().forEach(builder::addInterface); |
112 | builder.methodHandlerDecorator(chainMethodHandlerDecorators(a.methodHandlerDecorator(), b.methodHandlerDecorator())); | |
113 |
1
1. merge : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::merge → KILLED |
return builder.build(); |
114 | } | |
115 | ||
116 | public List<Class<?>> interfaces() { | |
117 |
1
1. interfaces : replaced return value with Collections.emptyList for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::interfaces → KILLED |
return unmodifiableList(this.interfaces); |
118 | } | |
119 | ||
120 | public MethodHandlerDecorator methodHandlerDecorator() { | |
121 |
1
1. methodHandlerDecorator : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::methodHandlerDecorator → KILLED |
return this.methodHandlerDecorator; |
122 | } | |
123 | ||
124 | public Object fallbackObject() { | |
125 |
1
1. fallbackObject : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::fallbackObject → KILLED |
return this.fallbackObject; |
126 | } | |
127 | ||
128 | public List<MethodHandlerEntry> methodHandlerEntries() { | |
129 |
1
1. methodHandlerEntries : replaced return value with Collections.emptyList for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::methodHandlerEntries → KILLED |
return this.methodHandlers; |
130 | } | |
131 | ||
132 | @Override | |
133 | public int hashCode() { | |
134 |
1
1. hashCode : replaced int return with 0 for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::hashCode → SURVIVED |
return this.fallbackObject.hashCode(); |
135 | } | |
136 | ||
137 | @Override | |
138 | public boolean equals(Object anotherObject) { | |
139 |
1
1. equals : negated conditional → KILLED |
if (this == anotherObject) |
140 |
1
1. equals : replaced boolean return with false for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::equals → KILLED |
return true; |
141 |
1
1. equals : negated conditional → KILLED |
if (!(anotherObject instanceof Descriptor)) { |
142 |
1
1. equals : replaced boolean return with true for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::equals → KILLED |
return false; |
143 | } | |
144 | Descriptor another = (Descriptor) anotherObject; | |
145 | Set<MethodHandlerEntry> methodHandlerSet = new HashSet<>(nonBuiltInMethodHandlerEntriesOf(this)); | |
146 | Set<MethodHandlerEntry> methodHandlerSetFromAnother = new HashSet<>(nonBuiltInMethodHandlerEntriesOf(another)); | |
147 |
2
1. equals : negated conditional → KILLED 2. equals : replaced boolean return with true for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::equals → KILLED |
return Objects.equals(fallbackObject, another.fallbackObject) && |
148 |
1
1. equals : negated conditional → KILLED |
Objects.equals(methodHandlerDecorator, another.methodHandlerDecorator) && |
149 |
1
1. equals : negated conditional → KILLED |
Objects.equals(interfaces, another.interfaces) && |
150 |
1
1. equals : negated conditional → KILLED |
Objects.equals(methodHandlerSet, methodHandlerSetFromAnother); |
151 | } | |
152 | ||
153 | @Override | |
154 | public String toString() { | |
155 |
1
1. toString : replaced return value with "" for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::toString → KILLED |
return format("{methodHandlers=%s,interfaces=%s,fallback:%s}", |
156 | nonBuiltInMethodHandlerEntriesOf(this), | |
157 | this.interfaces(), | |
158 | this.fallbackObject()); | |
159 | } | |
160 | ||
161 | public Descriptor.Builder toBuilder() { | |
162 |
1
1. toBuilder : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor::toBuilder → KILLED |
return new Descriptor.Builder() { |
163 | { | |
164 |
1
1. <init> : removed call to java/util/List::forEach → SURVIVED |
Descriptor.this.interfaces().forEach(this::addInterface); |
165 | this.fallbackObject = Descriptor.this.fallbackObject; | |
166 | this.methodHandlerDecorator = Descriptor.this.methodHandlerDecorator(); | |
167 | this.methodHandlers.addAll(nonBuiltInMethodHandlerEntriesOf(this.build())); | |
168 | } | |
169 | }.fallbackObject(this.fallbackObject()); | |
170 | } | |
171 | ||
172 | public static class Builder { | |
173 | final List<Class<?>> interfaces; | |
174 | final List<MethodHandlerEntry> methodHandlers; | |
175 | MethodHandlerDecorator methodHandlerDecorator; | |
176 | Object fallbackObject; | |
177 | ||
178 | public Builder() { | |
179 | interfaces = new LinkedList<>(); | |
180 | methodHandlers = new LinkedList<>(); | |
181 | } | |
182 | ||
183 | public Builder(Descriptor descriptor) { | |
184 | this(); | |
185 | this.interfaces.addAll(descriptor.interfaces()); | |
186 | this.methodHandlers.addAll(descriptor.methodHandlerEntries()); | |
187 | this.methodHandlerDecorator = descriptor.methodHandlerDecorator(); | |
188 | this.fallbackObject = descriptor.fallbackObject(); | |
189 | } | |
190 | ||
191 | public Builder fallbackObject(Object fallbackObject) { | |
192 | this.fallbackObject = fallbackObject; | |
193 |
1
1. fallbackObject : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor$Builder::fallbackObject → KILLED |
return this; |
194 | } | |
195 | ||
196 | public Builder addInterface(Class<?> interfaceClass) { | |
197 | this.interfaces.add(interfaceClass); | |
198 |
1
1. addInterface : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor$Builder::addInterface → SURVIVED |
return this; |
199 | } | |
200 | ||
201 | public Builder methodHandlerDecorator(MethodHandlerDecorator methodHandlerDecorator) { | |
202 | this.methodHandlerDecorator = methodHandlerDecorator; | |
203 |
1
1. methodHandlerDecorator : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor$Builder::methodHandlerDecorator → KILLED |
return this; |
204 | } | |
205 | ||
206 | public void addMethodHandler(MethodHandlerEntry methodHandlerEntry) { | |
207 | this.methodHandlers.add(methodHandlerEntry); | |
208 | } | |
209 | ||
210 | ||
211 | public List<Class<?>> interfaces() { | |
212 |
1
1. interfaces : replaced return value with Collections.emptyList for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor$Builder::interfaces → KILLED |
return unmodifiableList(this.interfaces); |
213 | } | |
214 | ||
215 | public MethodHandlerDecorator methodHandlerDecorator() { | |
216 |
1
1. methodHandlerDecorator : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor$Builder::methodHandlerDecorator → KILLED |
return this.methodHandlerDecorator; |
217 | } | |
218 | ||
219 | public Descriptor build() { | |
220 |
1
1. build : replaced return value with null for com/github/dakusui/osynth/core/SynthesizedObject$Descriptor$Builder::build → KILLED |
return new Descriptor(this.interfaces, this.methodHandlers, this.methodHandlerDecorator, this.fallbackObject); |
221 | } | |
222 | } | |
223 | } | |
224 | } | |
Mutations | ||
36 |
1.1 |
|
39 |
1.1 |
|
40 |
1.1 |
|
59 |
1.1 |
|
63 |
1.1 |
|
67 |
1.1 |
|
68 |
1.1 2.2 |
|
95 |
1.1 2.2 3.3 |
|
108 |
1.1 |
|
110 |
1.1 |
|
111 |
1.1 |
|
113 |
1.1 |
|
117 |
1.1 |
|
121 |
1.1 |
|
125 |
1.1 |
|
129 |
1.1 |
|
134 |
1.1 |
|
139 |
1.1 |
|
140 |
1.1 |
|
141 |
1.1 |
|
142 |
1.1 |
|
147 |
1.1 2.2 |
|
148 |
1.1 |
|
149 |
1.1 |
|
150 |
1.1 |
|
155 |
1.1 |
|
162 |
1.1 |
|
164 |
1.1 |
|
193 |
1.1 |
|
198 |
1.1 |
|
203 |
1.1 |
|
212 |
1.1 |
|
216 |
1.1 |
|
220 |
1.1 |