Skip to content

Commit 4674b52

Browse files
committed
- remove method-ref from Projection, just keep string projection
- ref #3853
1 parent 016e37d commit 4674b52

File tree

4 files changed

+0
-111
lines changed

4 files changed

+0
-111
lines changed

jooby/src/main/java/io/jooby/Projected.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.jooby;
77

88
import java.util.*;
9-
import java.util.function.Consumer;
109

1110
/**
1211
* A wrapper for a value and its associated {@link Projection}.
@@ -56,17 +55,6 @@ public Projected<T> include(String... paths) {
5655
return this;
5756
}
5857

59-
@SafeVarargs
60-
public final Projected<T> include(Projection.Property<T, ?>... props) {
61-
projection.include(props);
62-
return this;
63-
}
64-
65-
public <R> Projected<T> include(Projection.Property<T, R> prop, Consumer<Projection<R>> child) {
66-
projection.include(prop, child);
67-
return this;
68-
}
69-
7058
@Override
7159
public String toString() {
7260
return projection.toString();

jooby/src/main/java/io/jooby/Projection.java

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
*/
66
package io.jooby;
77

8-
import java.io.Serializable;
9-
import java.lang.invoke.SerializedLambda;
108
import java.lang.reflect.Field;
119
import java.lang.reflect.Method;
1210
import java.lang.reflect.ParameterizedType;
1311
import java.lang.reflect.Type;
1412
import java.util.*;
1513
import java.util.concurrent.ConcurrentHashMap;
16-
import java.util.function.Consumer;
1714

1815
import io.jooby.value.ValueFactory;
1916

@@ -90,23 +87,6 @@
9087
*/
9188
public class Projection<T> {
9289

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-
11090
private static final Map<Class<?>, String> PROP_CACHE = new ConcurrentHashMap<>();
11191

11292
private final Class<T> type;
@@ -152,44 +132,6 @@ public Projection<T> include(String... paths) {
152132
return this;
153133
}
154134

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-
193135
public Map<String, Projection<?>> getChildren() {
194136
return Collections.unmodifiableMap(children);
195137
}
@@ -552,23 +494,6 @@ private Class<?> resolveFieldType(Class<?> currentType, String fieldName) {
552494
return rawType;
553495
}
554496

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-
572497
@Override
573498
public boolean equals(Object o) {
574499
if (this == o) return true;

jooby/src/test/java/io/jooby/ProjectionTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,6 @@ public void testMixedNotationRecursive() {
131131
assertEquals("(address(loc(lat,lon)),roles(name))", p.toView());
132132
}
133133

134-
@Test
135-
public void testTypeSafeInclude() {
136-
// Type-safe references also follow the defined order
137-
Projection<User> p = Projection.of(User.class).include(User::getName, User::getId);
138-
assertEquals("(name,id)", p.toView());
139-
assertEquals("User(name,id)", p.toString());
140-
}
141-
142134
@Test
143135
public void testCollectionGenericUnwrapping() {
144136
Projection<User> p = Projection.of(User.class).include("roles.name");

tests/src/test/java/io/jooby/i3853/Issue3853.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ public void shouldProjectData(ServerTestRunner runner, Extension extension) {
102102
ctx -> {
103103
return Projected.wrap(createUser()).include("(id, name, address(loc(lat)))");
104104
});
105-
app.get(
106-
"/stub/address-stub-ref",
107-
ctx -> {
108-
return Projected.wrap(createUser())
109-
.include(U3853::getId, U3853::getName)
110-
.include(U3853::getAddress, addr -> addr.include(A3853::getCity));
111-
});
112105
})
113106
.ready(
114107
http -> {
@@ -189,15 +182,6 @@ public void shouldProjectData(ServerTestRunner runner, Extension extension) {
189182
{"id":"cobb-001","name":"Dom Cobb","address":{"city":"Snow Fortress (Level 3)"}}
190183
""");
191184
});
192-
http.get(
193-
"/stub/address-stub-ref",
194-
rsp -> {
195-
assertThat(rsp.body().string())
196-
.isEqualToIgnoringNewLines(
197-
"""
198-
{"id":"cobb-001","name":"Dom Cobb","address":{"city":"Snow Fortress (Level 3)"}}
199-
""");
200-
});
201185
http.get(
202186
"/stub/address-loc-lat",
203187
rsp -> {

0 commit comments

Comments
 (0)