1 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 ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.ResourceBundle ; 33 34 public class XDocletCompilerUtils 35 { 36 private static final ResourceBundle MESSAGES = 37 ResourceBundle.getBundle( XDocletCompilerUtils.class.getPackage().getName() + ".Messages" ); 38 39 public static void addError( SourcePosition sourcePosition, String messageKey, String [] args ) 40 { 41 assert sourcePosition != null; 42 String message = getMessage( messageKey, args ); 43 NetuiDocletTask.addError( message, sourcePosition ); 44 } 45 46 public static void addWarning( SourcePosition sourcePosition, String messageKey, String [] args ) 47 { 48 assert sourcePosition != null; 49 String message = getMessage( messageKey, args ); 50 NetuiDocletTask.addWarning( message, sourcePosition ); 51 } 52 53 public static String getMessage( String messageKey, String [] args ) 54 { 55 String message = MESSAGES.getString( messageKey ); 56 if ( args != null ) message = MessageFormat.format( message, args ); 57 return message; 58 } 59 60 public static TypeDeclaration resolveTypeDeclaration( String 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 typeName, XJavaDoc xJavaDoc ) 67 { 68 assert ! typeName.endsWith( "[]" ) : "array type not allowed here: " + typeName; 69 70 XClass type = xJavaDoc.getXClass( typeName ); 71 72 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 typeName, boolean allowErrorType, XClass currentClass ) 94 { 95 return ( TypeInstance ) resolveTypeInstanceOrTypeDecl( typeName, allowErrorType, currentClass, true ); 96 } 97 98 private static Object resolveTypeInstanceOrTypeDecl( String 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 List importedClasses = currentClass.getImportedClasses(); 126 String dotPrepended = '.' + typeName; 127 128 for ( Iterator 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 List importedPackages = currentClass.getImportedPackages(); 143 String dotPrepended = '.' + typeName; 144 145 for ( Iterator 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 String outerClassName = currentClass.getQualifiedName(); 161 attemptedResolvedType = getXClass( outerClassName + '.' + typeName, xJavaDoc ); 162 } 163 164 if ( isUnknownClass( attemptedResolvedType ) ) 165 { 166 String 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 |