001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.codemodel.internal;
027:
028: /**
029: * Field Reference
030: */
031:
032: public class JFieldRef extends JExpressionImpl implements
033: JAssignmentTarget {
034: /**
035: * Object expression upon which this field will be accessed, or
036: * null for the implicit 'this'.
037: */
038: private JGenerable object;
039:
040: /**
041: * Name of the field to be accessed. Either this or {@link #var} is set.
042: */
043: private String name;
044:
045: /**
046: * Variable to be accessed.
047: */
048: private JVar var;
049:
050: /**
051: * Indicates if an explicit this should be generated
052: */
053: private boolean explicitThis;
054:
055: /**
056: * Field reference constructor given an object expression and field name
057: *
058: * @param object
059: * JExpression for the object upon which
060: * the named field will be accessed,
061: *
062: * @param name
063: * Name of field to access
064: */
065: JFieldRef(JExpression object, String name) {
066: this (object, name, false);
067: }
068:
069: JFieldRef(JExpression object, JVar v) {
070: this (object, v, false);
071: }
072:
073: /**
074: * Static field reference.
075: */
076: JFieldRef(JType type, String name) {
077: this (type, name, false);
078: }
079:
080: JFieldRef(JType type, JVar v) {
081: this (type, v, false);
082: }
083:
084: JFieldRef(JGenerable object, String name, boolean explicitThis) {
085: this .explicitThis = explicitThis;
086: this .object = object;
087: if (name.indexOf('.') >= 0)
088: throw new IllegalArgumentException(
089: "Field name contains '.': " + name);
090: this .name = name;
091: }
092:
093: JFieldRef(JGenerable object, JVar var, boolean explicitThis) {
094: this .explicitThis = explicitThis;
095: this .object = object;
096: this .var = var;
097: }
098:
099: public void generate(JFormatter f) {
100: String name = this .name;
101: if (name == null)
102: name = var.name();
103:
104: if (object != null) {
105: f.g(object).p('.').p(name);
106: } else {
107: if (explicitThis) {
108: f.p("this.").p(name);
109: } else {
110: f.id(name);
111: }
112: }
113: }
114:
115: public JExpression assign(JExpression rhs) {
116: return JExpr.assign(this , rhs);
117: }
118:
119: public JExpression assignPlus(JExpression rhs) {
120: return JExpr.assignPlus(this, rhs);
121: }
122: }
|