Repository navigation
fix: warn about uncopyable builder properties - #483
igel-devin-ai wants to merge 9 commits into
Conversation
| private static List<ExecutableElement> findGetterCandidates( | ||
| TypeElement dtoType, String fieldName, BuilderProcessingContext context) { | ||
| if (dtoType == null) { | ||
| return List.of(); | ||
| } | ||
| return ElementFilter.methodsIn(context.getAllMembers(dtoType)).stream() | ||
| .filter( | ||
| candidate -> { | ||
| String name = candidate.getSimpleName().toString(); | ||
| return Strings.CI.equalsAny(name, fieldName, "is" + fieldName, "get" + fieldName) | ||
| && candidate.getParameters().isEmpty(); | ||
| }) | ||
| .toList(); | ||
| } |
There was a problem hiding this comment.
But we already have a helper for reading getters?
There was a problem hiding this comment.
Yes. I removed the parallel getter-candidate analysis and reuse the existing getter resolution; the warning now derives copyability from the resolved getter metadata.
| private static List<ExecutableElement> findGetterCandidates( | ||
| TypeElement dtoType, String fieldName, BuilderProcessingContext context) { | ||
| if (dtoType == null) { | ||
| return List.of(); | ||
| } | ||
| return ElementFilter.methodsIn(context.getAllMembers(dtoType)).stream() | ||
| .filter( | ||
| candidate -> { | ||
| String name = candidate.getSimpleName().toString(); | ||
| return Strings.CI.equalsAny(name, fieldName, "is" + fieldName, "get" + fieldName) | ||
| && candidate.getParameters().isEmpty(); | ||
| }) | ||
| .toList(); | ||
| } | ||
|
|
||
| private static String uncopyablePropertyReason( | ||
| String fieldName, | ||
| TypeMirror fieldTypeMirror, | ||
| List<ExecutableElement> typeMatchingGetters, | ||
| List<ExecutableElement> getterCandidates) { | ||
| if (!typeMatchingGetters.isEmpty()) { | ||
| return fieldName + " (getter excluded via @IgnoreInBuilder)"; | ||
| } | ||
| if (getterCandidates.isEmpty()) { | ||
| return fieldName + " (no getter found)"; | ||
| } | ||
| return fieldName | ||
| + " (getter returns " | ||
| + getterCandidates.get(0).getReturnType() | ||
| + ", expected " | ||
| + fieldTypeMirror | ||
| + ")"; | ||
| } | ||
|
|
||
| private static void addGetterOnlyUncopyableProperties( | ||
| TypeElement annotatedType, | ||
| BuilderDefinitionDto builderDto, | ||
| BuilderProcessingContext context, | ||
| List<String> uncopyableProperties) { | ||
| Set<String> builderProperties = new HashSet<>(); | ||
| for (BuilderFieldDto field : builderDto.getAllFieldsForBuilder()) { | ||
| builderProperties.add(field.getOriginalFieldName()); | ||
| builderProperties.add(field.getFieldNameInBuilder()); | ||
| } | ||
| for (ExecutableElement method : ElementFilter.methodsIn(context.getAllMembers(annotatedType))) { | ||
| Optional<String> propertyName = getterOnlyPropertyName(method, context); | ||
| if (propertyName.isPresent() | ||
| && !builderProperties.contains(propertyName.get()) | ||
| && !hasIgnoredSetter(annotatedType, propertyName.get(), context) | ||
| && uncopyableProperties.stream() | ||
| .noneMatch(reason -> reason.startsWith(propertyName.get() + " ("))) { | ||
| uncopyableProperties.add(propertyName.get() + " (getter-only state cannot be set)"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean hasIgnoredSetter( | ||
| TypeElement annotatedType, String propertyName, BuilderProcessingContext context) { | ||
| String setterName = "set" + StringUtils.capitalize(propertyName); | ||
| return ElementFilter.methodsIn(context.getAllMembers(annotatedType)).stream() | ||
| .anyMatch( | ||
| method -> | ||
| Strings.CI.equals(method.getSimpleName().toString(), setterName) | ||
| && method.getParameters().size() == 1 | ||
| && !hasNotAnnotation(IgnoreInBuilder.class, method)); | ||
| } | ||
|
|
||
| private static Optional<String> getterOnlyPropertyName( | ||
| ExecutableElement method, BuilderProcessingContext context) { | ||
| if (!method.getParameters().isEmpty() | ||
| || method.getReturnType().getKind() == javax.lang.model.type.TypeKind.VOID | ||
| || method.getModifiers().contains(Modifier.STATIC) | ||
| || method.getModifiers().contains(Modifier.PRIVATE) | ||
| || !shouldMethodBeIncludedInBuilder(method) | ||
| || !context.isMemberAccessibleFromBuilderPackage(method) | ||
| || method.getEnclosingElement() instanceof TypeElement owner | ||
| && "java.lang.Object".equals(owner.getQualifiedName().toString())) { | ||
| return Optional.empty(); | ||
| } | ||
| String name = method.getSimpleName().toString(); | ||
| String propertyName = null; | ||
| if (name.startsWith("get") && name.length() > 3 && Character.isUpperCase(name.charAt(3))) { | ||
| propertyName = StringUtils.uncapitalize(name.substring(3)); | ||
| } else if (name.startsWith("is") | ||
| && name.length() > 2 | ||
| && Character.isUpperCase(name.charAt(2)) | ||
| && method.getReturnType().getKind() == javax.lang.model.type.TypeKind.BOOLEAN) { | ||
| propertyName = StringUtils.uncapitalize(name.substring(2)); | ||
| } | ||
| return Optional.ofNullable(propertyName).filter(SourceVersion::isName); | ||
| } | ||
|
|
||
| private static void warnAboutUncopyableProperties( | ||
| TypeElement annotatedType, | ||
| BuilderProcessingContext context, | ||
| List<String> uncopyableProperties) { | ||
| if (!uncopyableProperties.isEmpty()) { | ||
| context.warning( | ||
| annotatedType, | ||
| "%s: with()/copy constructor cannot copy %d propert%s: %s", | ||
| annotatedType.getSimpleName(), | ||
| uncopyableProperties.size(), | ||
| uncopyableProperties.size() == 1 ? "y" : "ies", | ||
| String.join(", ", uncopyableProperties)); | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
So many changes just for the feature to have a warning instead of silent drop? Could we not just compare the results? Which fields are used for copying and which fields are recognized for builder? The exact reason is not needed, just which fields would not be mapped!
There was a problem hiding this comment.
Reworked as suggested. The implementation now compares builder properties with the properties that have copy-readable getters and lists only the unmappable property names; the reason-specific collection and parameter threading are removed.
|
|
||
| public int getA() { return a; } | ||
| public String getName() { return name; } | ||
| @org.javahelpers.simple.builders.core.annotations.IgnoreInBuilder |
There was a problem hiding this comment.
Add it to
Import and just use the short name
There was a problem hiding this comment.
Done. The fixture imports IgnoreInBuilder and uses the short annotation name.
| public static void setUtil(int util) {} | ||
| public void setRisky(int risk) throws Exception {} | ||
| public int returnsInt() { return 42; } | ||
| @org.javahelpers.simple.builders.core.annotations.IgnoreInBuilder |
There was a problem hiding this comment.
Done here as well. The generated fixture now receives the import through the test utility and uses the short annotation name.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Strict reviewchanges needed Reviewed head
Verification
Other-PR integration review
Recheck at
|
| # | Original finding (short) | Status | Remaining action |
|---|---|---|---|
| 1 | False warning for copyable isX property |
fixed | — |
| 2 | Source-helper string convention | partially fixed — nit | Concatenation removed; convert import fragment to a text block (ProcessorTestUtils.java:179). |
Build: Java 17 module install — BUILD SUCCESS; 1,384 tests, 0 failures/errors; boolean probes have zero warnings; example compile succeeds without tracked drift.
Recheck at 1f3b8635 (2026-12-06)
Verdict: ready
| Severity | What is missing | Where (file:line) |
|---|---|---|
| — | None. | — |
…ncopyable scan - is-getters now resolve both candidate property names (stripped 'ready' and prefixed 'isReady'), matching setter-derived properties like setIsReady -> isReady; fully copyable boolean state no longer warns - ignored-setter exclusion checks all candidates, so an explicitly ignored isX setter suppresses the warning as intended - regression tests for both combinations - nit: text-block/formatted assembly in ProcessorTestUtils fixture helper
|
Strict-review findings fixed:
Verified: full processor suite green, 693 tests. |
…yable-warnings' into devin/1796199106-issue-457-uncopyable-warnings
|
Recheck nit done: the import fragment in |
|



Fixes #457.
Summary
with()cannot copygenerateWithInterfaceVerification
mvn clean verify