Repository navigation
fix: keep generic owners of inner classes and drop redundant enclosing check - #438
igel-devin-ai wants to merge 9 commits into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
| // 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); |
There was a problem hiding this comment.
Moving this into a helper method?
There was a problem hiding this comment.
Done — extracted addParameterWithNestedGenerics(...) so addParameter only branches on the nested-owner case. In 58a0007.
| && !(typeElement.getNestingKind() == NestingKind.MEMBER | ||
| && !typeElement.getModifiers().contains(Modifier.STATIC)) |
There was a problem hiding this comment.
Adding helper functions for is not static and for isnotmember?
There was a problem hiding this comment.
Done — added isMember(...) and isNotStatic(...) helpers; both call sites (setEmptyConstructorInfo and toTypeName) now use them. In 58a0007.
…cognitive-complexity
…plexity' into fix/javalangmapper-cognitive-complexity
…cognitive-complexity
…and member/static checks
| TypeName typeName, TypeElement typeElement, TypeMappingContext context) { | ||
| if (!typeName.hasEmptyConstructor() | ||
| && isConcreteClass(typeElement) | ||
| && !(isMember(typeElement) && isNotStatic(typeElement)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
All which are members of the and not static are nested?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
What does this mean? NestinKgind=Member
There was a problem hiding this comment.
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.
| 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); |
There was a problem hiding this comment.
Please have a deep check that former behavior matches new one!
There was a problem hiding this comment.
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



Summary
Fixes #439 and resolves Sonar's cognitive-complexity finding on
JavaLangMapper.extractType(17 > 15).1. Redundant enclosing-type check removed (
visitDeclared)The resolved
enclosingTypeName was only read byt.getTypeArguments().isEmpty() && !(enclosing instanceof TypeNameGeneric). Both outcomes produced the samewrapInCollectionTypeIfApplicable(rawType, List.of(), ...), so the check never had an effect. It and the redundant secondisEmptyguard are collapsed into onetypeArguments.isEmpty()branch.2. Generic owners of non-static inner classes are kept (#439)
Previously
Outer<String>.Innerwas generated as the rawOuter.Inner, andOuter<String>.Inner2<Integer>asOuter.Inner2<Integer>, which does not compile.JavaLangMapper: for non-static member types, the actual enclosingDeclaredTypeis mapped into theTypeNameNestedowner. Static nested types keep their raw owner as before.TypeNameNested/TypeNameGeneric:getFullQualifiedName()andgetSimpleNameWithGenerics()render the owner's type arguments;getClassName()stays raw.ImportCollector: imports the erased nested name and collects the owner's type arguments.RoasterCodeGenerator: Roaster'saddParametercannot parse a type with generics on both owner and member (Outer<String>.Inner2<Integer>), so for types containing>.the parameter list is set viasetParameters. All other parameters useaddParameteras before.3. No instantiation of non-static inner classes (#439)
setEmptyConstructorInfono longer marks non-static member classes as constructible, sincenew Outer.Inner()needs an enclosing instance. This removes the invalid consumer helper and thenew X()/X::newJavadoc examples for these types.Tests
Written by Devin for @igel-devin-ai