01: /*
02: * The contents of this file are subject to the terms
03: * of the Common Development and Distribution License
04: * (the "License"). You may not use this file except
05: * in compliance with the License.
06: *
07: * You can obtain a copy of the license at
08: * https://jwsdp.dev.java.net/CDDLv1.0.html
09: * See the License for the specific language governing
10: * permissions and limitations under the License.
11: *
12: * When distributing Covered Code, include this CDDL
13: * HEADER in each file and include the License file at
14: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
15: * add the following below this CDDL HEADER, with the
16: * fields enclosed by brackets "[]" replaced with your
17: * own identifying information: Portions Copyright [yyyy]
18: * [name of copyright owner]
19: */
20:
21: package com.sun.codemodel;
22:
23: /**
24: * A field that can have a {@link JDocComment} associated with it
25: */
26: public class JFieldVar extends JVar {
27:
28: /**
29: * javadoc comments for this JFieldVar
30: */
31: private JDocComment jdoc = null;
32:
33: private final JDefinedClass owner;
34:
35: /**
36: * JFieldVar constructor
37: *
38: * @param type
39: * Datatype of this variable
40: *
41: * @param name
42: * Name of this variable
43: *
44: * @param init
45: * Value to initialize this variable to
46: */
47: JFieldVar(JDefinedClass owner, JMods mods, JType type, String name,
48: JExpression init) {
49: super (mods, type, name, init);
50: this .owner = owner;
51: }
52:
53: @Override
54: public void name(String name) {
55: // make sure that the new name is available
56: if (owner.fields.containsKey(name))
57: throw new IllegalArgumentException("name " + name
58: + " is already in use");
59: String oldName = name();
60: super .name(name);
61: owner.fields.remove(oldName);
62: owner.fields.put(name, this );
63: }
64:
65: /**
66: * Creates, if necessary, and returns the class javadoc for this
67: * JDefinedClass
68: *
69: * @return JDocComment containing javadocs for this class
70: */
71: public JDocComment javadoc() {
72: if (jdoc == null)
73: jdoc = new JDocComment(owner.owner());
74: return jdoc;
75: }
76:
77: public void declare(JFormatter f) {
78: if (jdoc != null)
79: f.g(jdoc);
80: super.declare(f);
81: }
82:
83: }
|