Thu, 12 Oct 2017 19:44:07 +0800
merge
1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.codemodel.internal;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.lang.annotation.Annotation;
34 /**
35 * Enum Constant.
36 *
37 * When used as an {@link JExpression}, this object represents a reference to the enum constant.
38 *
39 * @author
40 * Bhakti Mehta (Bhakti.Mehta@sun.com)
41 */
42 public final class JEnumConstant extends JExpressionImpl implements JDeclaration, JAnnotatable, JDocCommentable {
44 /**
45 * The constant.
46 */
47 private final String name;
48 /**
49 * The enum class.
50 */
51 private final JDefinedClass type;
52 /**
53 * javadoc comments, if any.
54 */
55 private JDocComment jdoc = null;
57 /**
58 * Annotations on this variable. Lazily created.
59 */
60 private List<JAnnotationUse> annotations = null;
63 /**
64 * List of the constructor argument expressions.
65 * Lazily constructed.
66 */
67 private List<JExpression> args = null;
69 JEnumConstant(JDefinedClass type,String name) {
70 this.name = name;
71 this.type = type;
72 }
74 /**
75 * Add an expression to this constructor's argument list
76 *
77 * @param arg
78 * Argument to add to argument list
79 */
80 public JEnumConstant arg(JExpression arg) {
81 if(arg==null) throw new IllegalArgumentException();
82 if(args==null)
83 args = new ArrayList<JExpression>();
84 args.add(arg);
85 return this;
86 }
88 /**
89 * Returns the name of this constant.
90 *
91 * @return never null.
92 */
93 public String getName() {
94 return this.type.fullName().concat(".").concat(this.name);
95 }
97 /**
98 * Creates, if necessary, and returns the enum constant javadoc.
99 *
100 * @return JDocComment containing javadocs for this constant.
101 */
102 public JDocComment javadoc() {
103 if (jdoc == null)
104 jdoc = new JDocComment(type.owner());
105 return jdoc;
106 }
108 /**
109 * Adds an annotation to this variable.
110 * @param clazz
111 * The annotation class to annotate the field with
112 */
113 public JAnnotationUse annotate(JClass clazz){
114 if(annotations==null)
115 annotations = new ArrayList<JAnnotationUse>();
116 JAnnotationUse a = new JAnnotationUse(clazz);
117 annotations.add(a);
118 return a;
119 }
121 /**
122 * Adds an annotation to this variable.
123 *
124 * @param clazz
125 * The annotation class to annotate the field with
126 */
127 public JAnnotationUse annotate(Class <? extends Annotation> clazz){
128 return annotate(type.owner().ref(clazz));
129 }
131 public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
132 return TypedAnnotationWriter.create(clazz,this);
133 }
135 /**
136 * {@link JAnnotatable#annotations()}
137 */
138 public Collection<JAnnotationUse> annotations() {
139 if (annotations == null)
140 annotations = new ArrayList<JAnnotationUse>();
141 return Collections.unmodifiableList(annotations);
142 }
144 public void declare(JFormatter f) {
145 if( jdoc != null )
146 f.nl().g( jdoc );
147 if (annotations != null) {
148 for( int i=0; i<annotations.size(); i++ )
149 f.g(annotations.get(i)).nl();
150 }
151 f.id(name);
152 if(args!=null) {
153 f.p('(').g(args).p(')');
154 }
155 }
157 public void generate(JFormatter f) {
158 f.t(type).p('.').p(name);
159 }
160 }