Skip to content

Commit

Permalink
Fix handling of generics
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLooman committed Sep 17, 2023
1 parent c4c9f25 commit 908f12f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,10 @@ private void buildMethodSignatureDoc(final Method method, final StringBuilder bu

private void buildTypeSignatureDoc(final AbstractType type, final StringBuilder builder) {
// Type signature.
final TypeString typeStr = type.getTypeString();
builder
.append("## ")
.append(type.getFullName())
.append(typeStr.getFullString().replace("<", "&lt;").replace(">", "&gt;"))
.append(SECTION_END);

// Type doc.
Expand Down Expand Up @@ -502,12 +503,12 @@ private void buildTypeSignatureDoc(final AbstractType type, final StringBuilder
.append(SECTION_END);
}

// Type generics.
final Collection<GenericDeclaration> generics = type.getGenerics();
if (!generics.isEmpty()) {
// Type generic declarations.
final Collection<GenericDeclaration> genericDeclarations = type.getGenerics();
if (!genericDeclarations.isEmpty()) {
builder
.append("## Generics\n");
generics.stream()
.append("## Generic declarations\n");
genericDeclarations.stream()
.forEach(generic ->
builder
.append("* ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private void handleDefinition(final MagikFile magikFile, final SlottedExemplarDe
// Slots.
final TypeDocParser docParser = new TypeDocParser(node);
final Map<String, TypeString> slotTypes = docParser.getSlotTypes();
// This needs a default value ("") due to https://bugs.openjdk.java.net/browse/JDK-8148463
// This needs a default value (TypeString.UNDEFINED) due to https://bugs.openjdk.java.net/browse/JDK-8148463
final Map<String, TypeString> slots = definition.getSlots().stream()
.map(SlottedExemplarDefinition.Slot::getName)
.collect(Collectors.toMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import nl.ramsolutions.sw.magik.analysis.typing.ITypeKeeper;
import nl.ramsolutions.sw.magik.api.TypeStringGrammar;

/**
* MagikType for which Generics are filled.
Expand Down Expand Up @@ -220,19 +221,24 @@ public String toString() {
(a, b) -> a,
LinkedHashMap::new)); // We want order!

final String genericsStr = generics.entrySet().stream()
.map(entry -> {
final GenericDeclaration genericDecl = entry.getKey();
final GenericDefinition genericDef = entry.getValue();
if (genericDef == null) {
return genericDecl.getTypeString().getFullString();
}

return genericDef.getTypeString().getFullString();
})
.collect(Collectors.joining(","));
final String genericsStr = !generics.isEmpty()
? generics.entrySet().stream()
.map(entry -> {
final GenericDeclaration genericDecl = entry.getKey();
final GenericDefinition genericDef = entry.getValue();
if (genericDef == null) {
return genericDecl.getTypeString().getFullString();
}

return genericDef.getTypeString().getFullString();
})
.collect(Collectors.joining(
TypeStringGrammar.Punctuator.TYPE_GENERIC_SEPARATOR.getValue(),
TypeStringGrammar.Punctuator.TYPE_GENERIC_OPEN.getValue(),
TypeStringGrammar.Punctuator.TYPE_GENERIC_CLOSE.getValue()))
: "";
return String.format(
"%s@%s(%s<%s>)",
"%s@%s(%s%s)",
this.getClass().getName(), Integer.toHexString(this.hashCode()),
this.getFullName(), genericsStr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public final class TypeString {
@SuppressWarnings("checkstyle:JavadocVariable")
public static final TypeString SELF = new TypeString(SelfType.SERIALIZED_NAME, DEFAULT_PACKAGE);

private static final String GENERIC = "_generic";
private static final String PARAMETER = "_parameter";

private final String string;
private final String currentPackage;
private final List<TypeString> combinedTypes;
Expand Down Expand Up @@ -73,7 +76,7 @@ private TypeString(final String currentPackage, final TypeString... combinations
* @return {@link TypeString}.
*/
public static TypeString ofGeneric(final String identifier) {
return new TypeString(identifier, "_generic");
return new TypeString(identifier, TypeString.GENERIC);
}

/**
Expand All @@ -82,7 +85,7 @@ public static TypeString ofGeneric(final String identifier) {
* @return {@link TypeString}.
*/
public static TypeString ofParameterRef(final String identifier) {
return new TypeString(identifier, "_parameter");
return new TypeString(identifier, PARAMETER);
}

/**
Expand Down Expand Up @@ -170,11 +173,20 @@ public String getFullString() {
return this.string;
}

final String genericDefs = !this.generics.isEmpty()
? this.generics.stream()
.map(TypeString::getFullString)
.collect(Collectors.joining(
TypeStringGrammar.Punctuator.TYPE_GENERIC_SEPARATOR.getValue(),
TypeStringGrammar.Punctuator.TYPE_GENERIC_OPEN.getValue(),
TypeStringGrammar.Punctuator.TYPE_GENERIC_CLOSE.getValue()))
: "";

if (this.string.contains(":")) {
return this.string;
return this.string + genericDefs;
}

return this.currentPackage + ":" + this.string;
return this.currentPackage + ":" + this.string + genericDefs;
}

/**
Expand Down Expand Up @@ -266,6 +278,14 @@ public TypeString substituteType(final TypeString from, final TypeString to) {
return to;
}

if (this.isCombined()) {
final TypeString[] combinedSubstitutedArr = this.combinedTypes.stream()
.map(typeString -> typeString.substituteType(from, to))
.collect(Collectors.toList())
.toArray(TypeString[]::new);
return TypeString.ofCombination(this.currentPackage, combinedSubstitutedArr);
}

return this;
}

Expand Down

0 comments on commit 908f12f

Please sign in to comment.