Skip to content

Commit 4c3e2d9

Browse files
committed
Let weak join support lambda API
1 parent 9ab8bb9 commit 4c3e2d9

File tree

63 files changed

+6972
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+6972
-14
lines changed

project/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
group=org.babyfish.jimmer
2-
version=0.9.95
2+
version=0.9.96

project/jimmer-apt/src/main/java/org/babyfish/jimmer/apt/immutable/generator/TableGenerator.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ private TypeSpec generateTable() {
105105
addDisableJoin();
106106
addWeakJoin(false);
107107
addWeakJoin(true);
108+
addLambdaWeakJoin(false);
109+
addLambdaWeakJoin(true);
108110
addRemote();
109111
return typeBuilder.build();
110112
} finally {
@@ -307,6 +309,73 @@ private void addWeakJoin(boolean withJoinType) {
307309
typeBuilder.addMethod(builder.build());
308310
}
309311

312+
private void addLambdaWeakJoin(boolean withJoinType) {
313+
if (!isTableEx) {
314+
return;
315+
}
316+
MethodSpec.Builder builder = MethodSpec
317+
.methodBuilder("weakJoin")
318+
.addModifiers(Modifier.PUBLIC)
319+
.addTypeVariable(
320+
TypeVariableName.get(
321+
"TT",
322+
ParameterizedTypeName.get(
323+
Constants.TABLE_CLASS_NAME,
324+
WildcardTypeName.subtypeOf(TypeName.OBJECT)
325+
)
326+
)
327+
)
328+
.returns(TypeVariableName.get("TT"))
329+
.addParameter(
330+
ParameterSpec
331+
.builder(
332+
ParameterizedTypeName.get(
333+
Constants.CLASS_CLASS_NAME,
334+
TypeVariableName.get("TT")
335+
),
336+
"targetTableType"
337+
)
338+
.build()
339+
);
340+
if (withJoinType) {
341+
builder.addParameter(
342+
ParameterSpec
343+
.builder(Constants.JOIN_TYPE_CLASS_NAME, "joinType")
344+
.build()
345+
);
346+
builder.addAnnotation(suppressAllAnnotation());
347+
}
348+
builder.addParameter(
349+
ParameterSpec
350+
.builder(
351+
ParameterizedTypeName.get(
352+
Constants.WEAK_JOIN_CLASS_NAME,
353+
type.getTableClassName(),
354+
TypeVariableName.get("TT")
355+
),
356+
"weakJoinLambda"
357+
)
358+
.build()
359+
);
360+
if (withJoinType) {
361+
builder
362+
.addStatement("__beforeJoin()")
363+
.beginControlFlow("if (raw != null)")
364+
.addStatement(
365+
"return (TT)$T.wrap(raw.weakJoinImplementor(targetTableType, joinType, weakJoinLambda))",
366+
Constants.TABLE_PROXIES_CLASS_NAME
367+
)
368+
.endControlFlow()
369+
.addStatement(
370+
"return (TT)$T.fluent(joinOperation(targetTableType, joinType, weakJoinLambda))",
371+
Constants.TABLE_PROXIES_CLASS_NAME
372+
);
373+
} else {
374+
builder.addStatement("return weakJoin(targetTableType, JoinType.INNER, weakJoinLambda)");
375+
}
376+
typeBuilder.addMethod(builder.build());
377+
}
378+
310379
private void addRemote() {
311380
if (isTableEx) {
312381
return;

project/jimmer-core/src/main/java/org/babyfish/jimmer/ImmutableObjects.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,69 @@ public static boolean isLogicalDeleted(Object o) {
479479
return info.isDeleted(spi.__get(propId));
480480
}
481481

482+
public static <T> Set<T> emptyStringToNull(Set<T> s) {
483+
Set<T> newSet = new LinkedHashSet<>((s.size() * 4 + 2) / 3);
484+
for (T e : s) {
485+
newSet.add(emptyStringToNullImpl(e));
486+
}
487+
return newSet;
488+
}
489+
490+
public static <T> List<T> emptyStringToNull(Collection<T> c) {
491+
List<T> newList = new ArrayList<>(c.size());
492+
for (T e : c) {
493+
newList.add(emptyStringToNullImpl(e));
494+
}
495+
return newList;
496+
}
497+
498+
@SuppressWarnings("unchecked")
499+
public static <T> T emptyStringToNull(T o) {
500+
if (o instanceof Set<?>) {
501+
return (T) emptyStringToNull((Set<?>) o);
502+
}
503+
if (o instanceof Collection<?>) {
504+
return (T) emptyStringToNull((List<?>) o);
505+
}
506+
return emptyStringToNullImpl(o);
507+
}
508+
509+
@SuppressWarnings("unchecked")
510+
private static <T> T emptyStringToNullImpl(T o) {
511+
if (!(o instanceof ImmutableSpi)) {
512+
return o;
513+
}
514+
ImmutableSpi spi = (ImmutableSpi) o;
515+
return (T) Internal.produce(spi.__type(), o, draft -> {
516+
DraftSpi draftSpi = (DraftSpi) draft;
517+
for (ImmutableProp prop : spi.__type().getProps().values()) {
518+
if (!prop.isNullable()) {
519+
continue;
520+
}
521+
PropId propId = prop.getId();
522+
if (!spi.__isLoaded(propId)) {
523+
continue;
524+
}
525+
if (prop.getReturnClass() == String.class) {
526+
Object value = spi.__get(propId);
527+
if ("".equals(value)) {
528+
draftSpi.__set(propId, null);
529+
}
530+
} else if (prop.isAssociation(TargetLevel.OBJECT)) {
531+
Object value = spi.__get(propId);
532+
if (value == null) {
533+
continue;
534+
}
535+
if (value instanceof Collection<?>) {
536+
draftSpi.__set(propId, emptyStringToNull((Collection<?>)value));
537+
} else {
538+
draftSpi.__set(propId, emptyStringToNullImpl(value));
539+
}
540+
}
541+
}
542+
});
543+
}
544+
482545
static {
483546
ObjectMapper mapper = new ObjectMapper();
484547
mapper.registerModule(new JavaTimeModule());

project/jimmer-core/src/main/java/org/babyfish/jimmer/impl/asm/Type.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,25 @@ public int getSize() {
702702
}
703703
}
704704

705+
public static int getArgumentCount(final String methodDescriptor) {
706+
int argumentCount = 0;
707+
// Skip the first character, which is always a '('.
708+
int currentOffset = 1;
709+
// Parse the argument types, one at a each loop iteration.
710+
while (methodDescriptor.charAt(currentOffset) != ')') {
711+
while (methodDescriptor.charAt(currentOffset) == '[') {
712+
currentOffset++;
713+
}
714+
if (methodDescriptor.charAt(currentOffset++) == 'L') {
715+
// Skip the argument descriptor content.
716+
int semiColumnOffset = methodDescriptor.indexOf(';', currentOffset);
717+
currentOffset = Math.max(currentOffset, semiColumnOffset + 1);
718+
}
719+
++argumentCount;
720+
}
721+
return argumentCount;
722+
}
723+
705724
/**
706725
* Returns the size of the arguments and of the return value of methods of this type. This method
707726
* should only be used for method types.

0 commit comments

Comments
 (0)