1 /* 2 * Copyright 2009 Werner Guttmann 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package org.exolab.javasource; 17 18 /** 19 * A (abstract) base class which holds information about fields. Modeled closely after the 20 * Java Reflection API. This class is part of package which is used to create 21 * source code in memory. 22 * 23 * @author <a href="mailto:wguttmn AT codehaus DOT org">Werner Guttmann</a> 24 * @since 1.3 25 */ 26 public class AbstractJField extends JAnnotatedElementHelper implements JMember { 27 28 /** The set of modifiers for this JField. */ 29 private JModifiers _modifiers = null; 30 31 /** Type of this field. */ 32 private final JType _type; 33 34 /** Name of the field. */ 35 private String _name = null; 36 37 /** JavaDoc for this field. */ 38 private JDocComment _comment = new JDocComment(); 39 40 /** Initialization string for this field. */ 41 private String _initString = null; 42 43 /** Indicates whether this field is of type date/time. */ 44 private boolean _isDateTime = false; 45 46 /** The Class in this JField has been declared. */ 47 private JClass _declaringClass = null; 48 49 /** 50 * Creates a new JField. 51 * 52 * @param type JType of this new field. 53 * @param name Name of this new field. 54 */ 55 public AbstractJField(final JType type, final String name) { 56 setName(name); 57 58 _type = type; 59 60 setModifiers(new JModifiers()); 61 } 62 63 /** 64 * Returns the JavaDoc comment describing this member. 65 * 66 * @return The JavaDoc comment describing this member, or null if no comment 67 * has been set. 68 */ 69 public JDocComment getComment() { 70 return _comment; 71 } 72 73 /** 74 * Returns the class in which this JField has been declared. 75 * 76 * @return The class in which this JField has been declared. 77 */ 78 public JClass getDeclaringClass() { 79 return _declaringClass; 80 } 81 82 /** 83 * Returns the initialization String for this JField. 84 * 85 * @return The initialization String for this JField, or null if no 86 * initialization String was specified. 87 */ 88 public String getInitString() { 89 return _initString; 90 } 91 92 /** 93 * Returns the modifiers for this JField. 94 * 95 * @return The modifiers for this JField. 96 */ 97 public JModifiers getModifiers() { 98 return _modifiers; 99 } 100 101 /** 102 * Returns the name of this JField. 103 * 104 * @return The name of this JField. 105 */ 106 public String getName() { 107 return _name; 108 } 109 110 /** 111 * Returns the JType representing the type of this JField. 112 * 113 * @return The JType representing the type of this JField. 114 */ 115 public JType getType() { 116 return _type; 117 } 118 119 /** 120 * Sets the JavaDoc comment describing this JField. 121 * 122 * @param comment The JavaDoc comment for this JField. 123 */ 124 public void setComment(final JDocComment comment) { 125 _comment = comment; 126 } 127 128 /** 129 * Sets the JavaDoc comment describing this JField. 130 * 131 * @param comment The JavaDoc comment for this JField. 132 */ 133 public void setComment(final String comment) { 134 _comment.setComment(comment); 135 } 136 137 /** 138 * Sets the initialization string for this JField. This allows some 139 * flexibility in declaring default values. 140 * 141 * @param init The initialization string for this member. 142 */ 143 public void setInitString(final String init) { 144 _initString = init; 145 } 146 147 /** 148 * Sets the name of this JField. 149 * 150 * @param name The name of this JField. 151 */ 152 public void setName(final String name) { 153 if (!JNaming.isValidJavaIdentifier(name)) { 154 String err = "'" + name + "' is "; 155 if (JNaming.isKeyword(name)) { 156 err += "a reserved word and may not be used as " 157 + " a field name."; 158 } else { 159 err += "not a valid Java identifier."; 160 } 161 throw new IllegalArgumentException(err); 162 } 163 _name = name; 164 } 165 166 /** 167 * Sets the access modifiers on this JField. 168 * 169 * @param modifiers The access modifiers to be used for this JField. 170 */ 171 public void setModifiers(final JModifiers modifiers) { 172 _modifiers = modifiers; 173 } 174 175 /** 176 * Sets the class that declares this JField. 177 * 178 * @param declaringClass The class in which this Jfield is declared. 179 */ 180 protected void setDeclaringClass(final JClass declaringClass) { 181 _declaringClass = declaringClass; 182 } 183 184 /** 185 * Indicates whether this JField instance represents a field of type date/time. 186 * 187 * @return True if this field is of type date/time. 188 */ 189 public boolean isDateTime() { 190 return _isDateTime; 191 } 192 193 /** 194 * To indicate whether this JField instance represents a field of type date/time. 195 * 196 * @param isDateTime True if this field is of type date/time. 197 */ 198 public void setDateTime(final boolean isDateTime) { 199 _isDateTime = isDateTime; 200 } 201 202 /** 203 * {@inheritDoc} 204 */ 205 public String toString() { 206 StringBuilder sb = new StringBuilder(); 207 sb.append(_modifiers.toString()); 208 sb.append(' '); 209 sb.append(_type); 210 sb.append(' '); 211 sb.append(_name); 212 return sb.toString(); 213 } 214 215 }