Menu:
Add sharedtype dependency, the annotation @SharedType
is only used at compile time on source code:
<dependency>
<groupId>online.sharedtype</groupId>
<artifactId>sharedtype</artifactId>
<version>${sharedtype.version}</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
Add Maven properties:
<properties>
<sharedtype.compilerArg /> <!-- Placeholder -->
<sharedtype.version>0.2.0</sharedtype.version>
</properties>
Setup annotation processing:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>online.sharedtype</groupId>
<artifactId>sharedtype-ap</artifactId>
<version>${sharedtype.version}</version>
</path>
</annotationProcessorPaths>
<showWarnings>true</showWarnings> <!-- Show annotation processing info log -->
<compilerArgs>
<arg>${sharedtype.compilerArg}</arg> <!-- supplied as a property from cmd -->
</compilerArgs>
</configuration>
</plugin>
Annotate on a class:
@SharedType
record User(String name, int age, String email) {}
Execute annotation processing:
- maven:
./mvnw clean compile "-Dsharedtype.compilerArg=-proc:only"
By default, below code will be generated:
export interface User {
name: string;
age: number;
email: string;
}
By default, the file sharedtype.properties
on current cmd path will be picked up.
You can customize the path by config maven-compiler-plugin
:
<compilerArgs>
<arg>-Asharedtype.propsFile=${your.properties.path}</arg>
</compilerArgs>
See Default Properties for details.
See Javadoc on @SharedType for details.
- Current design only retain
@SharedType
on source level. That means they are not visible if it is in a dependency jar during its dependent's compilation. You have to execute the annotation processing on the same classpath with source code. For multiple module builds, a workaround is to execute on every module. - Non-static inner classes are not supported. Instance class may refer to its enclosing class's generic type without the type declaration on its own, which could break the generated code. Later version of SharedType may loosen this limitation.