KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > xdoclet > typesystem > impl > WrapperFactory


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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  * $Header:$
17  */

18 package org.apache.beehive.netui.compiler.xdoclet.typesystem.impl;
19
20 import org.apache.beehive.netui.compiler.typesystem.declaration.*;
21 import org.apache.beehive.netui.compiler.typesystem.type.ArrayType;
22 import org.apache.beehive.netui.compiler.typesystem.type.ClassType;
23 import org.apache.beehive.netui.compiler.typesystem.type.DeclaredType;
24 import org.apache.beehive.netui.compiler.typesystem.type.InterfaceType;
25 import org.apache.beehive.netui.compiler.typesystem.type.PrimitiveType;
26 import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
27 import org.apache.beehive.netui.compiler.typesystem.type.VoidType;
28 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.declaration.ClassDeclarationImpl;
29 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.declaration.ConstructorDeclarationImpl;
30 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.declaration.FieldDeclarationImpl;
31 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.declaration.InterfaceDeclarationImpl;
32 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.declaration.MethodDeclarationImpl;
33 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.declaration.PackageDeclarationImpl;
34 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.declaration.ParameterDeclarationImpl;
35 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.type.ArrayTypeImpl;
36 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.type.ClassTypeImpl;
37 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.type.InterfaceTypeImpl;
38 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.type.PrimitiveTypeImpl;
39 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.type.VoidTypeImpl;
40 import xjavadoc.*;
41
42 import java.util.HashMap JavaDoc;
43 import java.util.HashSet JavaDoc;
44 import java.util.Collections JavaDoc;
45 import java.util.Map JavaDoc;
46
47 public class WrapperFactory
48 {
49     private static final WrapperFactory INSTANCE = new WrapperFactory();
50     private static final HashSet JavaDoc PRIMITIVE_TYPES = new HashSet JavaDoc();
51     
52     static
53     {
54         PRIMITIVE_TYPES.add( "boolean" );
55         PRIMITIVE_TYPES.add( "byte" );
56         PRIMITIVE_TYPES.add( "short" );
57         PRIMITIVE_TYPES.add( "int" );
58         PRIMITIVE_TYPES.add( "long" );
59         PRIMITIVE_TYPES.add( "char" );
60         PRIMITIVE_TYPES.add( "float" );
61         PRIMITIVE_TYPES.add( "double" );
62     }
63     
64     private WrapperFactory()
65     {
66     }
67     
68     public static WrapperFactory get()
69     {
70         return INSTANCE;
71     }
72     
73     public TypeInstance getTypeInstance( Type delegate )
74     {
75         if ( delegate == null ) return null;
76         
77         if ( delegate.getDimension() > 0 )
78         {
79             return getArrayType( delegate );
80         }
81         else
82         {
83             return getTypeInstance( delegate.getType() );
84         }
85     }
86     
87     public TypeInstance getTypeInstance( XClass delegate, int dimension )
88     {
89         if ( delegate == null ) return null;
90         
91         if ( dimension > 0 )
92         {
93             return getArrayType( new SynthesizedXJavaDocArrayType( delegate, dimension ) );
94         }
95         else
96         {
97             return getTypeInstance( delegate );
98         }
99     }
100     
101     private static class SynthesizedXJavaDocArrayType
102             implements Type
103     {
104         private XClass _baseType;
105         private int _dimension;
106         
107         public SynthesizedXJavaDocArrayType( XClass type, int dimension )
108         {
109             _baseType = type;
110             _dimension = dimension;
111         }
112         
113         public int getDimension()
114         {
115             return _dimension;
116         }
117         
118         public String JavaDoc getDimensionAsString()
119         {
120             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
121             for ( int i = 0; i < _dimension; ++i )
122             {
123                 buf.append( "[]" );
124             }
125             return buf.toString();
126         }
127         
128         public XClass getType()
129         {
130             return _baseType;
131         }
132     }
133     
134     public TypeInstance getTypeInstance( XClass delegate )
135     {
136         if ( delegate == null ) return null;
137         
138         String JavaDoc name = delegate.getName();
139         
140         if ( name.equals( "void" ) )
141         {
142             return getVoidType( delegate );
143         }
144         else if ( PRIMITIVE_TYPES.contains( name ) )
145         {
146             return getPrimitiveType( delegate );
147         }
148         else
149         {
150             return getDeclaredType( delegate );
151         }
152     }
153     
154     public ArrayType getArrayType( Type delegate )
155     {
156         return new ArrayTypeImpl( delegate );
157     }
158     
159     public VoidType getVoidType( XClass delegate )
160     {
161         if ( delegate == null ) return null;
162         return new VoidTypeImpl( delegate );
163     }
164     
165     public PrimitiveType getPrimitiveType( XClass delegate )
166     {
167         if ( delegate == null ) return null;
168         return new PrimitiveTypeImpl( delegate );
169     }
170     
171     public DeclaredType getDeclaredType( XClass delegate )
172     {
173         if ( delegate == null ) return null;
174         return isInterface( delegate ) ? ( DeclaredType ) getInterfaceType( delegate ) : getClassType( delegate );
175     }
176     
177     private static boolean isInterface( XClass xClass )
178     {
179         // There's a bug where some returned XClass objects won't think they're interfaces, even when they are.
180
// In these cases, the word "interface" appears in the list of Modifiers.
181
if ( xClass.isInterface() ) return true;
182         return xClass.getModifiers().indexOf( "interface" ) != -1;
183     }
184     
185     public ClassType getClassType( XClass delegate )
186     {
187         if ( delegate == null ) return null;
188         return new ClassTypeImpl( delegate );
189     }
190     
191     public InterfaceType getInterfaceType( XClass delegate )
192     {
193         if ( delegate == null ) return null;
194         return new InterfaceTypeImpl( delegate );
195     }
196     
197     public Declaration getDeclaration( XProgramElement delegate )
198     {
199         if ( delegate == null ) return null;
200         
201         if ( delegate instanceof XMember )
202         {
203             return getMemberDeclaration( ( XMember ) delegate );
204         }
205         else
206         {
207             assert delegate instanceof XType : delegate.getClass().getName();
208             return getTypeDeclaration( ( XType ) delegate );
209         }
210     }
211     
212     public MemberDeclaration getMemberDeclaration( XMember delegate )
213     {
214         if ( delegate == null ) return null;
215         
216         else if ( delegate instanceof XExecutableMember )
217         {
218             return getExecutableDeclaration( ( XExecutableMember ) delegate );
219         }
220         else
221         {
222             assert delegate instanceof XField : delegate.getClass().getName();
223             return getFieldDeclaration( ( XField ) delegate );
224         }
225     }
226     
227     public TypeDeclaration getTypeDeclaration( XType delegate )
228     {
229         if ( delegate == null ) return null;
230         assert delegate instanceof XClass : delegate.getClass().getName();
231         XClass xclass = ( XClass ) delegate;
232         return isInterface( xclass ) ? ( TypeDeclaration ) getInterfaceDeclaration( xclass ) : getClassDeclaration( xclass );
233     }
234     
235     public ClassDeclaration getClassDeclaration( XClass delegate )
236     {
237         if ( delegate == null ) return null;
238         
239         String JavaDoc qualifiedName = delegate.getQualifiedName();
240         //ClassDeclaration decl = ( ClassDeclaration ) _classDeclarations.get( qualifiedName );
241
// if ( decl != null ) return decl;
242

243         return new ClassDeclarationImpl( delegate );
244 // _classDeclarations.put( qualifiedName, decl );
245
// return decl;
246
}
247     
248     public InterfaceDeclaration getInterfaceDeclaration( XClass delegate )
249     {
250         if ( delegate == null ) return null;
251         return new InterfaceDeclarationImpl( delegate );
252     }
253     
254     public ExecutableDeclaration getExecutableDeclaration( XExecutableMember delegate )
255     {
256         if ( delegate == null ) return null;
257         
258         if ( delegate instanceof XMethod )
259         {
260             return getMethodDeclaration( ( XMethod ) delegate );
261         }
262         
263         assert delegate instanceof XConstructor : delegate.getClass().getName();
264         return getConstructorDeclaration( ( XConstructor ) delegate );
265     }
266     
267     public ParameterDeclaration getParameterDeclaration( XParameter delegate )
268     {
269         if ( delegate == null ) return null;
270         return new ParameterDeclarationImpl( delegate );
271     }
272     
273     public PackageDeclaration getPackageDeclaration( XPackage delegate )
274     {
275         if ( delegate == null ) return null;
276         return new PackageDeclarationImpl( delegate );
277     }
278     
279     public ConstructorDeclaration getConstructorDeclaration( XConstructor delegate )
280     {
281         if ( delegate == null ) return null;
282         return new ConstructorDeclarationImpl( delegate );
283     }
284     
285     public MethodDeclaration getMethodDeclaration( XMethod delegate )
286     {
287         if ( delegate == null ) return null;
288         return new MethodDeclarationImpl( delegate );
289     }
290     
291     public FieldDeclaration getFieldDeclaration( XField delegate )
292     {
293         if ( delegate == null ) return null;
294         return new FieldDeclarationImpl( delegate );
295     }
296 }
297
Popular Tags