01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.codemodel.internal;
27:
28: /**
29: * A field that can have a {@link JDocComment} associated with it
30: */
31: public class JFieldVar extends JVar {
32:
33: /**
34: * javadoc comments for this JFieldVar
35: */
36: private JDocComment jdoc = null;
37:
38: private final JDefinedClass owner;
39:
40: /**
41: * JFieldVar constructor
42: *
43: * @param type
44: * Datatype of this variable
45: *
46: * @param name
47: * Name of this variable
48: *
49: * @param init
50: * Value to initialize this variable to
51: */
52: JFieldVar(JDefinedClass owner, JMods mods, JType type, String name,
53: JExpression init) {
54: super (mods, type, name, init);
55: this .owner = owner;
56: }
57:
58: @Override
59: public void name(String name) {
60: // make sure that the new name is available
61: if (owner.fields.containsKey(name))
62: throw new IllegalArgumentException("name " + name
63: + " is already in use");
64: String oldName = name();
65: super .name(name);
66: owner.fields.remove(oldName);
67: owner.fields.put(name, this );
68: }
69:
70: /**
71: * Creates, if necessary, and returns the class javadoc for this
72: * JDefinedClass
73: *
74: * @return JDocComment containing javadocs for this class
75: */
76: public JDocComment javadoc() {
77: if (jdoc == null)
78: jdoc = new JDocComment(owner.owner());
79: return jdoc;
80: }
81:
82: public void declare(JFormatter f) {
83: if (jdoc != null)
84: f.g(jdoc);
85: super.declare(f);
86: }
87:
88: }
|