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