Skip to content

Commit

Permalink
DocDocument++
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZZank committed May 29, 2024
1 parent f2147ad commit 8549fd8
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/probejs/document/DocumentComment.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class DocumentComment implements IDecorative, MultiFormatter {
Expand Down Expand Up @@ -48,8 +49,8 @@ public DocumentComment(List<String> lines) {
.map(String::trim)
.collect(Collectors.toList());
this.rawLines.stream()
.filter(CommentHandler::isCommentLineSpecial)
.map(t -> CommentHandler.specialCommentHandler.get(t.split(" ", 2)[0]).apply(t))
.map(CommentHandler::tryParseSpecialComment)
.filter(Objects::nonNull)
.forEach(c -> specials.put(c.getClass(), c));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public static boolean isCommentLineSpecial(String line) {
return specialCommentHandler.containsKey(line.split(" ", 2)[0]);
}

public static SpecialComment tryParseSpecialComment(String line) {
line = line.trim();
if (!line.startsWith("@")) {
return null;
}
return specialCommentHandler.get(line.split(" ")[0]).apply(line);
}

public static void init() {
specialCommentHandler.put("@hidden", CommentHidden::new);
specialCommentHandler.put("@modify", CommentModify::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.probejs.document.type.DocTypeResolver;
import lombok.Getter;

/**
* type modify
*/
@Getter
public class CommentModify extends SpecialComment {

Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/probejs/rewrite/ClazzPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
@AllArgsConstructor
public class ClazzPath {
public static final List<String> NAMESPACE_INTERNAL = Collections.singletonList("Internal");
public static final List<String> NAMESPACE_NONE = Collections.emptyList();
public static final String NAME_UNRESOLVED = "Unresolved";
public static final ClazzPath UNRESOLVED = new ClazzPath(Collections.emptyList(), NAME_UNRESOLVED, false);
public static final ClazzPath UNRESOLVED = new ClazzPath(NAMESPACE_NONE, NAME_UNRESOLVED, false);

private final List<String> namespace;
private final String name;
Expand All @@ -30,12 +31,6 @@ public class ClazzPath {
this.isInternal = false;
}

public List<String> getNamespace() {
return this.isInternal && !this.namespace.isEmpty()
? ClazzPath.NAMESPACE_INTERNAL
: this.namespace;
}

public String asStrPath(String delimiter) {
return String.join(delimiter, this.asStrNamespace(delimiter), this.name);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/probejs/rewrite/doc/DocClazz.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ public class DocClazz implements CommentHolder {
private final List<DocType> assignables;

private DocClazz(Class<?> clazz) {
//doc properties
val cInfo = ClassInfo.ofCache(clazz);
this.path = PathResolver.resolve(cInfo.getRaw());
REGISTRIES.put(clazz, this);
this.assignables = new ArrayList<>();
this.comment = new DocComment();

//properties from ClassInfo
this.fields = cInfo.getFields().stream().map(DocField::new).collect(Collectors.toList());
this.methods = cInfo.getMethods().stream().map(DocMethod::new).collect(Collectors.toList());
}
Expand Down
49 changes: 47 additions & 2 deletions src/main/java/com/probejs/rewrite/doc/DocComment.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,73 @@
package com.probejs.rewrite.doc;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.probejs.document.comment.CommentHandler;
import com.probejs.document.comment.SpecialComment;
import com.probejs.util.Pair;
import lombok.Getter;
import lombok.val;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* only java doc comment, regular line comment / block comment are not supported by this
*/
@Getter
public class DocComment {

private final List<SpecialComment> specials;
private final ListMultimap<Class<? extends SpecialComment>, SpecialComment> specials;
private final List<String> normals;

/**
* construct a new, empty DocComment
*/
DocComment() {
this.specials = new ArrayList<>(0);
this.specials = ArrayListMultimap.create(3,1);
this.normals = new ArrayList<>(0);
}

DocComment(List<String> lines) {
this();
//TODO: parse
}

/**
* @return {trimmed line, special comment}
*/
private static Pair<String, SpecialComment> parseFirstLine(String line) {
line = line.trim();
if (!line.startsWith("/**")) {
throw new IllegalArgumentException("arg not first line of a javadoc comment");
}
line = line.substring(3);
return new Pair<>(line, CommentHandler.tryParseSpecialComment(line));
}

private static Pair<String, SpecialComment> parseLastLine(String line) {
line = line.trim();
if (!line.endsWith("*/")) {
throw new IllegalArgumentException("arg not last line of a javadoc comment");
}
//TODO
return null;
}

public <T extends SpecialComment> void acceptSpecials(Collection<T> specials) {
for (T special : specials) {
this.specials.put(special.getClass(), special);
}
}

@SuppressWarnings("unchecked")
public <T extends SpecialComment> List<T> getSpecialComment(Class<T> type) {
val comments = this.specials.get(type);
val special = new ArrayList<T>(comments.size());
for (val comment : comments) {
special.add((T) comment);
}
return special;
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/probejs/rewrite/doc/DocField.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.probejs.info.type.IType;
import com.probejs.rewrite.doc.comments.CommentHolder;
import lombok.Getter;
import lombok.val;

@Getter
public class DocField implements CommentHolder {
Expand All @@ -21,6 +22,6 @@ public class DocField implements CommentHolder {

@Override
public void applyComment() {

val specials = this.comment.getSpecials();
}
}
61 changes: 56 additions & 5 deletions src/main/java/com/probejs/rewrite/doc/DocMethod.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,79 @@
package com.probejs.rewrite.doc;

import com.probejs.document.comment.SpecialComment;
import com.probejs.document.comment.special.CommentModify;
import com.probejs.document.comment.special.CommentRename;
import com.probejs.document.type.IDocType;
import com.probejs.info.clazz.MethodInfo;
import com.probejs.info.type.IType;
import com.probejs.rewrite.doc.comments.CommentHolder;
import lombok.Getter;
import lombok.Setter;
import lombok.val;

import java.util.List;
import java.util.stream.Collectors;

@Getter
public class DocMethod implements CommentHolder {

private final DocComment comment;
private final List<MethodInfo.ParamInfo> params;
private IType returnType;
private final List<DocParam> params;
private IDocType returnType;

DocMethod(MethodInfo mInfo) {
this.comment = new DocComment();
this.params = mInfo.getParams().stream().map(DocParam::new).collect(Collectors.toList());
//TODO: doc type
this.params = mInfo.getParams();
this.returnType = mInfo.getType();
this.returnType = (IDocType) mInfo.getType();
}

@Override
public void applyComment() {
//rename
for (val rename : this.comment.getSpecialComment(CommentRename.class)) {
for (val param : this.params) {
if (param.applyComment(rename)) {
break;
}
}
}
//type modify
val modifys = this.comment.getSpecialComment(CommentModify.class);
if (!modifys.isEmpty()) {
val modify = modifys.get(0);
if (modify.getName().equals(this.returnType.getTypeName())) {
this.returnType = modify.getType();
}
}
}

@Getter
@Setter
public static class DocParam {

private String name;
private IDocType type;

DocParam(MethodInfo.ParamInfo pInfo) {
this.name = pInfo.getName();
//TODO: doc type
this.type = (IDocType) pInfo.getType();
}

public boolean applyComment(SpecialComment comment) {
//TODO: totally mess
if (comment instanceof CommentRename rename) {
if (rename.getName().equals(this.name)) {
this.name = rename.getTo();
}
} else if (comment instanceof CommentModify modify) {
if (modify.getName().equals(this.type.getTypeName())) {
this.type = modify.getType();
}
} else {
return false;
}
return true;
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/probejs/rewrite/doc/type/DocTypeArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.probejs.rewrite.doc.type;

import com.probejs.info.type.IType;
import com.probejs.info.type.TypeArray;
import lombok.Getter;

@Getter
public class DocTypeArray implements DocType {

private final DocType base;

DocTypeArray(IType type) {
this.base = DocTypeResolver.of(((TypeArray)type).getBase());
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/probejs/rewrite/doc/type/DocTypeClazz.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.probejs.rewrite.doc.type;

import com.probejs.info.clazz.ClassInfo;
import com.probejs.info.type.IType;
import com.probejs.info.type.TypeClass;
import com.probejs.rewrite.ClazzPath;
import com.probejs.rewrite.doc.DocClazz;
import lombok.Getter;
Expand All @@ -21,4 +23,8 @@ public class DocTypeClazz implements DocType {
DocTypeClazz(ClassInfo clazz) {
this(clazz.getRaw());
}

public DocTypeClazz(IType iType) {
this(((TypeClass) iType).getRaw());
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/probejs/rewrite/doc/type/DocTypeResolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.probejs.rewrite.doc.type;

import com.probejs.info.type.IType;
import com.probejs.info.type.TypeArray;
import com.probejs.info.type.TypeClass;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public abstract class DocTypeResolver {

private static final Map<Class<? extends IType>, Function<IType, DocType>> REGISTRIES;

static {
REGISTRIES = new HashMap<>();
REGISTRIES.put(TypeClass.class, DocTypeClazz::new);
REGISTRIES.put(TypeArray.class, DocTypeClazz::new);
}

public static DocType of(IType type) {
return REGISTRIES.get(type.getClass()).apply(type);
}
}

0 comments on commit 8549fd8

Please sign in to comment.