View Javadoc
1   /*
2    * Copyright 2009 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.javasource;
15  
16  /**
17   * A class which holds information about a constant. Modeled closely after the Java Reflection API.
18   * This class is part of package which is used to create source code in memory.
19   *
20   * @author <a href="mailto:wguttmn AT codehaus DOT org">Werner Guttmann</a>
21   * @since 1.3
22   */
23  public final class JConstant extends AbstractJField {
24  
25    /**
26     * Creates a new JConstant.
27     * 
28     * @param type JType of this new constant.
29     * @param name Name of this new constant.
30     */
31    public JConstant(final JType type, final String name) {
32      this(type, name, false);
33    }
34  
35  
36    /**
37     * Creates a new JConstant.
38     * 
39     * @param type JType of this new constant.
40     * @param name Name of this new constant.
41     * @param makePrivate True if constant definition should have private visibility.
42     */
43    public JConstant(final JType type, final String name, final boolean makePrivate) {
44      super(type, name);
45  
46      JModifiers modifiers = getModifiers();
47      if (makePrivate) {
48        modifiers.makePrivate();
49      } else {
50        modifiers.makePublic();
51      }
52  
53      modifiers.setFinal(true);
54      modifiers.setStatic(true);
55  
56      JDocComment comment = new JDocComment();
57      comment.appendComment("Constant " + name + ".");
58      setComment(comment);
59    }
60  
61  }