1 /*
   2  * Copyright (c) 1997, 2012, 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.tools.internal.xjc.generator.bean.field;
  27 
  28 import com.sun.codemodel.internal.JAnnotatable;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 
  32 import com.sun.codemodel.internal.JBlock;
  33 import com.sun.codemodel.internal.JClass;
  34 import com.sun.codemodel.internal.JExpr;
  35 import com.sun.codemodel.internal.JExpression;
  36 import com.sun.codemodel.internal.JMethod;
  37 import com.sun.codemodel.internal.JVar;
  38 import com.sun.tools.internal.xjc.generator.bean.ClassOutlineImpl;
  39 import com.sun.tools.internal.xjc.model.CPropertyInfo;
  40 import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
  41 import com.sun.xml.internal.bind.annotation.OverrideAnnotationOf;
  42 
  43 /**
  44  * Realizes a property as an untyped {@link List}.
  45  *
  46  * <pre>
  47  * List getXXX();
  48  * </pre>
  49  *
  50  * <h2>Default value handling</h2>
  51  * <p>
  52  * Since unmarshaller just adds new values into the storage,
  53  * we can't fill the storage by default values at the time of
  54  * instanciation. (or oherwise values found in the document will
  55  * be appended to default values, where it should overwrite them.)
  56  * <p>
  57  * Therefore, when the object is created, the storage will be empty.
  58  * When the getXXX method is called, we'll check if the storage is
  59  * modified in anyway. If it is modified, it must mean that the values
  60  * are found in the document, so we just return it.
  61  *
  62  * Otherwise we will fill in default values and return it to the user.
  63  *
  64  * <p>
  65  * When a list has default values, its dirty flag is set to true.
  66  * Marshaller will check this and treat it appropriately.
  67  *
  68  *
  69  * @author
  70  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  71  */
  72 public class DummyListField extends AbstractListField {
  73 
  74     /**
  75      * A concrete class that imp    lements the List interface.
  76      * An instance of this class will be used to store data
  77      * for this field.
  78      */
  79     private final JClass coreList;
  80 
  81 
  82     /** List getFIELD() method. */
  83     private JMethod $get;
  84 
  85     /**
  86      * @param coreList
  87      *      A concrete class that implements the List interface.
  88      *      An instance of this class will be used to store data
  89      *      for this field.
  90      */
  91     protected DummyListField(ClassOutlineImpl context, CPropertyInfo prop, JClass coreList) {
  92         // the JAXB runtime picks ArrayList if the signature is List,
  93         // so don't do eager allocation if it's ArrayList.
  94         // otherwise we need to do eager allocation so that the collection type specified by the user
  95         // will be used.
  96         super(context, prop, !coreList.fullName().equals("java.util.ArrayList"));
  97         this.coreList = coreList.narrow(exposedType.boxify());
  98         generate();
  99     }
 100 
 101     /**
 102      * Annotate the field according to the recipes given as {@link CPropertyInfo}.
 103      */
 104     @Override
 105     protected void annotate( JAnnotatable field ) {
 106         super.annotate(field);
 107 
 108         if (prop instanceof CReferencePropertyInfo) {
 109             CReferencePropertyInfo pref = (CReferencePropertyInfo)prop;
 110             if (pref.isDummy()) {
 111                 annotateDummy(field);
 112             }
 113         }
 114 
 115     }
 116 
 117     private void annotateDummy(JAnnotatable field) {
 118         field.annotate(OverrideAnnotationOf.class);
 119     }
 120 
 121     protected final JClass getCoreListType() {
 122         return coreList;
 123     }
 124 
 125     @Override
 126     public void generateAccessors() { }
 127 
 128     public Accessor create(JExpression targetObject) {
 129         return new Accessor(targetObject);
 130     }
 131 
 132     class Accessor extends AbstractListField.Accessor {
 133         protected Accessor( JExpression $target ) {
 134             super($target);
 135         }
 136 
 137         public void toRawValue(JBlock block, JVar $var) {
 138             // [RESULT]
 139             // $<var>.addAll(bean.getLIST());
 140             // list.toArray( array );
 141             block.assign($var,JExpr._new(codeModel.ref(ArrayList.class).narrow(exposedType.boxify())).arg(
 142                 $target.invoke($get)
 143             ));
 144         }
 145 
 146         public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
 147             // [RESULT]
 148             // bean.getLIST().addAll($<var>);
 149             JVar $list = block.decl(listT,uniqueName+'l',$target.invoke($get));
 150             block.invoke($list,"addAll").arg($var);
 151         }
 152     }
 153 }