src/share/classes/com/sun/tools/javadoc/SeeTagImpl.java

changeset 1
9a66ca7c79fa
child 74
5a9172b251dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javadoc/SeeTagImpl.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,477 @@
     1.4 +/*
     1.5 + * Copyright 1997-2005 Sun Microsystems, Inc.  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.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javadoc;
    1.30 +
    1.31 +import com.sun.tools.javac.util.*;
    1.32 +
    1.33 +import com.sun.javadoc.*;
    1.34 +
    1.35 +/**
    1.36 + * Represents a see also documentation tag.
    1.37 + * The @see tag can be plain text, or reference a class or member.
    1.38 + *
    1.39 + * @author Kaiyang Liu (original)
    1.40 + * @author Robert Field (rewrite)
    1.41 + * @author Atul M Dambalkar
    1.42 + *
    1.43 + */
    1.44 +class SeeTagImpl extends TagImpl implements SeeTag, LayoutCharacters {
    1.45 +
    1.46 +    //### TODO: Searching for classes, fields, and methods
    1.47 +    //### should follow the normal rules applied by the compiler.
    1.48 +
    1.49 +    /**
    1.50 +     * where of  where#what - i.e. the class name (may be empty)
    1.51 +     */
    1.52 +    private String where;
    1.53 +
    1.54 +    /**
    1.55 +     * what of  where#what - i.e. the member (may be null)
    1.56 +     */
    1.57 +    private String what;
    1.58 +
    1.59 +    private PackageDoc referencedPackage;
    1.60 +    private ClassDoc referencedClass;
    1.61 +    private MemberDoc referencedMember;
    1.62 +
    1.63 +    String label = "";
    1.64 +
    1.65 +    SeeTagImpl(DocImpl holder, String name, String text) {
    1.66 +        super(holder, name, text);
    1.67 +        parseSeeString();
    1.68 +        if (where != null) {
    1.69 +            ClassDocImpl container = null;
    1.70 +            if (holder instanceof MemberDoc) {
    1.71 +                container =
    1.72 +                  (ClassDocImpl)((ProgramElementDoc)holder).containingClass();
    1.73 +            } else if (holder instanceof ClassDoc) {
    1.74 +                container = (ClassDocImpl)holder;
    1.75 +            }
    1.76 +            findReferenced(container);
    1.77 +        }
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * get the class name part of @see, For instance,
    1.82 +     * if the comment is @see String#startsWith(java.lang.String) .
    1.83 +     *      This function returns String.
    1.84 +     * Returns null if format was not that of java reference.
    1.85 +     * Return empty string if class name was not specified..
    1.86 +     */
    1.87 +    public String referencedClassName() {
    1.88 +        return where;
    1.89 +    }
    1.90 +
    1.91 +    /**
    1.92 +     * get the package referenced by  @see. For instance,
    1.93 +     * if the comment is @see java.lang
    1.94 +     *      This function returns a PackageDocImpl for java.lang
    1.95 +     * Returns null if no known package found.
    1.96 +     */
    1.97 +    public PackageDoc referencedPackage() {
    1.98 +        return referencedPackage;
    1.99 +    }
   1.100 +
   1.101 +    /**
   1.102 +     * get the class referenced by the class name part of @see, For instance,
   1.103 +     * if the comment is @see String#startsWith(java.lang.String) .
   1.104 +     *      This function returns a ClassDocImpl for java.lang.String.
   1.105 +     * Returns null if class is not a class specified on the javadoc command line..
   1.106 +     */
   1.107 +    public ClassDoc referencedClass() {
   1.108 +        return referencedClass;
   1.109 +    }
   1.110 +
   1.111 +    /**
   1.112 +     * get the name of the member referenced by the prototype part of @see,
   1.113 +     * For instance,
   1.114 +     * if the comment is @see String#startsWith(java.lang.String) .
   1.115 +     *      This function returns "startsWith(java.lang.String)"
   1.116 +     * Returns null if format was not that of java reference.
   1.117 +     * Return empty string if member name was not specified..
   1.118 +     */
   1.119 +    public String referencedMemberName() {
   1.120 +        return what;
   1.121 +    }
   1.122 +
   1.123 +    /**
   1.124 +     * get the member referenced by the prototype part of @see,
   1.125 +     * For instance,
   1.126 +     * if the comment is @see String#startsWith(java.lang.String) .
   1.127 +     *      This function returns a MethodDocImpl for startsWith.
   1.128 +     * Returns null if member could not be determined.
   1.129 +     */
   1.130 +    public MemberDoc referencedMember() {
   1.131 +        return referencedMember;
   1.132 +    }
   1.133 +
   1.134 +
   1.135 +    /**
   1.136 +     * parse @see part of comment. Determine 'where' and 'what'
   1.137 +     */
   1.138 +    private void parseSeeString() {
   1.139 +        int len = text.length();
   1.140 +        if (len == 0) {
   1.141 +            return;
   1.142 +        }
   1.143 +        switch (text.charAt(0)) {
   1.144 +            case '<':
   1.145 +                if (text.charAt(len-1) != '>') {
   1.146 +                    docenv().warning(holder,
   1.147 +                                     "tag.see.no_close_bracket_on_url",
   1.148 +                                     name, text);
   1.149 +                }
   1.150 +                return;
   1.151 +            case '"':
   1.152 +                if (len == 1 || text.charAt(len-1) != '"') {
   1.153 +                    docenv().warning(holder,
   1.154 +                                     "tag.see.no_close_quote",
   1.155 +                                     name, text);
   1.156 +                } else {
   1.157 +//                    text = text.substring(1,len-1); // strip quotes
   1.158 +                }
   1.159 +                return;
   1.160 +        }
   1.161 +
   1.162 +        // check that the text is one word, with possible parentheses
   1.163 +        // this part of code doesn't allow
   1.164 +        // @see <a href=.....>asfd</a>
   1.165 +        // comment it.
   1.166 +
   1.167 +        // the code assumes that there is no initial white space.
   1.168 +        int parens = 0;
   1.169 +        int commentstart = 0;
   1.170 +        int start = 0;
   1.171 +        int cp;
   1.172 +        for (int i = start; i < len ; i += Character.charCount(cp)) {
   1.173 +            cp = text.codePointAt(i);
   1.174 +            switch (cp) {
   1.175 +                case '(': parens++; break;
   1.176 +                case ')': parens--; break;
   1.177 +                case '[': case ']': case '.': case '#': break;
   1.178 +                case ',':
   1.179 +                    if (parens <= 0) {
   1.180 +                        docenv().warning(holder,
   1.181 +                                         "tag.see.malformed_see_tag",
   1.182 +                                         name, text);
   1.183 +                        return;
   1.184 +                    }
   1.185 +                    break;
   1.186 +                case ' ': case '\t': case '\n': case CR:
   1.187 +                    if (parens == 0) { //here onwards the comment starts.
   1.188 +                        commentstart = i;
   1.189 +                        i = len;
   1.190 +                    }
   1.191 +                    break;
   1.192 +                default:
   1.193 +                    if (!Character.isJavaIdentifierPart(cp)) {
   1.194 +                        docenv().warning(holder,
   1.195 +                                         "tag.see.illegal_character",
   1.196 +                                         name, ""+cp, text);
   1.197 +                    }
   1.198 +                    break;
   1.199 +            }
   1.200 +        }
   1.201 +        if (parens != 0) {
   1.202 +            docenv().warning(holder,
   1.203 +                             "tag.see.malformed_see_tag",
   1.204 +                             name, text);
   1.205 +            return;
   1.206 +        }
   1.207 +
   1.208 +        String seetext = "";
   1.209 +        String labeltext = "";
   1.210 +
   1.211 +        if (commentstart > 0) {
   1.212 +            seetext = text.substring(start, commentstart);
   1.213 +            labeltext = text.substring(commentstart + 1);
   1.214 +            // strip off the white space which can be between seetext and the
   1.215 +            // actual label.
   1.216 +            for (int i = 0; i < labeltext.length(); i++) {
   1.217 +                char ch2 = labeltext.charAt(i);
   1.218 +                if (!(ch2 == ' ' || ch2 == '\t' || ch2 == '\n')) {
   1.219 +                    label = labeltext.substring(i);
   1.220 +                    break;
   1.221 +                }
   1.222 +            }
   1.223 +        } else {
   1.224 +            seetext = text;
   1.225 +            label = "";
   1.226 +        }
   1.227 +
   1.228 +        int sharp = seetext.indexOf('#');
   1.229 +        if (sharp >= 0) {
   1.230 +            // class#member
   1.231 +            where = seetext.substring(0, sharp);
   1.232 +            what = seetext.substring(sharp + 1);
   1.233 +        } else {
   1.234 +            if (seetext.indexOf('(') >= 0) {
   1.235 +                docenv().warning(holder,
   1.236 +                                 "tag.see.missing_sharp",
   1.237 +                                 name, text);
   1.238 +                where = "";
   1.239 +                what = seetext;
   1.240 +            }
   1.241 +            else {
   1.242 +                // no member specified, text names class
   1.243 +                where = seetext;
   1.244 +                what = null;
   1.245 +            }
   1.246 +        }
   1.247 +    }
   1.248 +
   1.249 +    /**
   1.250 +     * Find what is referenced by the see also.  If possible, sets
   1.251 +     * referencedClass and referencedMember.
   1.252 +     *
   1.253 +     * @param containingClass the class containing the comment containing
   1.254 +     * the tag. May be null, if, for example, it is a package comment.
   1.255 +     */
   1.256 +    private void findReferenced(ClassDocImpl containingClass) {
   1.257 +        if (where.length() > 0) {
   1.258 +            if (containingClass != null) {
   1.259 +                referencedClass = containingClass.findClass(where);
   1.260 +            } else {
   1.261 +                referencedClass = docenv().lookupClass(where);
   1.262 +            }
   1.263 +            if (referencedClass == null && holder() instanceof ProgramElementDoc) {
   1.264 +                referencedClass = docenv().lookupClass(
   1.265 +                    ((ProgramElementDoc) holder()).containingPackage().name() + "." + where);
   1.266 +            }
   1.267 +
   1.268 +            if (referencedClass == null) { /* may just not be in this run */
   1.269 +//                docenv().warning(holder, "tag.see.class_not_found",
   1.270 +//                                 where, text);
   1.271 +                // check if it's a package name
   1.272 +                referencedPackage = docenv().lookupPackage(where);
   1.273 +                return;
   1.274 +            }
   1.275 +        } else {
   1.276 +            if (containingClass == null) {
   1.277 +                docenv().warning(holder,
   1.278 +                                 "tag.see.class_not_specified",
   1.279 +                                 name, text);
   1.280 +                return;
   1.281 +            } else {
   1.282 +                referencedClass = containingClass;
   1.283 +            }
   1.284 +        }
   1.285 +        where = referencedClass.qualifiedName();
   1.286 +
   1.287 +        if (what == null) {
   1.288 +            return;
   1.289 +        } else {
   1.290 +            int paren = what.indexOf('(');
   1.291 +            String memName = (paren >= 0 ? what.substring(0, paren) : what);
   1.292 +            String[] paramarr;
   1.293 +            if (paren > 0) {
   1.294 +                // has parameter list -- should be method or constructor
   1.295 +                paramarr = new ParameterParseMachine(what.
   1.296 +                        substring(paren, what.length())).parseParameters();
   1.297 +                if (paramarr != null) {
   1.298 +                    referencedMember = findExecutableMember(memName, paramarr,
   1.299 +                                                            referencedClass);
   1.300 +                } else {
   1.301 +                    referencedMember = null;
   1.302 +                }
   1.303 +            } else {
   1.304 +                // no parameter list -- should be field
   1.305 +                referencedMember = findExecutableMember(memName, null,
   1.306 +                                                        referencedClass);
   1.307 +                FieldDoc fd = ((ClassDocImpl)referencedClass).
   1.308 +                                                            findField(memName);
   1.309 +                // when no args given, prefer fields over methods
   1.310 +                if (referencedMember == null ||
   1.311 +                    (fd != null &&
   1.312 +                     fd.containingClass()
   1.313 +                         .subclassOf(referencedMember.containingClass()))) {
   1.314 +                    referencedMember = fd;
   1.315 +                }
   1.316 +            }
   1.317 +            if (referencedMember == null) {
   1.318 +                docenv().warning(holder,
   1.319 +                                 "tag.see.can_not_find_member",
   1.320 +                                 name, what, where);
   1.321 +            }
   1.322 +        }
   1.323 +    }
   1.324 +
   1.325 +    private MemberDoc findReferencedMethod(String memName, String[] paramarr,
   1.326 +                                           ClassDoc referencedClass) {
   1.327 +        MemberDoc meth = findExecutableMember(memName, paramarr, referencedClass);
   1.328 +        ClassDoc[] nestedclasses = referencedClass.innerClasses();
   1.329 +        if (meth == null) {
   1.330 +            for (int i = 0; i < nestedclasses.length; i++) {
   1.331 +                meth = findReferencedMethod(memName, paramarr, nestedclasses[i]);
   1.332 +                if (meth != null) {
   1.333 +                    return meth;
   1.334 +                }
   1.335 +            }
   1.336 +        }
   1.337 +        return null;
   1.338 +    }
   1.339 +
   1.340 +    private MemberDoc findExecutableMember(String memName, String[] paramarr,
   1.341 +                                           ClassDoc referencedClass) {
   1.342 +        if (memName.equals(referencedClass.name())) {
   1.343 +            return ((ClassDocImpl)referencedClass).findConstructor(memName,
   1.344 +                                                                   paramarr);
   1.345 +        } else {   // it's a method.
   1.346 +            return ((ClassDocImpl)referencedClass).findMethod(memName,
   1.347 +                                                              paramarr);
   1.348 +        }
   1.349 +    }
   1.350 +
   1.351 +    // separate "int, String" from "(int, String)"
   1.352 +    // (int i, String s) ==> [0] = "int",  [1] = String
   1.353 +    // (int[][], String[]) ==> [0] = "int[][]" // [1] = "String[]"
   1.354 +    class ParameterParseMachine {
   1.355 +        final int START = 0;
   1.356 +        final int TYPE = 1;
   1.357 +        final int NAME = 2;
   1.358 +        final int TNSPACE = 3;  // space between type and name
   1.359 +        final int ARRAYDECORATION = 4;
   1.360 +        final int ARRAYSPACE = 5;
   1.361 +
   1.362 +        String parameters;
   1.363 +
   1.364 +        StringBuffer typeId;
   1.365 +
   1.366 +        ListBuffer<String> paramList;
   1.367 +
   1.368 +        ParameterParseMachine(String parameters) {
   1.369 +            this.parameters = parameters;
   1.370 +            this.paramList = new ListBuffer<String>();
   1.371 +            typeId = new StringBuffer();
   1.372 +        }
   1.373 +
   1.374 +        public String[] parseParameters() {
   1.375 +            if (parameters.equals("()")) {
   1.376 +                return new String[0];
   1.377 +            }   // now strip off '(' and ')'
   1.378 +            int state = START;
   1.379 +            int prevstate = START;
   1.380 +            parameters = parameters.substring(1, parameters.length() - 1);
   1.381 +            int cp;
   1.382 +            for (int index = 0; index < parameters.length(); index += Character.charCount(cp)) {
   1.383 +                cp = parameters.codePointAt(index);
   1.384 +                switch (state) {
   1.385 +                    case START:
   1.386 +                        if (Character.isJavaIdentifierStart(cp)) {
   1.387 +                            typeId.append(Character.toChars(cp));
   1.388 +                            state = TYPE;
   1.389 +                        }
   1.390 +                        prevstate = START;
   1.391 +                        break;
   1.392 +                    case TYPE:
   1.393 +                        if (Character.isJavaIdentifierPart(cp) || cp == '.') {
   1.394 +                            typeId.append(Character.toChars(cp));
   1.395 +                        } else if (cp == '[') {
   1.396 +                            typeId.append('[');
   1.397 +                            state = ARRAYDECORATION;
   1.398 +                        } else if (Character.isWhitespace(cp)) {
   1.399 +                            state = TNSPACE;
   1.400 +                        } else if (cp == ',') {  // no name, just type
   1.401 +                            addTypeToParamList();
   1.402 +                            state = START;
   1.403 +                        }
   1.404 +                        prevstate = TYPE;
   1.405 +                        break;
   1.406 +                    case TNSPACE:
   1.407 +                        if (Character.isJavaIdentifierStart(cp)) { // name
   1.408 +                            if (prevstate == ARRAYDECORATION) {
   1.409 +                                docenv().warning(holder,
   1.410 +                                                 "tag.missing_comma_space",
   1.411 +                                                 name,
   1.412 +                                                 "(" + parameters + ")");
   1.413 +                                return (String[])null;
   1.414 +                            }
   1.415 +                            addTypeToParamList();
   1.416 +                            state = NAME;
   1.417 +                        } else if (cp == '[') {
   1.418 +                            typeId.append('[');
   1.419 +                            state = ARRAYDECORATION;
   1.420 +                        } else if (cp == ',') {   // just the type
   1.421 +                            addTypeToParamList();
   1.422 +                            state = START;
   1.423 +                        } // consume rest all
   1.424 +                        prevstate = TNSPACE;
   1.425 +                        break;
   1.426 +                    case ARRAYDECORATION:
   1.427 +                        if (cp == ']') {
   1.428 +                            typeId.append(']');
   1.429 +                            state = TNSPACE;
   1.430 +                        } else if (!Character.isWhitespace(cp)) {
   1.431 +                            docenv().warning(holder,
   1.432 +                                             "tag.illegal_char_in_arr_dim",
   1.433 +                                             name,
   1.434 +                                             "(" + parameters + ")");
   1.435 +                            return (String[])null;
   1.436 +                        }
   1.437 +                        prevstate = ARRAYDECORATION;
   1.438 +                        break;
   1.439 +                    case NAME:
   1.440 +                        if (cp == ',') {  // just consume everything till ','
   1.441 +                            state = START;
   1.442 +                        }
   1.443 +                        prevstate = NAME;
   1.444 +                        break;
   1.445 +                }
   1.446 +            }
   1.447 +            if (state == ARRAYDECORATION ||
   1.448 +                (state == START && prevstate == TNSPACE)) {
   1.449 +                docenv().warning(holder,
   1.450 +                                 "tag.illegal_see_tag",
   1.451 +                                 "(" + parameters + ")");
   1.452 +            }
   1.453 +            if (typeId.length() > 0) {
   1.454 +                paramList.append(typeId.toString());
   1.455 +            }
   1.456 +            return (String[])paramList.toArray(new String[paramList.length()]);
   1.457 +        }
   1.458 +
   1.459 +        void addTypeToParamList() {
   1.460 +            if (typeId.length() > 0) {
   1.461 +                paramList.append(typeId.toString());
   1.462 +                typeId.setLength(0);
   1.463 +            }
   1.464 +        }
   1.465 +    }
   1.466 +
   1.467 +    /**
   1.468 +     * Return the kind of this tag.
   1.469 +     */
   1.470 +    public String kind() {
   1.471 +        return "@see";
   1.472 +    }
   1.473 +
   1.474 +    /**
   1.475 +     * Return the label of the see tag.
   1.476 +     */
   1.477 +    public String label() {
   1.478 +        return label;
   1.479 +    }
   1.480 +}

mercurial