From e9e902395dfd7bd2ef18ead8458cb3a3914b3362 Mon Sep 17 00:00:00 2001 From: ZZZank <3410764033@qq.com> Date: Fri, 31 May 2024 18:51:45 +0800 Subject: [PATCH] better jdoc for DocType --- .../document/type/DocTypeResolver.java | 21 +++++++++---------- .../com/probejs/document/type/TypeArray.java | 4 ++++ .../com/probejs/document/type/TypeClazz.java | 2 ++ .../document/type/TypeIntersection.java | 5 +++++ .../probejs/document/type/TypeLiteral.java | 4 ++++ .../com/probejs/document/type/TypeNamed.java | 3 +++ .../document/type/TypeParameterized.java | 4 ++++ .../com/probejs/document/type/TypeUnion.java | 4 ++++ 8 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/probejs/document/type/DocTypeResolver.java b/src/main/java/com/probejs/document/type/DocTypeResolver.java index 33b7b473..1fdb1b62 100644 --- a/src/main/java/com/probejs/document/type/DocTypeResolver.java +++ b/src/main/java/com/probejs/document/type/DocTypeResolver.java @@ -1,7 +1,6 @@ package com.probejs.document.type; import com.probejs.info.type.*; -import com.probejs.util.Pair; import com.probejs.util.StringUtil; import lombok.val; @@ -39,17 +38,17 @@ public static DocType resolve(String type) { type = type.trim(); //TODO: Resolve object type - if (type.startsWith("{")) { - // {[x in string]: string} + if (type.startsWith("{") || type.startsWith("[")) { + // {[x in string]: string}, or [int, int, int] return new TypeLiteral(type); } - Pair splitUnion = StringUtil.splitFirst(type, "<", ">", "|"); + val splitUnion = StringUtil.splitFirst(type, "|"); if (splitUnion != null) { return new TypeUnion(resolve(splitUnion.first()), resolve(splitUnion.second())); } - Pair splitIntersection = StringUtil.splitFirst(type, "<", ">", "&"); + val splitIntersection = StringUtil.splitFirst(type, "&"); if (splitIntersection != null) { return new TypeIntersection( resolve(splitIntersection.first()), @@ -62,10 +61,10 @@ public static DocType resolve(String type) { } if (type.endsWith(">")) { - int indexLeft = type.indexOf("<"); - String rawType = type.substring(0, indexLeft); - String typeParams = type.substring(indexLeft + 1, type.length() - 1); - List params = StringUtil.splitLayer(typeParams, "<", ">", ","); + val indexLeft = type.indexOf("<"); + val rawType = type.substring(0, indexLeft); + val typeParams = type.substring(indexLeft + 1, type.length() - 1); + val params = StringUtil.splitLayer(typeParams, ","); return new TypeParameterized( resolve(rawType), params.stream().map(DocTypeResolver::resolve).collect(Collectors.toList()) @@ -82,8 +81,8 @@ public static boolean typeEquals(DocType docType, JavaType param) { return typeEquals(((TypeArray) docType).getBase(), array.getBase()); } if (docType instanceof TypeParameterized && param instanceof JavaTypeParameterized parameterized) { - List paramInfo = parameterized.getParamTypes(); - List paramDoc = ((TypeParameterized) docType).getParamTypes(); + val paramInfo = parameterized.getParamTypes(); + val paramDoc = ((TypeParameterized) docType).getParamTypes(); if (paramDoc.size() != paramInfo.size()) { return false; } diff --git a/src/main/java/com/probejs/document/type/TypeArray.java b/src/main/java/com/probejs/document/type/TypeArray.java index bcee4902..f549c36c 100644 --- a/src/main/java/com/probejs/document/type/TypeArray.java +++ b/src/main/java/com/probejs/document/type/TypeArray.java @@ -5,6 +5,10 @@ import java.util.function.BiFunction; +/** + * "string[]" + * @author ZZZank + */ @Getter public class TypeArray implements DocType { private final DocType base; diff --git a/src/main/java/com/probejs/document/type/TypeClazz.java b/src/main/java/com/probejs/document/type/TypeClazz.java index 9f49f39d..2c506106 100644 --- a/src/main/java/com/probejs/document/type/TypeClazz.java +++ b/src/main/java/com/probejs/document/type/TypeClazz.java @@ -3,6 +3,8 @@ import com.probejs.info.type.JavaTypeClass; /** + * "String", "Map" + * note that there's no attached type variables * @author ZZZank */ public class TypeClazz implements DocType { diff --git a/src/main/java/com/probejs/document/type/TypeIntersection.java b/src/main/java/com/probejs/document/type/TypeIntersection.java index 4a5bd5a8..187fd37d 100644 --- a/src/main/java/com/probejs/document/type/TypeIntersection.java +++ b/src/main/java/com/probejs/document/type/TypeIntersection.java @@ -2,6 +2,11 @@ import java.util.function.BiFunction; +/** + * "Formatter & Document" + * "string & number" + * @author ZZZank + */ public class TypeIntersection implements DocType { private final DocType leftType; private final DocType rightType; diff --git a/src/main/java/com/probejs/document/type/TypeLiteral.java b/src/main/java/com/probejs/document/type/TypeLiteral.java index fece9be7..d5bc74d9 100644 --- a/src/main/java/com/probejs/document/type/TypeLiteral.java +++ b/src/main/java/com/probejs/document/type/TypeLiteral.java @@ -1,5 +1,9 @@ package com.probejs.document.type; +/** + * string literal + * @author ZZZank + */ public class TypeLiteral implements DocType { private final String literal; diff --git a/src/main/java/com/probejs/document/type/TypeNamed.java b/src/main/java/com/probejs/document/type/TypeNamed.java index 2997778d..49a85147 100644 --- a/src/main/java/com/probejs/document/type/TypeNamed.java +++ b/src/main/java/com/probejs/document/type/TypeNamed.java @@ -2,6 +2,9 @@ import com.probejs.formatter.resolver.NameResolver; +/** + * also literal, but allows underscore + */ public class TypeNamed implements DocType { private final String typeName; diff --git a/src/main/java/com/probejs/document/type/TypeParameterized.java b/src/main/java/com/probejs/document/type/TypeParameterized.java index d5440e7c..c748de11 100644 --- a/src/main/java/com/probejs/document/type/TypeParameterized.java +++ b/src/main/java/com/probejs/document/type/TypeParameterized.java @@ -7,6 +7,10 @@ import java.util.function.BiFunction; import java.util.stream.Collectors; +/** + * "Map" + * @author ZZZank + */ @Getter public class TypeParameterized implements DocType { diff --git a/src/main/java/com/probejs/document/type/TypeUnion.java b/src/main/java/com/probejs/document/type/TypeUnion.java index 4d55ad3b..1cc846e3 100644 --- a/src/main/java/com/probejs/document/type/TypeUnion.java +++ b/src/main/java/com/probejs/document/type/TypeUnion.java @@ -2,6 +2,10 @@ import java.util.function.BiFunction; +/** + * "string | number" + * @author ZZZank + */ public class TypeUnion implements DocType { private final DocType leftType; private final DocType rightType;