1/* 2 * Copyright (c) 1997, 2014, 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 */ 25 26package com.sun.codemodel.internal; 27 28import java.util.ArrayList; 29import java.util.List; 30import java.util.Collection; 31import java.util.Collections; 32import java.lang.annotation.Annotation; 33 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 */ 42public final class JEnumConstant extends JExpressionImpl implements JDeclaration, JAnnotatable, JDocCommentable { 43 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; 56 57 /** 58 * Annotations on this variable. Lazily created. 59 */ 60 private List<JAnnotationUse> annotations = null; 61 62 63 /** 64 * List of the constructor argument expressions. 65 * Lazily constructed. 66 */ 67 private List<JExpression> args = null; 68 69 JEnumConstant(JDefinedClass type,String name) { 70 this.name = name; 71 this.type = type; 72 } 73 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 } 87 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 } 96 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 } 107 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 } 120 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 } 130 131 public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) { 132 return TypedAnnotationWriter.create(clazz,this); 133 } 134 135 public boolean removeAnnotation(JAnnotationUse annotation) { 136 return this.annotations.remove(annotation); 137 } 138 /** 139 * {@link JAnnotatable#annotations()} 140 */ 141 public Collection<JAnnotationUse> annotations() { 142 if (annotations == null) 143 annotations = new ArrayList<JAnnotationUse>(); 144 return Collections.unmodifiableList(annotations); 145 } 146 147 public void declare(JFormatter f) { 148 if( jdoc != null ) 149 f.nl().g( jdoc ); 150 if (annotations != null) { 151 for( int i=0; i<annotations.size(); i++ ) 152 f.g(annotations.get(i)).nl(); 153 } 154 f.id(name); 155 if(args!=null) { 156 f.p('(').g(args).p(')'); 157 } 158 } 159 160 public void generate(JFormatter f) { 161 f.t(type).p('.').p(name); 162 } 163} 164