|
1 /* |
|
2 * Copyright 2006 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * 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 import java.util.ArrayList; |
|
29 import java.util.List; |
|
30 import java.lang.annotation.Annotation; |
|
31 |
|
32 /** |
|
33 * Enum Constant. |
|
34 * |
|
35 * When used as an {@link JExpression}, this object represents a reference to the enum constant. |
|
36 * |
|
37 * @author |
|
38 * Bhakti Mehta (Bhakti.Mehta@sun.com) |
|
39 */ |
|
40 public final class JEnumConstant extends JExpressionImpl implements JDeclaration, JAnnotatable { |
|
41 |
|
42 /** |
|
43 * The constant. |
|
44 */ |
|
45 private final String name; |
|
46 /** |
|
47 * The enum class. |
|
48 */ |
|
49 private final JDefinedClass type; |
|
50 /** |
|
51 * javadoc comments, if any. |
|
52 */ |
|
53 private JDocComment jdoc = null; |
|
54 |
|
55 /** |
|
56 * Annotations on this variable. Lazily created. |
|
57 */ |
|
58 private List<JAnnotationUse> annotations = null; |
|
59 |
|
60 |
|
61 /** |
|
62 * List of the constructor argument expressions. |
|
63 * Lazily constructed. |
|
64 */ |
|
65 private List<JExpression> args = null; |
|
66 |
|
67 JEnumConstant(JDefinedClass type,String name) { |
|
68 this.name = name; |
|
69 this.type = type; |
|
70 } |
|
71 |
|
72 /** |
|
73 * Add an expression to this constructor's argument list |
|
74 * |
|
75 * @param arg |
|
76 * Argument to add to argument list |
|
77 */ |
|
78 public JEnumConstant arg(JExpression arg) { |
|
79 if(arg==null) throw new IllegalArgumentException(); |
|
80 if(args==null) |
|
81 args = new ArrayList<JExpression>(); |
|
82 args.add(arg); |
|
83 return this; |
|
84 } |
|
85 |
|
86 /** |
|
87 * Returns the name of this constant. |
|
88 * |
|
89 * @return never null. |
|
90 */ |
|
91 public String getName() { |
|
92 return this.type.fullName().concat(".").concat(this.name); |
|
93 } |
|
94 |
|
95 /** |
|
96 * Creates, if necessary, and returns the enum constant javadoc. |
|
97 * |
|
98 * @return JDocComment containing javadocs for this constant. |
|
99 */ |
|
100 public JDocComment javadoc() { |
|
101 if (jdoc == null) |
|
102 jdoc = new JDocComment(type.owner()); |
|
103 return jdoc; |
|
104 } |
|
105 |
|
106 /** |
|
107 * Adds an annotation to this variable. |
|
108 * @param clazz |
|
109 * The annotation class to annotate the field with |
|
110 */ |
|
111 public JAnnotationUse annotate(JClass clazz){ |
|
112 if(annotations==null) |
|
113 annotations = new ArrayList<JAnnotationUse>(); |
|
114 JAnnotationUse a = new JAnnotationUse(clazz); |
|
115 annotations.add(a); |
|
116 return a; |
|
117 } |
|
118 |
|
119 /** |
|
120 * Adds an annotation to this variable. |
|
121 * |
|
122 * @param clazz |
|
123 * The annotation class to annotate the field with |
|
124 */ |
|
125 public JAnnotationUse annotate(Class <? extends Annotation> clazz){ |
|
126 return annotate(type.owner().ref(clazz)); |
|
127 } |
|
128 |
|
129 public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) { |
|
130 return TypedAnnotationWriter.create(clazz,this); |
|
131 } |
|
132 |
|
133 public void declare(JFormatter f) { |
|
134 if( jdoc != null ) |
|
135 f.nl().g( jdoc ); |
|
136 if (annotations != null) { |
|
137 for( int i=0; i<annotations.size(); i++ ) |
|
138 f.g(annotations.get(i)).nl(); |
|
139 } |
|
140 f.id(name); |
|
141 if(args!=null) { |
|
142 f.p('(').g(args).p(')'); |
|
143 } |
|
144 } |
|
145 |
|
146 public void generate(JFormatter f) { |
|
147 f.t(type).p('.').p(name); |
|
148 } |
|
149 } |