KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 import java.util.Collection JavaDoc;
21
22 import com.sun.mirror.declaration.ClassDeclaration;
23 import com.sun.mirror.declaration.FieldDeclaration;
24 import com.sun.mirror.declaration.InterfaceDeclaration;
25 import com.sun.mirror.declaration.TypeDeclaration;
26 import com.sun.mirror.type.DeclaredType;
27 import com.sun.mirror.type.InterfaceType;
28 import com.sun.mirror.type.TypeMirror;
29
30 import org.apache.beehive.controls.api.bean.ControlExtension;
31 import org.apache.beehive.controls.api.bean.ControlInterface;
32 import org.apache.beehive.controls.api.versioning.VersionRequired;
33 import org.apache.beehive.controls.runtime.generator.apt.TwoPhaseAnnotationProcessor;
34
35 /**
36  * The AptControlField class contains information about a field that refers to a nested control.
37  */

38 public class AptControlField extends AptEventField
39 {
40     /**
41      * Base constructor, protected so only a custom subclass can invoke
42      * @param controlClient the declaring AptType
43      */

44     public AptControlField(AptType controlClient, FieldDeclaration controlDecl,
45                            TwoPhaseAnnotationProcessor ap)
46     {
47         super( controlDecl );
48         _controlClient = controlClient;
49         _ap = ap;
50         _controlBean = new ControlBean(getControlInterface());
51     };
52
53     /**
54      * Does this control field have a VersionRequired annotation?
55      * @return
56      */

57     public boolean hasVersionRequired()
58     {
59         return ( _fieldDecl.getAnnotation( VersionRequired.class ) != null );
60     }
61
62     /**
63      * Initializes the ControlInterface associated with this ControlField
64      */

65     protected AptControlInterface initControlInterface()
66     {
67         TypeMirror controlType = _fieldDecl.getType();
68         if (! (controlType instanceof DeclaredType))
69         {
70             _ap.printError( _fieldDecl, "control.field.bad.type" );
71             return null;
72         }
73
74         //
75
// The field can either be declared as the bean type or the public interface type.
76
// If it is the bean type, then we need to reflect to find the public interface
77
// type it implements.
78
//
79
TypeDeclaration typeDecl = ((DeclaredType)controlType).getDeclaration();
80         InterfaceDeclaration controlIntf = null;
81
82         //
83
// It is possible that the declared type is associated with a to-be-generated
84
// bean type. In this case, look for the associated control interface on the
85
// processor input list.
86
//
87
if ( typeDecl == null )
88         {
89             String JavaDoc className = controlType.toString();
90             String JavaDoc intfName = className.substring(0, className.length() - 4);
91             controlIntf = (InterfaceDeclaration)_ap.getAnnotationProcessorEnvironment().getTypeDeclaration(intfName);
92             if (controlIntf == null)
93             {
94                 // The specified class name may not be fully qualified. In this case, the
95
// best we can do is look for a best fit match against the input types
96
for (TypeDeclaration td :_ap.getAnnotationProcessorEnvironment().getSpecifiedTypeDeclarations())
97                 {
98                     if (td instanceof InterfaceDeclaration &&
99                         td.getSimpleName().equals(intfName))
100                     {
101                         controlIntf = (InterfaceDeclaration)td;
102                         break;
103                     }
104                 }
105             }
106         }
107         else if (typeDecl instanceof ClassDeclaration)
108         {
109             Collection JavaDoc<InterfaceType> implIntfs = ((ClassDeclaration)typeDecl).getSuperinterfaces();
110             for (InterfaceType intfType : implIntfs)
111             {
112                 InterfaceDeclaration intfDecl = intfType.getDeclaration();
113
114                 if ( intfDecl == null )
115                     return null;
116                 
117                 if (intfDecl.getAnnotation(ControlInterface.class) != null||
118                     intfDecl.getAnnotation(ControlExtension.class) != null)
119                 {
120                     controlIntf = intfDecl;
121                     break;
122                 }
123             }
124         }
125         else if (typeDecl instanceof InterfaceDeclaration)
126         {
127             controlIntf = (InterfaceDeclaration)typeDecl;
128         }
129
130         if (controlIntf == null)
131         {
132             _ap.printError( _fieldDecl, "control.field.bad.type.2" );
133             return null;
134         }
135
136         return new AptControlInterface(controlIntf, _ap);
137     }
138
139     /**
140      * Returns the ControlBean associated with this ControlField
141      */

142     public ControlBean getControlBean() { return _controlBean; }
143
144     private TwoPhaseAnnotationProcessor _ap;
145     private AptType _controlClient;
146     private ControlBean _controlBean;
147 }
148
Popular Tags