1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.model.impl;
  27 
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.Method;
  30 import java.lang.reflect.Type;
  31 import java.util.Collection;
  32 import java.util.Collections;
  33 import java.util.List;
  34 
  35 import javax.xml.bind.JAXBElement;
  36 import javax.xml.bind.annotation.adapters.XmlAdapter;
  37 
  38 import com.sun.xml.internal.bind.v2.model.core.Adapter;
  39 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeClassInfo;
  40 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementInfo;
  41 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo;
  42 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeNonElement;
  43 import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
  44 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef;
  45 import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
  46 import com.sun.xml.internal.bind.v2.runtime.Transducer;
  47 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
  48 
  49 /**
  50  * @author Kohsuke Kawaguchi
  51  */
  52 final class RuntimeElementInfoImpl extends ElementInfoImpl<Type,Class,Field,Method>
  53     implements RuntimeElementInfo {
  54 
  55     public RuntimeElementInfoImpl(RuntimeModelBuilder modelBuilder, RegistryInfoImpl registry, Method method) throws IllegalAnnotationException {
  56         super(modelBuilder, registry, method);
  57 
  58         Adapter<Type,Class> a = getProperty().getAdapter();
  59 
  60         if(a!=null)
  61             adapterType = a.adapterType;
  62         else
  63             adapterType = null;
  64     }
  65 
  66     @Override
  67     protected PropertyImpl createPropertyImpl() {
  68         return new RuntimePropertyImpl();
  69     }
  70 
  71     class RuntimePropertyImpl extends PropertyImpl implements RuntimeElementPropertyInfo, RuntimeTypeRef {
  72         public Accessor getAccessor() {
  73             if(adapterType==null)
  74                 return Accessor.JAXB_ELEMENT_VALUE;
  75             else
  76                 return Accessor.JAXB_ELEMENT_VALUE.adapt(
  77                         (Class)getAdapter().defaultType,(Class)adapterType);
  78         }
  79 
  80         public Type getRawType() {
  81             return Collection.class;
  82         }
  83 
  84         public Type getIndividualType() {
  85              return getContentType().getType();
  86         }
  87 
  88 
  89         public boolean elementOnlyContent() {
  90             return false;   // this method doesn't make sense here
  91         }
  92 
  93         public List<? extends RuntimeTypeRef> getTypes() {
  94             return Collections.singletonList(this);
  95         }
  96 
  97         public List<? extends RuntimeNonElement> ref() {
  98             return (List<? extends RuntimeNonElement>)super.ref();
  99         }
 100 
 101         public RuntimeNonElement getTarget() {
 102             return (RuntimeNonElement)super.getTarget();
 103         }
 104 
 105         public RuntimePropertyInfo getSource() {
 106             return this;
 107         }
 108 
 109         public Transducer getTransducer() {
 110             return RuntimeModelBuilder.createTransducer(this);
 111         }
 112     }
 113 
 114     /**
 115      * The adapter specified by <code>getProperty().getAdapter()</code>.
 116      */
 117     private final Class<? extends XmlAdapter> adapterType;
 118 
 119     public RuntimeElementPropertyInfo getProperty() {
 120         return (RuntimeElementPropertyInfo)super.getProperty();
 121     }
 122 
 123     public Class<? extends JAXBElement> getType() {
 124         //noinspection unchecked
 125         return (Class<? extends JAXBElement>) Utils.REFLECTION_NAVIGATOR.erasure(super.getType());
 126     }
 127 
 128     public RuntimeClassInfo getScope() {
 129         return (RuntimeClassInfo)super.getScope();
 130     }
 131 
 132     public RuntimeNonElement getContentType() {
 133         return (RuntimeNonElement)super.getContentType();
 134     }
 135 }