Skip to content

Commit 73f8eb9

Browse files
authored
118 enum value referencing constants (#122)
* Refactor value resolution logic to support constant and enum value referencing * Simplify TypeInfoParser contract * Move typeInfo to package * Move domain objs to packages * Update doc
1 parent 8757a97 commit 73f8eb9

107 files changed

Lines changed: 1156 additions & 763 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

annotation/src/main/java/online/sharedtype/SharedType.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* <p>
1313
* <b>Annotation Processing:</b><br>
14-
* This annotation is retained only in source code.
14+
* This annotation is retained only at source code level.
1515
* That means it is not visible if source code is not directly participating in annotation processing.
1616
* E.g. if it is in a dependency jar in a project with multi-module build.
1717
* <br>
@@ -24,6 +24,13 @@
2424
* </p>
2525
*
2626
* <p>
27+
* <b>Serialization:</b><br>
28+
* Although SharedType does not depend on any serialization format, it assumes the contract of JSON.
29+
* That means a client can use any format of serializations, but may not exceed JSON capacity.
30+
* E.g. SharedType treats {@code List} and {@code Set} as arrays, and will not differentiate them in target code.
31+
* </p>
32+
*
33+
* <p>
2734
* <b>Configurations:</b><br>
2835
* Global properties can be defined in a client provided property file or system properties.
2936
* By default, SharedType will look for file name "sharedtype.properties" on cmd path.
@@ -57,11 +64,26 @@
5764
* </ul>
5865
*
5966
* <p>
67+
* <b>Enums:</b><br>
68+
* Enums are emitted as according to target schema:
69+
* <ul>
70+
* <li>Typescript: type union or enum. By default enum values are strings.</li>
71+
* <li>Rust: plain enum (Enum values of different types are not supported yet.)</li>
72+
* </ul>
73+
* See {@link EnumValue} for how to mark an enum value.
74+
*
75+
* <p>
6076
* <b>Constants:</b><br>
61-
* Static fields are treated as constants. Only compile-time resolvable values are supported.
77+
* Static fields are treated as constants.
6278
* By default, constants are not included, see {@link #includes()}.
6379
* Only constants in explicitly annotated types will be emitted.
64-
* </p>
80+
* <br>
81+
* Only compile-time resolvable values are supported, which include:
82+
* <ul>
83+
* <li>literal values: primitives, boxed primitives, String</li>
84+
* <li>enums (with compile-time resolvable values)</li>
85+
* <li>references that eventually reach other compile-time resolvable values</li>
86+
* </ul>
6587
*
6688
* <p>
6789
* <b>Generics:</b><br>
@@ -258,22 +280,25 @@
258280
}
259281

260282
/**
261-
* Mark enum value. By default, enum value is the enum constant name. The enum value must be literals (e.g. 1, "a", true) in enum constant expressions.
262-
* <b>Note: When there are multiple enum constant constructor parameters, the value is resolved by field order.</b>
283+
* Mark enum value. By default, enum value is the enum constant name.
284+
* The enum value must be "compile-time resolvable" in enum constant expressions (See "Constant" section in {@link SharedType}).
285+
* Note: When there are multiple enum constant constructor parameters, the value is resolved by field order.
263286
* If the constructor parameter order is different from the field order, value will not be resolved correctly.
287+
* Additional custom annotation types can be configured via global properties.
264288
* <pre>
265-
* Example:
266289
* {@code
290+
* //A simple example:
267291
* enum Enum {
268292
* A(1), B(2);
269293
*
270294
* @SharedType.EnumValue
271295
* private final int value;
272296
* }
297+
* //output in typescript union:
298+
* type Enum = 1 | 2;
273299
* }
274300
* </pre>
275301
* <br>
276-
* Additional annotation types can be configured via global properties.
277302
*/
278303
@Target({ElementType.FIELD})
279304
@Retention(RetentionPolicy.CLASS)

client-test/typescript/src/types.java17.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
type JavaTimeClass,
77
type JodaTimeClass,
88
type MathClass,
9+
type EnumConstReference,
10+
type EnumEnumReference,
911
} from "./index.java17";
1012

1113
export const list1: EnumGalaxy[] = ["Andromeda", "MilkyWay", "Triangulum"];
@@ -135,3 +137,7 @@ export const mathClass: MathClass = {
135137
bigDecimal: 1,
136138
bigInteger: 1,
137139
}
140+
141+
142+
export const enumConstValue1: EnumConstReference = 156;
143+
export const enumEnumValue1: EnumEnumReference = 3;

internal/src/main/java/online/sharedtype/processor/domain/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package online.sharedtype.processor.domain;
22

33
import online.sharedtype.SharedType;
4+
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
5+
import online.sharedtype.processor.domain.type.TypeInfo;
46

57
import javax.lang.model.type.TypeKind;
68
import java.util.HashMap;

internal/src/main/java/online/sharedtype/processor/domain/DependingKind.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

internal/src/main/java/online/sharedtype/processor/domain/ComponentInfo.java renamed to internal/src/main/java/online/sharedtype/processor/domain/component/ComponentInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package online.sharedtype.processor.domain;
1+
package online.sharedtype.processor.domain.component;
2+
3+
import online.sharedtype.processor.domain.def.TypeDef;
24

35
import java.io.Serializable;
46

internal/src/main/java/online/sharedtype/processor/domain/ConstantField.java renamed to internal/src/main/java/online/sharedtype/processor/domain/component/ConstantField.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package online.sharedtype.processor.domain;
1+
package online.sharedtype.processor.domain.component;
22

33
import lombok.EqualsAndHashCode;
44
import lombok.RequiredArgsConstructor;
5-
import lombok.Setter;
5+
import online.sharedtype.processor.domain.type.TypeInfo;
6+
import online.sharedtype.processor.domain.value.ValueHolder;
67

78
/**
89
* Represents a constant literal.
@@ -16,7 +17,7 @@ public final class ConstantField implements ComponentInfo {
1617
private static final long serialVersionUID = -155863067131290289L;
1718
private final String name;
1819
private final TypeInfo type;
19-
private final Object value;
20+
private final ValueHolder value;
2021

2122
public String name() {
2223
return name;
@@ -26,7 +27,7 @@ public TypeInfo type() {
2627
return type;
2728
}
2829

29-
public Object value() {
30+
public ValueHolder value() {
3031
return value;
3132
}
3233

internal/src/main/java/online/sharedtype/processor/domain/EnumValueInfo.java renamed to internal/src/main/java/online/sharedtype/processor/domain/component/EnumValueInfo.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
package online.sharedtype.processor.domain;
1+
package online.sharedtype.processor.domain.component;
22

33
import lombok.EqualsAndHashCode;
44
import lombok.RequiredArgsConstructor;
55
import online.sharedtype.SharedType;
6+
import online.sharedtype.processor.domain.def.EnumDef;
7+
import online.sharedtype.processor.domain.type.TypeInfo;
8+
import online.sharedtype.processor.domain.value.EnumConstantValue;
69

710
/**
811
* Represents an enum value, which is the value in the target code that corresponds to an enum constant.
@@ -19,7 +22,7 @@ public final class EnumValueInfo implements ComponentInfo {
1922
private static final long serialVersionUID = 1117324458104635595L;
2023
private final String name;
2124
private final TypeInfo type;
22-
private final Object value;
25+
private final EnumConstantValue value;
2326

2427
public String name() {
2528
return name;
@@ -29,7 +32,7 @@ public TypeInfo type() {
2932
return type;
3033
}
3134

32-
public Object value() {
35+
public EnumConstantValue value() {
3336
return value;
3437
}
3538

internal/src/main/java/online/sharedtype/processor/domain/FieldComponentInfo.java renamed to internal/src/main/java/online/sharedtype/processor/domain/component/FieldComponentInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package online.sharedtype.processor.domain;
1+
package online.sharedtype.processor.domain.component;
22

33
import lombok.Builder;
44
import lombok.Setter;
5+
import online.sharedtype.processor.domain.type.TypeInfo;
56

67
import javax.lang.model.element.Modifier;
78
import java.util.Set;

internal/src/main/java/online/sharedtype/processor/domain/ClassDef.java renamed to internal/src/main/java/online/sharedtype/processor/domain/def/ClassDef.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
package online.sharedtype.processor.domain;
1+
package online.sharedtype.processor.domain.def;
22

33
import lombok.Builder;
44
import lombok.EqualsAndHashCode;
55
import lombok.experimental.SuperBuilder;
6+
import online.sharedtype.processor.domain.component.FieldComponentInfo;
7+
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
8+
import online.sharedtype.processor.domain.type.TypeInfo;
9+
import online.sharedtype.processor.domain.type.TypeVariableInfo;
610

711
import java.util.ArrayList;
812
import java.util.HashMap;

internal/src/main/java/online/sharedtype/processor/domain/ConcreteTypeDef.java renamed to internal/src/main/java/online/sharedtype/processor/domain/def/ConcreteTypeDef.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
package online.sharedtype.processor.domain;
1+
package online.sharedtype.processor.domain.def;
22

33
import lombok.Getter;
44
import lombok.Setter;
55
import lombok.experimental.SuperBuilder;
6+
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
67

78
import javax.lang.model.element.Element;
89
import java.util.Set;

0 commit comments

Comments
 (0)