Skip to content

Commit

Permalink
Fixing GH-737 : compute class type with generics handle
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentschoelens authored and lukasj committed Feb 9, 2024
1 parent 9ba8ba0 commit dde82c2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Expand Up @@ -29,6 +29,12 @@
<itemizedlist>
<listitem><para>Bug fixes:
<itemizedlist>
<listitem><para>
<link xlink:href="https://github.com/eclipse-ee4j/jaxb-ri/issues/209">#209</link>: Schema-driven XmlAdapter using xjc:javaType
</para></listitem>
<listitem><para>
<link xlink:href="https://github.com/eclipse-ee4j/jaxb-ri/issues/737">#737</link>: xjc:javaType does not handle generic types
</para></listitem>
<listitem><para>
<link xlink:href="https://github.com/eclipse-ee4j/jaxb-ri/issues/1783">#1783</link>: Dependency Exclusion for stax-ex in jaxb-bom is incompatible with dependency:analyze-dep-mgt
</para></listitem>
Expand Down
@@ -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
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
}
}
}

0 comments on commit dde82c2

Please sign in to comment.