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 committed Dec 4, 2023
1 parent e7a598a commit 02b5def
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2023 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 02b5def

Please sign in to comment.