Skip to content
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

Closed
Tomas-Kraus opened this issue Feb 14, 2010 · 7 comments · Fixed by #1781
Closed

xjc:javaType does not handle generic types #737

Tomas-Kraus opened this issue Feb 14, 2010 · 7 comments · Fixed by #1781

Comments

@Tomas-Kraus
Copy link
Member

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

            1. Output #####

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]

@Tomas-Kraus
Copy link
Member Author

@glassfishrobot Commented
Reported by royalpeasantry

@Tomas-Kraus
Copy link
Member Author

@glassfishrobot Commented
Was assigned to snajper

@Tomas-Kraus
Copy link
Member Author

@glassfishrobot Commented
royalpeasantry said:
This should be platform independent.

@Tomas-Kraus
Copy link
Member Author

@glassfishrobot Commented
snajper said:
assigning

@Tomas-Kraus
Copy link
Member Author

@glassfishrobot Commented
This issue was imported from java.net JIRA JAXB-737

@Tomas-Kraus
Copy link
Member Author

@laurentschoelens
Copy link
Contributor

@lukasj : found a way to fix this, will provide PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants