KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > xdoclet > XDocletCompilerUtils


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.xdoclet;
19
20 import org.apache.beehive.netui.compiler.typesystem.util.SourcePosition;
21 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
22 import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
23 import org.apache.beehive.netui.compiler.xdoclet.typesystem.impl.WrapperFactory;
24 import xjavadoc.XClass;
25 import xjavadoc.XJavaDoc;
26 import xjavadoc.XPackage;
27 import xjavadoc.Type;
28
29 import java.text.MessageFormat JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33
34 public class XDocletCompilerUtils
35 {
36     private static final ResourceBundle JavaDoc MESSAGES =
37             ResourceBundle.getBundle( XDocletCompilerUtils.class.getPackage().getName() + ".Messages" );
38     
39     public static void addError( SourcePosition sourcePosition, String JavaDoc messageKey, String JavaDoc[] args )
40     {
41         assert sourcePosition != null;
42         String JavaDoc message = getMessage( messageKey, args );
43         NetuiDocletTask.addError( message, sourcePosition );
44     }
45     
46     public static void addWarning( SourcePosition sourcePosition, String JavaDoc messageKey, String JavaDoc[] args )
47     {
48         assert sourcePosition != null;
49         String JavaDoc message = getMessage( messageKey, args );
50         NetuiDocletTask.addWarning( message, sourcePosition );
51     }
52     
53     public static String JavaDoc getMessage( String JavaDoc messageKey, String JavaDoc[] args )
54     {
55         String JavaDoc message = MESSAGES.getString( messageKey );
56         if ( args != null ) message = MessageFormat.format( message, args );
57         return message;
58     }
59     
60     public static TypeDeclaration resolveTypeDeclaration( String JavaDoc typeName )
61     {
62         assert ! typeName.endsWith( "[]" ) : "array type not allowed here: " + typeName;
63         return ( TypeDeclaration ) resolveTypeInstanceOrTypeDecl( typeName, true, null, false );
64     }
65     
66     private static XClass getXClass( String JavaDoc typeName, XJavaDoc xJavaDoc )
67     {
68         assert ! typeName.endsWith( "[]" ) : "array type not allowed here: " + typeName;
69         
70         XClass type = xJavaDoc.getXClass( typeName );
71         
72         //
73
// This may be an inner class, which needs a '$' instead of a '.'.
74
//
75
if ( isUnknownClass( type ) )
76         {
77             int lastDot = typeName.lastIndexOf( '.' );
78             
79             if ( lastDot != -1 )
80             {
81                 return getXClass( typeName.substring( 0, lastDot ) + '$' + typeName.substring( lastDot + 1 ), xJavaDoc );
82             }
83         }
84         
85         return type;
86     }
87     
88     private static boolean isUnknownClass( XClass xclass )
89     {
90         return xclass == null || xclass.getClass().getName().equals( "xjavadoc.UnknownClass" );
91     }
92     
93     public static TypeInstance resolveType( String JavaDoc typeName, boolean allowErrorType, XClass currentClass )
94     {
95         return ( TypeInstance ) resolveTypeInstanceOrTypeDecl( typeName, allowErrorType, currentClass, true );
96     }
97     
98     private static Object JavaDoc resolveTypeInstanceOrTypeDecl( String JavaDoc typeName, boolean allowUnknownType, XClass currentClass,
99                                                          boolean returnTypeInstance )
100     {
101         int arrayDimensions = 0;
102         
103         if ( typeName.endsWith( ".class" ) ) typeName = typeName.substring( 0, typeName.length() - 6 );
104         
105         while ( typeName.endsWith( "[]" ) )
106         {
107             typeName = typeName.substring( 0, typeName.length() - 2 );
108             ++arrayDimensions;
109         }
110         
111         if ( currentClass == null ) currentClass = NetuiSubTask.get().getCurrentSourceClass();
112         XJavaDoc xJavaDoc = currentClass.getXJavaDoc();
113         
114         XClass originalResolvedType = getXClass( typeName, xJavaDoc );
115         XClass attemptedResolvedType = originalResolvedType;
116         
117         if ( isUnknownClass( attemptedResolvedType ) )
118         {
119             attemptedResolvedType = getXClass( "java.lang." + typeName, xJavaDoc );
120         }
121             
122         if ( isUnknownClass( attemptedResolvedType ) )
123         {
124             // See if it was an imported class.
125
List JavaDoc importedClasses = currentClass.getImportedClasses();
126             String JavaDoc dotPrepended = '.' + typeName;
127                   
128             for ( Iterator JavaDoc i = importedClasses.iterator(); i.hasNext(); )
129             {
130                 XClass importedClass = ( XClass ) i.next();
131                 if ( importedClass.getQualifiedName().endsWith( dotPrepended ) )
132                 {
133                     attemptedResolvedType = getXClass( importedClass.getQualifiedName(), xJavaDoc );
134                     break;
135                 }
136             }
137         }
138               
139         if ( isUnknownClass( attemptedResolvedType ) )
140         {
141             // See if it was in an imported package.
142
List JavaDoc importedPackages = currentClass.getImportedPackages();
143             String JavaDoc dotPrepended = '.' + typeName;
144                   
145             for ( Iterator JavaDoc i = importedPackages.iterator(); i.hasNext(); )
146             {
147                 XPackage importedPackage = ( XPackage ) i.next();
148                 XClass implicitImportedClass = getXClass( importedPackage.getName() + dotPrepended, xJavaDoc );
149                 if ( ! isUnknownClass( implicitImportedClass ) )
150                 {
151                     attemptedResolvedType = implicitImportedClass;
152                     break;
153                 }
154             }
155         }
156                   
157         if ( isUnknownClass( attemptedResolvedType ) )
158         {
159             // Try it with the full outer classname appended.
160
String JavaDoc outerClassName = currentClass.getQualifiedName();
161             attemptedResolvedType = getXClass( outerClassName + '.' + typeName, xJavaDoc );
162         }
163                   
164         if ( isUnknownClass( attemptedResolvedType ) )
165         {
166             // Finally, it may be of the form <outer-class-short-name>.<inner-class-name>
167
String JavaDoc outerClassName = currentClass.getQualifiedName();
168             int lastDot = outerClassName.lastIndexOf( '.' );
169             outerClassName = lastDot != -1 ? outerClassName.substring( 0, lastDot ) : outerClassName;
170             attemptedResolvedType = getXClass( outerClassName + '.' + typeName, xJavaDoc );
171         }
172         
173         if ( isUnknownClass( attemptedResolvedType ) )
174         {
175             if ( ! allowUnknownType ) return null;
176             if ( returnTypeInstance ) return WrapperFactory.get().getTypeInstance( originalResolvedType );
177             return WrapperFactory.get().getTypeDeclaration( originalResolvedType );
178         }
179         
180         if ( returnTypeInstance ) return WrapperFactory.get().getTypeInstance( attemptedResolvedType, arrayDimensions );
181         return WrapperFactory.get().getTypeDeclaration( attemptedResolvedType );
182     }
183 }
184
Popular Tags