Skip to content

Commit

Permalink
Use RecordCompatibility constant to check for java.lang.Record equality
Browse files Browse the repository at this point in the history
  • Loading branch information
ripdajacker committed Oct 17, 2023
1 parent 16d2cb6 commit 22627f8
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ClientClassnameForMethod(Map<String, String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -162,4 +163,4 @@ protected String getPackageSeparator() {
public String convert(PackageElement packageDeclaration) {
throw new UnsupportedOperationException("packages don't have a classname.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public ClientClassnameForMethod(Map<String, String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -226,7 +227,7 @@ protected Map<String, JsonType> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright © 2006-2016 Web Cohesion (info@webcohesion.com)
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
Expand All @@ -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.
Expand All @@ -43,14 +45,20 @@ 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 {
return JsonTypeFactory.getJsonType(superclass, this.context);
}
}

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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,7 +85,7 @@ public ClientClassnameForMethod(Map<String, String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected Map<String, XmlType> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 22627f8

Please sign in to comment.