1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javadoc/SeeTagImpl.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,541 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javadoc; 1.30 + 1.31 +import java.io.File; 1.32 +import java.util.Locale; 1.33 + 1.34 +import com.sun.javadoc.*; 1.35 +import com.sun.tools.javac.code.Kinds; 1.36 +import com.sun.tools.javac.code.Printer; 1.37 +import com.sun.tools.javac.code.Symbol; 1.38 +import com.sun.tools.javac.code.Type.CapturedType; 1.39 +import com.sun.tools.javac.util.*; 1.40 + 1.41 +/** 1.42 + * Represents a see also documentation tag. 1.43 + * The @see tag can be plain text, or reference a class or member. 1.44 + * 1.45 + * <p><b>This is NOT part of any supported API. 1.46 + * If you write code that depends on this, you do so at your own risk. 1.47 + * This code and its internal interfaces are subject to change or 1.48 + * deletion without notice.</b> 1.49 + * 1.50 + * @author Kaiyang Liu (original) 1.51 + * @author Robert Field (rewrite) 1.52 + * @author Atul M Dambalkar 1.53 + * 1.54 + */ 1.55 +class SeeTagImpl extends TagImpl implements SeeTag, LayoutCharacters { 1.56 + 1.57 + //### TODO: Searching for classes, fields, and methods 1.58 + //### should follow the normal rules applied by the compiler. 1.59 + 1.60 + /** 1.61 + * where of where#what - i.e. the class name (may be empty) 1.62 + */ 1.63 + private String where; 1.64 + 1.65 + /** 1.66 + * what of where#what - i.e. the member (may be null) 1.67 + */ 1.68 + private String what; 1.69 + 1.70 + private PackageDoc referencedPackage; 1.71 + private ClassDoc referencedClass; 1.72 + private MemberDoc referencedMember; 1.73 + 1.74 + String label = ""; 1.75 + 1.76 + SeeTagImpl(DocImpl holder, String name, String text) { 1.77 + super(holder, name, text); 1.78 + parseSeeString(); 1.79 + if (where != null) { 1.80 + ClassDocImpl container = null; 1.81 + if (holder instanceof MemberDoc) { 1.82 + container = 1.83 + (ClassDocImpl)((ProgramElementDoc)holder).containingClass(); 1.84 + } else if (holder instanceof ClassDoc) { 1.85 + container = (ClassDocImpl)holder; 1.86 + } 1.87 + findReferenced(container); 1.88 + if (showRef) showRef(); 1.89 + } 1.90 + } 1.91 + 1.92 + private static final boolean showRef = false; 1.93 + 1.94 + private void showRef() { 1.95 + Symbol sym; 1.96 + if (referencedMember != null) { 1.97 + if (referencedMember instanceof MethodDocImpl) 1.98 + sym = ((MethodDocImpl) referencedMember).sym; 1.99 + else if (referencedMember instanceof FieldDocImpl) 1.100 + sym = ((FieldDocImpl) referencedMember).sym; 1.101 + else 1.102 + sym = ((ConstructorDocImpl) referencedMember).sym; 1.103 + } else if (referencedClass != null) { 1.104 + sym = ((ClassDocImpl) referencedClass).tsym; 1.105 + } else if (referencedPackage != null) { 1.106 + sym = ((PackageDocImpl) referencedPackage).sym; 1.107 + } else 1.108 + return; 1.109 + 1.110 + final JavacMessages messages = JavacMessages.instance(docenv().context); 1.111 + Locale locale = Locale.getDefault(); 1.112 + Printer printer = new Printer() { 1.113 + int count; 1.114 + @Override 1.115 + protected String localize(Locale locale, String key, Object... args) { 1.116 + return messages.getLocalizedString(locale, key, args); 1.117 + } 1.118 + @Override 1.119 + protected String capturedVarId(CapturedType t, Locale locale) { 1.120 + return "CAP#" + (++count); 1.121 + } 1.122 + }; 1.123 + 1.124 + String s = text.replaceAll("\\s+", " "); // normalize white space 1.125 + int sp = s.indexOf(" "); 1.126 + int lparen = s.indexOf("("); 1.127 + int rparen = s.indexOf(")"); 1.128 + String seetext = (sp == -1) ? s 1.129 + : (lparen == -1 || sp < lparen) ? s.substring(0, sp) 1.130 + : s.substring(0, rparen + 1); 1.131 + 1.132 + File file = new File(holder.position().file().getAbsoluteFile().toURI().normalize()); 1.133 + 1.134 + StringBuilder sb = new StringBuilder(); 1.135 + sb.append("+++ ").append(file).append(": ") 1.136 + .append(name()).append(" ").append(seetext).append(": "); 1.137 + sb.append(sym.getKind()).append(" "); 1.138 + if (sym.kind == Kinds.MTH || sym.kind == Kinds.VAR) 1.139 + sb.append(printer.visit(sym.owner, locale)).append("."); 1.140 + sb.append(printer.visit(sym, locale)); 1.141 + 1.142 + System.err.println(sb); 1.143 + } 1.144 + 1.145 + /** 1.146 + * get the class name part of @see, For instance, 1.147 + * if the comment is @see String#startsWith(java.lang.String) . 1.148 + * This function returns String. 1.149 + * Returns null if format was not that of java reference. 1.150 + * Return empty string if class name was not specified.. 1.151 + */ 1.152 + public String referencedClassName() { 1.153 + return where; 1.154 + } 1.155 + 1.156 + /** 1.157 + * get the package referenced by @see. For instance, 1.158 + * if the comment is @see java.lang 1.159 + * This function returns a PackageDocImpl for java.lang 1.160 + * Returns null if no known package found. 1.161 + */ 1.162 + public PackageDoc referencedPackage() { 1.163 + return referencedPackage; 1.164 + } 1.165 + 1.166 + /** 1.167 + * get the class referenced by the class name part of @see, For instance, 1.168 + * if the comment is @see String#startsWith(java.lang.String) . 1.169 + * This function returns a ClassDocImpl for java.lang.String. 1.170 + * Returns null if class is not a class specified on the javadoc command line.. 1.171 + */ 1.172 + public ClassDoc referencedClass() { 1.173 + return referencedClass; 1.174 + } 1.175 + 1.176 + /** 1.177 + * get the name of the member referenced by the prototype part of @see, 1.178 + * For instance, 1.179 + * if the comment is @see String#startsWith(java.lang.String) . 1.180 + * This function returns "startsWith(java.lang.String)" 1.181 + * Returns null if format was not that of java reference. 1.182 + * Return empty string if member name was not specified.. 1.183 + */ 1.184 + public String referencedMemberName() { 1.185 + return what; 1.186 + } 1.187 + 1.188 + /** 1.189 + * get the member referenced by the prototype part of @see, 1.190 + * For instance, 1.191 + * if the comment is @see String#startsWith(java.lang.String) . 1.192 + * This function returns a MethodDocImpl for startsWith. 1.193 + * Returns null if member could not be determined. 1.194 + */ 1.195 + public MemberDoc referencedMember() { 1.196 + return referencedMember; 1.197 + } 1.198 + 1.199 + 1.200 + /** 1.201 + * parse @see part of comment. Determine 'where' and 'what' 1.202 + */ 1.203 + private void parseSeeString() { 1.204 + int len = text.length(); 1.205 + if (len == 0) { 1.206 + return; 1.207 + } 1.208 + switch (text.charAt(0)) { 1.209 + case '<': 1.210 + if (text.charAt(len-1) != '>') { 1.211 + docenv().warning(holder, 1.212 + "tag.see.no_close_bracket_on_url", 1.213 + name, text); 1.214 + } 1.215 + return; 1.216 + case '"': 1.217 + if (len == 1 || text.charAt(len-1) != '"') { 1.218 + docenv().warning(holder, 1.219 + "tag.see.no_close_quote", 1.220 + name, text); 1.221 + } else { 1.222 +// text = text.substring(1,len-1); // strip quotes 1.223 + } 1.224 + return; 1.225 + } 1.226 + 1.227 + // check that the text is one word, with possible parentheses 1.228 + // this part of code doesn't allow 1.229 + // @see <a href=.....>asfd</a> 1.230 + // comment it. 1.231 + 1.232 + // the code assumes that there is no initial white space. 1.233 + int parens = 0; 1.234 + int commentstart = 0; 1.235 + int start = 0; 1.236 + int cp; 1.237 + for (int i = start; i < len ; i += Character.charCount(cp)) { 1.238 + cp = text.codePointAt(i); 1.239 + switch (cp) { 1.240 + case '(': parens++; break; 1.241 + case ')': parens--; break; 1.242 + case '[': case ']': case '.': case '#': break; 1.243 + case ',': 1.244 + if (parens <= 0) { 1.245 + docenv().warning(holder, 1.246 + "tag.see.malformed_see_tag", 1.247 + name, text); 1.248 + return; 1.249 + } 1.250 + break; 1.251 + case ' ': case '\t': case '\n': case CR: 1.252 + if (parens == 0) { //here onwards the comment starts. 1.253 + commentstart = i; 1.254 + i = len; 1.255 + } 1.256 + break; 1.257 + default: 1.258 + if (!Character.isJavaIdentifierPart(cp)) { 1.259 + docenv().warning(holder, 1.260 + "tag.see.illegal_character", 1.261 + name, ""+cp, text); 1.262 + } 1.263 + break; 1.264 + } 1.265 + } 1.266 + if (parens != 0) { 1.267 + docenv().warning(holder, 1.268 + "tag.see.malformed_see_tag", 1.269 + name, text); 1.270 + return; 1.271 + } 1.272 + 1.273 + String seetext = ""; 1.274 + String labeltext = ""; 1.275 + 1.276 + if (commentstart > 0) { 1.277 + seetext = text.substring(start, commentstart); 1.278 + labeltext = text.substring(commentstart + 1); 1.279 + // strip off the white space which can be between seetext and the 1.280 + // actual label. 1.281 + for (int i = 0; i < labeltext.length(); i++) { 1.282 + char ch2 = labeltext.charAt(i); 1.283 + if (!(ch2 == ' ' || ch2 == '\t' || ch2 == '\n')) { 1.284 + label = labeltext.substring(i); 1.285 + break; 1.286 + } 1.287 + } 1.288 + } else { 1.289 + seetext = text; 1.290 + label = ""; 1.291 + } 1.292 + 1.293 + int sharp = seetext.indexOf('#'); 1.294 + if (sharp >= 0) { 1.295 + // class#member 1.296 + where = seetext.substring(0, sharp); 1.297 + what = seetext.substring(sharp + 1); 1.298 + } else { 1.299 + if (seetext.indexOf('(') >= 0) { 1.300 + docenv().warning(holder, 1.301 + "tag.see.missing_sharp", 1.302 + name, text); 1.303 + where = ""; 1.304 + what = seetext; 1.305 + } 1.306 + else { 1.307 + // no member specified, text names class 1.308 + where = seetext; 1.309 + what = null; 1.310 + } 1.311 + } 1.312 + } 1.313 + 1.314 + /** 1.315 + * Find what is referenced by the see also. If possible, sets 1.316 + * referencedClass and referencedMember. 1.317 + * 1.318 + * @param containingClass the class containing the comment containing 1.319 + * the tag. May be null, if, for example, it is a package comment. 1.320 + */ 1.321 + private void findReferenced(ClassDocImpl containingClass) { 1.322 + if (where.length() > 0) { 1.323 + if (containingClass != null) { 1.324 + referencedClass = containingClass.findClass(where); 1.325 + } else { 1.326 + referencedClass = docenv().lookupClass(where); 1.327 + } 1.328 + if (referencedClass == null && holder() instanceof ProgramElementDoc) { 1.329 + referencedClass = docenv().lookupClass( 1.330 + ((ProgramElementDoc) holder()).containingPackage().name() + "." + where); 1.331 + } 1.332 + 1.333 + if (referencedClass == null) { /* may just not be in this run */ 1.334 + // check if it's a package name 1.335 + referencedPackage = docenv().lookupPackage(where); 1.336 + return; 1.337 + } 1.338 + } else { 1.339 + if (containingClass == null) { 1.340 + docenv().warning(holder, 1.341 + "tag.see.class_not_specified", 1.342 + name, text); 1.343 + return; 1.344 + } else { 1.345 + referencedClass = containingClass; 1.346 + } 1.347 + } 1.348 + where = referencedClass.qualifiedName(); 1.349 + 1.350 + if (what == null) { 1.351 + return; 1.352 + } else { 1.353 + int paren = what.indexOf('('); 1.354 + String memName = (paren >= 0 ? what.substring(0, paren) : what); 1.355 + String[] paramarr; 1.356 + if (paren > 0) { 1.357 + // has parameter list -- should be method or constructor 1.358 + paramarr = new ParameterParseMachine(what. 1.359 + substring(paren, what.length())).parseParameters(); 1.360 + if (paramarr != null) { 1.361 + referencedMember = findExecutableMember(memName, paramarr, 1.362 + referencedClass); 1.363 + } else { 1.364 + referencedMember = null; 1.365 + } 1.366 + } else { 1.367 + // no parameter list -- should be field 1.368 + referencedMember = findExecutableMember(memName, null, 1.369 + referencedClass); 1.370 + FieldDoc fd = ((ClassDocImpl)referencedClass). 1.371 + findField(memName); 1.372 + // when no args given, prefer fields over methods 1.373 + if (referencedMember == null || 1.374 + (fd != null && 1.375 + fd.containingClass() 1.376 + .subclassOf(referencedMember.containingClass()))) { 1.377 + referencedMember = fd; 1.378 + } 1.379 + } 1.380 + if (referencedMember == null) { 1.381 + docenv().warning(holder, 1.382 + "tag.see.can_not_find_member", 1.383 + name, what, where); 1.384 + } 1.385 + } 1.386 + } 1.387 + 1.388 + private MemberDoc findReferencedMethod(String memName, String[] paramarr, 1.389 + ClassDoc referencedClass) { 1.390 + MemberDoc meth = findExecutableMember(memName, paramarr, referencedClass); 1.391 + ClassDoc[] nestedclasses = referencedClass.innerClasses(); 1.392 + if (meth == null) { 1.393 + for (int i = 0; i < nestedclasses.length; i++) { 1.394 + meth = findReferencedMethod(memName, paramarr, nestedclasses[i]); 1.395 + if (meth != null) { 1.396 + return meth; 1.397 + } 1.398 + } 1.399 + } 1.400 + return null; 1.401 + } 1.402 + 1.403 + private MemberDoc findExecutableMember(String memName, String[] paramarr, 1.404 + ClassDoc referencedClass) { 1.405 + if (memName.equals(referencedClass.name())) { 1.406 + return ((ClassDocImpl)referencedClass).findConstructor(memName, 1.407 + paramarr); 1.408 + } else { // it's a method. 1.409 + return ((ClassDocImpl)referencedClass).findMethod(memName, 1.410 + paramarr); 1.411 + } 1.412 + } 1.413 + 1.414 + // separate "int, String" from "(int, String)" 1.415 + // (int i, String s) ==> [0] = "int", [1] = String 1.416 + // (int[][], String[]) ==> [0] = "int[][]" // [1] = "String[]" 1.417 + class ParameterParseMachine { 1.418 + static final int START = 0; 1.419 + static final int TYPE = 1; 1.420 + static final int NAME = 2; 1.421 + static final int TNSPACE = 3; // space between type and name 1.422 + static final int ARRAYDECORATION = 4; 1.423 + static final int ARRAYSPACE = 5; 1.424 + 1.425 + String parameters; 1.426 + 1.427 + StringBuilder typeId; 1.428 + 1.429 + ListBuffer<String> paramList; 1.430 + 1.431 + ParameterParseMachine(String parameters) { 1.432 + this.parameters = parameters; 1.433 + this.paramList = new ListBuffer<String>(); 1.434 + typeId = new StringBuilder(); 1.435 + } 1.436 + 1.437 + public String[] parseParameters() { 1.438 + if (parameters.equals("()")) { 1.439 + return new String[0]; 1.440 + } // now strip off '(' and ')' 1.441 + int state = START; 1.442 + int prevstate = START; 1.443 + parameters = parameters.substring(1, parameters.length() - 1); 1.444 + int cp; 1.445 + for (int index = 0; index < parameters.length(); index += Character.charCount(cp)) { 1.446 + cp = parameters.codePointAt(index); 1.447 + switch (state) { 1.448 + case START: 1.449 + if (Character.isJavaIdentifierStart(cp)) { 1.450 + typeId.append(Character.toChars(cp)); 1.451 + state = TYPE; 1.452 + } 1.453 + prevstate = START; 1.454 + break; 1.455 + case TYPE: 1.456 + if (Character.isJavaIdentifierPart(cp) || cp == '.') { 1.457 + typeId.append(Character.toChars(cp)); 1.458 + } else if (cp == '[') { 1.459 + typeId.append('['); 1.460 + state = ARRAYDECORATION; 1.461 + } else if (Character.isWhitespace(cp)) { 1.462 + state = TNSPACE; 1.463 + } else if (cp == ',') { // no name, just type 1.464 + addTypeToParamList(); 1.465 + state = START; 1.466 + } 1.467 + prevstate = TYPE; 1.468 + break; 1.469 + case TNSPACE: 1.470 + if (Character.isJavaIdentifierStart(cp)) { // name 1.471 + if (prevstate == ARRAYDECORATION) { 1.472 + docenv().warning(holder, 1.473 + "tag.missing_comma_space", 1.474 + name, 1.475 + "(" + parameters + ")"); 1.476 + return (String[])null; 1.477 + } 1.478 + addTypeToParamList(); 1.479 + state = NAME; 1.480 + } else if (cp == '[') { 1.481 + typeId.append('['); 1.482 + state = ARRAYDECORATION; 1.483 + } else if (cp == ',') { // just the type 1.484 + addTypeToParamList(); 1.485 + state = START; 1.486 + } // consume rest all 1.487 + prevstate = TNSPACE; 1.488 + break; 1.489 + case ARRAYDECORATION: 1.490 + if (cp == ']') { 1.491 + typeId.append(']'); 1.492 + state = TNSPACE; 1.493 + } else if (!Character.isWhitespace(cp)) { 1.494 + docenv().warning(holder, 1.495 + "tag.illegal_char_in_arr_dim", 1.496 + name, 1.497 + "(" + parameters + ")"); 1.498 + return (String[])null; 1.499 + } 1.500 + prevstate = ARRAYDECORATION; 1.501 + break; 1.502 + case NAME: 1.503 + if (cp == ',') { // just consume everything till ',' 1.504 + state = START; 1.505 + } 1.506 + prevstate = NAME; 1.507 + break; 1.508 + } 1.509 + } 1.510 + if (state == ARRAYDECORATION || 1.511 + (state == START && prevstate == TNSPACE)) { 1.512 + docenv().warning(holder, 1.513 + "tag.illegal_see_tag", 1.514 + "(" + parameters + ")"); 1.515 + } 1.516 + if (typeId.length() > 0) { 1.517 + paramList.append(typeId.toString()); 1.518 + } 1.519 + return paramList.toArray(new String[paramList.length()]); 1.520 + } 1.521 + 1.522 + void addTypeToParamList() { 1.523 + if (typeId.length() > 0) { 1.524 + paramList.append(typeId.toString()); 1.525 + typeId.setLength(0); 1.526 + } 1.527 + } 1.528 + } 1.529 + 1.530 + /** 1.531 + * Return the kind of this tag. 1.532 + */ 1.533 + @Override 1.534 + public String kind() { 1.535 + return "@see"; 1.536 + } 1.537 + 1.538 + /** 1.539 + * Return the label of the see tag. 1.540 + */ 1.541 + public String label() { 1.542 + return label; 1.543 + } 1.544 +}