KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > runtime > generator > AptField


1 package org.apache.beehive.controls.runtime.generator;
2 /*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * $Header:$
18  */

19 import java.util.Collection JavaDoc;
20
21 import com.sun.mirror.declaration.FieldDeclaration;
22 import com.sun.mirror.declaration.Modifier;
23
24 /**
25  * The AptField class is a helper class that knows how to generate useful information
26  * about a Field using APT metadata
27  */

28 public class AptField
29 {
30     AptField(FieldDeclaration fieldDecl)
31     {
32         _fieldDecl = fieldDecl;
33     }
34
35     /**
36      * Returns the name of the method
37      */

38     public String JavaDoc getName()
39     {
40         if ( _fieldDecl == null )
41             return "";
42         return _fieldDecl.getSimpleName();
43     }
44
45     /**
46      * Returns a local variable used when setting the field value
47      */

48     public String JavaDoc getLocalName() { return "_" + getName(); }
49
50     /**
51      * Returns the type of the field
52      */

53     public String JavaDoc getType()
54     {
55         if ( _fieldDecl == null || _fieldDecl.getType() == null )
56             return "";
57         
58         return _fieldDecl.getType().toString();
59     }
60
61     /**
62      * Returns the class name of the field (does not include any formal type parameters
63      */

64     public String JavaDoc getClassName()
65     {
66         if ( _fieldDecl == null || _fieldDecl.getType() == null )
67             return "";
68
69         //
70
// This is lazily... but much easier than navigating the APT type system and just
71
// as effective ;)
72
String JavaDoc typeName = _fieldDecl.getType().toString();
73         int formalIndex = typeName.indexOf('<');
74         if (formalIndex > 0)
75             return typeName.substring(0, formalIndex);
76         return typeName;
77     }
78
79     /**
80      * Returns the access modifier associated with the field
81      */

82     public String JavaDoc getAccessModifier()
83     {
84         if ( _fieldDecl == null )
85             return "";
86         
87         Collection JavaDoc<Modifier> modifiers = _fieldDecl.getModifiers();
88         if (modifiers.contains(Modifier.PRIVATE))
89             return "private";
90         if (modifiers.contains(Modifier.PROTECTED))
91             return "protected";
92         if (modifiers.contains(Modifier.PUBLIC))
93             return "public";
94
95         return "";
96     }
97
98     /**
99      * Returns the name of a static local field using to refer to this Field
100      */

101     public String JavaDoc getReflectField()
102     {
103         return "_" + getName() + "Field";
104     }
105
106     protected FieldDeclaration _fieldDecl;
107 }
108
Popular Tags