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 */ 25 26 package com.sun.codemodel.internal; 27 28 29 /** 30 * Field Reference 31 */ 32 33 public class JFieldRef extends JExpressionImpl implements JAssignmentTarget { 34 /** 35 * Object expression upon which this field will be accessed, or 36 * null for the implicit 'this'. 37 */ 38 private JGenerable object; 39 40 /** 41 * Name of the field to be accessed. Either this or {@link #var} is set. 42 */ 43 private String name; 44 45 /** 46 * Variable to be accessed. 47 */ 48 private JVar var; 49 50 /** 51 * Indicates if an explicit this should be generated 52 */ 53 private boolean explicitThis; 54 55 /** 56 * Field reference constructor given an object expression and field name 57 * 58 * @param object 59 * JExpression for the object upon which 60 * the named field will be accessed, 61 * 62 * @param name 63 * Name of field to access 64 */ JFieldRef(JExpression object, String name)65 JFieldRef(JExpression object, String name) { 66 this(object, name, false); 67 } 68 JFieldRef(JExpression object, JVar v)69 JFieldRef(JExpression object, JVar v) { 70 this(object, v, false); 71 } 72 73 /** 74 * Static field reference. 75 */ JFieldRef(JType type, String name)76 JFieldRef(JType type, String name) { 77 this(type, name, false); 78 } 79 JFieldRef(JType type, JVar v)80 JFieldRef(JType type, JVar v) { 81 this(type, v, false); 82 } 83 JFieldRef(JGenerable object, String name, boolean explicitThis)84 JFieldRef(JGenerable object, String name, boolean explicitThis) { 85 this.explicitThis = explicitThis; 86 this.object = object; 87 if (name.indexOf('.') >= 0) 88 throw new IllegalArgumentException("Field name contains '.': " + name); 89 this.name = name; 90 } 91 JFieldRef(JGenerable object, JVar var, boolean explicitThis)92 JFieldRef(JGenerable object, JVar var, boolean explicitThis) { 93 this.explicitThis = explicitThis; 94 this.object = object; 95 this.var = var; 96 } 97 generate(JFormatter f)98 public void generate(JFormatter f) { 99 String name = this.name; 100 if(name==null) name=var.name(); 101 102 if (object != null) { 103 f.g(object).p('.').p(name); 104 } else { 105 if (explicitThis) { 106 f.p("this.").p(name); 107 } else { 108 f.id(name); 109 } 110 } 111 } 112 assign(JExpression rhs)113 public JExpression assign(JExpression rhs) { 114 return JExpr.assign(this, rhs); 115 } assignPlus(JExpression rhs)116 public JExpression assignPlus(JExpression rhs) { 117 return JExpr.assignPlus(this, rhs); 118 } 119 } 120