001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.core.ext.typeinfo;
017:
018: import java.lang.annotation.Annotation;
019: import java.util.Map;
020:
021: /**
022: * Represents a field declaration.
023: */
024: public class JField implements HasAnnotations, HasMetaData {
025:
026: private final Annotations annotations;
027:
028: private final JClassType enclosingType;
029:
030: private final HasMetaData metaData = new MetaData();
031:
032: private int modifierBits;
033:
034: private final String name;
035:
036: private JType type;
037:
038: public JField(JClassType enclosingType, String name) {
039: this (enclosingType, name, null);
040: }
041:
042: public JField(
043: JClassType enclosingType,
044: String name,
045: Map<Class<? extends Annotation>, Annotation> declaredAnnotations) {
046: assert (enclosingType != null);
047: this .enclosingType = enclosingType;
048: this .name = name;
049: this .enclosingType.addField(this );
050: annotations = new Annotations();
051: annotations.addAnnotations(declaredAnnotations);
052: }
053:
054: JField(JClassType enclosingType, JField srcField) {
055: this .annotations = new Annotations(srcField.annotations);
056: this .enclosingType = enclosingType;
057: this .modifierBits = srcField.modifierBits;
058: this .name = srcField.name;
059: this .type = srcField.type;
060:
061: MetaData.copy(this , srcField);
062: }
063:
064: public void addMetaData(String tagName, String[] values) {
065: metaData.addMetaData(tagName, values);
066: }
067:
068: public void addModifierBits(int modifierBits) {
069: this .modifierBits |= modifierBits;
070: }
071:
072: public <T extends Annotation> T getAnnotation(
073: Class<T> annotationClass) {
074: return annotations.getAnnotation(annotationClass);
075: }
076:
077: public Annotation[] getAnnotations() {
078: return annotations.getAnnotations();
079: }
080:
081: public Annotation[] getDeclaredAnnotations() {
082: return annotations.getDeclaredAnnotations();
083: }
084:
085: public JClassType getEnclosingType() {
086: return enclosingType;
087: }
088:
089: public String[][] getMetaData(String tagName) {
090: return metaData.getMetaData(tagName);
091: }
092:
093: public String[] getMetaDataTags() {
094: return metaData.getMetaDataTags();
095: }
096:
097: public String getName() {
098: assert (name != null);
099: return name;
100: }
101:
102: public JType getType() {
103: assert (type != null);
104: return type;
105: }
106:
107: public boolean isAnnotationPresent(
108: Class<? extends Annotation> annotationClass) {
109: return annotations.isAnnotationPresent(annotationClass);
110: }
111:
112: public boolean isDefaultAccess() {
113: return 0 == (modifierBits & (TypeOracle.MOD_PUBLIC
114: | TypeOracle.MOD_PRIVATE | TypeOracle.MOD_PROTECTED));
115: }
116:
117: public JEnumConstant isEnumConstant() {
118: return null;
119: }
120:
121: public boolean isFinal() {
122: return 0 != (modifierBits & TypeOracle.MOD_FINAL);
123: }
124:
125: public boolean isPrivate() {
126: return 0 != (modifierBits & TypeOracle.MOD_PRIVATE);
127: }
128:
129: public boolean isProtected() {
130: return 0 != (modifierBits & TypeOracle.MOD_PROTECTED);
131: }
132:
133: public boolean isPublic() {
134: return 0 != (modifierBits & TypeOracle.MOD_PUBLIC);
135: }
136:
137: public boolean isStatic() {
138: return 0 != (modifierBits & TypeOracle.MOD_STATIC);
139: }
140:
141: public boolean isTransient() {
142: return 0 != (modifierBits & TypeOracle.MOD_TRANSIENT);
143: }
144:
145: public boolean isVolatile() {
146: return 0 != (modifierBits & TypeOracle.MOD_VOLATILE);
147: }
148:
149: public void setType(JType type) {
150: this .type = type;
151: }
152:
153: public String toString() {
154: String[] names = TypeOracle.modifierBitsToNames(modifierBits);
155: StringBuffer sb = new StringBuffer();
156: for (int i = 0; i < names.length; i++) {
157: if (i > 0) {
158: sb.append(" ");
159: }
160: sb.append(names[i]);
161: }
162: if (names.length > 0) {
163: sb.append(" ");
164: }
165: sb.append(type.getQualifiedSourceName());
166: sb.append(" ");
167: sb.append(getName());
168: return sb.toString();
169: }
170: }
|