|
| 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