Sun, 24 Feb 2013 11:36:58 -0800
7112427: The doclet needs to be able to generate JavaFX documentation.
Reviewed-by: jjg
Contributed-by: jan.valenta@oracle.com
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 a string, escape all special html characters and |
duke@1 | 424 | * return the result. |
duke@1 | 425 | * |
duke@1 | 426 | * @param s The string to check. |
jjg@1410 | 427 | * @return the original string with all of the HTML characters escaped. |
duke@1 | 428 | */ |
duke@1 | 429 | public static String escapeHtmlChars(String s) { |
jjg@1410 | 430 | for (int i = 0; i < s.length(); i++) { |
jjg@1410 | 431 | char ch = s.charAt(i); |
jjg@1410 | 432 | switch (ch) { |
jjg@1410 | 433 | // only start building a new string if we need to |
jjg@1410 | 434 | case '<': case '>': case '&': |
jjg@1410 | 435 | StringBuilder sb = new StringBuilder(s.substring(0, i)); |
jjg@1410 | 436 | for ( ; i < s.length(); i++) { |
jjg@1410 | 437 | ch = s.charAt(i); |
jjg@1410 | 438 | switch (ch) { |
jjg@1410 | 439 | case '<': sb.append("<"); break; |
jjg@1410 | 440 | case '>': sb.append(">"); break; |
jjg@1410 | 441 | case '&': sb.append("&"); break; |
jjg@1410 | 442 | default: sb.append(ch); break; |
jjg@1410 | 443 | } |
jjg@1410 | 444 | } |
jjg@1410 | 445 | return sb.toString(); |
jjg@1410 | 446 | } |
duke@1 | 447 | } |
jjg@1410 | 448 | return s; |
jjg@1410 | 449 | } |
jjg@1410 | 450 | |
jjg@1410 | 451 | /** |
jjg@1410 | 452 | * Escape all special html characters in a string buffer. |
jjg@1410 | 453 | * |
jjg@1410 | 454 | * @param sb The string buffer to update |
jjg@1410 | 455 | */ |
jjg@1410 | 456 | public static void escapeHtmlChars(StringBuilder sb) { |
jjg@1410 | 457 | // scan backwards, replacing characters as needed. |
jjg@1410 | 458 | for (int i = sb.length() - 1; i >= 0; i--) { |
jjg@1410 | 459 | switch (sb.charAt(i)) { |
jjg@1410 | 460 | case '<': sb.replace(i, i+1, "<"); break; |
jjg@1410 | 461 | case '>': sb.replace(i, i+1, ">"); break; |
jjg@1410 | 462 | case '&': sb.replace(i, i+1, "&"); break; |
jjg@1410 | 463 | } |
jjg@1410 | 464 | } |
duke@1 | 465 | } |
duke@1 | 466 | |
duke@1 | 467 | /** |
bpatel@766 | 468 | * Given a string, strips all html characters and |
bpatel@766 | 469 | * return the result. |
bpatel@766 | 470 | * |
bpatel@766 | 471 | * @param rawString The string to check. |
bpatel@766 | 472 | * @return the original string with all of the HTML characters |
bpatel@766 | 473 | * stripped. |
bpatel@766 | 474 | * |
bpatel@766 | 475 | */ |
bpatel@766 | 476 | public static String stripHtml(String rawString) { |
bpatel@766 | 477 | // remove HTML tags |
bpatel@766 | 478 | rawString = rawString.replaceAll("\\<.*?>", " "); |
bpatel@766 | 479 | // consolidate multiple spaces between a word to a single space |
bpatel@766 | 480 | rawString = rawString.replaceAll("\\b\\s{2,}\\b", " "); |
bpatel@766 | 481 | // remove extra whitespaces |
bpatel@766 | 482 | return rawString.trim(); |
bpatel@766 | 483 | } |
bpatel@766 | 484 | |
bpatel@766 | 485 | /** |
duke@1 | 486 | * Given an annotation, return true if it should be documented and false |
duke@1 | 487 | * otherwise. |
duke@1 | 488 | * |
duke@1 | 489 | * @param annotationDoc the annotation to check. |
duke@1 | 490 | * |
duke@1 | 491 | * @return true return true if it should be documented and false otherwise. |
duke@1 | 492 | */ |
duke@1 | 493 | public static boolean isDocumentedAnnotation(AnnotationTypeDoc annotationDoc) { |
duke@1 | 494 | AnnotationDesc[] annotationDescList = annotationDoc.annotations(); |
duke@1 | 495 | for (int i = 0; i < annotationDescList.length; i++) { |
duke@1 | 496 | if (annotationDescList[i].annotationType().qualifiedName().equals( |
duke@1 | 497 | java.lang.annotation.Documented.class.getName())){ |
duke@1 | 498 | return true; |
duke@1 | 499 | } |
duke@1 | 500 | } |
duke@1 | 501 | return false; |
duke@1 | 502 | } |
duke@1 | 503 | |
jjg@1521 | 504 | private static boolean isDeclarationTarget(AnnotationDesc targetAnno) { |
jjg@1521 | 505 | // The error recovery steps here are analogous to TypeAnnotations |
jjg@1521 | 506 | ElementValuePair[] elems = targetAnno.elementValues(); |
jjg@1521 | 507 | if (elems == null |
jjg@1521 | 508 | || elems.length != 1 |
jjg@1521 | 509 | || !"value".equals(elems[0].element().name()) |
jjg@1521 | 510 | || !(elems[0].value().value() instanceof AnnotationValue[])) |
jjg@1521 | 511 | return true; // error recovery |
jjg@1521 | 512 | |
jjg@1521 | 513 | AnnotationValue[] values = (AnnotationValue[])elems[0].value().value(); |
jjg@1521 | 514 | for (int i = 0; i < values.length; i++) { |
jjg@1521 | 515 | Object value = values[i].value(); |
jjg@1521 | 516 | if (!(value instanceof FieldDoc)) |
jjg@1521 | 517 | return true; // error recovery |
jjg@1521 | 518 | |
jjg@1521 | 519 | FieldDoc eValue = (FieldDoc)value; |
jjg@1521 | 520 | if (Util.isJava5DeclarationElementType(eValue)) { |
jjg@1521 | 521 | return true; |
jjg@1521 | 522 | } |
jjg@1521 | 523 | } |
jjg@1521 | 524 | |
jjg@1521 | 525 | return false; |
jjg@1521 | 526 | } |
jjg@1521 | 527 | |
jjg@1521 | 528 | /** |
jjg@1521 | 529 | * Returns true if the {@code annotationDoc} is to be treated |
jjg@1521 | 530 | * as a declaration annotation, when targeting the |
jjg@1521 | 531 | * {@code elemType} element type. |
jjg@1521 | 532 | * |
jjg@1521 | 533 | * @param annotationDoc the annotationDoc to check |
jjg@1521 | 534 | * @param elemType the targeted elemType |
jjg@1521 | 535 | * @return true if annotationDoc is a declaration annotation |
jjg@1521 | 536 | */ |
jjg@1521 | 537 | public static boolean isDeclarationAnnotation(AnnotationTypeDoc annotationDoc, |
jjg@1521 | 538 | boolean isJava5DeclarationLocation) { |
jjg@1521 | 539 | if (!isJava5DeclarationLocation) |
jjg@1521 | 540 | return false; |
jjg@1521 | 541 | AnnotationDesc[] annotationDescList = annotationDoc.annotations(); |
jjg@1521 | 542 | // Annotations with no target are treated as declaration as well |
jjg@1521 | 543 | if (annotationDescList.length==0) |
jjg@1521 | 544 | return true; |
jjg@1521 | 545 | for (int i = 0; i < annotationDescList.length; i++) { |
jjg@1521 | 546 | if (annotationDescList[i].annotationType().qualifiedName().equals( |
jjg@1521 | 547 | java.lang.annotation.Target.class.getName())) { |
jjg@1521 | 548 | if (isDeclarationTarget(annotationDescList[i])) |
jjg@1521 | 549 | return true; |
jjg@1521 | 550 | } |
jjg@1521 | 551 | } |
jjg@1521 | 552 | return false; |
jjg@1521 | 553 | } |
jjg@1521 | 554 | |
duke@1 | 555 | /** |
duke@1 | 556 | * Return true if this class is linkable and false if we can't link to the |
duke@1 | 557 | * desired class. |
duke@1 | 558 | * <br> |
duke@1 | 559 | * <b>NOTE:</b> You can only link to external classes if they are public or |
duke@1 | 560 | * protected. |
duke@1 | 561 | * |
duke@1 | 562 | * @param classDoc the class to check. |
duke@1 | 563 | * @param configuration the current configuration of the doclet. |
duke@1 | 564 | * |
duke@1 | 565 | * @return true if this class is linkable and false if we can't link to the |
duke@1 | 566 | * desired class. |
duke@1 | 567 | */ |
duke@1 | 568 | public static boolean isLinkable(ClassDoc classDoc, |
duke@1 | 569 | Configuration configuration) { |
duke@1 | 570 | return |
duke@1 | 571 | ((classDoc.isIncluded() && configuration.isGeneratedDoc(classDoc))) || |
duke@1 | 572 | (configuration.extern.isExternal(classDoc) && |
duke@1 | 573 | (classDoc.isPublic() || classDoc.isProtected())); |
duke@1 | 574 | } |
duke@1 | 575 | |
duke@1 | 576 | /** |
duke@1 | 577 | * Given a class, return the closest visible super class. |
duke@1 | 578 | * |
duke@1 | 579 | * @param classDoc the class we are searching the parent for. |
duke@1 | 580 | * @param configuration the current configuration of the doclet. |
duke@1 | 581 | * @return the closest visible super class. Return null if it cannot |
duke@1 | 582 | * be found (i.e. classDoc is java.lang.Object). |
duke@1 | 583 | */ |
duke@1 | 584 | public static Type getFirstVisibleSuperClass(ClassDoc classDoc, |
duke@1 | 585 | Configuration configuration) { |
duke@1 | 586 | if (classDoc == null) { |
duke@1 | 587 | return null; |
duke@1 | 588 | } |
duke@1 | 589 | Type sup = classDoc.superclassType(); |
duke@1 | 590 | ClassDoc supClassDoc = classDoc.superclass(); |
duke@1 | 591 | while (sup != null && |
duke@1 | 592 | (! (supClassDoc.isPublic() || |
duke@1 | 593 | isLinkable(supClassDoc, configuration))) ) { |
duke@1 | 594 | if (supClassDoc.superclass().qualifiedName().equals(supClassDoc.qualifiedName())) |
duke@1 | 595 | break; |
duke@1 | 596 | sup = supClassDoc.superclassType(); |
duke@1 | 597 | supClassDoc = supClassDoc.superclass(); |
duke@1 | 598 | } |
duke@1 | 599 | if (classDoc.equals(supClassDoc)) { |
duke@1 | 600 | return null; |
duke@1 | 601 | } |
duke@1 | 602 | return sup; |
duke@1 | 603 | } |
duke@1 | 604 | |
duke@1 | 605 | /** |
duke@1 | 606 | * Given a class, return the closest visible super class. |
duke@1 | 607 | * |
duke@1 | 608 | * @param classDoc the class we are searching the parent for. |
duke@1 | 609 | * @param configuration the current configuration of the doclet. |
duke@1 | 610 | * @return the closest visible super class. Return null if it cannot |
duke@1 | 611 | * be found (i.e. classDoc is java.lang.Object). |
duke@1 | 612 | */ |
duke@1 | 613 | public static ClassDoc getFirstVisibleSuperClassCD(ClassDoc classDoc, |
duke@1 | 614 | Configuration configuration) { |
duke@1 | 615 | if (classDoc == null) { |
duke@1 | 616 | return null; |
duke@1 | 617 | } |
duke@1 | 618 | ClassDoc supClassDoc = classDoc.superclass(); |
duke@1 | 619 | while (supClassDoc != null && |
duke@1 | 620 | (! (supClassDoc.isPublic() || |
duke@1 | 621 | isLinkable(supClassDoc, configuration))) ) { |
duke@1 | 622 | supClassDoc = supClassDoc.superclass(); |
duke@1 | 623 | } |
duke@1 | 624 | if (classDoc.equals(supClassDoc)) { |
duke@1 | 625 | return null; |
duke@1 | 626 | } |
duke@1 | 627 | return supClassDoc; |
duke@1 | 628 | } |
duke@1 | 629 | |
duke@1 | 630 | /** |
duke@1 | 631 | * Given a ClassDoc, return the name of its type (Class, Interface, etc.). |
duke@1 | 632 | * |
duke@1 | 633 | * @param cd the ClassDoc to check. |
duke@1 | 634 | * @param lowerCaseOnly true if you want the name returned in lower case. |
jjg@1397 | 635 | * If false, the first letter of the name is capitalized. |
duke@1 | 636 | * @return |
duke@1 | 637 | */ |
duke@1 | 638 | public static String getTypeName(Configuration config, |
duke@1 | 639 | ClassDoc cd, boolean lowerCaseOnly) { |
duke@1 | 640 | String typeName = ""; |
duke@1 | 641 | if (cd.isOrdinaryClass()) { |
duke@1 | 642 | typeName = "doclet.Class"; |
duke@1 | 643 | } else if (cd.isInterface()) { |
duke@1 | 644 | typeName = "doclet.Interface"; |
duke@1 | 645 | } else if (cd.isException()) { |
duke@1 | 646 | typeName = "doclet.Exception"; |
duke@1 | 647 | } else if (cd.isError()) { |
duke@1 | 648 | typeName = "doclet.Error"; |
duke@1 | 649 | } else if (cd.isAnnotationType()) { |
duke@1 | 650 | typeName = "doclet.AnnotationType"; |
duke@1 | 651 | } else if (cd.isEnum()) { |
duke@1 | 652 | typeName = "doclet.Enum"; |
duke@1 | 653 | } |
duke@1 | 654 | return config.getText( |
duke@1 | 655 | lowerCaseOnly ? typeName.toLowerCase() : typeName); |
duke@1 | 656 | } |
duke@1 | 657 | |
duke@1 | 658 | /** |
jjg@1410 | 659 | * Replace all tabs with the appropriate number of spaces. |
jjg@1410 | 660 | * @param configuration the doclet configuration defining the setting for the |
jjg@1410 | 661 | * tab length. |
jjg@1410 | 662 | * @param sb the StringBuilder in which to replace the tabs |
duke@1 | 663 | */ |
jjg@1410 | 664 | public static void replaceTabs(Configuration configuration, StringBuilder sb) { |
jjg@1410 | 665 | int tabLength = configuration.sourcetab; |
jjg@1410 | 666 | String whitespace = configuration.tabSpaces; |
jjg@910 | 667 | int index = 0; |
jjg@1410 | 668 | while ((index = sb.indexOf("\t", index)) != -1) { |
jjg@910 | 669 | int spaceCount = tabLength - index % tabLength; |
jjg@1410 | 670 | sb.replace(index, index+1, whitespace.substring(0, spaceCount)); |
jjg@910 | 671 | index += spaceCount; |
duke@1 | 672 | } |
duke@1 | 673 | } |
duke@1 | 674 | |
duke@1 | 675 | /** |
duke@1 | 676 | * The documentation for values() and valueOf() in Enums are set by the |
duke@1 | 677 | * doclet. |
duke@1 | 678 | */ |
duke@1 | 679 | public static void setEnumDocumentation(Configuration configuration, |
duke@1 | 680 | ClassDoc classDoc) { |
duke@1 | 681 | MethodDoc[] methods = classDoc.methods(); |
duke@1 | 682 | for (int j = 0; j < methods.length; j++) { |
duke@1 | 683 | MethodDoc currentMethod = methods[j]; |
duke@1 | 684 | if (currentMethod.name().equals("values") && |
duke@1 | 685 | currentMethod.parameters().length == 0) { |
duke@1 | 686 | currentMethod.setRawCommentText( |
duke@1 | 687 | configuration.getText("doclet.enum_values_doc", classDoc.name())); |
duke@1 | 688 | } else if (currentMethod.name().equals("valueOf") && |
duke@1 | 689 | currentMethod.parameters().length == 1) { |
duke@1 | 690 | Type paramType = currentMethod.parameters()[0].type(); |
duke@1 | 691 | if (paramType != null && |
duke@1 | 692 | paramType.qualifiedTypeName().equals(String.class.getName())) { |
duke@1 | 693 | currentMethod.setRawCommentText( |
duke@1 | 694 | configuration.getText("doclet.enum_valueof_doc")); |
duke@1 | 695 | } |
duke@1 | 696 | } |
duke@1 | 697 | } |
duke@1 | 698 | } |
duke@1 | 699 | |
duke@1 | 700 | /** |
duke@1 | 701 | * Return true if the given Doc is deprecated. |
duke@1 | 702 | * |
duke@1 | 703 | * @param doc the Doc to check. |
duke@1 | 704 | * @return true if the given Doc is deprecated. |
duke@1 | 705 | */ |
bpatel@995 | 706 | public static boolean isDeprecated(Doc doc) { |
duke@1 | 707 | if (doc.tags("deprecated").length > 0) { |
duke@1 | 708 | return true; |
duke@1 | 709 | } |
bpatel@995 | 710 | AnnotationDesc[] annotationDescList; |
bpatel@995 | 711 | if (doc instanceof PackageDoc) |
bpatel@995 | 712 | annotationDescList = ((PackageDoc)doc).annotations(); |
bpatel@995 | 713 | else |
bpatel@995 | 714 | annotationDescList = ((ProgramElementDoc)doc).annotations(); |
duke@1 | 715 | for (int i = 0; i < annotationDescList.length; i++) { |
duke@1 | 716 | if (annotationDescList[i].annotationType().qualifiedName().equals( |
duke@1 | 717 | java.lang.Deprecated.class.getName())){ |
duke@1 | 718 | return true; |
duke@1 | 719 | } |
duke@1 | 720 | } |
duke@1 | 721 | return false; |
duke@1 | 722 | } |
jjg@1521 | 723 | |
jjg@1521 | 724 | /** |
jjg@1606 | 725 | * A convenience method to get property name from the name of the |
jjg@1606 | 726 | * getter or setter method. |
jjg@1606 | 727 | * @param name name of the getter or setter method. |
jjg@1606 | 728 | * @return the name of the property of the given setter of getter. |
jjg@1606 | 729 | */ |
jjg@1606 | 730 | public static String propertyNameFromMethodName(String name) { |
jjg@1606 | 731 | String propertyName = null; |
jjg@1606 | 732 | if (name.startsWith("get") || name.startsWith("set")) { |
jjg@1606 | 733 | propertyName = name.substring(3); |
jjg@1606 | 734 | } else if (name.startsWith("is")) { |
jjg@1606 | 735 | propertyName = name.substring(2); |
jjg@1606 | 736 | } |
jjg@1606 | 737 | if ((propertyName == null) || propertyName.isEmpty()){ |
jjg@1606 | 738 | return ""; |
jjg@1606 | 739 | } |
jjg@1606 | 740 | return propertyName.substring(0, 1).toLowerCase() |
jjg@1606 | 741 | + propertyName.substring(1); |
jjg@1606 | 742 | } |
jjg@1606 | 743 | |
jjg@1606 | 744 | /** |
jjg@1606 | 745 | * In case of JavaFX mode on, filters out classes that are private, |
jjg@1606 | 746 | * package private or having the @treatAsPrivate annotation. Those are not |
jjg@1606 | 747 | * documented in JavaFX mode. |
jjg@1606 | 748 | * |
jjg@1606 | 749 | * @param classes array of classes to be filtered. |
jjg@1606 | 750 | * @param javafx set to true if in JavaFX mode. |
jjg@1606 | 751 | * @return list of filtered classes. |
jjg@1606 | 752 | */ |
jjg@1606 | 753 | public static ClassDoc[] filterOutPrivateClasses(final ClassDoc[] classes, |
jjg@1606 | 754 | boolean javafx) { |
jjg@1606 | 755 | if (!javafx) { |
jjg@1606 | 756 | return classes; |
jjg@1606 | 757 | } |
jjg@1606 | 758 | final List<ClassDoc> filteredOutClasses = |
jjg@1606 | 759 | new ArrayList<ClassDoc>(classes.length); |
jjg@1606 | 760 | for (ClassDoc classDoc : classes) { |
jjg@1606 | 761 | if (classDoc.isPrivate() || classDoc.isPackagePrivate()) { |
jjg@1606 | 762 | continue; |
jjg@1606 | 763 | } |
jjg@1606 | 764 | Tag[] aspTags = classDoc.tags("treatAsPrivate"); |
jjg@1606 | 765 | if (aspTags != null && aspTags.length > 0) { |
jjg@1606 | 766 | continue; |
jjg@1606 | 767 | } |
jjg@1606 | 768 | filteredOutClasses.add(classDoc); |
jjg@1606 | 769 | } |
jjg@1606 | 770 | |
jjg@1606 | 771 | return filteredOutClasses.toArray(new ClassDoc[0]); |
jjg@1606 | 772 | } |
jjg@1606 | 773 | |
jjg@1606 | 774 | /** |
jjg@1521 | 775 | * Test whether the given FieldDoc is one of the declaration annotation ElementTypes |
jjg@1521 | 776 | * defined in Java 5. |
jjg@1521 | 777 | * Instead of testing for one of the new enum constants added in Java 8, test for |
jjg@1521 | 778 | * the old constants. This prevents bootstrapping problems. |
jjg@1521 | 779 | * |
jjg@1521 | 780 | * @param elt The FieldDoc to test |
jjg@1521 | 781 | * @return true, iff the given ElementType is one of the constants defined in Java 5 |
jjg@1521 | 782 | * @since 1.8 |
jjg@1521 | 783 | */ |
jjg@1521 | 784 | public static boolean isJava5DeclarationElementType(FieldDoc elt) { |
jjg@1521 | 785 | return elt.name().contentEquals(ElementType.ANNOTATION_TYPE.name()) || |
jjg@1521 | 786 | elt.name().contentEquals(ElementType.CONSTRUCTOR.name()) || |
jjg@1521 | 787 | elt.name().contentEquals(ElementType.FIELD.name()) || |
jjg@1521 | 788 | elt.name().contentEquals(ElementType.LOCAL_VARIABLE.name()) || |
jjg@1521 | 789 | elt.name().contentEquals(ElementType.METHOD.name()) || |
jjg@1521 | 790 | elt.name().contentEquals(ElementType.PACKAGE.name()) || |
jjg@1521 | 791 | elt.name().contentEquals(ElementType.PARAMETER.name()) || |
jjg@1521 | 792 | elt.name().contentEquals(ElementType.TYPE.name()); |
jjg@1521 | 793 | } |
duke@1 | 794 | } |