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.codemodel.internal;
  27 
  28 
  29 
  30 
  31 /**
  32  * A field that can have a {@link JDocComment} associated with it
  33  */
  34 public class JFieldVar extends JVar implements JDocCommentable {
  35 
  36     /**
  37      * javadoc comments for this JFieldVar
  38      */
  39     private JDocComment jdoc = null;
  40 
  41     private final JDefinedClass owner;
  42 
  43 
  44     /**
  45      * JFieldVar constructor
  46      *
  47      * @param type
  48      *        Datatype of this variable
  49      *
  50      * @param name
  51      *        Name of this variable
  52      *
  53      * @param init
  54      *        Value to initialize this variable to
  55      */
  56     JFieldVar(JDefinedClass owner, JMods mods, JType type, String name, JExpression init) {
  57         super( mods, type, name, init );
  58         this.owner = owner;
  59     }
  60 
  61     @Override
  62     public void name(String name) {
  63         // make sure that the new name is available
  64         if(owner.fields.containsKey(name))
  65             throw new IllegalArgumentException("name "+name+" is already in use");
  66         String oldName = name();
  67         super.name(name);
  68         owner.fields.remove(oldName);
  69         owner.fields.put(name,this);
  70     }
  71 
  72     /**
  73      * Creates, if necessary, and returns the class javadoc for this
  74      * JDefinedClass
  75      *
  76      * @return JDocComment containing javadocs for this class
  77      */
  78     public JDocComment javadoc() {
  79         if( jdoc == null )
  80             jdoc = new JDocComment(owner.owner());
  81         return jdoc;
  82     }
  83 
  84     public void declare(JFormatter f) {
  85         if( jdoc != null )
  86             f.g( jdoc );
  87         super.declare( f );
  88     }
  89 
  90 
  91 }