Skip to content

Commit 23148b7

Browse files
committed
add a type for representing literal
1 parent e441640 commit 23148b7

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

src/main/java/com/probejs/formatter/formatter/FormatterType.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public abstract class FormatterType<T extends ITypeInfo> {
1313

1414
public static final Map<Class<? extends ITypeInfo>, Function<ITypeInfo, FormatterType<?>>> REGISTRIES = new HashMap<>();
15-
public static final Dummy DUMMY_ANY = new Dummy("any");
15+
public static final Literal LITERAL_ANY = new Literal("any");
1616
protected boolean underscored = false;
1717

1818
static {
@@ -33,7 +33,7 @@ public static FormatterType<? extends ITypeInfo> of(ITypeInfo tInfo, boolean all
3333
Class<?> rawClass = tInfo.getResolvedClass();
3434
Function<ITypeInfo, String> special = NameResolver.specialTypeFormatters.get(rawClass);
3535
if (special != null) {
36-
return new Dummy(special.apply(tInfo));
36+
return new Literal(special.apply(tInfo));
3737
}
3838
}
3939
//general
@@ -42,7 +42,7 @@ public static FormatterType<? extends ITypeInfo> of(ITypeInfo tInfo, boolean all
4242
return builder.apply(tInfo);
4343
}
4444
//fallback
45-
return DUMMY_ANY;
45+
return LITERAL_ANY;
4646
}
4747

4848
/**
@@ -80,17 +80,17 @@ public FormatterType<T> underscored(Predicate<ITypeInfo> predicate) {
8080

8181
public abstract String format();
8282

83-
public static class Dummy extends FormatterType<ITypeInfo> {
83+
public static class Literal extends FormatterType<TypeLiteral> {
8484

8585
private final String value;
8686

87-
private Dummy(String value) {
87+
public Literal(String value) {
8888
this.value = value;
8989
}
9090

9191
@Override
92-
public ITypeInfo getInfo() {
93-
return null;
92+
public TypeLiteral getInfo() {
93+
return new TypeLiteral(this.value);
9494
}
9595

9696
@Override
@@ -105,7 +105,6 @@ public static class Clazz extends FormatterType<ITypeInfo> {
105105
/**
106106
* use {@link FormatterType#of(ITypeInfo)} instead
107107
*/
108-
@Deprecated
109108
public Clazz(ITypeInfo tInfo) {
110109
this.tInfo = (TypeInfoClass) tInfo;
111110
}
@@ -131,7 +130,6 @@ public static class Wildcard extends FormatterType<ITypeInfo> {
131130
/**
132131
* use {@link FormatterType#of(ITypeInfo)} instead
133132
*/
134-
@Deprecated
135133
public Wildcard(ITypeInfo tInfo) {
136134
this.tInfo = (TypeInfoWildcard) tInfo;
137135
}
@@ -153,7 +151,6 @@ public static class Variable extends FormatterType<ITypeInfo> {
153151
/**
154152
* use {@link FormatterType#of(ITypeInfo)} instead
155153
*/
156-
@Deprecated
157154
public Variable(ITypeInfo tInfo) {
158155
this.tInfo = (TypeInfoVariable) tInfo;
159156
}
@@ -188,7 +185,6 @@ public static class Array extends FormatterType<ITypeInfo> {
188185
/**
189186
* use {@link FormatterType#of(ITypeInfo)} instead
190187
*/
191-
@Deprecated
192188
public Array(ITypeInfo tInfo) {
193189
this.tInfo = (TypeInfoArray) tInfo;
194190
}
@@ -200,7 +196,7 @@ public ITypeInfo getInfo() {
200196

201197
@Override
202198
public String format() {
203-
return FormatterType.of(this.tInfo.getBaseType()).format() + "[]";
199+
return FormatterType.of(this.tInfo.getBaseType()).underscored(this.underscored).format() + "[]";
204200
}
205201
}
206202

@@ -210,7 +206,6 @@ public static class Parameterized extends FormatterType<ITypeInfo> {
210206
/**
211207
* use {@link FormatterType#of(ITypeInfo)} instead
212208
*/
213-
@Deprecated
214209
public Parameterized(ITypeInfo tInfo) {
215210
this.tInfo = (TypeInfoParameterized) tInfo;
216211
}
@@ -227,7 +222,6 @@ public String format() {
227222
this.tInfo.getParamTypes()
228223
.stream()
229224
.map(FormatterType::of)
230-
.map(fmtr -> fmtr.underscored(false))
231225
.map(FormatterType::format)
232226
.collect(Collectors.joining(", "))
233227
);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.probejs.info.type;
2+
3+
import java.lang.reflect.Type;
4+
5+
public class TypeLiteral implements ITypeInfo {
6+
7+
private final String value;
8+
9+
public TypeLiteral(String value) {
10+
this.value = value;
11+
}
12+
13+
@Override
14+
public Type getRaw() {
15+
return null;
16+
}
17+
18+
@Override
19+
public ITypeInfo getBaseType() {
20+
return this;
21+
}
22+
23+
@Override
24+
public Class<?> getResolvedClass() {
25+
return null;
26+
}
27+
28+
@Override
29+
public String getTypeName() {
30+
return this.value;
31+
}
32+
33+
@Override
34+
public String wrapTypeName(String rawName) {
35+
return rawName;
36+
}
37+
38+
@Override
39+
public ITypeInfo copy() {
40+
return this;
41+
}
42+
43+
@Override
44+
public boolean assignableFrom(ITypeInfo info) {
45+
return false;
46+
}
47+
}

0 commit comments

Comments
 (0)