|
5 | 5 | */ |
6 | 6 | package io.jooby; |
7 | 7 |
|
8 | | -import java.io.Serializable; |
9 | | -import java.lang.invoke.SerializedLambda; |
10 | 8 | import java.lang.reflect.Field; |
11 | 9 | import java.lang.reflect.Method; |
12 | 10 | import java.lang.reflect.ParameterizedType; |
13 | 11 | import java.lang.reflect.Type; |
14 | 12 | import java.util.*; |
15 | 13 | import java.util.concurrent.ConcurrentHashMap; |
16 | | -import java.util.function.Consumer; |
17 | 14 |
|
18 | 15 | import io.jooby.value.ValueFactory; |
19 | 16 |
|
|
90 | 87 | */ |
91 | 88 | public class Projection<T> { |
92 | 89 |
|
93 | | - /** |
94 | | - * Functional interface for capturing method references. |
95 | | - * |
96 | | - * @param <T> The type containing the property. |
97 | | - * @param <R> The return type of the property. |
98 | | - */ |
99 | | - @FunctionalInterface |
100 | | - public interface Property<T, R> extends Serializable { |
101 | | - /** |
102 | | - * Captures the property method reference. |
103 | | - * |
104 | | - * @param instance The instance to apply the method to. |
105 | | - * @return The property value. |
106 | | - */ |
107 | | - R apply(T instance); |
108 | | - } |
109 | | - |
110 | 90 | private static final Map<Class<?>, String> PROP_CACHE = new ConcurrentHashMap<>(); |
111 | 91 |
|
112 | 92 | private final Class<T> type; |
@@ -152,44 +132,6 @@ public Projection<T> include(String... paths) { |
152 | 132 | return this; |
153 | 133 | } |
154 | 134 |
|
155 | | - /** |
156 | | - * Includes fields via type-safe method references. |
157 | | - * |
158 | | - * @param props Method references. |
159 | | - * @return This projection instance. |
160 | | - */ |
161 | | - @SafeVarargs |
162 | | - public final Projection<T> include(Property<T, ?>... props) { |
163 | | - for (Property<T, ?> prop : props) { |
164 | | - String name = getFieldName(prop); |
165 | | - children.computeIfAbsent( |
166 | | - name, k -> new Projection<>(resolveFieldType(this.type, name), false, validate)); |
167 | | - } |
168 | | - rebuild(); |
169 | | - return this; |
170 | | - } |
171 | | - |
172 | | - /** |
173 | | - * Includes a nested field and configures its sub-projection using a lambda. This provides full |
174 | | - * type-safety for nested objects. |
175 | | - * |
176 | | - * @param <R> The type of the nested field. |
177 | | - * @param prop The method reference to the nested field. |
178 | | - * @param childSpec A consumer that configures the nested projection. |
179 | | - * @return This projection instance. |
180 | | - */ |
181 | | - public <R> Projection<T> include(Property<T, R> prop, Consumer<Projection<R>> childSpec) { |
182 | | - String name = getFieldName(prop); |
183 | | - Class<R> childType = (Class<R>) resolveFieldType(this.type, name); |
184 | | - Projection<R> child = |
185 | | - (Projection<R>) |
186 | | - children.computeIfAbsent(name, k -> new Projection<>(childType, false, validate)); |
187 | | - childSpec.accept(child); |
188 | | - child.rebuild(); |
189 | | - rebuild(); |
190 | | - return this; |
191 | | - } |
192 | | - |
193 | 135 | public Map<String, Projection<?>> getChildren() { |
194 | 136 | return Collections.unmodifiableMap(children); |
195 | 137 | } |
@@ -552,23 +494,6 @@ private Class<?> resolveFieldType(Class<?> currentType, String fieldName) { |
552 | 494 | return rawType; |
553 | 495 | } |
554 | 496 |
|
555 | | - private static String getFieldName(Property<?, ?> property) { |
556 | | - return PROP_CACHE.computeIfAbsent( |
557 | | - property.getClass(), |
558 | | - clz -> { |
559 | | - try { |
560 | | - Method m = clz.getDeclaredMethod("writeReplace"); |
561 | | - m.setAccessible(true); |
562 | | - SerializedLambda l = (SerializedLambda) m.invoke(property); |
563 | | - String n = l.getImplMethodName(); |
564 | | - int s = n.startsWith("get") ? 3 : (n.startsWith("is") ? 2 : 0); |
565 | | - return Character.toLowerCase(n.charAt(s)) + n.substring(s + 1); |
566 | | - } catch (Exception x) { |
567 | | - throw new IllegalArgumentException("Could not resolve field from method reference.", x); |
568 | | - } |
569 | | - }); |
570 | | - } |
571 | | - |
572 | 497 | @Override |
573 | 498 | public boolean equals(Object o) { |
574 | 499 | if (this == o) return true; |
|
0 commit comments