Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions annotation/src/main/java/online/sharedtype/SharedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* <p>
* <b>Annotation Processing:</b><br>
* This annotation is retained only in source code.
* This annotation is retained only at source code level.
* That means it is not visible if source code is not directly participating in annotation processing.
* E.g. if it is in a dependency jar in a project with multi-module build.
* <br>
Expand All @@ -24,6 +24,13 @@
* </p>
*
* <p>
* <b>Serialization:</b><br>
* Although SharedType does not depend on any serialization format, it assumes the contract of JSON.
* That means a client can use any format of serializations, but may not exceed JSON capacity.
* E.g. SharedType treats {@code List} and {@code Set} as arrays, and will not differentiate them in target code.
* </p>
*
* <p>
* <b>Configurations:</b><br>
* Global properties can be defined in a client provided property file or system properties.
* By default, SharedType will look for file name "sharedtype.properties" on cmd path.
Expand Down Expand Up @@ -57,11 +64,26 @@
* </ul>
*
* <p>
* <b>Enums:</b><br>
* Enums are emitted as according to target schema:
* <ul>
* <li>Typescript: type union or enum. By default enum values are strings.</li>
* <li>Rust: plain enum (Enum values of different types are not supported yet.)</li>
* </ul>
* See {@link EnumValue} for how to mark an enum value.
*
* <p>
* <b>Constants:</b><br>
* Static fields are treated as constants. Only compile-time resolvable values are supported.
* Static fields are treated as constants.
* By default, constants are not included, see {@link #includes()}.
* Only constants in explicitly annotated types will be emitted.
* </p>
* <br>
* Only compile-time resolvable values are supported, which include:
* <ul>
* <li>literal values: primitives, boxed primitives, String</li>
* <li>enums (with compile-time resolvable values)</li>
* <li>references that eventually reach other compile-time resolvable values</li>
* </ul>
*
* <p>
* <b>Generics:</b><br>
Expand Down Expand Up @@ -258,22 +280,25 @@
}

/**
* 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.
* <b>Note: When there are multiple enum constant constructor parameters, the value is resolved by field order.</b>
* Mark enum value. By default, enum value is the enum constant name.
* The enum value must be "compile-time resolvable" in enum constant expressions (See "Constant" section in {@link SharedType}).
* Note: When there are multiple enum constant constructor parameters, the value is resolved by field order.
* If the constructor parameter order is different from the field order, value will not be resolved correctly.
* Additional custom annotation types can be configured via global properties.
* <pre>
* Example:
* {@code
* //A simple example:
* enum Enum {
* A(1), B(2);
*
* @SharedType.EnumValue
* private final int value;
* }
* //output in typescript union:
* type Enum = 1 | 2;
* }
* </pre>
* <br>
* Additional annotation types can be configured via global properties.
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.CLASS)
Expand Down
6 changes: 6 additions & 0 deletions client-test/typescript/src/types.java17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
type JavaTimeClass,
type JodaTimeClass,
type MathClass,
type EnumConstReference,
type EnumEnumReference,
} from "./index.java17";

export const list1: EnumGalaxy[] = ["Andromeda", "MilkyWay", "Triangulum"];
Expand Down Expand Up @@ -135,3 +137,7 @@ export const mathClass: MathClass = {
bigDecimal: 1,
bigInteger: 1,
}


export const enumConstValue1: EnumConstReference = 156;
export const enumEnumValue1: EnumEnumReference = 3;
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package online.sharedtype.processor.domain;

import online.sharedtype.SharedType;
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
import online.sharedtype.processor.domain.type.TypeInfo;

import javax.lang.model.type.TypeKind;
import java.util.HashMap;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.component;

import online.sharedtype.processor.domain.def.TypeDef;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.component;

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import online.sharedtype.processor.domain.type.TypeInfo;
import online.sharedtype.processor.domain.value.ValueHolder;

/**
* Represents a constant literal.
Expand All @@ -16,7 +17,7 @@ public final class ConstantField implements ComponentInfo {
private static final long serialVersionUID = -155863067131290289L;
private final String name;
private final TypeInfo type;
private final Object value;
private final ValueHolder value;

public String name() {
return name;
Expand All @@ -26,7 +27,7 @@ public TypeInfo type() {
return type;
}

public Object value() {
public ValueHolder value() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.component;

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import online.sharedtype.SharedType;
import online.sharedtype.processor.domain.def.EnumDef;
import online.sharedtype.processor.domain.type.TypeInfo;
import online.sharedtype.processor.domain.value.EnumConstantValue;

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

public String name() {
return name;
Expand All @@ -29,7 +32,7 @@ public TypeInfo type() {
return type;
}

public Object value() {
public EnumConstantValue value() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.component;

import lombok.Builder;
import lombok.Setter;
import online.sharedtype.processor.domain.type.TypeInfo;

import javax.lang.model.element.Modifier;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.def;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;
import online.sharedtype.processor.domain.component.FieldComponentInfo;
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;
import online.sharedtype.processor.domain.type.TypeInfo;
import online.sharedtype.processor.domain.type.TypeVariableInfo;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.def;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;

import javax.lang.model.element.Element;
import java.util.Set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.def;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import online.sharedtype.processor.domain.component.ConstantField;

import javax.lang.model.element.Element;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.def;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;
import online.sharedtype.processor.domain.component.EnumValueInfo;
import online.sharedtype.processor.domain.type.ConcreteTypeInfo;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.def;

import online.sharedtype.processor.domain.component.ComponentInfo;
import online.sharedtype.processor.domain.type.TypeInfo;

import javax.lang.model.element.Element;
import java.io.Serializable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.type;

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import online.sharedtype.processor.domain.def.TypeDef;

import java.util.Map;

Expand Down Expand Up @@ -36,6 +37,10 @@ public TypeInfo reify(Map<TypeVariableInfo, TypeInfo> mappings) {
TypeInfo reifiedComponent = component.reify(mappings);
return reifiedComponent == component ? this : new ArrayTypeInfo(reifiedComponent);
}
@Override
public void addReferencingType(TypeDef typeDef) {
component.addReferencingType(typeDef);
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.type;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import online.sharedtype.SharedType;
import online.sharedtype.processor.domain.def.ConcreteTypeDef;
import online.sharedtype.processor.domain.MappableType;
import online.sharedtype.processor.domain.TargetCodeType;
import online.sharedtype.processor.domain.def.TypeDef;

import javax.annotation.Nullable;
import java.util.Collections;
Expand All @@ -27,9 +31,9 @@
* @see ArrayTypeInfo
* @see Kind
*/
@EqualsAndHashCode(of = {"qualifiedName", "typeArgs"})
@EqualsAndHashCode(of = {"qualifiedName", "typeArgs"}, callSuper = false)
@Builder(toBuilder = true)
public final class ConcreteTypeInfo implements TypeInfo, MappableType {
public final class ConcreteTypeInfo extends ReferableTypeInfo implements MappableType {
private static final long serialVersionUID = 6912267731376244613L;
private final String qualifiedName;
private final String simpleName;
Expand All @@ -45,11 +49,6 @@ public final class ConcreteTypeInfo implements TypeInfo, MappableType {
@Getter
private final boolean baseMapType;

/**
* Qualified names of types from where this typeInfo is strongly referenced, i.e. as a component type.
*/
@Builder.Default
private final Set<TypeDef> referencingTypes = new HashSet<>();
@Builder.Default
private boolean resolved = true;

Expand All @@ -65,7 +64,7 @@ public final class ConcreteTypeInfo implements TypeInfo, MappableType {
private final Map<TargetCodeType, String> mappedNames = new EnumMap<>(TargetCodeType.class);


static ConcreteTypeInfo ofPredefined(String qualifiedName, String simpleName) {
public static ConcreteTypeInfo ofPredefined(String qualifiedName, String simpleName) {
return ConcreteTypeInfo.builder().qualifiedName(qualifiedName).simpleName(simpleName).build();
}

Expand Down Expand Up @@ -120,10 +119,6 @@ public String simpleName() {
return simpleName;
}

public Set<TypeDef> referencingTypes() {
return referencingTypes;
}

public List<TypeInfo> typeArgs() {
return typeArgs;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package online.sharedtype.processor.domain;
package online.sharedtype.processor.domain.type;

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import online.sharedtype.SharedType;
import online.sharedtype.processor.domain.MappableType;
import online.sharedtype.processor.domain.TargetCodeType;

import javax.annotation.Nullable;
import java.util.EnumMap;
Expand All @@ -14,9 +16,9 @@
*
* @author Cause Chung
*/
@EqualsAndHashCode(of = "qualifiedName")
@EqualsAndHashCode(of = "qualifiedName", callSuper = false)
@RequiredArgsConstructor
public final class DateTimeInfo implements TypeInfo, MappableType {
public final class DateTimeInfo extends ReferableTypeInfo implements MappableType {
private static final long serialVersionUID = 5428192893749821359L;

private final String qualifiedName;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package online.sharedtype.processor.domain.type;

import online.sharedtype.processor.domain.def.TypeDef;

import java.util.HashSet;
import java.util.Set;

public abstract class ReferableTypeInfo implements TypeInfo {
private static final long serialVersionUID = -8637192825773596439L;
/**
* Qualified names of types from where this typeInfo is strongly referenced, i.e. as a component type.
*/
private final Set<TypeDef> referencingTypes = new HashSet<>();

@Override
public final void addReferencingType(TypeDef typeDef) {
referencingTypes.add(typeDef);
}

public final Set<TypeDef> referencingTypes() {
return referencingTypes;
}
}
Loading