325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.codemodel.internal;
325N/A
325N/Aimport java.lang.annotation.Annotation;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.List;
325N/Aimport java.util.Collection;
325N/Aimport java.util.Collections;
325N/A
325N/A
325N/A/**
325N/A * Variables and fields.
325N/A */
325N/A
325N/Apublic class JVar extends JExpressionImpl implements JDeclaration, JAssignmentTarget, JAnnotatable {
325N/A
325N/A /**
325N/A * Modifiers.
325N/A */
325N/A private JMods mods;
325N/A
325N/A /**
325N/A * JType of the variable
325N/A */
325N/A private JType type;
325N/A
325N/A /**
325N/A * Name of the variable
325N/A */
325N/A private String name;
325N/A
325N/A /**
325N/A * Initialization of the variable in its declaration
325N/A */
325N/A private JExpression init;
325N/A
325N/A /**
325N/A * Annotations on this variable. Lazily created.
325N/A */
325N/A private List<JAnnotationUse> annotations = null;
325N/A
325N/A
325N/A
325N/A /**
325N/A * JVar constructor
325N/A *
325N/A * @param type
325N/A * Datatype of this variable
325N/A *
325N/A * @param name
325N/A * Name of this variable
325N/A *
325N/A * @param init
325N/A * Value to initialize this variable to
325N/A */
325N/A JVar(JMods mods, JType type, String name, JExpression init) {
325N/A this.mods = mods;
325N/A this.type = type;
325N/A this.name = name;
325N/A this.init = init;
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Initialize this variable
325N/A *
325N/A * @param init
325N/A * JExpression to be used to initialize this field
325N/A */
325N/A public JVar init(JExpression init) {
325N/A this.init = init;
325N/A return this;
325N/A }
325N/A
325N/A /**
325N/A * Get the name of this variable
325N/A *
325N/A * @return Name of the variable
325N/A */
325N/A public String name() {
325N/A return name;
325N/A }
325N/A
325N/A /**
325N/A * Changes the name of this variable.
325N/A */
325N/A public void name(String name) {
325N/A if(!JJavaName.isJavaIdentifier(name))
325N/A throw new IllegalArgumentException();
325N/A this.name = name;
325N/A }
325N/A
325N/A /**
325N/A * Return the type of this variable.
325N/A * @return
325N/A * always non-null.
325N/A */
325N/A public JType type() {
325N/A return type;
325N/A }
325N/A
325N/A /**
325N/A * @return
325N/A * the current modifiers of this method.
325N/A * Always return non-null valid object.
325N/A */
325N/A public JMods mods() {
325N/A return mods;
325N/A }
325N/A
325N/A /**
325N/A * Sets the type of this variable.
325N/A *
325N/A * @param newType
325N/A * must not be null.
325N/A *
325N/A * @return
325N/A * the old type value. always non-null.
325N/A */
325N/A public JType type(JType newType) {
325N/A JType r = type;
325N/A if(newType==null)
325N/A throw new IllegalArgumentException();
325N/A type = newType;
325N/A return r;
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Adds an annotation to this variable.
325N/A * @param clazz
325N/A * The annotation class to annotate the field with
325N/A */
325N/A public JAnnotationUse annotate(JClass clazz){
325N/A if(annotations==null)
325N/A annotations = new ArrayList<JAnnotationUse>();
325N/A JAnnotationUse a = new JAnnotationUse(clazz);
325N/A annotations.add(a);
325N/A return a;
325N/A }
325N/A
325N/A /**
325N/A * Adds an annotation to this variable.
325N/A *
325N/A * @param clazz
325N/A * The annotation class to annotate the field with
325N/A */
325N/A public JAnnotationUse annotate(Class <? extends Annotation> clazz){
325N/A return annotate(type.owner().ref(clazz));
325N/A }
325N/A
325N/A public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
325N/A return TypedAnnotationWriter.create(clazz,this);
325N/A }
325N/A
325N/A public Collection<JAnnotationUse> annotations() {
325N/A if (annotations == null)
325N/A annotations = new ArrayList<JAnnotationUse>();
325N/A return Collections.unmodifiableList(annotations);
325N/A }
325N/A
325N/A protected boolean isAnnotated() {
325N/A return annotations!=null;
325N/A }
325N/A
325N/A public void bind(JFormatter f) {
325N/A if (annotations != null){
325N/A for( int i=0; i<annotations.size(); i++ )
325N/A f.g(annotations.get(i)).nl();
325N/A }
325N/A f.g(mods).g(type).id(name);
325N/A if (init != null)
325N/A f.p('=').g(init);
325N/A }
325N/A
325N/A public void declare(JFormatter f) {
325N/A f.b(this).p(';').nl();
325N/A }
325N/A
325N/A public void generate(JFormatter f) {
325N/A f.id(name);
325N/A }
325N/A
325N/A
325N/A public JExpression assign(JExpression rhs) {
325N/A return JExpr.assign(this,rhs);
325N/A }
325N/A public JExpression assignPlus(JExpression rhs) {
325N/A return JExpr.assignPlus(this,rhs);
325N/A }
325N/A
325N/A}