Skip to content

Commit 881f011

Browse files
authored
99 common math types (#101)
* Add basic mapping for BigDecimal and BitInteger * Fix annotation optional type detection
1 parent 35a8b52 commit 881f011

19 files changed

Lines changed: 79 additions & 17 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
* <p>
6060
* <b>Constants:</b><br>
6161
* Static fields are treated as constants. Only compile-time resolvable values are supported.
62+
* By default, constants are not included, see {@link #includes()}.
6263
* Only constants in explicitly annotated types will be emitted.
6364
* </p>
6465
*
@@ -103,6 +104,15 @@
103104
* </p><br>
104105
*
105106
* <p>
107+
* <b>Math types:</b><br>
108+
* By default {@link java.math.BigInteger} and {@link java.math.BigDecimal} are emitted number types.
109+
* A client can use type mappings to override the emitted types. Default mappings are:
110+
* <ul>
111+
* <li>Typescript: {@code number}</li>
112+
* <li>Rust: {@code i128} and {@code f64}</li>
113+
* </ul>
114+
*
115+
* <p>
106116
* <b>Type literal mappings:</b><br>
107117
* You dan define any 1-to-1 type mappings via global properties. This is useful when e.g. a 3rd party type is referenced.
108118
* Note, SharedType will still try to resolve the type and emit it with the mapped name.
@@ -138,18 +148,17 @@
138148
/**
139149
* <p>
140150
* Configure whether to include fields, record components, accessors, or constants in a type.
141-
* All included by default.
142151
* </p>
143152
* <p>
144153
* To exclude a particular component, use {@link Ignore}.
145-
* Fields and accessors effectively with the same name will be merged.
154+
* Fields and accessors with the same name and type will be merged.
146155
* </p>
147156
*
148157
* @see ComponentType#FIELDS
149158
* @see ComponentType#ACCESSORS
150159
* @see ComponentType#CONSTANTS
151160
*/
152-
ComponentType[] includes() default {ComponentType.FIELDS, ComponentType.ACCESSORS, ComponentType.CONSTANTS};
161+
ComponentType[] includes() default {ComponentType.FIELDS, ComponentType.ACCESSORS};
153162

154163
/**
155164
* Java fields have to reside in a class, which provides a natural namespace.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public final class Constants {
3131
public static final ConcreteTypeInfo BOXED_INT_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.Integer", "Integer");
3232
public static final ConcreteTypeInfo BOXED_LONG_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.Long", "Long");
3333
public static final ConcreteTypeInfo BOXED_SHORT_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.Short", "Short");
34+
public static final ConcreteTypeInfo BIG_DECIMAL_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.math.BigDecimal", "BigDecimal");
35+
public static final ConcreteTypeInfo BIG_INTEGER_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.math.BigInteger", "BigInteger");
36+
3437
public static final ConcreteTypeInfo STRING_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.String", "String");
3538
public static final ConcreteTypeInfo VOID_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.Void", "Void");
3639
public static final ConcreteTypeInfo OBJECT_TYPE_INFO = ConcreteTypeInfo.ofPredefined("java.lang.Object", "Object");

it/java17/src/main/java/online/sharedtype/it/java17/JavaRecord.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@
99

1010
import java.util.Collection;
1111
import java.util.List;
12-
import java.util.Map;
1312
import java.util.Set;
1413

15-
@SharedType(constantNamespaced = SharedType.OptionalBool.FALSE)
14+
@SharedType(
15+
constantNamespaced = SharedType.OptionalBool.FALSE,
16+
includes = {
17+
SharedType.ComponentType.CONSTANTS,
18+
SharedType.ComponentType.FIELDS,
19+
SharedType.ComponentType.ACCESSORS,
20+
}
21+
)
1622
public record JavaRecord<T>(
1723
String string,
1824
byte primitiveByte,

it/java8/src/main/java/online/sharedtype/it/java8/MyConstants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import online.sharedtype.SharedType;
44

5-
@SharedType(constantNamespaced = SharedType.OptionalBool.FALSE)
5+
@SharedType(constantNamespaced = SharedType.OptionalBool.FALSE, includes = SharedType.ComponentType.CONSTANTS)
66
final class MyConstants {
77
private static final float FLOAT_VALUE = 1.888f;
88
private static final Long LONG_VALUE = 999L;
99
}
1010

11-
@SharedType
11+
@SharedType(includes = SharedType.ComponentType.CONSTANTS)
1212
enum MyEnumConstants {
1313
;
1414
private static final int INT_VALUE = 1;

processor/src/main/java/online/sharedtype/processor/context/Context.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import online.sharedtype.processor.support.annotation.VisibleForTesting;
77

88
import javax.annotation.processing.ProcessingEnvironment;
9+
import javax.lang.model.element.AnnotationMirror;
910
import javax.lang.model.element.Element;
1011
import javax.lang.model.element.ElementKind;
1112
import javax.lang.model.element.TypeElement;
@@ -100,6 +101,16 @@ public boolean isIgnored(Element element) {
100101
return false;
101102
}
102103

104+
public boolean isOptionalAnnotated(Element element) {
105+
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
106+
String annoTypeQualifiedName = annotationMirror.getAnnotationType().toString();
107+
if (props.getOptionalAnnotations().contains(annoTypeQualifiedName)) {
108+
return true;
109+
}
110+
}
111+
return false;
112+
}
113+
103114
public boolean isOptionalType(String qualifiedName) {
104115
return props.getOptionalContainerTypes().contains(qualifiedName);
105116
}

processor/src/main/java/online/sharedtype/processor/context/Props.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public final class Props {
2020
private final Typescript typescript;
2121
private final Rust rust;
2222

23-
private final Set<Class<? extends Annotation>> optionalAnnotations;
23+
private final Set<String> optionalAnnotations;
2424
private final Set<String> optionalContainerTypes;
2525
private final Set<String> accessorGetterPrefixes;
2626
private final Set<String> arraylikeTypeQualifiedNames;

processor/src/main/java/online/sharedtype/processor/context/PropsFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static Props loadProps(@Nullable Path userPropertiesFile) {
4545
private static Props loadProps(Properties properties) {
4646
return Props.builder()
4747
.targets(parseEnumSet(properties, "sharedtype.targets", OutputTarget.class, OutputTarget::valueOf))
48-
.optionalAnnotations(parseClassSet(properties, "sharedtype.optional-annotations"))
48+
.optionalAnnotations(splitArray(properties.getProperty("sharedtype.optional-annotations")))
4949
.optionalContainerTypes(splitArray(properties.getProperty("sharedtype.optional-container-types")))
5050
.accessorGetterPrefixes(splitArray(properties.getProperty("sharedtype.accessor.getter-prefixes")))
5151
.arraylikeTypeQualifiedNames(splitArray(properties.getProperty("sharedtype.array-like-types")))

processor/src/main/java/online/sharedtype/processor/parser/ClassTypeDefParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private List<FieldComponentInfo> parseComponents(TypeElement typeElement, Config
136136
FieldComponentInfo fieldInfo = FieldComponentInfo.builder()
137137
.name(tuple.b())
138138
.modifiers(element.getModifiers())
139-
.optional(ctx.getProps().getOptionalAnnotations().stream().anyMatch(e -> element.getAnnotation(e) != null))
139+
.optional(ctx.isOptionalAnnotated(element))
140140
.type(typeInfoParser.parse(element.asType(), typeContext))
141141
.build();
142142
fields.add(fieldInfo);

processor/src/main/java/online/sharedtype/processor/parser/ConstantTypeDefParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ private Object parseConstantValue(Element fieldElement, TypeElement ctxTypeEleme
104104
return ((LiteralTree) valueTree).getValue();
105105
} else {
106106
throw new SharedTypeException(String.format("Only literal value is supported for constant field." +
107-
" Field: %s in %s", fieldElement.getSimpleName(), ctxTypeElement.getQualifiedName()));
107+
" Field: %s in %s. Consider use @SharedType.Ignore to ignore this field or exclude constants generation for this type.",
108+
fieldElement.getSimpleName(), ctxTypeElement.getQualifiedName()));
108109
}
109110
}
110111
}

processor/src/main/java/online/sharedtype/processor/parser/type/TypeContext.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import lombok.Builder;
44
import lombok.EqualsAndHashCode;
55
import lombok.Getter;
6+
import lombok.ToString;
67
import online.sharedtype.processor.domain.DependingKind;
78
import online.sharedtype.processor.domain.TypeDef;
89

910
@EqualsAndHashCode
1011
@Builder
1112
@Getter
13+
@ToString
1214
public final class TypeContext {
1315
/** The context type definition. */
1416
private final TypeDef typeDef;

0 commit comments

Comments
 (0)