-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
xjc:javaType does not handle generic types #737
Comments
@glassfishrobot Commented |
@glassfishrobot Commented |
@glassfishrobot Commented |
@glassfishrobot Commented |
@glassfishrobot Commented |
|
@lukasj : found a way to fix this, will provide PR |
laurentschoelens
added a commit
to laurentschoelens/jaxb-ri
that referenced
this issue
Dec 4, 2023
laurentschoelens
added a commit
to laurentschoelens/jaxb-ri
that referenced
this issue
Feb 9, 2024
lukasj
pushed a commit
that referenced
this issue
Feb 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When the type being mapped to is generic, xjc does not strip generics off of the
import statement.
Example:
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
<xjc:javaType adapter="ListAdapter" name="java.util.List" />
generates to
import java.util.List; // <-- Problem
import javax.xml.bind.annotation.XmlAccessType;
*
*
*
@xmlelement(type = String.class)
@XmlJavaTypeAdapter(ListAdapter.class)
protected Listitems;
*
*
*
If you try with java.util.List, it will work but will have raw type warnings.
However, this is not as simple as stripping out the generics. Its entirely
possible that it could be a list of some custom element, and thus that custom
element needs to be imported as well. It could also be a Map, and thus have
multiple generic parameters, or a list of maps, so the solution needs to handle
that. (Ex: Map<String, Map<String, List<String, OtherType<String, Map<String,
String>, String>>>> ..)
I wrote up a fix Someone just needs to figure out where it needs to go,
refactor it to fit JAXB coding conventions, and possibly add some sensible error
messages that can be passed to the user.
final Set imports = new TreeSet();
final Matcher typeMatcher = Pattern.compile("[^<>,
p
{Blank}
]+").matcher(name);
final StringBuffer sb = new StringBuffer();
while (typeMatcher.find()) {
final String qualifiedType = typeMatcher.group().trim();
if (qualifiedType.isEmpty())
{ break; }
final int endIndex = qualifiedType.lastIndexOf(".");
if (endIndex < 0)
{ typeMatcher.appendReplacement(sb, qualifiedType); }
else
{ imports.add(qualifiedType); // Might want to do error checking ("name = java.util.") final String type = qualifiedType.substring(endIndex + 1); typeMatcher.appendReplacement(sb, type); }
}
typeMatcher.appendTail(sb);
final String javaType = sb.toString();
Environment
Operating System: All
Platform: All
Affected Versions
[2.1.12]
The text was updated successfully, but these errors were encountered: