环境:jdk1.6
自己写了一个类@XmlRootElement
@XmlType(name = "req")
@XmlAccessorType(XmlAccessType.FIELD)
public class Req<T> { private T ctr; public T getCtr() {
return ctr;
} public void setCtr(T ctr) {
this.ctr = ctr;
}}
然后在命令行下,执行:schemagen -classpath . Reqjava,生成了对应当schema文件(schema1.xsd)<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">  <xs:element name="req" type="req"/>  <xs:complexType name="req">
    <xs:sequence>
      <xs:element name="ctr" type="xs:anyType" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
注意:ctr生成的schema中的类型是anyType
之后同样在命令行下,执行:xjc schema1.xsd,反向生成java类//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2011.11.22 at 01:57:26 下午 CST 
//
package generated;import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
 * <p>Java class for req complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="req">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="ctr" type="{http://www.w3.org/2001/XMLSchema}anyType" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "req", propOrder = {
    "ctr"
})
public class Req {    protected Object ctr;    /**
     * Gets the value of the ctr property.
     * 
     * @return
     *     possible object is
     *     {@link Object }
     *     
     */
    public Object getCtr() {
        return ctr;
    }    /**
     * Sets the value of the ctr property.
     * 
     * @param value
     *     allowed object is
     *     {@link Object }
     *     
     */
    public void setCtr(Object value) {
        this.ctr = value;
    }}
生产的ctr是Object类型,但是我要生成泛型T,也就是跟之前一样的类,不知道是否有办法
没有多少分,但是希望各路英雄多加指导,先谢过了

解决方案 »

  1.   

    我对 xml中的 schema一直没有个什么概念 
      

  2.   

    其实,jaxb将anyType映射成Object也可以理解,毕竟Object也可以在某种程度上实现泛型T的功能,而且我昨天又去搜了下关于泛型的相关问题,其中一个就是关于如何获取泛型的类型这样一个问题,也就是在你new了一个XX<T>的时候,能否获得T的类型,有人就此指出了<T>为全局变量与内部变量的区别,以及<T>分别处在声明部分与赋值部分的区别。
    举例如:
    List<T> a = new ArrayList<T>();
    左侧为声明部分,右侧为赋值部分,
    位于声明一侧的,源码里写了什么到运行时就能看到什么;
    包括泛型类型(泛型类与泛型接口)声明、带有泛型参数的方法和域的声明。注意局部变量的声明不算在内
    位于赋值一侧的,源码里写什么到运行时都没了。
    也就是说,在方法体内的泛型局部变量、泛型方法调用之类的泛型信息编译后都消失了。相关引用为:http://www.iteye.com/topic/585900  名为 RednaxelaFX 的回帖