Thu, 12 Oct 2017 19:44:07 +0800
merge
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 */
26 package com.sun.codemodel.internal;
29 /**
30 * Field Reference
31 */
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;
40 /**
41 * Name of the field to be accessed. Either this or {@link #var} is set.
42 */
43 private String name;
45 /**
46 * Variable to be accessed.
47 */
48 private JVar var;
50 /**
51 * Indicates if an explicit this should be generated
52 */
53 private boolean explicitThis;
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 */
65 JFieldRef(JExpression object, String name) {
66 this(object, name, false);
67 }
69 JFieldRef(JExpression object, JVar v) {
70 this(object, v, false);
71 }
73 /**
74 * Static field reference.
75 */
76 JFieldRef(JType type, String name) {
77 this(type, name, false);
78 }
80 JFieldRef(JType type, JVar v) {
81 this(type, v, false);
82 }
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 }
92 JFieldRef(JGenerable object, JVar var, boolean explicitThis) {
93 this.explicitThis = explicitThis;
94 this.object = object;
95 this.var = var;
96 }
98 public void generate(JFormatter f) {
99 String name = this.name;
100 if(name==null) name=var.name();
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 }
113 public JExpression assign(JExpression rhs) {
114 return JExpr.assign(this, rhs);
115 }
116 public JExpression assignPlus(JExpression rhs) {
117 return JExpr.assignPlus(this, rhs);
118 }
119 }