Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

xjc:javaType does not handle generic types #737

Open
glassfishrobot opened this issue Feb 14, 2010 · 5 comments
Open

xjc:javaType does not handle generic types #737

glassfishrobot opened this issue Feb 14, 2010 · 5 comments

Comments

@glassfishrobot
Copy link
Contributor

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]

@glassfishrobot
Copy link
Contributor Author

Reported by royalpeasantry

@glassfishrobot
Copy link
Contributor Author

Was assigned to snajper

@glassfishrobot
Copy link
Contributor Author

royalpeasantry said:
This should be platform independent.

@glassfishrobot
Copy link
Contributor Author

snajper said:
assigning

@glassfishrobot
Copy link
Contributor Author

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

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

No branches or pull requests

1 participant