Skip to content

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

Open
igel-devin-ai wants to merge 9 commits into
java-helpers:mainfrom
igel-devin-ai:fix/javalangmapper-cognitive-complexity
Open

igel-devin-ai wants to merge 9 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

Fixes #439 and resolves Sonar's cognitive-complexity finding on JavaLangMapper.extractType (17 > 15).

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 (#439)

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.
  • ImportCollector: imports the erased nested name and collects the owner's type 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. No instantiation of non-static inner classes (#439)

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 for both types: exact property types, compiles without errors or raw/unchecked warnings, no instance consumer. Both tests fail on the previous head.
  • Focused tests for type rendering, mapping (with a static-nested control), import collection and Roaster parameter rendering (including annotated and varargs parameters).
  • Committed examples regenerate without diff.

Written by Devin for @igel-devin-ai

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
@sonarqubecloud

Copy link
Copy Markdown

Comment on lines +618 to +626
// Roaster's addParameter cannot split generics on both an owner and its member.
String existing =
method.getParameters().stream().map(Object::toString).collect(Collectors.joining(", "));
method.setParameters(
(existing.isEmpty() ? "" : existing + ", ")
+ typeSource
+ " "
+ paramDto.getParameterName());
parameter = method.getParameters().get(method.getParameters().size() - 1);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this into a helper method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — extracted addParameterWithNestedGenerics(...) so addParameter only branches on the nested-owner case. In 58a0007.

Comment on lines +182 to +183
&& !(typeElement.getNestingKind() == NestingKind.MEMBER
&& !typeElement.getModifiers().contains(Modifier.STATIC))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding helper functions for is not static and for isnotmember?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — added isMember(...) and isNotStatic(...) helpers; both call sites (setEmptyConstructorInfo and toTypeName) now use them. In 58a0007.

TypeName typeName, TypeElement typeElement, TypeMappingContext context) {
if (!typeName.hasEmptyConstructor()
&& isConcreteClass(typeElement)
&& !(isMember(typeElement) && isNotStatic(typeElement))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand this, why is this filtered? And is the condition correct? Should it not be
&& isNotMember(typeelement)
&& isNotStatic(typeelement)

Is that the check for nested?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter excludes exactly one case: a non-static member class (inner class). isMember && isNotStatic = 'inner class' — it can never be instantiated without an enclosing instance (outer.new Inner()), so it must not get hasEmptyConstructor (which drives generated new X() examples and the consumer-helper instantiation). Your suggested isNotMember && isNotStatic would also exclude static member classes (Outer.Static) — those are directly instantiable (new Outer.Static()), so they must stay marked. Hence the negation is on the combination, not on each part: allow everything except non-static members. Added an explaining comment on the condition in dd22512.


private static TypeName toTypeName(DeclaredType type, TypeMappingContext context) {
TypeElement element = (TypeElement) type.asElement();
if (isMember(element) && isNotStatic(element)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All which are members of the and not static are nested?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite — every member class ends up as TypeNameNested; this condition only picks the construction path. Non-static members (Outer.Inner, needing an enclosing instance) resolve the enclosing type via extractType, so a generic owner keeps its arguments (Outer<String>.Inner). Static members fall through to the element-based toTypeName, which also produces TypeNameNested but only needs the owner qualifier for naming (Outer.Static) — no enclosing instance semantics.

}

private static boolean isMember(TypeElement element) {
return element.getNestingKind() == NestingKind.MEMBER;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean? NestinKgind=Member

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NestingKind is the javax.lang.model enum for how a type is nested: TOP_LEVEL (own file), MEMBER (declared inside another type — inner or static-nested class), LOCAL (inside a method), ANONYMOUS. So isMember here = 'declared inside another class', regardless of static; isNotStatic then separates inner classes (need enclosing instance) from static nested classes.

Comment on lines 553 to +566
TypeElement elementOfParameter = (TypeElement) context.asElement(typeOfParameter);
String packageName = context.getPackageName(elementOfParameter);
TypeName rawType = toTypeName(elementOfParameter, packageName);
TypeName rawType = toTypeName(t, context);
if (elementOfParameter.getKind() == ElementKind.ENUM) {
rawType.setEnumType(true);
}
TypeMirror enclosingType = t.getEnclosingType();
TypeName enclosing =
(enclosingType.getKind() != NONE)
&& !t.asElement().getModifiers().contains(Modifier.STATIC)
? enclosingType.accept(this, null)
: null;
if (t.getTypeArguments().isEmpty() && !(enclosing instanceof TypeNameGeneric)) {
return wrapInCollectionTypeIfApplicable(
rawType, List.of(), typeOfParameter, context);
}

List<TypeMirror> typesExtracted = new ArrayList<>(t.getTypeArguments());
if (typesExtracted.isEmpty()) {
List<? extends TypeMirror> typeArguments = t.getTypeArguments();
if (typeArguments.isEmpty()) {
return wrapInCollectionTypeIfApplicable(
rawType, List.of(), typeOfParameter, context);
} else {
List<TypeName> argTypes = extractTypeForList(typesExtracted, context);
return wrapInCollectionTypeIfApplicable(
rawType, argTypes, typeOfParameter, context);
}
List<TypeName> argTypes =
extractTypeForList(new ArrayList<>(typeArguments), context);
return wrapInCollectionTypeIfApplicable(
rawType, argTypes, typeOfParameter, context);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a deep check that former behavior matches new one!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep check done — behavior is identical. Old code computed enclosing only to test instanceof TypeNameGeneric: when t.getTypeArguments().isEmpty(), the first if returned wrap(List.of()) if enclosing was not a generic; when it was a generic, execution fell through to the typesExtracted.isEmpty() check — which also returns wrap(List.of()). Both outcomes identical, so the enclosing computation was dead code (purely a lookup, no side effects on context or imports). Non-empty type arguments always reached extractTypeForList in both versions. JavaLangMapperTest (23 tests incl. the new nested-type cases) is green.

…cognitive-complexity

# Conflicts:
#	codegen-roaster/src/test/java/org/javahelpers/codegen/roaster/RoasterCodeGeneratorTest.java

This branch has not been deployed

No deployments
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.

Builder does not compile for non-static inner classes of generic outer types

2 participants