Tue, 14 May 2013 10:14:55 -0700
8012178: Cleanup use of Util.escapeHtmlChars
Reviewed-by: darcy
duke@1 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.doclets.internal.toolkit.util; |
duke@1 | 27 | |
jjg@197 | 28 | import java.io.*; |
jjg@1521 | 29 | import java.lang.annotation.ElementType; |
jjg@197 | 30 | import java.util.*; |
jjg@197 | 31 | |
duke@1 | 32 | import com.sun.javadoc.*; |
jjg@1521 | 33 | import com.sun.javadoc.AnnotationDesc.ElementValuePair; |
duke@1 | 34 | import com.sun.tools.doclets.internal.toolkit.*; |
jjg@1383 | 35 | import javax.tools.StandardLocation; |
duke@1 | 36 | |
duke@1 | 37 | /** |
duke@1 | 38 | * Utilities Class for Doclets. |
duke@1 | 39 | * |
jjg@1359 | 40 | * <p><b>This is NOT part of any supported API. |
jjg@1359 | 41 | * If you write code that depends on this, you do so at your own risk. |
jjg@1359 | 42 | * This code and its internal interfaces are subject to change or |
jjg@1359 | 43 | * deletion without notice.</b> |
duke@1 | 44 | * |
duke@1 | 45 | * @author Atul M Dambalkar |
duke@1 | 46 | * @author Jamie Ho |
duke@1 | 47 | */ |
duke@1 | 48 | public class Util { |
duke@1 | 49 | |
duke@1 | 50 | /** |
duke@1 | 51 | * Return array of class members whose documentation is to be generated. |
duke@1 | 52 | * If the member is deprecated do not include such a member in the |
duke@1 | 53 | * returned array. |
duke@1 | 54 | * |
duke@1 | 55 | * @param members Array of members to choose from. |
duke@1 | 56 | * @return ProgramElementDoc[] Array of eligible members for whom |
duke@1 | 57 | * documentation is getting generated. |
duke@1 | 58 | */ |
duke@1 | 59 | public static ProgramElementDoc[] excludeDeprecatedMembers( |
duke@1 | 60 | ProgramElementDoc[] members) { |
duke@1 | 61 | return |
duke@1 | 62 | toProgramElementDocArray(excludeDeprecatedMembersAsList(members)); |
duke@1 | 63 | } |
duke@1 | 64 | |
duke@1 | 65 | /** |
duke@1 | 66 | * Return array of class members whose documentation is to be generated. |
duke@1 | 67 | * If the member is deprecated do not include such a member in the |
duke@1 | 68 | * returned array. |
duke@1 | 69 | * |
duke@1 | 70 | * @param members Array of members to choose from. |
duke@1 | 71 | * @return List List of eligible members for whom |
duke@1 | 72 | * documentation is getting generated. |
duke@1 | 73 | */ |
jjg@74 | 74 | public static List<ProgramElementDoc> excludeDeprecatedMembersAsList( |
duke@1 | 75 | ProgramElementDoc[] members) { |
jjg@74 | 76 | List<ProgramElementDoc> list = new ArrayList<ProgramElementDoc>(); |
duke@1 | 77 | for (int i = 0; i < members.length; i++) { |
duke@1 | 78 | if (members[i].tags("deprecated").length == 0) { |
duke@1 | 79 | list.add(members[i]); |
duke@1 | 80 | } |
duke@1 | 81 | } |
duke@1 | 82 | Collections.sort(list); |
duke@1 | 83 | return list; |
duke@1 | 84 | } |
duke@1 | 85 | |
duke@1 | 86 | /** |
duke@1 | 87 | * Return the list of ProgramElementDoc objects as Array. |
duke@1 | 88 | */ |
mcimadamore@184 | 89 | public static ProgramElementDoc[] toProgramElementDocArray(List<ProgramElementDoc> list) { |
duke@1 | 90 | ProgramElementDoc[] pgmarr = new ProgramElementDoc[list.size()]; |
duke@1 | 91 | for (int i = 0; i < list.size(); i++) { |
mcimadamore@184 | 92 | pgmarr[i] = list.get(i); |
duke@1 | 93 | } |
duke@1 | 94 | return pgmarr; |
duke@1 | 95 | } |
duke@1 | 96 | |
duke@1 | 97 | /** |
duke@1 | 98 | * Return true if a non-public member found in the given array. |
duke@1 | 99 | * |
duke@1 | 100 | * @param members Array of members to look into. |
duke@1 | 101 | * @return boolean True if non-public member found, false otherwise. |
duke@1 | 102 | */ |
duke@1 | 103 | public static boolean nonPublicMemberFound(ProgramElementDoc[] members) { |
duke@1 | 104 | for (int i = 0; i < members.length; i++) { |
duke@1 | 105 | if (!members[i].isPublic()) { |
duke@1 | 106 | return true; |
duke@1 | 107 | } |
duke@1 | 108 | } |
duke@1 | 109 | return false; |
duke@1 | 110 | } |
duke@1 | 111 | |
duke@1 | 112 | /** |
duke@1 | 113 | * Search for the given method in the given class. |
duke@1 | 114 | * |
duke@1 | 115 | * @param cd Class to search into. |
duke@1 | 116 | * @param method Method to be searched. |
duke@1 | 117 | * @return MethodDoc Method found, null otherwise. |
duke@1 | 118 | */ |
duke@1 | 119 | public static MethodDoc findMethod(ClassDoc cd, MethodDoc method) { |
duke@1 | 120 | MethodDoc[] methods = cd.methods(); |
duke@1 | 121 | for (int i = 0; i < methods.length; i++) { |
duke@1 | 122 | if (executableMembersEqual(method, methods[i])) { |
duke@1 | 123 | return methods[i]; |
duke@1 | 124 | |
duke@1 | 125 | } |
duke@1 | 126 | } |
duke@1 | 127 | return null; |
duke@1 | 128 | } |
duke@1 | 129 | |
duke@1 | 130 | /** |
duke@1 | 131 | * @param member1 the first method to compare. |
duke@1 | 132 | * @param member2 the second method to compare. |
duke@1 | 133 | * @return true if member1 overrides/hides or is overriden/hidden by member2. |
duke@1 | 134 | */ |
duke@1 | 135 | public static boolean executableMembersEqual(ExecutableMemberDoc member1, |
duke@1 | 136 | ExecutableMemberDoc member2) { |
duke@1 | 137 | if (! (member1 instanceof MethodDoc && member2 instanceof MethodDoc)) |
duke@1 | 138 | return false; |
duke@1 | 139 | |
duke@1 | 140 | MethodDoc method1 = (MethodDoc) member1; |
duke@1 | 141 | MethodDoc method2 = (MethodDoc) member2; |
duke@1 | 142 | if (method1.isStatic() && method2.isStatic()) { |
duke@1 | 143 | Parameter[] targetParams = method1.parameters(); |
duke@1 | 144 | Parameter[] currentParams; |
duke@1 | 145 | if (method1.name().equals(method2.name()) && |
duke@1 | 146 | (currentParams = method2.parameters()).length == |
duke@1 | 147 | targetParams.length) { |
duke@1 | 148 | int j; |
duke@1 | 149 | for (j = 0; j < targetParams.length; j++) { |
duke@1 | 150 | if (! (targetParams[j].typeName().equals( |
duke@1 | 151 | currentParams[j].typeName()) || |
duke@1 | 152 | currentParams[j].type() instanceof TypeVariable || |
duke@1 | 153 | targetParams[j].type() instanceof TypeVariable)) { |
duke@1 | 154 | break; |
duke@1 | 155 | } |
duke@1 | 156 | } |
duke@1 | 157 | if (j == targetParams.length) { |
duke@1 | 158 | return true; |
duke@1 | 159 | } |
duke@1 | 160 | } |
duke@1 | 161 | return false; |
duke@1 | 162 | } else { |
duke@1 | 163 | return method1.overrides(method2) || |
duke@1 | 164 | method2.overrides(method1) || |
duke@1 | 165 | member1 == member2; |
duke@1 | 166 | } |
duke@1 | 167 | } |
duke@1 | 168 | |
duke@1 | 169 | /** |
jjh@972 | 170 | * According to |
jjh@972 | 171 | * <cite>The Java™ Language Specification</cite>, |
jjh@972 | 172 | * all the outer classes and static inner classes are core classes. |
duke@1 | 173 | */ |
duke@1 | 174 | public static boolean isCoreClass(ClassDoc cd) { |
duke@1 | 175 | return cd.containingClass() == null || cd.isStatic(); |
duke@1 | 176 | } |
duke@1 | 177 | |
duke@1 | 178 | public static boolean matches(ProgramElementDoc doc1, |
duke@1 | 179 | ProgramElementDoc doc2) { |
duke@1 | 180 | if (doc1 instanceof ExecutableMemberDoc && |
duke@1 | 181 | doc2 instanceof ExecutableMemberDoc) { |
duke@1 | 182 | ExecutableMemberDoc ed1 = (ExecutableMemberDoc)doc1; |
duke@1 | 183 | ExecutableMemberDoc ed2 = (ExecutableMemberDoc)doc2; |
duke@1 | 184 | return executableMembersEqual(ed1, ed2); |
duke@1 | 185 | } else { |
duke@1 | 186 | return doc1.name().equals(doc2.name()); |
duke@1 | 187 | } |
duke@1 | 188 | } |
duke@1 | 189 | |
duke@1 | 190 | /** |
duke@1 | 191 | * Copy the given directory contents from the source package directory |
duke@1 | 192 | * to the generated documentation directory. For example for a package |
duke@1 | 193 | * java.lang this method find out the source location of the package using |
duke@1 | 194 | * {@link SourcePath} and if given directory is found in the source |
duke@1 | 195 | * directory structure, copy the entire directory, to the generated |
duke@1 | 196 | * documentation hierarchy. |
duke@1 | 197 | * |
duke@1 | 198 | * @param configuration The configuration of the current doclet. |
duke@1 | 199 | * @param path The relative path to the directory to be copied. |
duke@1 | 200 | * @param dir The original directory name to copy from. |
duke@1 | 201 | * @param overwrite Overwrite files if true. |
duke@1 | 202 | */ |
jjg@1383 | 203 | public static void copyDocFiles(Configuration configuration, PackageDoc pd) { |
jjg@1383 | 204 | copyDocFiles(configuration, DocPath.forPackage(pd).resolve(DocPaths.DOC_FILES)); |
jjg@1383 | 205 | } |
jjg@1383 | 206 | |
jjg@1383 | 207 | public static void copyDocFiles(Configuration configuration, DocPath dir) { |
duke@1 | 208 | try { |
jjg@1383 | 209 | boolean first = true; |
jjg@1383 | 210 | for (DocFile f : DocFile.list(configuration, StandardLocation.SOURCE_PATH, dir)) { |
jjg@1383 | 211 | if (!f.isDirectory()) { |
jjg@1383 | 212 | continue; |
jjg@1383 | 213 | } |
jjg@1383 | 214 | DocFile srcdir = f; |
jjg@1383 | 215 | DocFile destdir = DocFile.createFileForOutput(configuration, dir); |
jjg@1383 | 216 | if (srcdir.isSameFile(destdir)) { |
jjg@1383 | 217 | continue; |
jjg@1383 | 218 | } |
jjg@1383 | 219 | |
jjg@1383 | 220 | for (DocFile srcfile: srcdir.list()) { |
jjg@1383 | 221 | DocFile destfile = destdir.resolve(srcfile.getName()); |
jjg@1383 | 222 | if (srcfile.isFile()) { |
jjg@1383 | 223 | if (destfile.exists() && !first) { |
jjg@1383 | 224 | configuration.message.warning((SourcePosition) null, |
jjg@1383 | 225 | "doclet.Copy_Overwrite_warning", |
jjg@1383 | 226 | srcfile.getPath(), destdir.getPath()); |
jjg@1383 | 227 | } else { |
jjg@1383 | 228 | configuration.message.notice( |
jjg@1383 | 229 | "doclet.Copying_File_0_To_Dir_1", |
jjg@1383 | 230 | srcfile.getPath(), destdir.getPath()); |
jjg@1383 | 231 | destfile.copyFile(srcfile); |
jjg@1383 | 232 | } |
jjg@1383 | 233 | } else if (srcfile.isDirectory()) { |
jjg@1383 | 234 | if (configuration.copydocfilesubdirs |
jjg@1383 | 235 | && !configuration.shouldExcludeDocFileDir(srcfile.getName())) { |
jjg@1383 | 236 | copyDocFiles(configuration, dir.resolve(srcfile.getName())); |
jjg@1383 | 237 | } |
duke@1 | 238 | } |
duke@1 | 239 | } |
jjg@1383 | 240 | |
jjg@1383 | 241 | first = false; |
duke@1 | 242 | } |
duke@1 | 243 | } catch (SecurityException exc) { |
duke@1 | 244 | throw new DocletAbortException(); |
duke@1 | 245 | } catch (IOException exc) { |
duke@1 | 246 | throw new DocletAbortException(); |
duke@1 | 247 | } |
duke@1 | 248 | } |
duke@1 | 249 | |
duke@1 | 250 | /** |
duke@1 | 251 | * We want the list of types in alphabetical order. However, types are not |
duke@1 | 252 | * comparable. We need a comparator for now. |
duke@1 | 253 | */ |
jjg@74 | 254 | private static class TypeComparator implements Comparator<Type> { |
jjg@74 | 255 | public int compare(Type type1, Type type2) { |
jjg@74 | 256 | return type1.qualifiedTypeName().toLowerCase().compareTo( |
jjg@74 | 257 | type2.qualifiedTypeName().toLowerCase()); |
duke@1 | 258 | } |
duke@1 | 259 | } |
duke@1 | 260 | |
duke@1 | 261 | /** |
duke@1 | 262 | * For the class return all implemented interfaces including the |
duke@1 | 263 | * superinterfaces of the implementing interfaces, also iterate over for |
duke@1 | 264 | * all the superclasses. For interface return all the extended interfaces |
duke@1 | 265 | * as well as superinterfaces for those extended interfaces. |
duke@1 | 266 | * |
duke@1 | 267 | * @param type type whose implemented or |
duke@1 | 268 | * super interfaces are sought. |
duke@1 | 269 | * @param configuration the current configuration of the doclet. |
duke@1 | 270 | * @param sort if true, return list of interfaces sorted alphabetically. |
duke@1 | 271 | * @return List of all the required interfaces. |
duke@1 | 272 | */ |
jjg@74 | 273 | public static List<Type> getAllInterfaces(Type type, |
duke@1 | 274 | Configuration configuration, boolean sort) { |
jjg@74 | 275 | Map<ClassDoc,Type> results = sort ? new TreeMap<ClassDoc,Type>() : new LinkedHashMap<ClassDoc,Type>(); |
duke@1 | 276 | Type[] interfaceTypes = null; |
duke@1 | 277 | Type superType = null; |
duke@1 | 278 | if (type instanceof ParameterizedType) { |
duke@1 | 279 | interfaceTypes = ((ParameterizedType) type).interfaceTypes(); |
duke@1 | 280 | superType = ((ParameterizedType) type).superclassType(); |
duke@1 | 281 | } else if (type instanceof ClassDoc) { |
duke@1 | 282 | interfaceTypes = ((ClassDoc) type).interfaceTypes(); |
duke@1 | 283 | superType = ((ClassDoc) type).superclassType(); |
duke@1 | 284 | } else { |
duke@1 | 285 | interfaceTypes = type.asClassDoc().interfaceTypes(); |
duke@1 | 286 | superType = type.asClassDoc().superclassType(); |
duke@1 | 287 | } |
duke@1 | 288 | |
duke@1 | 289 | for (int i = 0; i < interfaceTypes.length; i++) { |
duke@1 | 290 | Type interfaceType = interfaceTypes[i]; |
duke@1 | 291 | ClassDoc interfaceClassDoc = interfaceType.asClassDoc(); |
duke@1 | 292 | if (! (interfaceClassDoc.isPublic() || |
duke@1 | 293 | (configuration == null || |
duke@1 | 294 | isLinkable(interfaceClassDoc, configuration)))) { |
duke@1 | 295 | continue; |
duke@1 | 296 | } |
duke@1 | 297 | results.put(interfaceClassDoc, interfaceType); |
mcimadamore@184 | 298 | List<Type> superInterfaces = getAllInterfaces(interfaceType, configuration, sort); |
mcimadamore@184 | 299 | for (Iterator<Type> iter = superInterfaces.iterator(); iter.hasNext(); ) { |
mcimadamore@184 | 300 | Type t = iter.next(); |
duke@1 | 301 | results.put(t.asClassDoc(), t); |
duke@1 | 302 | } |
duke@1 | 303 | } |
duke@1 | 304 | if (superType == null) |
jjg@74 | 305 | return new ArrayList<Type>(results.values()); |
duke@1 | 306 | //Try walking the tree. |
duke@1 | 307 | addAllInterfaceTypes(results, |
duke@1 | 308 | superType, |
jjg@1521 | 309 | interfaceTypesOf(superType), |
duke@1 | 310 | false, configuration); |
jjg@74 | 311 | List<Type> resultsList = new ArrayList<Type>(results.values()); |
duke@1 | 312 | if (sort) { |
duke@1 | 313 | Collections.sort(resultsList, new TypeComparator()); |
duke@1 | 314 | } |
duke@1 | 315 | return resultsList; |
duke@1 | 316 | } |
duke@1 | 317 | |
jjg@1521 | 318 | private static Type[] interfaceTypesOf(Type type) { |
jjg@1521 | 319 | if (type instanceof AnnotatedType) |
jjg@1521 | 320 | type = ((AnnotatedType)type).underlyingType(); |
jjg@1521 | 321 | return type instanceof ClassDoc ? |
jjg@1521 | 322 | ((ClassDoc)type).interfaceTypes() : |
jjg@1521 | 323 | ((ParameterizedType)type).interfaceTypes(); |
jjg@1521 | 324 | } |
jjg@1521 | 325 | |
mcimadamore@184 | 326 | public static List<Type> getAllInterfaces(Type type, Configuration configuration) { |
duke@1 | 327 | return getAllInterfaces(type, configuration, true); |
duke@1 | 328 | } |
duke@1 | 329 | |
jjg@74 | 330 | private static void findAllInterfaceTypes(Map<ClassDoc,Type> results, ClassDoc c, boolean raw, |
duke@1 | 331 | Configuration configuration) { |
duke@1 | 332 | Type superType = c.superclassType(); |
duke@1 | 333 | if (superType == null) |
duke@1 | 334 | return; |
duke@1 | 335 | addAllInterfaceTypes(results, superType, |
jjg@1521 | 336 | interfaceTypesOf(superType), |
duke@1 | 337 | raw, configuration); |
duke@1 | 338 | } |
duke@1 | 339 | |
jjg@74 | 340 | private static void findAllInterfaceTypes(Map<ClassDoc,Type> results, ParameterizedType p, |
duke@1 | 341 | Configuration configuration) { |
duke@1 | 342 | Type superType = p.superclassType(); |
duke@1 | 343 | if (superType == null) |
duke@1 | 344 | return; |
duke@1 | 345 | addAllInterfaceTypes(results, superType, |
jjg@1521 | 346 | interfaceTypesOf(superType), |
duke@1 | 347 | false, configuration); |
duke@1 | 348 | } |
duke@1 | 349 | |
jjg@74 | 350 | private static void addAllInterfaceTypes(Map<ClassDoc,Type> results, Type type, |
duke@1 | 351 | Type[] interfaceTypes, boolean raw, |
duke@1 | 352 | Configuration configuration) { |
duke@1 | 353 | for (int i = 0; i < interfaceTypes.length; i++) { |
duke@1 | 354 | Type interfaceType = interfaceTypes[i]; |
duke@1 | 355 | ClassDoc interfaceClassDoc = interfaceType.asClassDoc(); |
duke@1 | 356 | if (! (interfaceClassDoc.isPublic() || |
duke@1 | 357 | (configuration != null && |
duke@1 | 358 | isLinkable(interfaceClassDoc, configuration)))) { |
duke@1 | 359 | continue; |
duke@1 | 360 | } |
duke@1 | 361 | if (raw) |
duke@1 | 362 | interfaceType = interfaceType.asClassDoc(); |
duke@1 | 363 | results.put(interfaceClassDoc, interfaceType); |
mcimadamore@184 | 364 | List<Type> superInterfaces = getAllInterfaces(interfaceType, configuration); |
mcimadamore@184 | 365 | for (Iterator<Type> iter = superInterfaces.iterator(); iter.hasNext(); ) { |
mcimadamore@184 | 366 | Type superInterface = iter.next(); |
duke@1 | 367 | results.put(superInterface.asClassDoc(), superInterface); |
duke@1 | 368 | } |
duke@1 | 369 | } |
jjg@1521 | 370 | if (type instanceof AnnotatedType) |
jjg@1521 | 371 | type = ((AnnotatedType)type).underlyingType(); |
jjg@1521 | 372 | |
duke@1 | 373 | if (type instanceof ParameterizedType) |
duke@1 | 374 | findAllInterfaceTypes(results, (ParameterizedType) type, configuration); |
duke@1 | 375 | else if (((ClassDoc) type).typeParameters().length == 0) |
duke@1 | 376 | findAllInterfaceTypes(results, (ClassDoc) type, raw, configuration); |
duke@1 | 377 | else |
duke@1 | 378 | findAllInterfaceTypes(results, (ClassDoc) type, true, configuration); |
duke@1 | 379 | } |
duke@1 | 380 | |
duke@1 | 381 | /** |
duke@1 | 382 | * Enclose in quotes, used for paths and filenames that contains spaces |
duke@1 | 383 | */ |
duke@1 | 384 | public static String quote(String filepath) { |
duke@1 | 385 | return ("\"" + filepath + "\""); |
duke@1 | 386 | } |
duke@1 | 387 | |
duke@1 | 388 | /** |
jjg@1372 | 389 | * Given a package, return its name. |
duke@1 | 390 | * @param packageDoc the package to check. |
duke@1 | 391 | * @return the name of the given package. |
duke@1 | 392 | */ |
duke@1 | 393 | public static String getPackageName(PackageDoc packageDoc) { |
duke@1 | 394 | return packageDoc == null || packageDoc.name().length() == 0 ? |
duke@1 | 395 | DocletConstants.DEFAULT_PACKAGE_NAME : packageDoc.name(); |
duke@1 | 396 | } |
duke@1 | 397 | |
duke@1 | 398 | /** |
jjg@1372 | 399 | * Given a package, return its file name without the extension. |
duke@1 | 400 | * @param packageDoc the package to check. |
duke@1 | 401 | * @return the file name of the given package. |
duke@1 | 402 | */ |
duke@1 | 403 | public static String getPackageFileHeadName(PackageDoc packageDoc) { |
duke@1 | 404 | return packageDoc == null || packageDoc.name().length() == 0 ? |
duke@1 | 405 | DocletConstants.DEFAULT_PACKAGE_FILE_NAME : packageDoc.name(); |
duke@1 | 406 | } |
duke@1 | 407 | |
duke@1 | 408 | /** |
jjg@1372 | 409 | * Given a string, replace all occurrences of 'newStr' with 'oldStr'. |
duke@1 | 410 | * @param originalStr the string to modify. |
duke@1 | 411 | * @param oldStr the string to replace. |
duke@1 | 412 | * @param newStr the string to insert in place of the old string. |
duke@1 | 413 | */ |
duke@1 | 414 | public static String replaceText(String originalStr, String oldStr, |
duke@1 | 415 | String newStr) { |
duke@1 | 416 | if (oldStr == null || newStr == null || oldStr.equals(newStr)) { |
duke@1 | 417 | return originalStr; |
duke@1 | 418 | } |
jjg@910 | 419 | return originalStr.replace(oldStr, newStr); |
duke@1 | 420 | } |
duke@1 | 421 | |
duke@1 | 422 | /** |
duke@1 | 423 | * Given an annotation, return true if it should be documented and false |
duke@1 | 424 | * otherwise. |
duke@1 | 425 | * |
duke@1 | 426 | * @param annotationDoc the annotation to check. |
duke@1 | 427 | * |
duke@1 | 428 | * @return true return true if it should be documented and false otherwise. |
duke@1 | 429 | */ |
duke@1 | 430 | public static boolean isDocumentedAnnotation(AnnotationTypeDoc annotationDoc) { |
duke@1 | 431 | AnnotationDesc[] annotationDescList = annotationDoc.annotations(); |
duke@1 | 432 | for (int i = 0; i < annotationDescList.length; i++) { |
duke@1 | 433 | if (annotationDescList[i].annotationType().qualifiedName().equals( |
duke@1 | 434 | java.lang.annotation.Documented.class.getName())){ |
duke@1 | 435 | return true; |
duke@1 | 436 | } |
duke@1 | 437 | } |
duke@1 | 438 | return false; |
duke@1 | 439 | } |
duke@1 | 440 | |
jjg@1521 | 441 | private static boolean isDeclarationTarget(AnnotationDesc targetAnno) { |
jjg@1521 | 442 | // The error recovery steps here are analogous to TypeAnnotations |
jjg@1521 | 443 | ElementValuePair[] elems = targetAnno.elementValues(); |
jjg@1521 | 444 | if (elems == null |
jjg@1521 | 445 | || elems.length != 1 |
jjg@1521 | 446 | || !"value".equals(elems[0].element().name()) |
jjg@1521 | 447 | || !(elems[0].value().value() instanceof AnnotationValue[])) |
jjg@1521 | 448 | return true; // error recovery |
jjg@1521 | 449 | |
jjg@1521 | 450 | AnnotationValue[] values = (AnnotationValue[])elems[0].value().value(); |
jjg@1521 | 451 | for (int i = 0; i < values.length; i++) { |
jjg@1521 | 452 | Object value = values[i].value(); |
jjg@1521 | 453 | if (!(value instanceof FieldDoc)) |
jjg@1521 | 454 | return true; // error recovery |
jjg@1521 | 455 | |
jjg@1521 | 456 | FieldDoc eValue = (FieldDoc)value; |
jjg@1521 | 457 | if (Util.isJava5DeclarationElementType(eValue)) { |
jjg@1521 | 458 | return true; |
jjg@1521 | 459 | } |
jjg@1521 | 460 | } |
jjg@1521 | 461 | |
jjg@1521 | 462 | return false; |
jjg@1521 | 463 | } |
jjg@1521 | 464 | |
jjg@1521 | 465 | /** |
jjg@1521 | 466 | * Returns true if the {@code annotationDoc} is to be treated |
jjg@1521 | 467 | * as a declaration annotation, when targeting the |
jjg@1521 | 468 | * {@code elemType} element type. |
jjg@1521 | 469 | * |
jjg@1521 | 470 | * @param annotationDoc the annotationDoc to check |
jjg@1521 | 471 | * @param elemType the targeted elemType |
jjg@1521 | 472 | * @return true if annotationDoc is a declaration annotation |
jjg@1521 | 473 | */ |
jjg@1521 | 474 | public static boolean isDeclarationAnnotation(AnnotationTypeDoc annotationDoc, |
jjg@1521 | 475 | boolean isJava5DeclarationLocation) { |
jjg@1521 | 476 | if (!isJava5DeclarationLocation) |
jjg@1521 | 477 | return false; |
jjg@1521 | 478 | AnnotationDesc[] annotationDescList = annotationDoc.annotations(); |
jjg@1521 | 479 | // Annotations with no target are treated as declaration as well |
jjg@1521 | 480 | if (annotationDescList.length==0) |
jjg@1521 | 481 | return true; |
jjg@1521 | 482 | for (int i = 0; i < annotationDescList.length; i++) { |
jjg@1521 | 483 | if (annotationDescList[i].annotationType().qualifiedName().equals( |
jjg@1521 | 484 | java.lang.annotation.Target.class.getName())) { |
jjg@1521 | 485 | if (isDeclarationTarget(annotationDescList[i])) |
jjg@1521 | 486 | return true; |
jjg@1521 | 487 | } |
jjg@1521 | 488 | } |
jjg@1521 | 489 | return false; |
jjg@1521 | 490 | } |
jjg@1521 | 491 | |
duke@1 | 492 | /** |
duke@1 | 493 | * Return true if this class is linkable and false if we can't link to the |
duke@1 | 494 | * desired class. |
duke@1 | 495 | * <br> |
duke@1 | 496 | * <b>NOTE:</b> You can only link to external classes if they are public or |
duke@1 | 497 | * protected. |
duke@1 | 498 | * |
duke@1 | 499 | * @param classDoc the class to check. |
duke@1 | 500 | * @param configuration the current configuration of the doclet. |
duke@1 | 501 | * |
duke@1 | 502 | * @return true if this class is linkable and false if we can't link to the |
duke@1 | 503 | * desired class. |
duke@1 | 504 | */ |
duke@1 | 505 | public static boolean isLinkable(ClassDoc classDoc, |
duke@1 | 506 | Configuration configuration) { |
duke@1 | 507 | return |
duke@1 | 508 | ((classDoc.isIncluded() && configuration.isGeneratedDoc(classDoc))) || |
duke@1 | 509 | (configuration.extern.isExternal(classDoc) && |
duke@1 | 510 | (classDoc.isPublic() || classDoc.isProtected())); |
duke@1 | 511 | } |
duke@1 | 512 | |
duke@1 | 513 | /** |
duke@1 | 514 | * Given a class, return the closest visible super class. |
duke@1 | 515 | * |
duke@1 | 516 | * @param classDoc the class we are searching the parent for. |
duke@1 | 517 | * @param configuration the current configuration of the doclet. |
duke@1 | 518 | * @return the closest visible super class. Return null if it cannot |
duke@1 | 519 | * be found (i.e. classDoc is java.lang.Object). |
duke@1 | 520 | */ |
duke@1 | 521 | public static Type getFirstVisibleSuperClass(ClassDoc classDoc, |
duke@1 | 522 | Configuration configuration) { |
duke@1 | 523 | if (classDoc == null) { |
duke@1 | 524 | return null; |
duke@1 | 525 | } |
duke@1 | 526 | Type sup = classDoc.superclassType(); |
duke@1 | 527 | ClassDoc supClassDoc = classDoc.superclass(); |
duke@1 | 528 | while (sup != null && |
duke@1 | 529 | (! (supClassDoc.isPublic() || |
duke@1 | 530 | isLinkable(supClassDoc, configuration))) ) { |
duke@1 | 531 | if (supClassDoc.superclass().qualifiedName().equals(supClassDoc.qualifiedName())) |
duke@1 | 532 | break; |
duke@1 | 533 | sup = supClassDoc.superclassType(); |
duke@1 | 534 | supClassDoc = supClassDoc.superclass(); |
duke@1 | 535 | } |
duke@1 | 536 | if (classDoc.equals(supClassDoc)) { |
duke@1 | 537 | return null; |
duke@1 | 538 | } |
duke@1 | 539 | return sup; |
duke@1 | 540 | } |
duke@1 | 541 | |
duke@1 | 542 | /** |
duke@1 | 543 | * Given a class, return the closest visible super class. |
duke@1 | 544 | * |
duke@1 | 545 | * @param classDoc the class we are searching the parent for. |
duke@1 | 546 | * @param configuration the current configuration of the doclet. |
duke@1 | 547 | * @return the closest visible super class. Return null if it cannot |
duke@1 | 548 | * be found (i.e. classDoc is java.lang.Object). |
duke@1 | 549 | */ |
duke@1 | 550 | public static ClassDoc getFirstVisibleSuperClassCD(ClassDoc classDoc, |
duke@1 | 551 | Configuration configuration) { |
duke@1 | 552 | if (classDoc == null) { |
duke@1 | 553 | return null; |
duke@1 | 554 | } |
duke@1 | 555 | ClassDoc supClassDoc = classDoc.superclass(); |
duke@1 | 556 | while (supClassDoc != null && |
duke@1 | 557 | (! (supClassDoc.isPublic() || |
duke@1 | 558 | isLinkable(supClassDoc, configuration))) ) { |
duke@1 | 559 | supClassDoc = supClassDoc.superclass(); |
duke@1 | 560 | } |
duke@1 | 561 | if (classDoc.equals(supClassDoc)) { |
duke@1 | 562 | return null; |
duke@1 | 563 | } |
duke@1 | 564 | return supClassDoc; |
duke@1 | 565 | } |
duke@1 | 566 | |
duke@1 | 567 | /** |
duke@1 | 568 | * Given a ClassDoc, return the name of its type (Class, Interface, etc.). |
duke@1 | 569 | * |
duke@1 | 570 | * @param cd the ClassDoc to check. |
duke@1 | 571 | * @param lowerCaseOnly true if you want the name returned in lower case. |
jjg@1397 | 572 | * If false, the first letter of the name is capitalized. |
duke@1 | 573 | * @return |
duke@1 | 574 | */ |
duke@1 | 575 | public static String getTypeName(Configuration config, |
duke@1 | 576 | ClassDoc cd, boolean lowerCaseOnly) { |
duke@1 | 577 | String typeName = ""; |
duke@1 | 578 | if (cd.isOrdinaryClass()) { |
duke@1 | 579 | typeName = "doclet.Class"; |
duke@1 | 580 | } else if (cd.isInterface()) { |
duke@1 | 581 | typeName = "doclet.Interface"; |
duke@1 | 582 | } else if (cd.isException()) { |
duke@1 | 583 | typeName = "doclet.Exception"; |
duke@1 | 584 | } else if (cd.isError()) { |
duke@1 | 585 | typeName = "doclet.Error"; |
duke@1 | 586 | } else if (cd.isAnnotationType()) { |
duke@1 | 587 | typeName = "doclet.AnnotationType"; |
duke@1 | 588 | } else if (cd.isEnum()) { |
duke@1 | 589 | typeName = "doclet.Enum"; |
duke@1 | 590 | } |
duke@1 | 591 | return config.getText( |
duke@1 | 592 | lowerCaseOnly ? typeName.toLowerCase() : typeName); |
duke@1 | 593 | } |
duke@1 | 594 | |
duke@1 | 595 | /** |
jjg@1410 | 596 | * Replace all tabs with the appropriate number of spaces. |
jjg@1410 | 597 | * @param configuration the doclet configuration defining the setting for the |
jjg@1410 | 598 | * tab length. |
jjg@1410 | 599 | * @param sb the StringBuilder in which to replace the tabs |
duke@1 | 600 | */ |
jjg@1410 | 601 | public static void replaceTabs(Configuration configuration, StringBuilder sb) { |
jjg@1410 | 602 | int tabLength = configuration.sourcetab; |
jjg@1410 | 603 | String whitespace = configuration.tabSpaces; |
jjg@910 | 604 | int index = 0; |
jjg@1410 | 605 | while ((index = sb.indexOf("\t", index)) != -1) { |
jjg@910 | 606 | int spaceCount = tabLength - index % tabLength; |
jjg@1410 | 607 | sb.replace(index, index+1, whitespace.substring(0, spaceCount)); |
jjg@910 | 608 | index += spaceCount; |
duke@1 | 609 | } |
duke@1 | 610 | } |
duke@1 | 611 | |
duke@1 | 612 | /** |
duke@1 | 613 | * The documentation for values() and valueOf() in Enums are set by the |
duke@1 | 614 | * doclet. |
duke@1 | 615 | */ |
duke@1 | 616 | public static void setEnumDocumentation(Configuration configuration, |
duke@1 | 617 | ClassDoc classDoc) { |
duke@1 | 618 | MethodDoc[] methods = classDoc.methods(); |
duke@1 | 619 | for (int j = 0; j < methods.length; j++) { |
duke@1 | 620 | MethodDoc currentMethod = methods[j]; |
duke@1 | 621 | if (currentMethod.name().equals("values") && |
duke@1 | 622 | currentMethod.parameters().length == 0) { |
duke@1 | 623 | currentMethod.setRawCommentText( |
duke@1 | 624 | configuration.getText("doclet.enum_values_doc", classDoc.name())); |
duke@1 | 625 | } else if (currentMethod.name().equals("valueOf") && |
duke@1 | 626 | currentMethod.parameters().length == 1) { |
duke@1 | 627 | Type paramType = currentMethod.parameters()[0].type(); |
duke@1 | 628 | if (paramType != null && |
duke@1 | 629 | paramType.qualifiedTypeName().equals(String.class.getName())) { |
duke@1 | 630 | currentMethod.setRawCommentText( |
duke@1 | 631 | configuration.getText("doclet.enum_valueof_doc")); |
duke@1 | 632 | } |
duke@1 | 633 | } |
duke@1 | 634 | } |
duke@1 | 635 | } |
duke@1 | 636 | |
duke@1 | 637 | /** |
duke@1 | 638 | * Return true if the given Doc is deprecated. |
duke@1 | 639 | * |
duke@1 | 640 | * @param doc the Doc to check. |
duke@1 | 641 | * @return true if the given Doc is deprecated. |
duke@1 | 642 | */ |
bpatel@995 | 643 | public static boolean isDeprecated(Doc doc) { |
duke@1 | 644 | if (doc.tags("deprecated").length > 0) { |
duke@1 | 645 | return true; |
duke@1 | 646 | } |
bpatel@995 | 647 | AnnotationDesc[] annotationDescList; |
bpatel@995 | 648 | if (doc instanceof PackageDoc) |
bpatel@995 | 649 | annotationDescList = ((PackageDoc)doc).annotations(); |
bpatel@995 | 650 | else |
bpatel@995 | 651 | annotationDescList = ((ProgramElementDoc)doc).annotations(); |
duke@1 | 652 | for (int i = 0; i < annotationDescList.length; i++) { |
duke@1 | 653 | if (annotationDescList[i].annotationType().qualifiedName().equals( |
duke@1 | 654 | java.lang.Deprecated.class.getName())){ |
duke@1 | 655 | return true; |
duke@1 | 656 | } |
duke@1 | 657 | } |
duke@1 | 658 | return false; |
duke@1 | 659 | } |
jjg@1521 | 660 | |
jjg@1521 | 661 | /** |
jjg@1606 | 662 | * A convenience method to get property name from the name of the |
jjg@1606 | 663 | * getter or setter method. |
jjg@1606 | 664 | * @param name name of the getter or setter method. |
jjg@1606 | 665 | * @return the name of the property of the given setter of getter. |
jjg@1606 | 666 | */ |
jjg@1606 | 667 | public static String propertyNameFromMethodName(String name) { |
jjg@1606 | 668 | String propertyName = null; |
jjg@1606 | 669 | if (name.startsWith("get") || name.startsWith("set")) { |
jjg@1606 | 670 | propertyName = name.substring(3); |
jjg@1606 | 671 | } else if (name.startsWith("is")) { |
jjg@1606 | 672 | propertyName = name.substring(2); |
jjg@1606 | 673 | } |
jjg@1606 | 674 | if ((propertyName == null) || propertyName.isEmpty()){ |
jjg@1606 | 675 | return ""; |
jjg@1606 | 676 | } |
jjg@1606 | 677 | return propertyName.substring(0, 1).toLowerCase() |
jjg@1606 | 678 | + propertyName.substring(1); |
jjg@1606 | 679 | } |
jjg@1606 | 680 | |
jjg@1606 | 681 | /** |
jjg@1606 | 682 | * In case of JavaFX mode on, filters out classes that are private, |
jjg@1606 | 683 | * package private or having the @treatAsPrivate annotation. Those are not |
jjg@1606 | 684 | * documented in JavaFX mode. |
jjg@1606 | 685 | * |
jjg@1606 | 686 | * @param classes array of classes to be filtered. |
jjg@1606 | 687 | * @param javafx set to true if in JavaFX mode. |
jjg@1606 | 688 | * @return list of filtered classes. |
jjg@1606 | 689 | */ |
jjg@1606 | 690 | public static ClassDoc[] filterOutPrivateClasses(final ClassDoc[] classes, |
jjg@1606 | 691 | boolean javafx) { |
jjg@1606 | 692 | if (!javafx) { |
jjg@1606 | 693 | return classes; |
jjg@1606 | 694 | } |
jjg@1606 | 695 | final List<ClassDoc> filteredOutClasses = |
jjg@1606 | 696 | new ArrayList<ClassDoc>(classes.length); |
jjg@1606 | 697 | for (ClassDoc classDoc : classes) { |
jjg@1606 | 698 | if (classDoc.isPrivate() || classDoc.isPackagePrivate()) { |
jjg@1606 | 699 | continue; |
jjg@1606 | 700 | } |
jjg@1606 | 701 | Tag[] aspTags = classDoc.tags("treatAsPrivate"); |
jjg@1606 | 702 | if (aspTags != null && aspTags.length > 0) { |
jjg@1606 | 703 | continue; |
jjg@1606 | 704 | } |
jjg@1606 | 705 | filteredOutClasses.add(classDoc); |
jjg@1606 | 706 | } |
jjg@1606 | 707 | |
jjg@1606 | 708 | return filteredOutClasses.toArray(new ClassDoc[0]); |
jjg@1606 | 709 | } |
jjg@1606 | 710 | |
jjg@1606 | 711 | /** |
jjg@1521 | 712 | * Test whether the given FieldDoc is one of the declaration annotation ElementTypes |
jjg@1521 | 713 | * defined in Java 5. |
jjg@1521 | 714 | * Instead of testing for one of the new enum constants added in Java 8, test for |
jjg@1521 | 715 | * the old constants. This prevents bootstrapping problems. |
jjg@1521 | 716 | * |
jjg@1521 | 717 | * @param elt The FieldDoc to test |
jjg@1521 | 718 | * @return true, iff the given ElementType is one of the constants defined in Java 5 |
jjg@1521 | 719 | * @since 1.8 |
jjg@1521 | 720 | */ |
jjg@1521 | 721 | public static boolean isJava5DeclarationElementType(FieldDoc elt) { |
jjg@1521 | 722 | return elt.name().contentEquals(ElementType.ANNOTATION_TYPE.name()) || |
jjg@1521 | 723 | elt.name().contentEquals(ElementType.CONSTRUCTOR.name()) || |
jjg@1521 | 724 | elt.name().contentEquals(ElementType.FIELD.name()) || |
jjg@1521 | 725 | elt.name().contentEquals(ElementType.LOCAL_VARIABLE.name()) || |
jjg@1521 | 726 | elt.name().contentEquals(ElementType.METHOD.name()) || |
jjg@1521 | 727 | elt.name().contentEquals(ElementType.PACKAGE.name()) || |
jjg@1521 | 728 | elt.name().contentEquals(ElementType.PARAMETER.name()) || |
jjg@1521 | 729 | elt.name().contentEquals(ElementType.TYPE.name()); |
jjg@1521 | 730 | } |
duke@1 | 731 | } |