|
1 /* |
|
2 * Copyright (c) 1997, 2011, 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.tools.internal.xjc.generator.bean.field; |
|
27 |
|
28 import com.sun.codemodel.internal.JBlock; |
|
29 import com.sun.codemodel.internal.JExpression; |
|
30 import com.sun.codemodel.internal.JFieldRef; |
|
31 import com.sun.codemodel.internal.JFieldVar; |
|
32 import com.sun.codemodel.internal.JMod; |
|
33 import com.sun.codemodel.internal.JType; |
|
34 import com.sun.codemodel.internal.JVar; |
|
35 import com.sun.tools.internal.xjc.generator.bean.ClassOutlineImpl; |
|
36 import com.sun.tools.internal.xjc.model.CPropertyInfo; |
|
37 |
|
38 /** |
|
39 * |
|
40 * |
|
41 * @author |
|
42 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com), Martin Grebac |
|
43 */ |
|
44 abstract class AbstractFieldWithVar extends AbstractField { |
|
45 |
|
46 /** |
|
47 * Field declaration of the actual list object that we use |
|
48 * to store data. |
|
49 */ |
|
50 private JFieldVar field; |
|
51 |
|
52 /** |
|
53 * Invoke {@link #createField()} after calling the |
|
54 * constructor. |
|
55 */ |
|
56 AbstractFieldWithVar( ClassOutlineImpl outline, CPropertyInfo prop ) { |
|
57 super(outline,prop); |
|
58 } |
|
59 |
|
60 protected final void createField() { |
|
61 field = outline.implClass.field( JMod.PROTECTED, |
|
62 getFieldType(), prop.getName(false) ); |
|
63 |
|
64 annotate(field); |
|
65 } |
|
66 |
|
67 /** |
|
68 * Gets the name of the getter method. |
|
69 * |
|
70 * <p> |
|
71 * This encapsulation is necessary because sometimes we use |
|
72 * {@code isXXXX} as the method name. |
|
73 */ |
|
74 protected String getGetterMethod() { |
|
75 if (getOptions().enableIntrospection) { |
|
76 return ((getFieldType().isPrimitive() && |
|
77 getFieldType().boxify().getPrimitiveType()==codeModel.BOOLEAN) ? |
|
78 "is":"get") + prop.getName(true); |
|
79 } else { |
|
80 return (getFieldType().boxify().getPrimitiveType()==codeModel.BOOLEAN?"is":"get")+prop.getName(true); |
|
81 } |
|
82 } |
|
83 |
|
84 /** |
|
85 * Returns the type used to store the value of the field in memory. |
|
86 */ |
|
87 protected abstract JType getFieldType(); |
|
88 |
|
89 protected JFieldVar ref() { return field; } |
|
90 |
|
91 public final JType getRawType() { |
|
92 return exposedType; |
|
93 } |
|
94 |
|
95 protected abstract class Accessor extends AbstractField.Accessor { |
|
96 |
|
97 protected Accessor(JExpression $target) { |
|
98 super($target); |
|
99 this.$ref = $target.ref(AbstractFieldWithVar.this.ref()); |
|
100 } |
|
101 |
|
102 /** |
|
103 * Reference to the field bound by the target object. |
|
104 */ |
|
105 protected final JFieldRef $ref; |
|
106 |
|
107 public final void toRawValue(JBlock block, JVar $var) { |
|
108 if (getOptions().enableIntrospection) { |
|
109 block.assign($var,$target.invoke(getGetterMethod())); |
|
110 } else { |
|
111 block.assign($var,$target.invoke(getGetterMethod())); |
|
112 } |
|
113 } |
|
114 |
|
115 public final void fromRawValue(JBlock block, String uniqueName, JExpression $var) { |
|
116 block.invoke($target,("set"+prop.getName(true))).arg($var); |
|
117 } |
|
118 } |
|
119 } |