Support JPMS.
ci/woodpecker/push/woodpecker Pipeline was successful Details

Since that seems to be incompatible with manifold, that has been removed.
Contains breaking changes!
This commit is contained in:
Johannes Frohnmeyer 2023-01-21 15:01:54 +01:00
parent f8a7c43559
commit 6805ff6b6f
Signed by: Johannes
GPG Key ID: E76429612C2929F4
35 changed files with 244 additions and 237 deletions

View File

@ -1,5 +1,5 @@
group = "io.gitlab.jfronny.gson"
version = "1.2-SNAPSHOT"
version = "1.3-SNAPSHOT"
subprojects {
group = rootProject.group

View File

@ -0,0 +1,3 @@
module io.gitlab.jfronny.gson.compile.annotations {
exports io.gitlab.jfronny.gson.compile.annotations;
}

View File

@ -9,8 +9,8 @@ repositories {
}
dependencies {
implementation("io.gitlab.jfronny:commons:1.0-SNAPSHOT")
api("io.gitlab.jfronny:commons-gson:1.0-SNAPSHOT")
implementation("io.gitlab.jfronny:commons:1.1-SNAPSHOT")
api("io.gitlab.jfronny:commons-gson:1.1-SNAPSHOT")
}
publishing {

View File

@ -0,0 +1,7 @@
module io.gitlab.jfronny.gson.compile.core {
requires io.gitlab.jfronny.commons.gson;
requires io.gitlab.jfronny.gson;
requires io.gitlab.jfronny.commons;
exports io.gitlab.jfronny.gson.compile.core;
exports io.gitlab.jfronny.gson.compile.util;
}

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.gson;
package io.gitlab.jfronny.gson.compile.example.manifold;
import io.gitlab.jfronny.gson.compile.annotations.GSerializable;

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.gson;
package io.gitlab.jfronny.gson.compile.example;
import io.gitlab.jfronny.gson.annotations.SerializedName;
import io.gitlab.jfronny.gson.compile.annotations.*;

View File

@ -1,4 +1,4 @@
package io.gitlab.jfronny.gson;
package io.gitlab.jfronny.gson.compile.example;
import io.gitlab.jfronny.gson.compile.annotations.GSerializable;

View File

@ -0,0 +1,5 @@
module io.gitlab.jfronny.gson.compile.example {
requires static io.gitlab.jfronny.gson.compile.annotations;
requires io.gitlab.jfronny.gson;
exports io.gitlab.jfronny.gson.compile.example;
}

View File

@ -1,7 +1,6 @@
plugins {
`java-library`
id("jf.maven-publish")
id("jf.manifold")
}
repositories {
@ -11,7 +10,7 @@ repositories {
dependencies {
implementation("org.jetbrains:annotations:24.0.0")
implementation("io.gitlab.jfronny:commons:1.0-SNAPSHOT")
implementation("io.gitlab.jfronny:commons:1.1-SNAPSHOT")
}
publishing {

View File

@ -31,14 +31,14 @@ public abstract class AbstractProcessor2 extends AbstractProcessor {
} catch (ClassNotFoundException e) {
hasManifold = false;
}
options = processingEnv.options;
message = processingEnv.messager;
filer = processingEnv.filer;
elements = processingEnv.elementUtils;
types = processingEnv.typeUtils;
sourceVersion = processingEnv.sourceVersion;
locale = processingEnv.locale;
isPreviewEnabled = processingEnv.isPreviewEnabled;
options = processingEnv.getOptions();
message = processingEnv.getMessager();
filer = processingEnv.getFiler();
elements = processingEnv.getElementUtils();
types = processingEnv.getTypeUtils();
sourceVersion = processingEnv.getSourceVersion();
locale = processingEnv.getLocale();
isPreviewEnabled = processingEnv.isPreviewEnabled();
valueCreator = new ValueCreator(processingEnv);
}

View File

@ -10,11 +10,11 @@ public class TypeHelper {
public static boolean isComplexType(TypeMirror type, Types typeUtils) {
Element element = typeUtils.asElement(type);
if (!(element instanceof TypeElement typeElement)) return false;
return !typeElement.typeParameters.isEmpty;
return !typeElement.getTypeParameters().isEmpty();
}
public static boolean isGenericType(TypeMirror type) {
return type.kind == TypeKind.TYPEVAR;
return type.getKind() == TypeKind.TYPEVAR;
}
public static List<? extends TypeMirror> getGenericTypes(TypeMirror type) {
@ -23,8 +23,8 @@ public class TypeHelper {
return Collections.emptyList();
}
ArrayList<TypeMirror> result = new ArrayList<>();
for (TypeMirror argType : declaredType.typeArguments) {
if (argType.kind == TypeKind.TYPEVAR) {
for (TypeMirror argType : declaredType.getTypeArguments()) {
if (argType.getKind() == TypeKind.TYPEVAR) {
result.add(argType);
}
}
@ -52,10 +52,10 @@ public class TypeHelper {
public static boolean isInstance(DeclaredType type, String parentClassName, Types typeUtils) {
if (type == null) return false;
TypeElement element = (TypeElement) type.asElement();
for (TypeMirror interfaceType : element.interfaces) {
for (TypeMirror interfaceType : element.getInterfaces()) {
if (typeUtils.erasure(interfaceType).toString().equals(parentClassName)) return true;
}
TypeMirror superclassType = element.superclass;
TypeMirror superclassType = element.getSuperclass();
if (superclassType != null) {
if (typeUtils.erasure(superclassType).toString().equals(parentClassName)) {
return true;
@ -67,7 +67,7 @@ public class TypeHelper {
}
public static String getDefaultValue(TypeMirror type) {
return switch (type.kind) {
return switch (type.getKind()) {
case BYTE, SHORT, INT, LONG, FLOAT, CHAR, DOUBLE -> "0";
case BOOLEAN -> "false";
default -> "null";

View File

@ -46,7 +46,7 @@ public sealed interface ConstructionSource {
@Override
public TypeElement getTargetClass() {
return targetClass != null ? targetClass : (targetClass = (TypeElement) constructor.enclosingElement);
return targetClass != null ? targetClass : (targetClass = (TypeElement) constructor.getEnclosingElement());
}
@Override
@ -82,7 +82,7 @@ public sealed interface ConstructionSource {
@Override
public TypeElement getTargetClass() {
return targetClass != null ? targetClass : (targetClass = (TypeElement) types.asElement(method.returnType));
return targetClass != null ? targetClass : (targetClass = (TypeElement) types.asElement(method.getReturnType()));
}
@Override
@ -135,7 +135,7 @@ public sealed interface ConstructionSource {
@Override
public TypeElement getTargetClass() {
return targetClass != null ? targetClass : (targetClass = (TypeElement) types.asElement(getBuildMethod().returnType));
return targetClass != null ? targetClass : (targetClass = (TypeElement) types.asElement(getBuildMethod().getReturnType()));
}
@Override
@ -150,12 +150,12 @@ public sealed interface ConstructionSource {
@Override
public TypeElement getBuilderClass() {
return builderClass != null ? builderClass : (builderClass = (TypeElement) constructor.enclosingElement);
return builderClass != null ? builderClass : (builderClass = (TypeElement) constructor.getEnclosingElement());
}
@Override
public ExecutableElement getBuildMethod() {
return buildMethod != null ? buildMethod : (buildMethod = findBuildMethod((TypeElement) constructor.enclosingElement));
return buildMethod != null ? buildMethod : (buildMethod = findBuildMethod((TypeElement) constructor.getEnclosingElement()));
}
}
@ -173,7 +173,7 @@ public sealed interface ConstructionSource {
@Override
public TypeElement getTargetClass() {
return targetClass != null ? targetClass : (targetClass = (TypeElement) types.asElement(getBuildMethod().returnType));
return targetClass != null ? targetClass : (targetClass = (TypeElement) types.asElement(getBuildMethod().getReturnType()));
}
@Override
@ -188,12 +188,12 @@ public sealed interface ConstructionSource {
@Override
public TypeElement getBuilderClass() {
return builderClass != null ? builderClass : (builderClass = (TypeElement) types.asElement(method.returnType));
return builderClass != null ? builderClass : (builderClass = (TypeElement) types.asElement(method.getReturnType()));
}
@Override
public ExecutableElement getBuildMethod() {
return buildMethod != null ? buildMethod : (buildMethod = findBuildMethod((TypeElement) types.asElement(method.returnType)));
return buildMethod != null ? buildMethod : (buildMethod = findBuildMethod((TypeElement) types.asElement(method.getReturnType())));
}
}
@ -237,7 +237,7 @@ public sealed interface ConstructionSource {
ExecutableElement candidate = null;
boolean foundMultipleCandidates = false;
boolean isCandidateReasonableBuilderMethodName = false;
for (ExecutableElement method : ElementFilter.methodsIn(builderClass.enclosedElements)) {
for (ExecutableElement method : ElementFilter.methodsIn(builderClass.getEnclosedElements())) {
if (isPossibleBuilderMethod(method, builderClass)) {
if (candidate == null) {
candidate = method;
@ -263,10 +263,10 @@ public sealed interface ConstructionSource {
}
// Last try, check to see if the immediate parent class makes sense.
{
Element candidate = builderClass.enclosingElement;
if (candidate.kind == ElementKind.CLASS) {
for (ExecutableElement method : ElementFilter.methodsIn(builderClass.enclosedElements)) {
if (method.returnType.equals(candidate.asType()) && method.parameters.isEmpty) {
Element candidate = builderClass.getEnclosingElement();
if (candidate.getKind() == ElementKind.CLASS) {
for (ExecutableElement method : ElementFilter.methodsIn(builderClass.getEnclosedElements())) {
if (method.getReturnType().equals(candidate.asType()) && method.getParameters().isEmpty()) {
return method;
}
}
@ -283,10 +283,10 @@ public sealed interface ConstructionSource {
*/
@ApiStatus.Internal
static boolean isPossibleBuilderMethod(ExecutableElement method, TypeElement builderClass) {
if (!method.parameters.isEmpty) return false;
TypeMirror returnType = method.returnType;
if (returnType.kind == TypeKind.VOID) return false;
if (returnType.kind.isPrimitive) return false;
if (!method.getParameters().isEmpty()) return false;
TypeMirror returnType = method.getReturnType();
if (returnType.getKind() == TypeKind.VOID) return false;
if (returnType.getKind().isPrimitive()) return false;
if (returnType.equals(builderClass.asType())) return false;
String returnTypeName = returnType.toString();
return !(returnTypeName.startsWith("java.") || returnTypeName.startsWith("javax.") || returnTypeName.startsWith("android."));
@ -294,7 +294,7 @@ public sealed interface ConstructionSource {
@ApiStatus.Internal
static boolean isReasonableBuilderMethodName(ExecutableElement method) {
String methodName = method.simpleName.toString().toLowerCase(Locale.ROOT);
String methodName = method.getSimpleName().toString().toLowerCase(Locale.ROOT);
return methodName.startsWith("build") || methodName.startsWith("create");
}
}

View File

@ -21,20 +21,20 @@ public class Properties extends DelegateList.Simple<Property<?>> {
public static Properties build(Types types, ConstructionSource constructionSource, boolean isStatic) throws ElementException {
Builder builder = new Builder(types, isStatic);
// constructor params
if (constructionSource.constructionElement != null) {
for (VariableElement param : constructionSource.constructionElement.parameters) {
if (constructionSource.getConstructionElement() != null) {
for (VariableElement param : constructionSource.getConstructionElement().getParameters()) {
builder.addConstructorParam(param);
}
}
if (constructionSource instanceof ConstructionSource.Builder csb) {
var builderClass = csb.builderClass;
for (ExecutableElement method : ElementFilter.methodsIn(builderClass.enclosedElements)) {
var builderClass = csb.getBuilderClass();
for (ExecutableElement method : ElementFilter.methodsIn(builderClass.getEnclosedElements())) {
builder.addBuilderParam(method);
}
}
var targetClass = constructionSource.targetClass;
var targetClass = constructionSource.getTargetClass();
builder.addFieldsAndAccessors(targetClass);
return builder.build();
@ -94,32 +94,32 @@ public class Properties extends DelegateList.Simple<Property<?>> {
public void addFieldsAndAccessors(TypeElement targetClass) {
// accessors
for (ExecutableElement method : ElementFilter.methodsIn(targetClass.enclosedElements)) {
for (ExecutableElement method : ElementFilter.methodsIn(targetClass.getEnclosedElements())) {
addGetter(targetClass, method);
addSetter(targetClass, method);
}
// fields
for (VariableElement field : ElementFilter.fieldsIn(targetClass.enclosedElements)) {
for (VariableElement field : ElementFilter.fieldsIn(targetClass.getEnclosedElements())) {
addField(field);
}
for (TypeMirror superInterface : targetClass.interfaces) {
for (TypeMirror superInterface : targetClass.getInterfaces()) {
addFieldsAndAccessors((TypeElement) types.asElement(superInterface));
}
TypeMirror superclass = targetClass.superclass;
if (superclass.kind != TypeKind.NONE && !superclass.toString().equals("java.lang.Object")) {
TypeMirror superclass = targetClass.getSuperclass();
if (superclass.getKind() != TypeKind.NONE && !superclass.toString().equals("java.lang.Object")) {
addFieldsAndAccessors((TypeElement) types.asElement(superclass));
}
}
public void addGetter(TypeElement classElement, ExecutableElement method) {
Set<Modifier> modifiers = method.modifiers;
Set<Modifier> modifiers = method.getModifiers();
if (modifiers.contains(Modifier.PRIVATE)
|| (isStatic != modifiers.contains(Modifier.STATIC))
|| method.returnType.kind == TypeKind.VOID
|| !method.parameters.isEmpty
|| method.getReturnType().getKind() == TypeKind.VOID
|| !method.getParameters().isEmpty()
|| isMethodToSkip(classElement, method)) {
return;
}
@ -127,20 +127,20 @@ public class Properties extends DelegateList.Simple<Property<?>> {
}
public void addSetter(TypeElement classElement, ExecutableElement method) {
Set<Modifier> modifiers = method.modifiers;
Set<Modifier> modifiers = method.getModifiers();
if (modifiers.contains(Modifier.PRIVATE)
|| (isStatic != modifiers.contains(Modifier.STATIC))
|| method.returnType.kind != TypeKind.VOID
|| method.parameters.size() != 1
|| method.getReturnType().getKind() != TypeKind.VOID
|| method.getParameters().size() != 1
|| isMethodToSkip(classElement, method)
|| !method.simpleName.toString().startsWith("set")) {
|| !method.getSimpleName().toString().startsWith("set")) {
return;
}
setters.add(new Property.Setter(method));
}
public void addField(VariableElement field) {
Set<Modifier> modifiers = field.modifiers;
Set<Modifier> modifiers = field.getModifiers();
if (isStatic != modifiers.contains(Modifier.STATIC)) return;
fields.add(new Property.Field(field));
}
@ -152,7 +152,7 @@ public class Properties extends DelegateList.Simple<Property<?>> {
}
public void addBuilderParam(ExecutableElement method) {
if (method.getParameters().size() == 1 && method.simpleName.toString().startsWith("set")) {
if (method.getParameters().size() == 1 && method.getSimpleName().toString().startsWith("set")) {
Property.Setter prop = new Property.Setter(method);
builderParams.add(prop);
params.add(prop);
@ -198,7 +198,7 @@ public class Properties extends DelegateList.Simple<Property<?>> {
private void removeExtraFields() {
fields.removeIf(field -> {
Set<Modifier> modifiers = field.element.modifiers;
Set<Modifier> modifiers = field.element.getModifiers();
return modifiers.contains(Modifier.PRIVATE) || modifiers.contains(Modifier.TRANSIENT);
});
}
@ -206,19 +206,19 @@ public class Properties extends DelegateList.Simple<Property<?>> {
private void removeGettersForTransientFields() {
getters.removeIf(getter -> {
Property<?> field = findName(fields, getter);
return field != null && field.element.modifiers.contains(Modifier.TRANSIENT);
return field != null && field.element.getModifiers().contains(Modifier.TRANSIENT);
});
}
private void removeSettersForTransientFields() {
getters.removeIf(getter -> {
Property<?> field = findName(fields, getter);
return field != null && field.element.modifiers.contains(Modifier.TRANSIENT);
return field != null && field.element.getModifiers().contains(Modifier.TRANSIENT);
});
}
private boolean isMethodToSkip(TypeElement classElement, ExecutableElement method) {
String name = method.simpleName.toString();
String name = method.getSimpleName().toString();
if (METHODS_TO_SKIP.contains(name)) {
return true;
}
@ -231,10 +231,10 @@ public class Properties extends DelegateList.Simple<Property<?>> {
List<AnnotationMirror> annotations = null;
for (Property<?> name : properties) {
if (name == null) continue;
if (!name.annotations.isEmpty) {
if (annotations == null) annotations = new ArrayList<>(name.annotations);
if (!name.getAnnotations().isEmpty()) {
if (annotations == null) annotations = new ArrayList<>(name.getAnnotations());
else {
for (AnnotationMirror annotation : name.annotations) {
for (AnnotationMirror annotation : name.getAnnotations()) {
if (annotations.contains(annotation)) {
throw new ElementException("Duplicate annotation " + annotation + " found on " + name, name.element);
} else annotations.add(annotation);
@ -264,7 +264,7 @@ public class Properties extends DelegateList.Simple<Property<?>> {
}
public static <N extends Property<?>> N findName(List<N> names, Property<?> property) {
return names.stream().filter(n -> n.name.equals(property.name)).findFirst().orElse(null);
return names.stream().filter(n -> n.getName().equals(property.getName())).findFirst().orElse(null);
}
public static boolean containsName(List<? extends Property<?>> properties, Property<?> property) {
@ -272,6 +272,7 @@ public class Properties extends DelegateList.Simple<Property<?>> {
}
private static boolean isKotlinClass(TypeElement element) {
return element.annotationMirrors.stream().anyMatch(m -> m.annotationType.toString().equals("kotlin.Metadata"));
return element.getAnnotationMirrors().stream()
.anyMatch(m -> m.getAnnotationType().toString().equals("kotlin.Metadata"));
}
}

View File

@ -13,7 +13,7 @@ public abstract sealed class Property<T extends Element> {
public Property(T element) {
this.element = element;
this.annotations = element.annotationMirrors;
this.annotations = element.getAnnotationMirrors();
}
public T getElement() {
@ -26,7 +26,7 @@ public abstract sealed class Property<T extends Element> {
* @see #getCallableName()
*/
public String getName() {
return element.simpleName.toString();
return element.getSimpleName().toString();
}
/**
@ -113,7 +113,7 @@ public abstract sealed class Property<T extends Element> {
@Override
public TypeMirror getType() {
return element.returnType;
return element.getReturnType();
}
@ApiStatus.Internal
@ -123,7 +123,7 @@ public abstract sealed class Property<T extends Element> {
private String getBeanPrefix() {
String name = super.getName();
if (element.returnType.kind == TypeKind.BOOLEAN) {
if (element.getReturnType().getKind() == TypeKind.BOOLEAN) {
if (name.length() > BEAN_PREFIX_BOOL.length() && name.startsWith(BEAN_PREFIX_BOOL)) {
return BEAN_PREFIX_BOOL;
}
@ -151,7 +151,7 @@ public abstract sealed class Property<T extends Element> {
public Setter(ExecutableElement method) {
super(method.getParameters().get(0));
this.method = method;
name = Character.toLowerCase(method.simpleName.toString().charAt(3)) + method.simpleName.toString().substring(4);
name = Character.toLowerCase(method.getSimpleName().toString().charAt(3)) + method.getSimpleName().toString().substring(4);
}
@Override
@ -161,7 +161,7 @@ public abstract sealed class Property<T extends Element> {
@Override
public String getCallableName() {
return method.simpleName.toString();
return method.getSimpleName().toString();
}
}
}

View File

@ -14,11 +14,11 @@ public class Value {
public Value(ProcessingEnvironment env, ConstructionSource constructionSource) {
this.env = env;
this.constructionSource = constructionSource;
this.element = constructionSource.targetClass;
this.element = constructionSource.getTargetClass();
}
public Properties getProperties() throws ElementException {
return properties != null ? properties : (properties = Properties.build(env.typeUtils, constructionSource, constructionSource.isStatic));
return properties != null ? properties : (properties = Properties.build(env.getTypeUtils(), constructionSource, constructionSource.isStatic()));
}
public ConstructionSource getConstructionSource() {

View File

@ -60,7 +60,7 @@ public class ValueCreator {
*/
public Value fromBuilderConstructor(ExecutableElement constructor) {
checkKind(constructor, ElementKind.CONSTRUCTOR);
return create(new ConstructionSource.BuilderConstructor(env.typeUtils, constructor));
return create(new ConstructionSource.BuilderConstructor(env.getTypeUtils(), constructor));
}
/**
@ -73,7 +73,7 @@ public class ValueCreator {
*/
public Value fromFactory(ExecutableElement factory) {
checkKind(factory, ElementKind.METHOD);
return create(new ConstructionSource.Factory(env.typeUtils, factory));
return create(new ConstructionSource.Factory(env.getTypeUtils(), factory));
}
/**
@ -87,7 +87,7 @@ public class ValueCreator {
*/
public Value fromBuilderFactory(ExecutableElement builderFactory) {
checkKind(builderFactory, ElementKind.METHOD);
return create(new ConstructionSource.BuilderFactory(env.typeUtils, builderFactory));
return create(new ConstructionSource.BuilderFactory(env.getTypeUtils(), builderFactory));
}
/**
@ -120,27 +120,27 @@ public class ValueCreator {
private static ExecutableElement findConstructorOrFactory(TypeElement klazz) throws ElementException {
ExecutableElement noArgConstructor = null;
List<ExecutableElement> constructors = ElementFilter.constructorsIn(klazz.enclosedElements);
List<ExecutableElement> constructors = ElementFilter.constructorsIn(klazz.getEnclosedElements());
if (constructors.size() == 1) {
ExecutableElement constructor = constructors[0];
if (constructor.parameters.isEmpty) {
ExecutableElement constructor = constructors.get(0);
if (constructor.getParameters().isEmpty()) {
noArgConstructor = constructor;
constructors.remove(0);
}
}
for (ExecutableElement method : ElementFilter.methodsIn(klazz.enclosedElements)) {
Set<Modifier> modifiers = method.modifiers;
for (ExecutableElement method : ElementFilter.methodsIn(klazz.getEnclosedElements())) {
Set<Modifier> modifiers = method.getModifiers();
if (modifiers.contains(Modifier.STATIC)
&& !modifiers.contains(Modifier.PRIVATE)
&& method.returnType.equals(klazz.asType())) {
&& method.getReturnType().equals(klazz.asType())) {
constructors.add(method);
}
}
if (constructors.isEmpty) {
if (constructors.isEmpty()) {
if (noArgConstructor != null) return noArgConstructor;
else throw new ElementException("Lacking constructor or factory method", klazz);
}
if (constructors.size() == 1) return constructors[0];
if (constructors.size() == 1) return constructors.get(0);
if (noArgConstructor != null) constructors.add(noArgConstructor);
if (preferAnnotation != null) {
List<ExecutableElement> preferred = new ArrayList<>();
@ -149,7 +149,7 @@ public class ValueCreator {
preferred.add(constructor);
}
}
if (preferred.size() == 1) return preferred[0];
if (preferred.size() == 1) return preferred.get(0);
}
List<ElementException.Message> messages = new ArrayList<>();
messages.add(new ElementException.Message("More than one constructor or factory method found.", klazz));
@ -158,7 +158,7 @@ public class ValueCreator {
}
private static void checkKind(Element element, ElementKind kind) {
if (element.kind != kind) {
if (element.getKind() != kind) {
throw new IllegalArgumentException("Expected " + kind + " but got: " + element);
}
}

View File

@ -0,0 +1,7 @@
module io.gitlab.jfronny.gson.compile.processor.core {
requires java.compiler;
requires static org.jetbrains.annotations;
requires io.gitlab.jfronny.commons;
exports io.gitlab.jfronny.gson.compile.processor.core;
exports io.gitlab.jfronny.gson.compile.processor.core.value;
}

View File

@ -1,7 +1,6 @@
plugins {
`java-library`
id("jf.maven-publish")
id("jf.manifold")
}
repositories {
@ -13,7 +12,7 @@ dependencies {
implementation(project(":gson-compile-processor-core"))
implementation(project(":gson-compile-annotations"))
implementation("org.jetbrains:annotations:24.0.0")
implementation("io.gitlab.jfronny:commons:1.0-SNAPSHOT")
implementation("io.gitlab.jfronny:commons:1.1-SNAPSHOT")
implementation("com.squareup:javapoet:1.13.0")
}

View File

@ -1,22 +0,0 @@
package extensions.com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ClassName;
import manifold.ext.rt.api.Extension;
import manifold.ext.rt.api.This;
import java.util.List;
@Extension
public class ClassNameExt {
public static String getSimpleName(@This ClassName thiz) {
return thiz.simpleName();
}
public static String getPackageName(@This ClassName thiz) {
return thiz.packageName();
}
public static List<String> getSimpleNames(@This ClassName thiz) {
return thiz.simpleNames();
}
}

View File

@ -8,8 +8,8 @@ public class Cl {
public static final ClassName GSON_ELEMENT = ClassName.get("io.gitlab.jfronny.gson", "JsonElement");
public static final ClassName GSON_WRITER = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonWriter");
public static final ClassName GSON_READER = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonReader");
public static final ClassName GSON_TREE_READER = ClassName.get("io.gitlab.jfronny.gson.internal.bind", "JsonTreeReader");
public static final ClassName GSON_TREE_WRITER = ClassName.get("io.gitlab.jfronny.gson.internal.bind", "JsonTreeWriter");
public static final ClassName GSON_TREE_READER = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonTreeReader");
public static final ClassName GSON_TREE_WRITER = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonTreeWriter");
public static final ClassName JSON_ADAPTER = ClassName.get("io.gitlab.jfronny.gson.annotations", "JsonAdapter");
public static final ClassName TYPE_TOKEN = ClassName.get("io.gitlab.jfronny.gson.reflect", "TypeToken");
public static final ClassName GSON_TOKEN = ClassName.get("io.gitlab.jfronny.gson.stream", "JsonToken");
@ -17,7 +17,7 @@ public class Cl {
public static final ClassName GSON_SYNTAX_EXCEPTION = ClassName.get("io.gitlab.jfronny.gson", "JsonSyntaxException");
public static final ClassName GCOMMENT = ClassName.get("io.gitlab.jfronny.gson.compile.annotations", "GComment");
public static final ClassName GISO8601UTILS = ClassName.get("io.gitlab.jfronny.gson.internal.bind.util", "ISO8601Utils");
public static final ClassName GISO8601UTILS = ClassName.get("io.gitlab.jfronny.gson.util", "ISO8601Utils");
public static final ClassName CCORE = ClassName.get("io.gitlab.jfronny.gson.compile.core", "CCore");
public static final ClassName MANIFOLD_EXTENSION = ClassName.get("manifold.ext.rt.api", "Extension");

View File

@ -48,9 +48,9 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
// Gather all serializable types
for (TypeElement annotation : annotations) {
for (Element element : roundEnvironment.getElementsAnnotatedWith(annotation)) {
for (AnnotationMirror mirror : element.annotationMirrors) {
for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
try {
if (mirror.annotationType.toString().equals(GSerializable.class.getCanonicalName())) {
if (mirror.getAnnotationType().toString().equals(GSerializable.class.getCanonicalName())) {
var bld = new Object() {
TypeMirror with = null;
TypeMirror builder = null;
@ -59,27 +59,27 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
Boolean isStatic = null;
};
elements.getElementValuesWithDefaults(mirror).forEach((executableElement, value) -> {
String name = executableElement.simpleName.toString();
String name = executableElement.getSimpleName().toString();
switch (name) {
case "with" -> {
if (bld.with != null) throw new IllegalArgumentException("Duplicate annotation parameter: with");
bld.with = (TypeMirror) value.value;
bld.with = (TypeMirror) value.getValue();
}
case "builder" -> {
if (bld.builder != null) throw new IllegalArgumentException("Duplicate annotation parameter: builder");
bld.builder = (TypeMirror) value.value;
bld.builder = (TypeMirror) value.getValue();
}
case "configure" -> {
if (bld.configure != null) throw new IllegalArgumentException("Duplicate annotation parameter: configure");
bld.configure = (TypeMirror) value.value;
bld.configure = (TypeMirror) value.getValue();
}
case "generateAdapter" -> {
if (bld.generateAdapter != null) throw new IllegalArgumentException("Duplicate annotation parameter: generateAdapter");
bld.generateAdapter = (Boolean) value.value;
bld.generateAdapter = (Boolean) value.getValue();
}
case "isStatic" -> {
if (bld.isStatic != null) throw new IllegalArgumentException("Duplicate annotation parameter: isStatic");
bld.isStatic = (Boolean) value.value;
bld.isStatic = (Boolean) value.getValue();
}
default -> throw new IllegalArgumentException("Unexpected annotation parameter: " + name);
}
@ -109,7 +109,7 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
}
}
for (var entry : seen.keySet().stream().collect(Collectors.groupingBy(ClassName::packageName)).entrySet()) {
Map<List<String>, TypeSpec.Builder> known = entry.value.stream()
Map<List<String>, TypeSpec.Builder> known = entry.getValue().stream()
.collect(Collectors.toMap(
ClassName::simpleNames,
seen::get,
@ -132,8 +132,8 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
}
// Add to parent class
for (var entry1 : known.entrySet()) {
if (entry1.key.size() == 1) continue;
find(known, entry1.key.subList(0, entry1.key.size() - 1)).addType(entry1.value.build());
if (entry1.getKey().size() == 1) continue;
find(known, entry1.getKey().subList(0, entry1.getKey().size() - 1)).addType(entry1.getValue().build());
}
// Print
// System.out.println("Got " + known.size() + " classes");
@ -145,8 +145,8 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
// }
// Write top-level classes
for (var entry1 : known.entrySet()) {
if (entry1.key.size() == 1) {
JavaFile javaFile = JavaFile.builder(entry.key, entry1.value.build())
if (entry1.getKey().size() == 1) {
JavaFile javaFile = JavaFile.builder(entry.getKey(), entry1.getValue().build())
.skipJavaLangImports(true)
.indent(" ")
.build();
@ -163,35 +163,35 @@ public class GsonCompileProcessor extends AbstractProcessor2 {
}
private <K, V> V find(Map<List<K>, V> map, List<K> key) {
for (var entry : map.entrySet()) if (entry.key.equals(key)) return entry.value;
for (var entry : map.entrySet()) if (entry.getKey().equals(key)) return entry.getValue();
return null;
}
private void process(SerializableClass toProcess, Set<SerializableClass> other) throws ElementException {
if (seen.containsKey(toProcess.generatedClassName)) return; // Don't process the same class more than once
if (seen.containsKey(toProcess.generatedClassName())) return; // Don't process the same class more than once
TypeName classType = toProcess.typeName;
TypeName classType = toProcess.getTypeName();
List<TypeVariableName> typeVariables = new ArrayList<>();
if (!toProcess.isStatic && classType instanceof ParameterizedTypeName type) {
if (!toProcess.isStatic() && classType instanceof ParameterizedTypeName type) {
for (TypeName argument : type.typeArguments) {
typeVariables.add(TypeVariableName.get(argument.toString()));
}
}
TypeSpec.Builder spec = TypeSpec.classBuilder(toProcess.generatedClassName.simpleName())
.addOriginatingElement(toProcess.classElement)
TypeSpec.Builder spec = TypeSpec.classBuilder(toProcess.generatedClassName().simpleName())
.addOriginatingElement(toProcess.classElement())
.addModifiers(Modifier.PUBLIC)
.addTypeVariables(typeVariables);
seen.put(toProcess.generatedClassName, spec);
seen.put(toProcess.generatedClassName(), spec);
GProcessor processor = toProcess.isStatic ? staticProcessor : instanceProcessor;
GProcessor processor = toProcess.isStatic() ? staticProcessor : instanceProcessor;
if (toProcess.adapter != null) {
processor.generateDelegateToAdapter(spec, classType, toProcess.adapter);
if (toProcess.adapter() != null) {
processor.generateDelegateToAdapter(spec, classType, toProcess.adapter());
} else {
if (toProcess.generateAdapter) {
processor.generateDelegatingAdapter(spec, classType, toProcess.generatedClassName);
if (toProcess.generateAdapter()) {
processor.generateDelegatingAdapter(spec, classType, toProcess.generatedClassName());
}
processor.generateSerialisation(spec, toProcess, typeVariables, other);
}

View File

@ -11,8 +11,8 @@ import javax.lang.model.type.TypeMirror;
public record SerializableClass(TypeElement classElement, ClassName generatedClassName, @Nullable TypeMirror adapter, @Nullable TypeMirror builder, @Nullable TypeMirror configure, boolean generateAdapter, boolean isStatic) {
public static SerializableClass of(TypeElement element, @Nullable TypeMirror with, @Nullable TypeMirror builder, @Nullable TypeMirror configure, boolean generateAdapter, boolean isStatic, boolean manifold) throws ElementException {
ClassName className = ClassName.get(element);
String pkg = manifold ? "gsoncompile.extensions." + className.packageName + '.' + className.simpleNames[0] : className.packageName;
ClassName generatedClassName = ClassName.get(pkg, "GC_" + className.simpleNames[0], className.simpleNames.subList(1, className.simpleNames.size()).toArray(String[]::new));
String pkg = manifold ? "gsoncompile.extensions." + className.packageName() + '.' + className.simpleNames().get(0) : className.packageName();
ClassName generatedClassName = ClassName.get(pkg, "GC_" + className.simpleNames().get(0), className.simpleNames().subList(1, className.simpleNames().size()).toArray(String[]::new));
return new SerializableClass(element, generatedClassName, voidToNull(with), voidToNull(builder), voidToNull(configure), generateAdapter, isStatic).validate();
}

View File

@ -38,7 +38,7 @@ public abstract class AdapterAdapter<T extends AdapterAdapter<T>.Hydrated> exten
if (TypeHelper.isComplexType(type, typeUtils)) {
TypeName typeTokenType = ParameterizedTypeName.get(Cl.TYPE_TOKEN, typeName);
List<? extends TypeMirror> typeParams = TypeHelper.getGenericTypes(type);
if (typeParams.isEmpty) {
if (typeParams.isEmpty()) {
code.add("new $T() {}", typeTokenType);
} else {
code.add("($T) $T.getParameterized($T.class, ", typeTokenType, Cl.TYPE_TOKEN, typeUtils.erasure(type));

View File

@ -37,7 +37,7 @@ public class Adapters {
}
private static void withAdapter(Property<?> prop, TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters, Messager message, Consumer<Adapter<?>.Hydrated> action) {
withAdapter(klazz, code, typeVariables, otherAdapters, prop.type, prop.name, prop.annotations, prop.element, message, action);
withAdapter(klazz, code, typeVariables, otherAdapters, prop.getType(), prop.getName(), prop.getAnnotations(), prop.getElement(), message, action);
}
public static void generateRead(TypeSpec.Builder klazz, CodeBlock.Builder code, List<TypeVariableName> typeVariables, Set<SerializableClass> other, TypeMirror type, String propName, List<? extends AnnotationMirror> annotations, Element sourceElement, Messager message) {

View File

@ -30,7 +30,7 @@ public class ArrayAdapter extends Adapter<ArrayAdapter.Hydrated> {
@Override
protected void afterHydrate() {
type = TypeHelper.asArrayType(super.type);
componentType = type == null ? null : type.componentType;
componentType = type == null ? null : type.getComponentType();
}
@Override
@ -41,7 +41,7 @@ public class ArrayAdapter extends Adapter<ArrayAdapter.Hydrated> {
.beginControlFlow("if ($N == null)", argName)
.addStatement("if (writer.getSerializeNulls()) writer.nullValue()")
.nextControlFlow("else");
generateWrite(code, componentType, argName, componentType.annotationMirrors, () -> code.add(argName));
generateWrite(code, componentType, argName, componentType.getAnnotationMirrors(), () -> code.add(argName));
code.endControlFlow().endControlFlow();
}
@ -51,7 +51,7 @@ public class ArrayAdapter extends Adapter<ArrayAdapter.Hydrated> {
// Coerce
kode.beginControlFlow("if (reader.isLenient() && reader.peek() != $T.BEGIN_ARRAY)", Cl.GSON_TOKEN)
.add("return new $T[] { ", componentType);
generateRead(kode, componentType, argName, componentType.annotationMirrors);
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
kode.add(" };\n").endControlFlow();
kode.addStatement("$T<$T> list = new $T<>()", List.class, componentType, ArrayList.class)
@ -62,7 +62,7 @@ public class ArrayAdapter extends Adapter<ArrayAdapter.Hydrated> {
.addStatement("list.add(null)")
.nextControlFlow("else")
.add("list.add(");
generateRead(kode, componentType, argName, componentType.annotationMirrors);
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
kode.add(");\n")
.endControlFlow()
.endControlFlow()

View File

@ -40,15 +40,15 @@ public class CollectionAdapter extends Adapter<CollectionAdapter.Hydrated> {
type = TypeHelper.asDeclaredType(super.type);
componentType = null;
if (type == null) return;
List<? extends TypeMirror> typeArguments = type.typeArguments;
List<? extends TypeMirror> typeArguments = type.getTypeArguments();
if (typeArguments.size() == 0) {
type = null;
} else {
componentType = typeArguments.get(0);
String ts = TypeHelper.asDeclaredType(typeUtils.erasure(type)).asElement().toString();
for (Map.Entry<Class<?>, List<Class<?>>> entry : SUPPORTED.entrySet()) {
if (entry.key.getCanonicalName().equals(ts)) {
implType = TypeName.get(entry.value[0]);
if (entry.getKey().getCanonicalName().equals(ts)) {
implType = TypeName.get(entry.getValue().get(0));
return;
}
for (Class<?> klazz : entry.getValue()) {
@ -72,7 +72,7 @@ public class CollectionAdapter extends Adapter<CollectionAdapter.Hydrated> {
.beginControlFlow("if ($N == null)", argName)
.addStatement("if (writer.getSerializeNulls()) writer.nullValue()")
.nextControlFlow("else");
generateWrite(code, componentType, argName, componentType.annotationMirrors, () -> code.add(argName));
generateWrite(code, componentType, argName, componentType.getAnnotationMirrors(), () -> code.add(argName));
code.endControlFlow().endControlFlow().addStatement("writer.endArray()");
}
@ -83,7 +83,7 @@ public class CollectionAdapter extends Adapter<CollectionAdapter.Hydrated> {
// Coerce
kode.beginControlFlow("if (reader.isLenient() && reader.peek() != $T.BEGIN_ARRAY)", Cl.GSON_TOKEN)
.add("list.add(");
generateRead(kode, componentType, argName, componentType.annotationMirrors);
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
kode.add(");\n").addStatement("return list").endControlFlow();
kode.addStatement("reader.beginArray()")
@ -93,7 +93,7 @@ public class CollectionAdapter extends Adapter<CollectionAdapter.Hydrated> {
.addStatement("list.add(null)")
.nextControlFlow("else")
.add("list.add(");
generateRead(kode, componentType, argName, componentType.annotationMirrors);
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
kode.add(");\n")
.endControlFlow()
.endControlFlow()

View File

@ -57,10 +57,10 @@ public class DeclaredAdapter extends AdapterAdapter<DeclaredAdapter.Hydrated> {
private static DeclaredType findTypeAdapterClass(List<? extends AnnotationMirror> annotations) {
for (AnnotationMirror annotation : annotations) {
String typeName = annotation.annotationType.toString();
String typeName = annotation.getAnnotationType().toString();
if (typeName.equals(Cl.JSON_ADAPTER.toString())) {
Map<? extends ExecutableElement, ? extends AnnotationValue> elements = annotation.elementValues;
if (!elements.isEmpty) {
Map<? extends ExecutableElement, ? extends AnnotationValue> elements = annotation.getElementValues();
if (!elements.isEmpty()) {
AnnotationValue value = elements.values().iterator().next();
return (DeclaredType) value.getValue();
}

View File

@ -23,7 +23,7 @@ public class EnumAdapter extends Adapter<EnumAdapter.Hydrated> {
tel = null;
DeclaredType declared = TypeHelper.asDeclaredType(type);
if (declared == null) return;
if (declared.asElement().kind != ElementKind.ENUM) return;
if (declared.asElement().getKind() != ElementKind.ENUM) return;
tel = declared;
}

View File

@ -36,20 +36,20 @@ public class MapAdapter extends Adapter<MapAdapter.Hydrated> {
type = TypeHelper.asDeclaredType(super.type);
componentType1 = componentType2 = null;
if (type == null) return;
List<? extends TypeMirror> typeArguments = type.typeArguments;
List<? extends TypeMirror> typeArguments = type.getTypeArguments();
if (typeArguments.size() != 2) {
type = null;
} else {
componentType1 = typeArguments[0];
componentType1 = typeArguments.get(0);
if (!isValidKey(componentType1)) {
type = null;
componentType1 = null;
return;
}
componentType2 = typeArguments[1];
componentType2 = typeArguments.get(1);
String ts = TypeHelper.asDeclaredType(typeUtils.erasure(type)).asElement().toString();
if (Map.class.getCanonicalName().equals(ts)) {
implType = TypeName.get(SUPPORTED[0]);
implType = TypeName.get(SUPPORTED.get(0));
return;
}
for (Class<?> klazz : SUPPORTED) {
@ -66,7 +66,7 @@ public class MapAdapter extends Adapter<MapAdapter.Hydrated> {
private boolean isValidKey(TypeMirror tm) {
if (tm.toString().equals(String.class.getCanonicalName())) return true;
if (tm.toString().equals(UUID.class.getCanonicalName())) return true;
if (unbox(tm).kind.isPrimitive) return true;
if (unbox(tm).getKind().isPrimitive()) return true;
if (isEnum(tm)) return true;
return false;
}
@ -78,8 +78,8 @@ public class MapAdapter extends Adapter<MapAdapter.Hydrated> {
else if (componentType1.toString().equals(UUID.class.getCanonicalName())) {
kode.add("name == null ? null : $T.fromString(name)", UUID.class);
}
else if (unbox(componentType1).kind.isPrimitive) {
kode.add("name == null ? null : " + switch (unbox(componentType1).kind) {
else if (unbox(componentType1).getKind().isPrimitive()) {
kode.add("name == null ? null : " + switch (unbox(componentType1).getKind()) {
case BOOLEAN -> "Boolean.parseBoolean(name)";
case BYTE -> "Byte.parseByte(name)";
case SHORT -> "Short.parseShort(name)";
@ -88,7 +88,7 @@ public class MapAdapter extends Adapter<MapAdapter.Hydrated> {
case CHAR -> "name.length() == 0 ? '\\0' : name.charAt(0)";
case FLOAT -> "Float.parseFloat(name)";
case DOUBLE -> "Double.parseDouble(name)";
default -> throw new IllegalArgumentException("Unsupported primitive: " + unbox(componentType1).kind);
default -> throw new IllegalArgumentException("Unsupported primitive: " + unbox(componentType1).getKind());
});
}
else if (isEnum(componentType1)) {
@ -123,7 +123,7 @@ public class MapAdapter extends Adapter<MapAdapter.Hydrated> {
private boolean isEnum(TypeMirror tm) {
DeclaredType declared = TypeHelper.asDeclaredType(tm);
return declared != null && declared.asElement().kind == ElementKind.ENUM;
return declared != null && declared.asElement().getKind() == ElementKind.ENUM;
}
@Override
@ -144,7 +144,7 @@ public class MapAdapter extends Adapter<MapAdapter.Hydrated> {
.beginControlFlow("if (value$N == null)", argName)
.addStatement("if (writer.getSerializeNulls()) writer.nullValue()")
.nextControlFlow("else");
generateWrite(code, componentType2, "value" + argName, componentType2.annotationMirrors, () -> code.add("value" + argName));
generateWrite(code, componentType2, "value" + argName, componentType2.getAnnotationMirrors(), () -> code.add("value" + argName));
code.endControlFlow().endControlFlow().endControlFlow().addStatement("writer.endObject()");
}
@ -165,7 +165,7 @@ public class MapAdapter extends Adapter<MapAdapter.Hydrated> {
.add("map.put(");
generateConvertKey(kode);
kode.add(", ");
generateRead(kode, componentType2, argName, componentType2.annotationMirrors);
generateRead(kode, componentType2, argName, componentType2.getAnnotationMirrors());
kode.addStatement(")")
.endControlFlow()
.endControlFlow()

View File

@ -26,9 +26,9 @@ public class OtherSerializableAdapter extends AdapterAdapter<OtherSerializableAd
@Override
protected void afterHydrate() {
for (SerializableClass adapter : other) {
if (TypeName.get(adapter.classElement.asType()).equals(typeName)) {
if (TypeName.get(adapter.classElement().asType()).equals(typeName)) {
// Use self-made adapter
this.adapter = adapter.generatedClassName.toString();
this.adapter = adapter.generatedClassName().toString();
}
}
}

View File

@ -11,7 +11,7 @@ public class PrimitiveAdapter extends Adapter<PrimitiveAdapter.Hydrated> {
public class Hydrated extends Adapter<Hydrated>.Hydrated {
@Override
public boolean applies() {
return unboxedType.kind.isPrimitive;
return unboxedType.getKind().isPrimitive();
}
@Override
@ -23,7 +23,7 @@ public class PrimitiveAdapter extends Adapter<PrimitiveAdapter.Hydrated> {
@Override
public void generateRead() {
code.add(switch (unboxedType.kind) {
code.add(switch (unboxedType.getKind()) {
case BOOLEAN -> "reader.nextBoolean()";
case BYTE -> "(byte) reader.nextInt()";
case SHORT -> "(short) reader.nextInt()";
@ -32,7 +32,7 @@ public class PrimitiveAdapter extends Adapter<PrimitiveAdapter.Hydrated> {
case CHAR -> "(char) reader.nextInt()";
case FLOAT -> "(float) reader.nextDouble()";
case DOUBLE -> "reader.nextDouble()";
default -> throw new IllegalArgumentException("Unsupported primitive: " + unboxedType.kind);
default -> throw new IllegalArgumentException("Unsupported primitive: " + unboxedType.getKind());
});
}
}

View File

@ -38,12 +38,12 @@ public abstract class GProcessor {
public abstract void generateSerialisation(TypeSpec.Builder spec, SerializableClass self, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters) throws ElementException;
protected String getSerializedName(Property<?> property) {
for (AnnotationMirror annotationMirror : property.annotations) {
if (annotationMirror.annotationType.asElement().toString().equals(Cl.SERIALIZED_NAME.toString())) {
return (String) annotationMirror.elementValues.values().iterator().next().value;
for (AnnotationMirror annotationMirror : property.getAnnotations()) {
if (annotationMirror.getAnnotationType().asElement().toString().equals(Cl.SERIALIZED_NAME.toString())) {
return (String) annotationMirror.getElementValues().values().iterator().next().getValue();
}
}
return property.name;
return property.getName();
}
protected MethodSpec.Builder extension(MethodSpec.Builder method) {
@ -64,9 +64,9 @@ public abstract class GProcessor {
}
protected void generateComments(Property<?> prop, CodeBlock.Builder code) {
for (AnnotationMirror annotation : prop.annotations) {
if (annotation.annotationType.asElement().toString().equals(Cl.GCOMMENT.toString())) {
String comment = (String) annotation.elementValues.values().iterator().next().value;
for (AnnotationMirror annotation : prop.getAnnotations()) {
if (annotation.getAnnotationType().asElement().toString().equals(Cl.GCOMMENT.toString())) {
String comment = (String) annotation.getElementValues().values().iterator().next().getValue();
code.addStatement("if (writer.isLenient()) writer.comment($S)", comment);
}
}

View File

@ -31,7 +31,7 @@ public class InstanceProcessor extends GProcessor {
.addParameter(Cl.GSON_WRITER, "writer")
.addParameter(classType, "value")
.addException(IOException.class)
.addCode(generatedClassName.simpleName + ".write(value, writer);")
.addCode(generatedClassName.simpleName() + ".write(value, writer);")
.build())
.addMethod(MethodSpec.methodBuilder("read")
.addAnnotation(Override.class)
@ -39,7 +39,7 @@ public class InstanceProcessor extends GProcessor {
.addParameter(Cl.GSON_READER, "reader")
.addException(IOException.class)
.returns(classType)
.addCode("return " + generatedClassName.simpleName + ".read(reader);")
.addCode("return " + generatedClassName.simpleName() + ".read(reader);")
.build())
.build()
);
@ -52,9 +52,9 @@ public class InstanceProcessor extends GProcessor {
// !!!WARNING!!!
@Override
public void generateSerialisation(TypeSpec.Builder spec, SerializableClass self, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters) throws ElementException {
Value value = self.builder == null ? valueCreator.from(self.classElement, false) : valueCreator.from(TypeHelper.asDeclaredType(self.builder).asElement(), true);
ConstructionSource constructionSource = value.constructionSource;
Properties properties = value.properties;
Value value = self.builder() == null ? valueCreator.from(self.classElement(), false) : valueCreator.from(TypeHelper.asDeclaredType(self.builder()).asElement(), true);
ConstructionSource constructionSource = value.getConstructionSource();
Properties properties = value.getProperties();
// public static void write(JsonWriter writer, T value) throws IOException
{
@ -67,16 +67,16 @@ public class InstanceProcessor extends GProcessor {
code.addStatement("writer.beginObject()");
for (Property.Field param : properties.fields) {
if (Properties.containsName(properties.getters, param)) continue;
Runnable writeGet = () -> code.add("value.$N", param.callableName);
if (param.type.kind.isPrimitive) {
Runnable writeGet = () -> code.add("value.$N", param.getCallableName());
if (param.getType().getKind().isPrimitive()) {
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, writeGet);
} else {
code.beginControlFlow("if (value.$N != null || writer.getSerializeNulls())", param.callableName);
code.beginControlFlow("if (value.$N != null || writer.getSerializeNulls())", param.getCallableName());
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
code.addStatement("if (value.$N == null) writer.nullValue()", param.callableName);
code.addStatement("if (value.$N == null) writer.nullValue()", param.getCallableName());
code.beginControlFlow("else");
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, writeGet);
code.endControlFlow();
@ -84,25 +84,25 @@ public class InstanceProcessor extends GProcessor {
}
}
for (Property.Getter param : properties.getters) {
if (param.type.kind.isPrimitive) {
if (param.getType().getKind().isPrimitive()) {
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, () -> code.add("value.$N()", param.callableName));
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, () -> code.add("value.$N()", param.getCallableName()));
} else {
code.addStatement("$T $L$N = value.$N()", param.type, "$", param.callableName, param.callableName);
code.beginControlFlow("if ($L$N != null || writer.getSerializeNulls())", "$", param.callableName);
code.addStatement("$T $L$N = value.$N()", param.getType(), "$", param.getCallableName(), param.getCallableName());
code.beginControlFlow("if ($L$N != null || writer.getSerializeNulls())", "$", param.getCallableName());
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
code.addStatement("if ($L$N == null) writer.nullValue()", "$", param.callableName);
code.addStatement("if ($L$N == null) writer.nullValue()", "$", param.getCallableName());
code.beginControlFlow("else");
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, () -> code.add("$L$N", "$", param.callableName));
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, () -> code.add("$L$N", "$", param.getCallableName()));
code.endControlFlow();
code.endControlFlow();
}
}
code.addStatement("writer.endObject()");
spec.addMethod(extension(MethodSpec.methodBuilder("write"), self.typeName)
spec.addMethod(extension(MethodSpec.methodBuilder("write"), self.getTypeName())
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addParameter(Cl.GSON_WRITER, "writer")
.addException(IOException.class)
@ -121,7 +121,7 @@ public class InstanceProcessor extends GProcessor {
boolean isEmpty = true;
for (Property<?> param : properties.names) {
isEmpty = false;
code.addStatement("$T _$N = $L", param.type, param.name, TypeHelper.getDefaultValue(param.type));
code.addStatement("$T _$N = $L", param.getType(), param.getName(), TypeHelper.getDefaultValue(param.getType()));
}
if (isEmpty) {
code.addStatement("reader.skipValue()");
@ -130,16 +130,16 @@ public class InstanceProcessor extends GProcessor {
.beginControlFlow("while (reader.hasNext())")
.beginControlFlow("switch (reader.nextName())");
for (Property<?> param : properties.names) {
if (param.type.kind.isPrimitive) {
code.add("case $S -> _$N = ", getSerializedName(param), param.name);
if (param.getType().getKind().isPrimitive()) {
code.add("case $S -> _$N = ", getSerializedName(param), param.getName());
Adapters.generateRead(param, spec, code, typeVariables, otherAdapters, message);
code.add(";\n");
} else {
code.beginControlFlow("case $S ->", getSerializedName(param))
.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
.addStatement("reader.nextNull()")
.addStatement("_$N = null", param.name);
code.unindent().add("} else _$N = ", param.name);
.addStatement("_$N = null", param.getName());
code.unindent().add("} else _$N = ", param.getName());
Adapters.generateRead(param, spec, code, typeVariables, otherAdapters, message);
code.add(";\n")
.endControlFlow();
@ -153,48 +153,48 @@ public class InstanceProcessor extends GProcessor {
.addStatement("reader.endObject()");
}
code.addStatement("$T result", self.typeName);
ClassName creatorName = ClassName.get((TypeElement) constructionSource.constructionElement.enclosingElement);
code.addStatement("$T result", self.getTypeName());
ClassName creatorName = ClassName.get((TypeElement) constructionSource.getConstructionElement().getEnclosingElement());
if (constructionSource instanceof ConstructionSource.Builder builder) {
StringBuilder args = new StringBuilder();
for (Property.ConstructorParam param : properties.constructorParams) {
args.append(", _").append(param.name);
args.append(", _").append(param.getName());
}
code.add("$T builder = ", builder.builderClass);
if (constructionSource.isConstructor) {
code.add("new $T($L)", builder.builderClass, args.length() > 0 ? args.substring(2) : "");
code.add("$T builder = ", builder.getBuilderClass());
if (constructionSource.isConstructor()) {
code.add("new $T($L)", builder.getBuilderClass(), args.length() > 0 ? args.substring(2) : "");
} else {
code.add("$T.$N($L)", creatorName, self.classElement.simpleName, args.length() > 0 ? args.substring(2) : "");
code.add("$T.$N($L)", creatorName, self.classElement().getSimpleName(), args.length() > 0 ? args.substring(2) : "");
}
code.add(";\n");
for (Property.Setter param : properties.builderParams) {
code.addStatement("builder.$N(_$N)", param.callableName, param.name);
code.addStatement("builder.$N(_$N)", param.getCallableName(), param.getName());
}
code.addStatement("result = builder.$N()", builder.buildMethod.simpleName);
code.addStatement("result = builder.$N()", builder.getBuildMethod().getSimpleName());
} else {
StringBuilder args = new StringBuilder();
for (Property.Param param : properties.params) {
args.append(", _").append(param.name);
args.append(", _").append(param.getName());
}
if (constructionSource.isConstructor) {
code.addStatement("result = new $T($L)", self.typeName, args.length() > 0 ? args.substring(2) : "");
if (constructionSource.isConstructor()) {
code.addStatement("result = new $T($L)", self.getTypeName(), args.length() > 0 ? args.substring(2) : "");
} else {
code.addStatement("result = $T.$N($L)", creatorName, constructionSource.constructionElement.simpleName, args.length() > 0 ? args.substring(2) : "");
code.addStatement("result = $T.$N($L)", creatorName, constructionSource.getConstructionElement().getSimpleName(), args.length() > 0 ? args.substring(2) : "");
}
}
for (Property.Setter setter : properties.setters) {
code.addStatement("result.$N(_$N)", setter.callableName, setter.name);
code.addStatement("result.$N(_$N)", setter.getCallableName(), setter.getName());
}
for (Property.Field field : properties.fields) {
if (Properties.containsName(properties.setters, field)) continue;
if (Properties.containsName(properties.params, field)) continue;
code.addStatement("result.$N = _$N", field.name, field.callableName);
code.addStatement("result.$N = _$N", field.getName(), field.getCallableName());
}
code.addStatement("return result");
spec.addMethod(extension(MethodSpec.methodBuilder("read"))
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.returns(self.typeName)
.returns(self.getTypeName())
.addParameter(Cl.GSON_READER, "reader")
.addException(IOException.class)
.addCode(code.build())

View File

@ -30,8 +30,8 @@ public class StaticProcessor extends GProcessor {
// !!!WARNING!!!
@Override
public void generateSerialisation(TypeSpec.Builder spec, SerializableClass self, List<TypeVariableName> typeVariables, Set<SerializableClass> otherAdapters) throws ElementException {
Value value = valueCreator.fromStatic(self.classElement);
Properties properties = value.properties;
Value value = valueCreator.fromStatic(self.classElement());
Properties properties = value.getProperties();
// public static void write(JsonWriter writer) throws IOException
{
@ -39,16 +39,16 @@ public class StaticProcessor extends GProcessor {
code.addStatement("writer.beginObject()");
for (Property.Field param : properties.fields) {
if (Properties.containsName(properties.getters, param)) continue;
Runnable writeGet = () -> code.add("$T.$N", self.typeName, param.callableName);
if (param.type.kind.isPrimitive) {
Runnable writeGet = () -> code.add("$T.$N", self.getTypeName(), param.getCallableName());
if (param.getType().getKind().isPrimitive()) {
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, writeGet);
} else {
code.beginControlFlow("if ($T.$N != null || writer.getSerializeNulls())", self.typeName, param.callableName);
code.beginControlFlow("if ($T.$N != null || writer.getSerializeNulls())", self.getTypeName(), param.getCallableName());
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
code.addStatement("if ($T.$N == null) writer.nullValue()", self.typeName, param.callableName);
code.addStatement("if ($T.$N == null) writer.nullValue()", self.getTypeName(), param.getCallableName());
code.beginControlFlow("else");
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, writeGet);
code.endControlFlow();
@ -56,18 +56,18 @@ public class StaticProcessor extends GProcessor {
}
}
for (Property.Getter param : properties.getters) {
if (param.type.kind.isPrimitive) {
if (param.getType().getKind().isPrimitive()) {
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, () -> code.add("$T.$N()", self.typeName, param.callableName));
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, () -> code.add("$T.$N()", self.getTypeName(), param.getCallableName()));
} else {
code.addStatement("$T $L$N = $T.$N()", param.type, "$", param.callableName, self.typeName, param.callableName);
code.beginControlFlow("if ($L$N != null || writer.getSerializeNulls())", "$", param.callableName);
code.addStatement("$T $L$N = $T.$N()", param.getType(), "$", param.getCallableName(), self.getTypeName(), param.getCallableName());
code.beginControlFlow("if ($L$N != null || writer.getSerializeNulls())", "$", param.getCallableName());
generateComments(param, code);
code.addStatement("writer.name($S)", getSerializedName(param));
code.addStatement("if ($L$N == null) writer.nullValue()", "$", param.callableName);
code.addStatement("if ($L$N == null) writer.nullValue()", "$", param.getCallableName());
code.beginControlFlow("else");
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, () -> code.add("$L$N", "$", param.callableName));
Adapters.generateWrite(param, spec, code, typeVariables, otherAdapters, message, () -> code.add("$L$N", "$", param.getCallableName()));
code.endControlFlow();
code.endControlFlow();
}
@ -93,7 +93,7 @@ public class StaticProcessor extends GProcessor {
boolean isEmpty = true;
for (Property<?> param : properties.names) {
isEmpty = false;
code.addStatement("$T.$N = $L", self.typeName, param.name, TypeHelper.getDefaultValue(param.type));
code.addStatement("$T.$N = $L", self.getTypeName(), param.getName(), TypeHelper.getDefaultValue(param.getType()));
}
if (isEmpty) {
code.addStatement("reader.skipValue()");
@ -102,16 +102,16 @@ public class StaticProcessor extends GProcessor {
.beginControlFlow("while (reader.hasNext())")
.beginControlFlow("switch (reader.nextName())");
for (Property<?> param : properties.names) {
if (param.type.kind.isPrimitive) {
code.add("case $S -> $T.$N = ", getSerializedName(param), self.typeName, param.name);
if (param.getType().getKind().isPrimitive()) {
code.add("case $S -> $T.$N = ", getSerializedName(param), self.getTypeName(), param.getName());
Adapters.generateRead(param, spec, code, typeVariables, otherAdapters, message);
code.add(";\n");
} else {
code.beginControlFlow("case $S ->", getSerializedName(param))
.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
.addStatement("reader.nextNull()")
.addStatement("$T.$N = null", self.typeName, param.name);
code.unindent().add("} else $T.$N = ", self.typeName, param.name);
.addStatement("$T.$N = null", self.getTypeName(), param.getName());
code.unindent().add("} else $T.$N = ", self.getTypeName(), param.getName());
Adapters.generateRead(param, spec, code, typeVariables, otherAdapters, message);
code.add(";\n")
.endControlFlow();

View File

@ -0,0 +1,8 @@
module io.gitlab.jfronny.gson.compile.processor {
requires com.squareup.javapoet;
requires java.compiler;
requires io.gitlab.jfronny.gson.compile.processor.core;
requires io.gitlab.jfronny.commons;
requires io.gitlab.jfronny.gson.compile.annotations;
requires static org.jetbrains.annotations;
}