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.model.nav.Navigator;
  46 import com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationException;
  47 import com.sun.xml.internal.bind.v2.runtime.Transducer;
  48 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
  49 
  50 /**
  51  * @author Kohsuke Kawaguchi
  52  */
  53 final class RuntimeElementInfoImpl extends ElementInfoImpl<Type,Class,Field,Method>
  54     implements RuntimeElementInfo {
  55 
  56     public RuntimeElementInfoImpl(RuntimeModelBuilder modelBuilder, RegistryInfoImpl registry, Method method) throws IllegalAnnotationException {
  57         super(modelBuilder, registry, method);
  58 
  59         Adapter<Type,Class> a = getProperty().getAdapter();
  60 
  61         if(a!=null)
  62             adapterType = a.adapterType;
  63         else
  64             adapterType = null;
  65     }
  66 
  67     @Override
  68     protected PropertyImpl createPropertyImpl() {
  69         return new RuntimePropertyImpl();
  70     }
  71 
  72     class RuntimePropertyImpl extends PropertyImpl implements RuntimeElementPropertyInfo, RuntimeTypeRef {
  73         public Accessor getAccessor() {
  74             if(adapterType==null)
  75                 return Accessor.JAXB_ELEMENT_VALUE;
  76             else
  77                 return Accessor.JAXB_ELEMENT_VALUE.adapt(
  78                         (Class)getAdapter().defaultType,(Class)adapterType);
  79         }
  80 
  81         public Type getRawType() {
  82             return Collection.class;
  83         }
  84 
  85         public Type getIndividualType() {
  86              return getContentType().getType();
  87         }
  88 
  89 
  90         public boolean elementOnlyContent() {
  91             return false;   // this method doesn't make sense here
  92         }
  93 
  94         public List<? extends RuntimeTypeRef> getTypes() {
  95             return Collections.singletonList(this);
  96         }
  97 
  98         public List<? extends RuntimeNonElement> ref() {
  99             return (List<? extends RuntimeNonElement>)super.ref();
 100         }
 101 
 102         public RuntimeNonElement getTarget() {
 103             return (RuntimeNonElement)super.getTarget();
 104         }
 105 
 106         public RuntimePropertyInfo getSource() {
 107             return this;
 108         }
 109 
 110         public Transducer getTransducer() {
 111             return RuntimeModelBuilder.createTransducer(this);
 112         }
 113     }
 114 
 115     /**
 116      * The adapter specified by <code>getProperty().getAdapter()</code>.
 117      */
 118     private final Class<? extends XmlAdapter> adapterType;
 119 
 120     public RuntimeElementPropertyInfo getProperty() {
 121         return (RuntimeElementPropertyInfo)super.getProperty();
 122     }
 123 
 124     public Class<? extends JAXBElement> getType() {
 125         return Navigator.REFLECTION.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 }