From dde82c250807c777a0ee6538ee9158a81ea5f5bb Mon Sep 17 00:00:00 2001 From: Laurent SCHOELENS <61973605+laurentschoelens@users.noreply.github.com> Date: Mon, 4 Dec 2023 09:58:43 +0100 Subject: [PATCH] Fixing GH-737 : compute class type with generics handle --- .../src/docbook/jaxb-changelog.xml | 6 +++++ .../xmlschema/bindinfo/BIConversion.java | 26 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/jaxb-ri/docs/release-documentation/src/docbook/jaxb-changelog.xml b/jaxb-ri/docs/release-documentation/src/docbook/jaxb-changelog.xml index 8126acac2..6dc1525a6 100644 --- a/jaxb-ri/docs/release-documentation/src/docbook/jaxb-changelog.xml +++ b/jaxb-ri/docs/release-documentation/src/docbook/jaxb-changelog.xml @@ -29,6 +29,12 @@ Bug fixes: + + #209: Schema-driven XmlAdapter using xjc:javaType + + + #737: xjc:javaType does not handle generic types + #1783: Dependency Exclusion for stax-ex in jaxb-bom is incompatible with dependency:analyze-dep-mgt diff --git a/jaxb-ri/xjc/src/main/java/com/sun/tools/xjc/reader/xmlschema/bindinfo/BIConversion.java b/jaxb-ri/xjc/src/main/java/com/sun/tools/xjc/reader/xmlschema/bindinfo/BIConversion.java index 7e0e7e59b..f15e0a38d 100644 --- a/jaxb-ri/xjc/src/main/java/com/sun/tools/xjc/reader/xmlschema/bindinfo/BIConversion.java +++ b/jaxb-ri/xjc/src/main/java/com/sun/tools/xjc/reader/xmlschema/bindinfo/BIConversion.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0, which is available at @@ -322,13 +322,13 @@ public TypeUse getTypeUse(XSSimpleType owner) { return typeUse; JCodeModel cm = getCodeModel(); + JClass classType = computeClassType(type); JDefinedClass a; try { a = cm._class(adapter); a.hide(); // we assume this is given by the user - a._extends(cm.ref(XmlAdapter.class).narrow(String.class).narrow( - cm.ref(type))); + a._extends(cm.ref(XmlAdapter.class).narrow(String.class).narrow(classType)); } catch (JClassAlreadyExistsException e) { a = e.getExistingClass(); } @@ -341,6 +341,26 @@ public TypeUse getTypeUse(XSSimpleType owner) { return typeUse; } + + private JClass computeClassType(String type) { + if (type.indexOf('<') < 0) { + return getCodeModel().ref(type); + } + + // We do assume that if type contains <, the wanted class with generics is well formed + JCodeModel cm = getCodeModel(); + // Get the main class (part before the <) + String mainType = type.substring(0, type.indexOf('<')); + JClass classType = cm.ref(mainType); + + // Get the narrowed class (part between < and >) + String subTypes = type.substring(type.indexOf('<') + 1, type.indexOf('>')); + for (String subType : subTypes.split(",")) { + JClass subClassType = computeClassType(subType.trim()); + classType = classType.narrow(subClassType); + } + return classType; + } } }