From 22627f8ba96507b7c31b4a05ea1728da2d2e9c61 Mon Sep 17 00:00:00 2001 From: Jesenko Mehmedbasic Date: Tue, 17 Oct 2023 13:30:12 +0200 Subject: [PATCH] Use RecordCompatibility constant to check for java.lang.Record equality --- .../c_client/ClientClassnameForMethod.java | 2 +- .../freemarker/ClientClassnameForMethod.java | 5 +-- .../ClientClassnameForMethod.java | 2 +- .../jackson/EnunciateJacksonContext.java | 3 +- .../jackson/model/ObjectTypeDefinition.java | 32 ++++++++++++------- .../modules/jackson/model/TypeDefinition.java | 2 +- .../enunciate/javac/RecordCompatibility.java | 1 + .../ClientClassnameForMethod.java | 3 +- .../modules/jaxb/EnunciateJaxbContext.java | 2 +- .../jaxb/model/ComplexTypeDefinition.java | 3 +- 10 files changed, 34 insertions(+), 21 deletions(-) diff --git a/c-xml-client/src/main/java/com/webcohesion/enunciate/modules/c_client/ClientClassnameForMethod.java b/c-xml-client/src/main/java/com/webcohesion/enunciate/modules/c_client/ClientClassnameForMethod.java index 22ce6c3ed..5aa9081d0 100644 --- a/c-xml-client/src/main/java/com/webcohesion/enunciate/modules/c_client/ClientClassnameForMethod.java +++ b/c-xml-client/src/main/java/com/webcohesion/enunciate/modules/c_client/ClientClassnameForMethod.java @@ -79,7 +79,7 @@ public ClientClassnameForMethod(Map conversions, EnunciateJaxbCo classConversions.put(javax.xml.datatype.Duration.class.getName(), "xmlChar"); classConversions.put(jakarta.xml.bind.JAXBElement.class.getName(), "struct xmlBasicNode"); classConversions.put(Object.class.getName(), "struct xmlBasicNode"); - classConversions.put("java.lang.Record", "struct xmlBasicNode"); + classConversions.put(RecordCompatibility.CLASS_RECORD, "struct xmlBasicNode"); classConversions.putAll(conversions); } diff --git a/core/src/main/java/com/webcohesion/enunciate/util/freemarker/ClientClassnameForMethod.java b/core/src/main/java/com/webcohesion/enunciate/util/freemarker/ClientClassnameForMethod.java index 6cc14dd17..2685afdc3 100644 --- a/core/src/main/java/com/webcohesion/enunciate/util/freemarker/ClientClassnameForMethod.java +++ b/core/src/main/java/com/webcohesion/enunciate/util/freemarker/ClientClassnameForMethod.java @@ -16,6 +16,7 @@ package com.webcohesion.enunciate.util.freemarker; import com.webcohesion.enunciate.EnunciateContext; +import com.webcohesion.enunciate.javac.RecordCompatibility; import com.webcohesion.enunciate.util.HasClientConvertibleType; import freemarker.template.TemplateModelException; @@ -93,7 +94,7 @@ else if (typeMirror instanceof TypeVariable) { conversion = super.convert(typeMirror); boolean isArray = typeMirror.getKind() == TypeKind.ARRAY; - if (typeMirror instanceof DeclaredType && !"java.lang.Object".equals(conversion) && !"java.lang.Record".equals(conversion)) { + if (typeMirror instanceof DeclaredType && !"java.lang.Object".equals(conversion) && !RecordCompatibility.CLASS_RECORD.equals(conversion)) { conversion += convertDeclaredTypeArguments(((DeclaredType) typeMirror).getTypeArguments()); } @@ -162,4 +163,4 @@ protected String getPackageSeparator() { public String convert(PackageElement packageDeclaration) { throw new UnsupportedOperationException("packages don't have a classname."); } -} \ No newline at end of file +} diff --git a/csharp-xml-client/src/main/java/com/webcohesion/enunciate/modules/csharp_client/ClientClassnameForMethod.java b/csharp-xml-client/src/main/java/com/webcohesion/enunciate/modules/csharp_client/ClientClassnameForMethod.java index 21a61ed50..e1e9ff253 100644 --- a/csharp-xml-client/src/main/java/com/webcohesion/enunciate/modules/csharp_client/ClientClassnameForMethod.java +++ b/csharp-xml-client/src/main/java/com/webcohesion/enunciate/modules/csharp_client/ClientClassnameForMethod.java @@ -87,7 +87,7 @@ public ClientClassnameForMethod(Map conversions, EnunciateJaxbCo classConversions.put(javax.xml.datatype.Duration.class.getName(), "TimeSpan?"); classConversions.put(jakarta.xml.bind.JAXBElement.class.getName(), "object"); classConversions.put(Object.class.getName(), "object"); - classConversions.put("java.lang.Record", "object"); + classConversions.put(RecordCompatibility.CLASS_RECORD, "object"); } @Override diff --git a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/EnunciateJacksonContext.java b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/EnunciateJacksonContext.java index c590ba5fb..ad32795f0 100644 --- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/EnunciateJacksonContext.java +++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/EnunciateJacksonContext.java @@ -24,6 +24,7 @@ import com.webcohesion.enunciate.CompletionFailureException; import com.webcohesion.enunciate.EnunciateContext; import com.webcohesion.enunciate.EnunciateException; +import com.webcohesion.enunciate.javac.RecordCompatibility; import com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment; import com.webcohesion.enunciate.javac.decorations.SourcePosition; import com.webcohesion.enunciate.javac.decorations.TypeMirrorDecorator; @@ -226,7 +227,7 @@ protected Map loadKnownTypes() { knownTypes.put(java.net.URI.class.getName(), KnownJsonType.STRING); knownTypes.put(java.net.URL.class.getName(), KnownJsonType.STRING); knownTypes.put(java.lang.Object.class.getName(), KnownJsonType.OBJECT); - knownTypes.put("java.lang.Record", KnownJsonType.OBJECT); + knownTypes.put(RecordCompatibility.CLASS_RECORD, KnownJsonType.OBJECT); knownTypes.put(java.io.Serializable.class.getName(), KnownJsonType.OBJECT); knownTypes.put(byte[].class.getName(), KnownJsonType.STRING); knownTypes.put(java.nio.ByteBuffer.class.getName(), KnownJsonType.STRING); diff --git a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/ObjectTypeDefinition.java b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/ObjectTypeDefinition.java index 2a5753841..28785d729 100644 --- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/ObjectTypeDefinition.java +++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/ObjectTypeDefinition.java @@ -1,12 +1,12 @@ /** * Copyright © 2006-2016 Web Cohesion (info@webcohesion.com) - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,16 +16,18 @@ package com.webcohesion.enunciate.modules.jackson.model; import com.fasterxml.jackson.annotation.JsonRootName; +import com.webcohesion.enunciate.javac.RecordCompatibility; import com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext; import com.webcohesion.enunciate.modules.jackson.model.types.JsonType; import com.webcohesion.enunciate.modules.jackson.model.types.JsonTypeFactory; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.XmlType; +import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; -import jakarta.xml.bind.annotation.XmlRootElement; -import jakarta.xml.bind.annotation.XmlType; /** * A type definition for a json type. @@ -43,7 +45,7 @@ public JsonType getSupertype() { if (superclass == null || superclass.getKind() == TypeKind.NONE) { return null; } - else if (superclass instanceof DeclaredType && (((TypeElement)((DeclaredType)superclass).asElement()).getQualifiedName().toString().equals(Object.class.getName()) || ((TypeElement)((DeclaredType)superclass).asElement()).getQualifiedName().toString().equals("java.lang.Record") || context.isIgnored((((DeclaredType)superclass).asElement())) || context.isCollapseTypeHierarchy())) { + else if (superclass instanceof DeclaredType && (isClasOrRecord(superclass) || context.isIgnored(((DeclaredType) superclass).asElement()) || context.isCollapseTypeHierarchy())) { return null; } else { @@ -51,6 +53,12 @@ else if (superclass instanceof DeclaredType && (((TypeElement)((DeclaredType)sup } } + private boolean isClasOrRecord(TypeMirror superclass) { + TypeElement typeElement = (TypeElement) ((DeclaredType) superclass).asElement(); + String qualifiedName = typeElement.getQualifiedName().toString(); + return qualifiedName.equals(Object.class.getName()) || qualifiedName.equals(RecordCompatibility.CLASS_RECORD); + } + @Override public boolean isSimple() { return false; @@ -70,11 +78,11 @@ public boolean isBaseObject() { TypeElement superDeclaration = (TypeElement) this.env.getTypeUtils().asElement(superclass); return superDeclaration == null - || Object.class.getName().equals(superDeclaration.getQualifiedName().toString()) - || Enum.class.getName().equals(superDeclaration.getQualifiedName().toString()) - || "java.lang.Record".equals(superDeclaration.getQualifiedName().toString()) - || this.context.isCollapseTypeHierarchy() - || this.context.isIgnored(superDeclaration); + || Object.class.getName().equals(superDeclaration.getQualifiedName().toString()) + || Enum.class.getName().equals(superDeclaration.getQualifiedName().toString()) + || RecordCompatibility.CLASS_RECORD.equals(superDeclaration.getQualifiedName().toString()) + || this.context.isCollapseTypeHierarchy() + || this.context.isIgnored(superDeclaration); } public String getJsonRootName() { diff --git a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/TypeDefinition.java b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/TypeDefinition.java index 27c706a90..bd8b99574 100644 --- a/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/TypeDefinition.java +++ b/jackson/src/main/java/com/webcohesion/enunciate/modules/jackson/model/TypeDefinition.java @@ -193,7 +193,7 @@ protected AccessorBag loadPotentialAccessors(AccessorFilter filter) { */ protected void aggregatePotentialAccessors(AccessorBag bag, DecoratedTypeElement clazz, AccessorFilter filter, boolean inlineAccessorsOfSuperclasses) { String fqn = clazz.getQualifiedName().toString(); - if (Object.class.getName().equals(fqn) || Enum.class.getName().equals(fqn) || "java.lang.Record".equals(fqn)) { + if (Object.class.getName().equals(fqn) || Enum.class.getName().equals(fqn) || RecordCompatibility.CLASS_RECORD.equals(fqn)) { return; } diff --git a/javac-support/src/main/java/com/webcohesion/enunciate/javac/RecordCompatibility.java b/javac-support/src/main/java/com/webcohesion/enunciate/javac/RecordCompatibility.java index 087ee8ce3..bdf03926c 100644 --- a/javac-support/src/main/java/com/webcohesion/enunciate/javac/RecordCompatibility.java +++ b/javac-support/src/main/java/com/webcohesion/enunciate/javac/RecordCompatibility.java @@ -9,6 +9,7 @@ public final class RecordCompatibility { public static final String KIND_RECORD = "RECORD"; public static final String KIND_RECORD_COMPONENT = "RECORD_COMPONENT"; + public static final String CLASS_RECORD = "java.lang.Record"; private RecordCompatibility() { } diff --git a/javascript-client/src/main/java/com/webcohesion/enunciate/modules/javascript_client/ClientClassnameForMethod.java b/javascript-client/src/main/java/com/webcohesion/enunciate/modules/javascript_client/ClientClassnameForMethod.java index 3eeeded0c..73aa76ece 100644 --- a/javascript-client/src/main/java/com/webcohesion/enunciate/modules/javascript_client/ClientClassnameForMethod.java +++ b/javascript-client/src/main/java/com/webcohesion/enunciate/modules/javascript_client/ClientClassnameForMethod.java @@ -19,6 +19,7 @@ import com.webcohesion.enunciate.api.datatype.DataTypeReference; import com.webcohesion.enunciate.api.resources.Entity; import com.webcohesion.enunciate.api.resources.MediaTypeDescriptor; +import com.webcohesion.enunciate.javac.RecordCompatibility; import com.webcohesion.enunciate.javac.decorations.TypeMirrorDecorator; import com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror; import com.webcohesion.enunciate.metadata.ClientName; @@ -84,7 +85,7 @@ public ClientClassnameForMethod(Map conversions, EnunciateJackso classConversions.put(javax.xml.datatype.Duration.class.getName(), "String"); classConversions.put(jakarta.xml.bind.JAXBElement.class.getName(), "Object"); classConversions.put(Object.class.getName(), "Object"); - classConversions.put("java.lang.Record", "Object"); + classConversions.put(RecordCompatibility.CLASS_RECORD, "Object"); } @Override diff --git a/jaxb/src/main/java/com/webcohesion/enunciate/modules/jaxb/EnunciateJaxbContext.java b/jaxb/src/main/java/com/webcohesion/enunciate/modules/jaxb/EnunciateJaxbContext.java index 326b135a0..befc22404 100644 --- a/jaxb/src/main/java/com/webcohesion/enunciate/modules/jaxb/EnunciateJaxbContext.java +++ b/jaxb/src/main/java/com/webcohesion/enunciate/modules/jaxb/EnunciateJaxbContext.java @@ -192,7 +192,7 @@ protected Map loadKnownTypes() { knownTypes.put(java.net.URI.class.getName(), KnownXmlType.STRING); knownTypes.put(javax.xml.datatype.Duration.class.getName(), KnownXmlType.DURATION); knownTypes.put(java.lang.Object.class.getName(), KnownXmlType.ANY_TYPE); - knownTypes.put("java.lang.Record", KnownXmlType.ANY_TYPE); + knownTypes.put(RecordCompatibility.CLASS_RECORD, KnownXmlType.ANY_TYPE); knownTypes.put(java.io.Serializable.class.getName(), KnownXmlType.ANY_TYPE); knownTypes.put(byte[].class.getName(), KnownXmlType.BASE64_BINARY); knownTypes.put(java.nio.ByteBuffer.class.getName(), KnownXmlType.BASE64_BINARY); diff --git a/jaxb/src/main/java/com/webcohesion/enunciate/modules/jaxb/model/ComplexTypeDefinition.java b/jaxb/src/main/java/com/webcohesion/enunciate/modules/jaxb/model/ComplexTypeDefinition.java index 8e271da8d..ccc352921 100644 --- a/jaxb/src/main/java/com/webcohesion/enunciate/modules/jaxb/model/ComplexTypeDefinition.java +++ b/jaxb/src/main/java/com/webcohesion/enunciate/modules/jaxb/model/ComplexTypeDefinition.java @@ -15,6 +15,7 @@ */ package com.webcohesion.enunciate.modules.jaxb.model; +import com.webcohesion.enunciate.javac.RecordCompatibility; import com.webcohesion.enunciate.modules.jaxb.EnunciateJaxbContext; import com.webcohesion.enunciate.modules.jaxb.model.types.KnownXmlType; import com.webcohesion.enunciate.modules.jaxb.model.types.XmlType; @@ -114,7 +115,7 @@ public boolean isBaseObject() { TypeElement superDeclaration = (TypeElement) this.env.getTypeUtils().asElement(superclass); return superDeclaration == null || Object.class.getName().equals(superDeclaration.getQualifiedName().toString()) - || "java.lang.Record".equals(superDeclaration.getQualifiedName().toString()) + || RecordCompatibility.CLASS_RECORD.equals(superDeclaration.getQualifiedName().toString()) || isXmlTransient(superDeclaration); }