Skip to content

Commit ccb5cb5

Browse files
committed
some idea
1 parent c277abb commit ccb5cb5

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed

dev_note.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# steps
3+
4+
- class
5+
- info
6+
- document
7+
- formatter
8+
- compiler
9+
## class
10+
aka raw class fetching
11+
12+
## info
13+
14+
## document
15+
generic applying should be here
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.probejs.formatter.api;
2+
3+
public interface WithFormatter {
4+
5+
IFormatter toFormatter();
6+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.probejs.rewrite;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.val;
6+
7+
import java.util.Arrays;
8+
import java.util.Collections;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
@Data
13+
@AllArgsConstructor
14+
public class ClazzPath {
15+
public static final List<String> NAMESPACE_INTERNAL = Collections.singletonList("Internal");
16+
public static final String NAME_UNRESOLVED = "Unresolved";
17+
public static final ClazzPath UNRESOLVED = new ClazzPath(Collections.emptyList(), NAME_UNRESOLVED, false);
18+
19+
private final List<String> namespace;
20+
private final String name;
21+
private boolean isInternal;
22+
23+
ClazzPath(String clazzName) {
24+
val path = Arrays
25+
.stream(clazzName.split("\\."))
26+
.map(PathResolver::getNameSafe)
27+
.collect(Collectors.toList());
28+
this.namespace = path.subList(1, path.size());
29+
this.name = path.get(0);
30+
this.isInternal = false;
31+
}
32+
33+
public List<String> getNamespace() {
34+
return this.isInternal && !this.namespace.isEmpty()
35+
? ClazzPath.NAMESPACE_INTERNAL
36+
: this.namespace;
37+
}
38+
39+
public String asStrPath(String delimiter) {
40+
return String.join(delimiter, this.asStrNamespace(delimiter), this.name);
41+
}
42+
43+
public String asStrNamespace(String delimiter) {
44+
return String.join(delimiter, this.getNamespace());
45+
}
46+
47+
public String dotPath() {
48+
return asStrPath(".");
49+
}
50+
51+
public String slashPath() {
52+
return asStrPath("/");
53+
}
54+
55+
public String dotNamespace() {
56+
return this.asStrNamespace(".");
57+
}
58+
59+
public String slashNamespace() {
60+
return this.asStrNamespace("/");
61+
}
62+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.probejs.rewrite;
2+
3+
import lombok.val;
4+
5+
import java.util.*;
6+
7+
public abstract class PathResolver {
8+
public static final HashMap<String, ClazzPath> resolved = new HashMap<>();
9+
public static final HashSet<String> lastNames = new HashSet<>();
10+
public static final HashSet<Class<?>> primitives = new HashSet<>();
11+
/**
12+
* language keywords used by typescript
13+
*/
14+
public static final Set<String> langKeywords = new HashSet<>();
15+
16+
static {
17+
//keywords
18+
langKeywords.addAll(Arrays.asList("function", "debugger", "in", "with", "java"));
19+
//pre-resolve primitive
20+
val ANY = new ClazzPath(Collections.emptyList(), "any", false);
21+
resolvePrimitive(Object.class, ANY);
22+
val STR = new ClazzPath(Collections.emptyList(), "string", false);
23+
resolvePrimitive(String.class, STR);
24+
resolvePrimitive(Character.class, STR);
25+
resolvePrimitive(Character.TYPE, STR);
26+
val VOID = new ClazzPath(Collections.emptyList(), "void", false);
27+
resolvePrimitive(Void.class, VOID);
28+
resolvePrimitive(Void.TYPE, VOID);
29+
val NUM = new ClazzPath(Collections.emptyList(), "number", false);
30+
resolvePrimitive(Long.class, NUM);
31+
resolvePrimitive(Long.TYPE, NUM);
32+
resolvePrimitive(Integer.class, NUM);
33+
resolvePrimitive(Integer.TYPE, NUM);
34+
resolvePrimitive(Short.class, NUM);
35+
resolvePrimitive(Short.TYPE, NUM);
36+
resolvePrimitive(Byte.class, NUM);
37+
resolvePrimitive(Byte.TYPE, NUM);
38+
resolvePrimitive(Double.class, NUM);
39+
resolvePrimitive(Double.TYPE, NUM);
40+
resolvePrimitive(Float.class, NUM);
41+
resolvePrimitive(Float.TYPE, NUM);
42+
val BOOL = new ClazzPath(Collections.emptyList(), "boolean", false);
43+
resolvePrimitive(Boolean.class, BOOL);
44+
resolvePrimitive(Boolean.TYPE, BOOL);
45+
}
46+
47+
public static void resolvePrimitive(Class<?> clazz, ClazzPath resolvedPath) {
48+
resolveManually(clazz, resolvedPath);
49+
primitives.add(clazz);
50+
}
51+
52+
public static ClazzPath resolve(Class<?> clazz) {
53+
return resolve(clazz.getName());
54+
}
55+
56+
public static ClazzPath resolve(String clazzName) {
57+
ClazzPath path = resolved.get(clazzName);
58+
if (path == null) {
59+
path = new ClazzPath(clazzName);
60+
path.setInternal(!lastNames.contains(path.getName()));
61+
resolveManually(clazzName, path);
62+
}
63+
return path;
64+
}
65+
66+
public static ClazzPath resolveManually(String name, ClazzPath resolvedPath) {
67+
resolved.put(name, resolvedPath);
68+
lastNames.add(resolvedPath.getName());
69+
return resolvedPath;
70+
}
71+
72+
public static ClazzPath resolveManually(Class<?> clazz, ClazzPath resolvedPath) {
73+
return resolveManually(clazz.getName(), resolvedPath);
74+
}
75+
76+
public static ClazzPath resolveManually(Class<?> clazz, String resolvedPath) {
77+
return resolveManually(clazz.getName(), new ClazzPath(resolvedPath));
78+
}
79+
80+
public static ClazzPath get(String name) {
81+
return resolved.getOrDefault(name, ClazzPath.UNRESOLVED);
82+
}
83+
84+
public static boolean isNameSafe(String name) {
85+
return langKeywords.contains(name);
86+
}
87+
88+
public static String getNameSafe(String name) {
89+
if (!isNameSafe(name)) {
90+
name = name + "_";
91+
}
92+
return name;
93+
}
94+
}

0 commit comments

Comments
 (0)