1 /*
   2  * Copyright (c) 1997, 2012, 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 
  26 package com.sun.codemodel.internal;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 import java.util.Collection;
  32 import java.util.Collections;
  33 
  34 
  35 /**
  36  * Variables and fields.
  37  */
  38 
  39 public class JVar extends JExpressionImpl implements JDeclaration, JAssignmentTarget, JAnnotatable {
  40 
  41     /**
  42      * Modifiers.
  43      */
  44     private JMods mods;
  45 
  46     /**
  47      * JType of the variable
  48      */
  49     private JType type;
  50 
  51     /**
  52      * Name of the variable
  53      */
  54     private String name;
  55 
  56     /**
  57      * Initialization of the variable in its declaration
  58      */
  59     private JExpression init;
  60 
  61     /**
  62      * Annotations on this variable. Lazily created.
  63      */
  64     private List<JAnnotationUse> annotations = null;
  65 
  66 
  67 
  68     /**
  69      * JVar constructor
  70      *
  71      * @param type
  72      *        Datatype of this variable
  73      *
  74      * @param name
  75      *        Name of this variable
  76      *
  77      * @param init
  78      *        Value to initialize this variable to
  79      */
  80     JVar(JMods mods, JType type, String name, JExpression init) {
  81         this.mods = mods;
  82         this.type = type;
  83         this.name = name;
  84         this.init = init;
  85     }
  86 
  87 
  88     /**
  89      * Initialize this variable
  90      *
  91      * @param init
  92      *        JExpression to be used to initialize this field
  93      */
  94     public JVar init(JExpression init) {
  95         this.init = init;
  96         return this;
  97     }
  98 
  99     /**
 100      * Get the name of this variable
 101      *
 102      * @return Name of the variable
 103      */
 104     public String name() {
 105         return name;
 106     }
 107 
 108     /**
 109      * Changes the name of this variable.
 110      */
 111     public void name(String name) {
 112         if(!JJavaName.isJavaIdentifier(name))
 113             throw new IllegalArgumentException();
 114         this.name = name;
 115     }
 116 
 117     /**
 118      * Return the type of this variable.
 119      * @return
 120      *      always non-null.
 121      */
 122     public JType type() {
 123         return type;
 124     }
 125 
 126     /**
 127      * @return
 128      *      the current modifiers of this method.
 129      *      Always return non-null valid object.
 130      */
 131     public JMods mods() {
 132         return mods;
 133     }
 134 
 135     /**
 136      * Sets the type of this variable.
 137      *
 138      * @param newType
 139      *      must not be null.
 140      *
 141      * @return
 142      *      the old type value. always non-null.
 143      */
 144     public JType type(JType newType) {
 145         JType r = type;
 146         if(newType==null)
 147             throw new IllegalArgumentException();
 148         type = newType;
 149         return r;
 150     }
 151 
 152 
 153     /**
 154      * Adds an annotation to this variable.
 155      * @param clazz
 156      *          The annotation class to annotate the field with
 157      */
 158     public JAnnotationUse annotate(JClass clazz){
 159         if(annotations==null)
 160            annotations = new ArrayList<JAnnotationUse>();
 161         JAnnotationUse a = new JAnnotationUse(clazz);
 162         annotations.add(a);
 163         return a;
 164     }
 165 
 166     /**
 167      * Adds an annotation to this variable.
 168      *
 169      * @param clazz
 170      *          The annotation class to annotate the field with
 171      */
 172     public JAnnotationUse annotate(Class <? extends Annotation> clazz){
 173         return annotate(type.owner().ref(clazz));
 174     }
 175 
 176     public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
 177         return TypedAnnotationWriter.create(clazz,this);
 178     }
 179 
 180     public Collection<JAnnotationUse> annotations() {
 181         if (annotations == null)
 182             annotations = new ArrayList<JAnnotationUse>();
 183         return Collections.unmodifiableList(annotations);
 184     }
 185 
 186     protected boolean isAnnotated() {
 187         return annotations!=null;
 188     }
 189 
 190     public void bind(JFormatter f) {
 191         if (annotations != null){
 192             for( int i=0; i<annotations.size(); i++ )
 193                 f.g(annotations.get(i)).nl();
 194         }
 195         f.g(mods).g(type).id(name);
 196         if (init != null)
 197             f.p('=').g(init);
 198     }
 199 
 200     public void declare(JFormatter f) {
 201         f.b(this).p(';').nl();
 202     }
 203 
 204     public void generate(JFormatter f) {
 205         f.id(name);
 206     }
 207 
 208 
 209     public JExpression assign(JExpression rhs) {
 210                 return JExpr.assign(this,rhs);
 211     }
 212     public JExpression assignPlus(JExpression rhs) {
 213                 return JExpr.assignPlus(this,rhs);
 214     }
 215 
 216 }