Skip to content

fix: keep generic owners of inner classes and drop redundant enclosing check - #438

Merged
AndreasIgel merged 25 commits into
java-helpers:mainfrom
igel-devin-ai:fix/javalangmapper-cognitive-complexity
Dec 1, 2026
Merged

AndreasIgel merged 25 commits into
java-helpers:mainfrom
igel-devin-ai:fix/javalangmapper-cognitive-complexity

Conversation

@igel-devin-ai

@igel-devin-ai igel-devin-ai commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Resolves Sonar's cognitive-complexity finding on JavaLangMapper.extractType (17 > 15) and fixes generation for parameterized inner classes, building on the inner-class groundwork merged in #439. Branch is current with main (including the merged #429 DTO builders).

1. Redundant enclosing-type check removed (visitDeclared)

The resolved enclosing TypeName was only read by t.getTypeArguments().isEmpty() && !(enclosing instanceof TypeNameGeneric). Both outcomes produced the same wrapInCollectionTypeIfApplicable(rawType, List.of(), ...), so the check never had an effect. It and the redundant second isEmpty guard are collapsed into one typeArguments.isEmpty() branch.

2. Generic owners of non-static inner classes are kept

Previously Outer<String>.Inner was generated as the raw Outer.Inner, and Outer<String>.Inner2<Integer> as Outer.Inner2<Integer>, which does not compile.

  • JavaLangMapper: for non-static member types, the actual enclosing DeclaredType is mapped into the TypeNameNested owner. Static nested types keep their raw owner as before.
  • TypeNameNested / TypeNameGeneric: getFullQualifiedName() and getSimpleNameWithGenerics() render the owner's type arguments; getClassName() stays raw. New TypeName.getRawQualifiedName() gives the erased declaration name.
  • Imports, import-collision keys and declaration lookups (BuilderProcessingContext, AnnotationProcessingContext) use getRawQualifiedName(), so owners like Outer<? extends Number>.Inner no longer produce import other.Outer<? extends java.lang.Number>.Inner;. Owner type arguments (incl. wildcard bounds) are still imported.
  • RoasterMapper: type-use annotations on owner arguments are kept (Outer<@Nullable String>.Inner), like on ordinary generic arguments.
  • RoasterCodeGenerator: Roaster's addParameter cannot parse a type with generics on both owner and member (Outer<String>.Inner2<Integer>), so for types containing >. the parameter list is set via setParameters. All other parameters use addParameter as before.

3. Generic DTOs keep their type variables in owners

For Dto<T> with Outer<T>.Inner, the copy constructor, conditional helpers and With<T> now use the parameterized DTO/builder types instead of raw ones. The nested With interface declares the DTO's type parameters; with() casts (Dto<T>) Dto.class.cast(this) under a local @SuppressWarnings("unchecked").

4. No instantiation of non-static inner classes

setEmptyConstructorInfo no longer marks non-static member classes as constructible, since new Outer.Inner() needs an enclosing instance. This removes the invalid consumer helper and the new X() / X::new Javadoc examples for these types.

Tests

  • Processor end-to-end (-Xlint:rawtypes -Xlint:unchecked, strict): Outer<String>.Inner, Inner2<Integer>, wildcard owners, Outer<T>.Inner in generic DTOs (unbounded and intersection bounds) and annotated owner arguments compile without warnings and without an instance consumer.
  • Focused tests for type rendering, raw names, mapping (with a static-nested control), import collection, context lookups and Roaster parameter rendering.
  • Not changed: varargs adders for parameterized element types (e.g. List<Outer<String>.Inner>, also List<Set<String>> on main) still emit the existing heap-pollution warning; simple-name collisions between a nested and a top-level type of the same name are out of scope.

The enclosing TypeName was resolved but only read by a condition whose both outcomes produced the same wrapInCollectionTypeIfApplicable(List.of()) result. Removing it reduces cognitive complexity below the Sonar limit (17 -> <15) with no behavior change.
@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@igel-devin-ai igel-devin-ai changed the title refactor: drop dead enclosing-type check in visitDeclared fix: keep generic owners of inner classes and drop redundant enclosing check Nov 23, 2026
…plexity' into fix/javalangmapper-cognitive-complexity

# Conflicts:
#	codegen-roaster/src/test/java/org/javahelpers/codegen/roaster/RoasterCodeGeneratorTest.java
…block test for generic With interface

The unresolved merge-conflict marker block in RoasterCodeGeneratorTest broke
test compilation and with it the CI pipeline. Also adds a textblock comparison
verifying the full generated shape of the generic With<T> interface for
generic DTOs (type parameter declaration, SuppressWarnings annotations,
typed builder references).
…DtoBuilder

NestedTypeDtoBuilder gains the full variant set for genericParameters:
singular element-add, consumer-based construction, varargs of both,
plus flattened shortcuts genericParameter(String),
genericParameter(String, TypeName...) and
genericParameterWithUpperBound(String, Class...).
BuilderMethodDtoBuilder gains the flattened shortcuts to match its
codegen-core sibling MethodDtoBuilder.
…meter helpers

- NestedTypeDto/Builder: javadoc on genericParameters field restored/placed,
  wholesale setters moved down grouped with the field's other functions,
  varargs-consumer overload renamed to plural genericParameters
- BuilderNestedTypeDtoBuilder: full genericParameter helper set (singular,
  consumer, varargs, flattened shortcuts) grouped below the fields
- BuilderProcessorTest: single-line text blocks converted to plain strings,
  multi-line source concatenations converted to text blocks, indent of test
  sources aligned with delimiters
…ders

Varargs consumers assign multiple elements in one call, so the plural
name fits the semantics: annotation->annotations, method->methods,
parameter->parameters, classField->classFields, constructor->constructors,
nestedType->nestedTypes, generic->generics, genericParameter->genericParameters.
Multi-lambda call sites in tests and main code swept and renamed.
devin-ai-integration Bot and others added 6 commits December 1, 2026 21:01
Covers singular/consumer/varargs/flattened-shortcut variants in
NestedTypeDtoBuilder, BuilderNestedTypeDtoBuilder and BuilderMethodDtoBuilder
including wholesale replacement and copy-constructor preservation.
…meters

Covers TypeNameWildcard mapping (unbounded, extends, super) in field types
and nested interface/class type-variable rendering incl. upper bounds.
@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

@AndreasIgel
AndreasIgel merged commit 58ab12b into java-helpers:main Dec 1, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants