# HG changeset patch # User lana # Date 1340685556 25200 # Node ID e111e4587ccada8eb93f72e834e378c76256f4b7 # Parent a39c991921843d1e096cd5e3600e4ed3acca8103# Parent 3468519d9b45568958e7cfa3556d13a5c5ea4585 Merge diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/api/JavacTrees.java --- a/src/share/classes/com/sun/tools/javac/api/JavacTrees.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTrees.java Mon Jun 25 21:39:16 2012 -0700 @@ -60,7 +60,7 @@ import com.sun.tools.javac.comp.MemberEnter; import com.sun.tools.javac.comp.Resolve; import com.sun.tools.javac.model.JavacElements; -import com.sun.tools.javac.parser.EndPosTable; +import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.processing.JavacProcessingEnvironment; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.*; @@ -240,10 +240,11 @@ public String getDocComment(TreePath path) { CompilationUnitTree t = path.getCompilationUnit(); - if (t instanceof JCTree.JCCompilationUnit) { + Tree leaf = path.getLeaf(); + if (t instanceof JCTree.JCCompilationUnit && leaf instanceof JCTree) { JCCompilationUnit cu = (JCCompilationUnit) t; if (cu.docComments != null) { - return cu.docComments.get(path.getLeaf()); + return cu.docComments.getCommentText((JCTree) leaf); } } return null; diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/comp/Enter.java --- a/src/share/classes/com/sun/tools/javac/comp/Enter.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/comp/Enter.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -312,7 +312,7 @@ tree.packge); if (addEnv || (tree0.packageAnnotations.isEmpty() && tree.docComments != null && - tree.docComments.get(tree) != null)) { + tree.docComments.hasComment(tree))) { typeEnvs.put(tree.packge, topEnv); } } diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/comp/Lower.java --- a/src/share/classes/com/sun/tools/javac/comp/Lower.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -40,7 +40,7 @@ import com.sun.tools.javac.code.Type.*; import com.sun.tools.javac.jvm.Target; -import com.sun.tools.javac.parser.EndPosTable; +import com.sun.tools.javac.tree.EndPosTable; import static com.sun.tools.javac.code.Flags.*; import static com.sun.tools.javac.code.Flags.BLOCK; diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/file/ZipFileIndex.java --- a/src/share/classes/com/sun/tools/javac/file/ZipFileIndex.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/file/ZipFileIndex.java Mon Jun 25 21:39:16 2012 -0700 @@ -36,6 +36,7 @@ import java.util.Arrays; import java.util.Calendar; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -284,7 +285,7 @@ try { checkIndex(); if (allDirs == Collections.EMPTY_SET) { - allDirs = new HashSet(directories.keySet()); + allDirs = new java.util.LinkedHashSet(directories.keySet()); } return allDirs; @@ -572,7 +573,7 @@ // Add each of the files if (entryCount > 0) { - directories = new HashMap(); + directories = new LinkedHashMap(); ArrayList entryList = new ArrayList(); int pos = 2; for (int i = 0; i < entryCount; i++) { @@ -867,7 +868,7 @@ if (zipFile.lastModified() != fileStamp) { ret = false; } else { - directories = new HashMap(); + directories = new LinkedHashMap(); int numDirs = raf.readInt(); for (int nDirs = 0; nDirs < numDirs; nDirs++) { int dirNameBytesLen = raf.readInt(); diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/jvm/CRTable.java --- a/src/share/classes/com/sun/tools/javac/jvm/CRTable.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/jvm/CRTable.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -31,7 +31,7 @@ import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.List; import com.sun.tools.javac.tree.JCTree.*; -import com.sun.tools.javac.parser.EndPosTable; +import com.sun.tools.javac.tree.EndPosTable; /** This class contains the CharacterRangeTable for some method * and the hashtable for mapping trees or lists of trees to their diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/jvm/Gen.java --- a/src/share/classes/com/sun/tools/javac/jvm/Gen.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/jvm/Gen.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -37,7 +37,7 @@ import com.sun.tools.javac.code.Type.*; import com.sun.tools.javac.jvm.Code.*; import com.sun.tools.javac.jvm.Items.*; -import com.sun.tools.javac.parser.EndPosTable; +import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree.*; import static com.sun.tools.javac.code.Flags.*; diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/model/JavacElements.java --- a/src/share/classes/com/sun/tools/javac/model/JavacElements.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/model/JavacElements.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -28,11 +28,14 @@ import java.lang.annotation.Annotation; import java.lang.annotation.Inherited; import java.util.Map; + import javax.lang.model.SourceVersion; import javax.lang.model.element.*; import javax.lang.model.type.DeclaredType; import javax.lang.model.util.Elements; import javax.tools.JavaFileObject; +import static javax.lang.model.util.ElementFilter.methodsIn; + import com.sun.tools.javac.code.*; import com.sun.tools.javac.code.Symbol.*; import com.sun.tools.javac.code.TypeTags; @@ -47,9 +50,7 @@ import com.sun.tools.javac.tree.TreeScanner; import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.Name; - import static com.sun.tools.javac.tree.JCTree.Tag.*; -import static javax.lang.model.util.ElementFilter.methodsIn; /** * Utility methods for operating on program elements. @@ -361,7 +362,7 @@ JCCompilationUnit toplevel = treeTop.snd; if (toplevel.docComments == null) return null; - return toplevel.docComments.get(tree); + return toplevel.docComments.getCommentText(tree); } public PackageElement getPackageOf(Element e) { diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/parser/EndPosTable.java --- a/src/share/classes/com/sun/tools/javac/parser/EndPosTable.java Thu Jun 21 17:08:31 2012 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -package com.sun.tools.javac.parser; - -import com.sun.tools.javac.tree.JCTree; - -/** - * Specifies the methods to access a mappings of syntax trees to end positions. - *

This is NOT part of any supported API. - * If you write code that depends on this, you do so at your own - * risk. This code and its internal interfaces are subject to change - * or deletion without notice.

- */ -public interface EndPosTable { - - /** - * This method will return the end position of a given tree, otherwise a - * Positions.NOPOS will be returned. - * @param tree JCTree - * @return position of the source tree or Positions.NOPOS for non-existent mapping - */ - public int getEndPos(JCTree tree); - - /** - * Give an old tree and a new tree, the old tree will be replaced with - * the new tree, the position of the new tree will be that of the old - * tree. - * not exist. - * @param oldtree a JCTree to be replaced - * @param newtree a JCTree to be replaced with - * @return position of the old tree or Positions.NOPOS for non-existent mapping - */ - public int replaceTree(JCTree oldtree, JCTree newtree); -} diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java --- a/src/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/parser/JavaTokenizer.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -571,7 +571,7 @@ reader.scanCommentChar(); } while (reader.ch != CR && reader.ch != LF && reader.bp < reader.buflen); if (reader.bp < reader.buflen) { - comments = addDocReader(comments, processComment(pos, reader.bp, CommentStyle.LINE)); + comments = addComment(comments, processComment(pos, reader.bp, CommentStyle.LINE)); } break; } else if (reader.ch == '*') { @@ -597,7 +597,7 @@ } if (reader.ch == '/') { reader.scanChar(); - comments = addDocReader(comments, processComment(pos, reader.bp, style)); + comments = addComment(comments, processComment(pos, reader.bp, style)); break; } else { lexError(pos, "unclosed.comment"); @@ -693,10 +693,10 @@ } } //where - List addDocReader(List docReaders, Comment docReader) { - return docReaders == null ? - List.of(docReader) : - docReaders.prepend(docReader); + List addComment(List comments, Comment comment) { + return comments == null ? + List.of(comment) : + comments.prepend(comment); } /** Return the position where a lexical error occurred; @@ -780,6 +780,10 @@ return null; } + public int getSourcePos(int pos) { + return -1; + } + public CommentStyle getStyle() { return cs; } diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/parser/JavacParser.java --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Jun 25 21:39:16 2012 -0700 @@ -117,7 +117,7 @@ this.allowMethodReferences = source.allowMethodReferences() && fac.options.isSet("allowMethodReferences"); this.keepDocComments = keepDocComments; - docComments = keepDocComments ? new HashMap() : null; + docComments = newDocCommentTable(keepDocComments); this.keepLineMap = keepLineMap; this.errorTree = F.Erroneous(); endPosTable = newEndPosTable(keepEndPositions); @@ -128,6 +128,11 @@ ? new SimpleEndPosTable() : new EmptyEndPosTable(); } + + protected DocCommentTable newDocCommentTable(boolean keepDocComments) { + return keepDocComments ? new SimpleDocCommentTable() : null; + } + /** Switch: Should generics be recognized? */ boolean allowGenerics; @@ -417,21 +422,21 @@ /* ---------- doc comments --------- */ - /** A hashtable to store all documentation comments + /** A table to store all documentation comments * indexed by the tree nodes they refer to. * defined only if option flag keepDocComment is set. */ - private final Map docComments; + private final DocCommentTable docComments; /** Make an entry into docComments hashtable, * provided flag keepDocComments is set and given doc comment is non-null. * @param tree The tree to be used as index in the hashtable * @param dc The doc comment to associate with the tree, or null. */ - void attach(JCTree tree, String dc) { + void attach(JCTree tree, Comment dc) { if (keepDocComments && dc != null) { // System.out.println("doc comment = ");System.out.println(dc);//DEBUG - docComments.put(tree, dc); + docComments.putComment(tree, dc); } } @@ -1858,7 +1863,7 @@ return List.of(parseStatement()); case MONKEYS_AT: case FINAL: { - String dc = token.comment(CommentStyle.JAVADOC); + Comment dc = token.comment(CommentStyle.JAVADOC); JCModifiers mods = modifiersOpt(); if (token.kind == INTERFACE || token.kind == CLASS || @@ -1875,13 +1880,13 @@ } } case ABSTRACT: case STRICTFP: { - String dc = token.comment(CommentStyle.JAVADOC); + Comment dc = token.comment(CommentStyle.JAVADOC); JCModifiers mods = modifiersOpt(); return List.of(classOrInterfaceOrEnumDeclaration(mods, dc)); } case INTERFACE: case CLASS: - String dc = token.comment(CommentStyle.JAVADOC); + Comment dc = token.comment(CommentStyle.JAVADOC); return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc)); case ENUM: case ASSERT: @@ -2418,7 +2423,7 @@ JCExpression type, Name name, boolean reqInit, - String dc, + Comment dc, T vdefs) { vdefs.append(variableDeclaratorRest(pos, mods, type, name, reqInit, dc)); @@ -2434,7 +2439,7 @@ /** VariableDeclarator = Ident VariableDeclaratorRest * ConstantDeclarator = Ident ConstantDeclaratorRest */ - JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, String dc) { + JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, Comment dc) { return variableDeclaratorRest(token.pos, mods, type, ident(), reqInit, dc); } @@ -2445,7 +2450,7 @@ * @param dc The documentation comment for the variable declarations, or null. */ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name, - boolean reqInit, String dc) { + boolean reqInit, Comment dc) { type = bracketsOpt(type); JCExpression init = null; if (token.kind == EQ) { @@ -2539,7 +2544,7 @@ seenImport = true; defs.append(importDeclaration()); } else { - String docComment = token.comment(CommentStyle.JAVADOC); + Comment docComment = token.comment(CommentStyle.JAVADOC); if (firstTypeDecl && !seenImport && !seenPackage) { docComment = firstToken.comment(CommentStyle.JAVADOC); consumedToplevelDoc = true; @@ -2597,7 +2602,7 @@ /** TypeDeclaration = ClassOrInterfaceOrEnumDeclaration * | ";" */ - JCTree typeDeclaration(JCModifiers mods, String docComment) { + JCTree typeDeclaration(JCModifiers mods, Comment docComment) { int pos = token.pos; if (mods == null && token.kind == SEMI) { nextToken(); @@ -2612,7 +2617,7 @@ * @param mods Any modifiers starting the class or interface declaration * @param dc The documentation comment for the class, or null. */ - JCStatement classOrInterfaceOrEnumDeclaration(JCModifiers mods, String dc) { + JCStatement classOrInterfaceOrEnumDeclaration(JCModifiers mods, Comment dc) { if (token.kind == CLASS) { return classDeclaration(mods, dc); } else if (token.kind == INTERFACE) { @@ -2656,7 +2661,7 @@ * @param mods The modifiers starting the class declaration * @param dc The documentation comment for the class, or null. */ - protected JCClassDecl classDeclaration(JCModifiers mods, String dc) { + protected JCClassDecl classDeclaration(JCModifiers mods, Comment dc) { int pos = token.pos; accept(CLASS); Name name = ident(); @@ -2685,7 +2690,7 @@ * @param mods The modifiers starting the interface declaration * @param dc The documentation comment for the interface, or null. */ - protected JCClassDecl interfaceDeclaration(JCModifiers mods, String dc) { + protected JCClassDecl interfaceDeclaration(JCModifiers mods, Comment dc) { int pos = token.pos; accept(INTERFACE); Name name = ident(); @@ -2708,7 +2713,7 @@ * @param mods The modifiers starting the enum declaration * @param dc The documentation comment for the enum, or null. */ - protected JCClassDecl enumDeclaration(JCModifiers mods, String dc) { + protected JCClassDecl enumDeclaration(JCModifiers mods, Comment dc) { int pos = token.pos; accept(ENUM); Name name = ident(); @@ -2767,7 +2772,7 @@ /** EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ] */ JCTree enumeratorDeclaration(Name enumName) { - String dc = token.comment(CommentStyle.JAVADOC); + Comment dc = token.comment(CommentStyle.JAVADOC); int flags = Flags.PUBLIC|Flags.STATIC|Flags.FINAL|Flags.ENUM; if (token.deprecatedFlag()) { flags |= Flags.DEPRECATED; @@ -2856,7 +2861,7 @@ nextToken(); return List.nil(); } else { - String dc = token.comment(CommentStyle.JAVADOC); + Comment dc = token.comment(CommentStyle.JAVADOC); int pos = token.pos; JCModifiers mods = modifiersOpt(); if (token.kind == CLASS || @@ -2936,7 +2941,7 @@ Name name, List typarams, boolean isInterface, boolean isVoid, - String dc) { + Comment dc) { List params = formalParameters(); if (!isVoid) type = bracketsOpt(type); List thrown = List.nil(); diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/parser/JavadocTokenizer.java --- a/src/share/classes/com/sun/tools/javac/parser/JavadocTokenizer.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/parser/JavadocTokenizer.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -62,19 +62,54 @@ @Override protected Comment processComment(int pos, int endPos, CommentStyle style) { char[] buf = reader.getRawCharacters(pos, endPos); - return new JavadocComment(new ColReader(fac, buf, buf.length), style); + return new JavadocComment(new DocReader(fac, buf, buf.length, pos), style); } /** * This is a specialized version of UnicodeReader that keeps track of the - * column position within a given character stream (used for Javadoc processing). + * column position within a given character stream (used for Javadoc processing), + * and which builds a table for mapping positions in the comment string to + * positions in the source file. */ - static class ColReader extends UnicodeReader { + static class DocReader extends UnicodeReader { int col; + int startPos; - ColReader(ScannerFactory fac, char[] input, int inputLength) { + /** + * A buffer for building a table for mapping positions in {@link #sbuf} + * to positions in the source buffer. + * + * The array is organized as a series of pairs of integers: the first + * number in each pair specifies a position in the comment text, + * the second number in each pair specifies the corresponding position + * in the source buffer. The pairs are sorted in ascending order. + * + * Since the mapping function is generally continuous, with successive + * positions in the string corresponding to successive positions in the + * source buffer, the table only needs to record discontinuities in + * the mapping. The values of intermediate positions can be inferred. + * + * Discontinuities may occur in a number of places: when a newline + * is followed by whitespace and asterisks (which are ignored), + * when a tab is expanded into spaces, and when unicode escapes + * are used in the source buffer. + * + * Thus, to find the source position of any position, p, in the comment + * string, find the index, i, of the pair whose string offset + * ({@code pbuf[i] }) is closest to but not greater than p. Then, + * {@code sourcePos(p) = pbuf[i+1] + (p - pbuf[i]) }. + */ + int[] pbuf = new int[128]; + + /** + * The index of the next empty slot in the pbuf buffer. + */ + int pp = 0; + + DocReader(ScannerFactory fac, char[] input, int inputLength, int startPos) { super(fac, input, inputLength); + this.startPos = startPos; } @Override @@ -147,19 +182,43 @@ break; } } + + @Override + public void putChar(char ch, boolean scan) { + // At this point, bp is the position of the current character in buf, + // and sp is the position in sbuf where this character will be put. + // Record a new entry in pbuf if pbuf is empty or if sp and its + // corresponding source position are not equidistant from the + // corresponding values in the latest entry in the pbuf array. + // (i.e. there is a discontinuity in the map function.) + if ((pp == 0) + || (sp - pbuf[pp - 2] != (startPos + bp) - pbuf[pp - 1])) { + if (pp + 1 >= pbuf.length) { + int[] new_pbuf = new int[pbuf.length * 2]; + System.arraycopy(pbuf, 0, new_pbuf, 0, pbuf.length); + pbuf = new_pbuf; + } + pbuf[pp] = sp; + pbuf[pp + 1] = startPos + bp; + pp += 2; + } + super.putChar(ch, scan); + } } - protected class JavadocComment extends JavaTokenizer.BasicComment { + protected class JavadocComment extends JavaTokenizer.BasicComment { /** * Translated and stripped contents of doc comment */ private String docComment = null; + private int[] docPosns = null; - JavadocComment(ColReader comment_reader, CommentStyle cs) { - super(comment_reader, cs); + JavadocComment(DocReader reader, CommentStyle cs) { + super(reader, cs); } + @Override public String getText() { if (!scanned && cs == CommentStyle.JAVADOC) { scanDocComment(); @@ -168,6 +227,33 @@ } @Override + public int getSourcePos(int pos) { + // Binary search to find the entry for which the string index is + // less than pos. Since docPosns is a list of pairs of integers + // we must make sure the index is always even. + // If we find an exact match for pos, the other item in the pair + // gives the source pos; otherwise, compute the source position + // relative to the best match found in the array. + if (pos < 0 || pos >= docComment.length()) + throw new StringIndexOutOfBoundsException(); + if (docPosns == null) + return -1; + int start = 0; + int end = docPosns.length; + while (start < end - 2) { + // find an even index midway between start and end + int index = ((start + end) / 4) * 2; + if (docPosns[index] < pos) + start = index; + else if (docPosns[index] == pos) + return docPosns[index + 1]; + else + end = index; + } + return docPosns[start + 1] + (pos - docPosns[start]); + } + + @Override @SuppressWarnings("fallthrough") protected void scanDocComment() { try { @@ -209,7 +295,8 @@ // whitespace, then it consumes any stars, then it // puts the rest of the line into our buffer. while (comment_reader.bp < comment_reader.buflen) { - + int begin_bp = comment_reader.bp; + char begin_ch = comment_reader.ch; // The wsLoop consumes whitespace from the beginning // of each line. wsLoop: @@ -263,10 +350,10 @@ break outerLoop; } } else if (! firstLine) { - //The current line does not begin with a '*' so we will indent it. - for (int i = 1; i < comment_reader.col; i++) { - comment_reader.putChar(' ', false); - } + // The current line does not begin with a '*' so we will + // treat it as comment + comment_reader.bp = begin_bp; + comment_reader.ch = begin_ch; } // The textLoop processes the rest of the characters // on the line, adding them to our buffer. @@ -334,11 +421,14 @@ // Store the text of the doc comment docComment = comment_reader.chars(); + docPosns = new int[comment_reader.pp]; + System.arraycopy(comment_reader.pbuf, 0, docPosns, 0, docPosns.length); } else { docComment = ""; } } finally { scanned = true; + comment_reader = null; if (docComment != null && docComment.matches("(?sm).*^\\s*@deprecated( |$).*")) { deprecatedFlag = true; diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/parser/SimpleDocCommentTable.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/share/classes/com/sun/tools/javac/parser/SimpleDocCommentTable.java Mon Jun 25 21:39:16 2012 -0700 @@ -0,0 +1,67 @@ +/* + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.sun.tools.javac.parser; + +import java.util.HashMap; +import java.util.Map; + +import com.sun.tools.javac.parser.Tokens.Comment; +import com.sun.tools.javac.tree.DocCommentTable; +import com.sun.tools.javac.tree.JCTree; + + +/** + * + *

This is NOT part of any supported API. + * If you write code that depends on this, you do so at your own risk. + * This code and its internal interfaces are subject to change or + * deletion without notice. + */ +public class SimpleDocCommentTable implements DocCommentTable { + Map table; + + SimpleDocCommentTable() { + table = new HashMap(); + } + + public boolean hasComment(JCTree tree) { + return table.containsKey(tree); + } + + public Comment getComment(JCTree tree) { + return table.get(tree); + } + + public String getCommentText(JCTree tree) { + Comment c = getComment(tree); + return (c == null) ? null : c.getText(); + } + + public void putComment(JCTree tree, Comment c) { + table.put(tree, c); + } + +} diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/parser/Tokens.java --- a/src/share/classes/com/sun/tools/javac/parser/Tokens.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/parser/Tokens.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -294,6 +294,7 @@ } String getText(); + int getSourcePos(int index); CommentStyle getStyle(); boolean isDeprecated(); } @@ -371,11 +372,11 @@ * Preserve classic semantics - if multiple javadocs are found on the token * the last one is returned */ - public String comment(Comment.CommentStyle style) { - List readers = getReaders(Comment.CommentStyle.JAVADOC); - return readers.isEmpty() ? + public Comment comment(Comment.CommentStyle style) { + List comments = getComments(Comment.CommentStyle.JAVADOC); + return comments.isEmpty() ? null : - readers.head.getText(); + comments.head; } /** @@ -383,22 +384,22 @@ * javadoc comment attached to this token contains the '@deprecated' string */ public boolean deprecatedFlag() { - for (Comment r : getReaders(Comment.CommentStyle.JAVADOC)) { - if (r.isDeprecated()) { + for (Comment c : getComments(Comment.CommentStyle.JAVADOC)) { + if (c.isDeprecated()) { return true; } } return false; } - private List getReaders(Comment.CommentStyle style) { + private List getComments(Comment.CommentStyle style) { if (comments == null) { return List.nil(); } else { ListBuffer buf = ListBuffer.lb(); - for (Comment r : comments) { - if (r.getStyle() == style) { - buf.add(r); + for (Comment c : comments) { + if (c.getStyle() == style) { + buf.add(c); } } return buf.toList(); diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/tree/DocCommentTable.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/share/classes/com/sun/tools/javac/tree/DocCommentTable.java Mon Jun 25 21:39:16 2012 -0700 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.sun.tools.javac.tree; + +import com.sun.tools.javac.parser.Tokens.Comment; + +/** + * A table giving the doc comment, if any, for any tree node. + * + *

This is NOT part of any supported API. + * If you write code that depends on this, you do so at your own + * risk. This code and its internal interfaces are subject to change + * or deletion without notice. + */ +public interface DocCommentTable { + /** + * Check if a tree node has a corresponding doc comment. + */ + public boolean hasComment(JCTree tree); + + /** + * Get the Comment token containing the doc comment, if any, for a tree node. + */ + public Comment getComment(JCTree tree); + + /** + * Get the plain text of the doc comment, if any, for a tree node. + */ + public String getCommentText(JCTree tree); + + /** + * Set the Comment to be associated with a tree node. + */ + public void putComment(JCTree tree, Comment c); +} diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/tree/EndPosTable.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/share/classes/com/sun/tools/javac/tree/EndPosTable.java Mon Jun 25 21:39:16 2012 -0700 @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.sun.tools.javac.tree; + +/** + * Specifies the methods to access a mappings of syntax trees to end positions. + *

This is NOT part of any supported API. + * If you write code that depends on this, you do so at your own + * risk. This code and its internal interfaces are subject to change + * or deletion without notice.

+ */ +public interface EndPosTable { + + /** + * This method will return the end position of a given tree, otherwise a + * Positions.NOPOS will be returned. + * @param tree JCTree + * @return position of the source tree or Positions.NOPOS for non-existent mapping + */ + public int getEndPos(JCTree tree); + + /** + * Give an old tree and a new tree, the old tree will be replaced with + * the new tree, the position of the new tree will be that of the old + * tree. + * not exist. + * @param oldtree a JCTree to be replaced + * @param newtree a JCTree to be replaced with + * @return position of the old tree or Positions.NOPOS for non-existent mapping + */ + public int replaceTree(JCTree oldtree, JCTree newtree); +} diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/tree/JCTree.java --- a/src/share/classes/com/sun/tools/javac/tree/JCTree.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/tree/JCTree.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,25 +25,23 @@ package com.sun.tools.javac.tree; +import java.io.IOException; +import java.io.StringWriter; import java.util.*; -import java.io.IOException; -import java.io.StringWriter; import javax.lang.model.element.Modifier; import javax.lang.model.type.TypeKind; import javax.tools.JavaFileObject; -import com.sun.tools.javac.util.*; -import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; -import com.sun.tools.javac.util.List; +import com.sun.source.tree.*; +import com.sun.source.tree.LambdaExpressionTree.BodyKind; +import com.sun.source.tree.MemberReferenceTree.ReferenceMode; import com.sun.tools.javac.code.*; import com.sun.tools.javac.code.Scope.*; import com.sun.tools.javac.code.Symbol.*; -import com.sun.tools.javac.parser.EndPosTable; -import com.sun.source.tree.*; -import com.sun.source.tree.LambdaExpressionTree.BodyKind; -import com.sun.source.tree.MemberReferenceTree.ReferenceMode; - +import com.sun.tools.javac.util.*; +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; +import com.sun.tools.javac.util.List; import static com.sun.tools.javac.code.BoundKind.*; import static com.sun.tools.javac.tree.JCTree.Tag.*; @@ -491,7 +489,7 @@ public ImportScope namedImportScope; public StarImportScope starImportScope; public Position.LineMap lineMap = null; - public Map docComments = null; + public DocCommentTable docComments = null; public EndPosTable endPositions = null; protected JCCompilationUnit(List packageAnnotations, JCExpression pid, diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/tree/Pretty.java --- a/src/share/classes/com/sun/tools/javac/tree/Pretty.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/tree/Pretty.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,14 +29,12 @@ import java.util.*; import com.sun.source.tree.MemberReferenceTree.ReferenceMode; - +import com.sun.tools.javac.code.*; +import com.sun.tools.javac.code.Symbol.*; +import com.sun.tools.javac.parser.Tokens.Comment; +import com.sun.tools.javac.tree.JCTree.*; import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.List; -import com.sun.tools.javac.code.*; - -import com.sun.tools.javac.code.Symbol.*; -import com.sun.tools.javac.tree.JCTree.*; - import static com.sun.tools.javac.code.Flags.*; import static com.sun.tools.javac.code.Flags.ANNOTATION; import static com.sun.tools.javac.tree.JCTree.Tag.*; @@ -78,10 +76,10 @@ */ Name enclClassName; - /** A hashtable mapping trees to their documentation comments + /** A table mapping trees to their documentation comments * (can be null) */ - Map docComments = null; + DocCommentTable docComments = null; /** Align code to be indented to left margin. */ @@ -233,7 +231,7 @@ */ public void printDocComment(JCTree tree) throws IOException { if (docComments != null) { - String dc = docComments.get(tree); + String dc = docComments.getCommentText(tree); if (dc != null) { print("/**"); println(); int pos = 0; @@ -480,7 +478,7 @@ public void visitVarDef(JCVariableDecl tree) { try { - if (docComments != null && docComments.get(tree) != null) { + if (docComments != null && docComments.hasComment(tree)) { println(); align(); } printDocComment(tree); diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/tree/TreeInfo.java --- a/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java Mon Jun 25 21:39:16 2012 -0700 @@ -25,15 +25,14 @@ package com.sun.tools.javac.tree; + import com.sun.source.tree.Tree; +import com.sun.tools.javac.code.*; import com.sun.tools.javac.comp.AttrContext; import com.sun.tools.javac.comp.Env; +import com.sun.tools.javac.tree.JCTree.*; import com.sun.tools.javac.util.*; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; -import com.sun.tools.javac.code.*; -import com.sun.tools.javac.parser.EndPosTable; -import com.sun.tools.javac.tree.JCTree.*; - import static com.sun.tools.javac.code.Flags.*; import static com.sun.tools.javac.tree.JCTree.Tag.*; import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK; @@ -282,6 +281,13 @@ return (lit.typetag == TypeTags.BOT); } + public static String getCommentText(Env env, JCTree tree) { + DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL)) + ? ((JCCompilationUnit) tree).docComments + : env.toplevel.docComments; + return (docComments == null) ? null : docComments.getCommentText(tree); + } + /** The position of the first statement in a block, or the position of * the block itself if it is empty. */ diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/util/DiagnosticSource.java --- a/src/share/classes/com/sun/tools/javac/util/DiagnosticSource.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/util/DiagnosticSource.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -32,7 +32,7 @@ import javax.tools.JavaFileObject; import com.sun.tools.javac.file.JavacFileManager; -import com.sun.tools.javac.parser.EndPosTable; +import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree; import static com.sun.tools.javac.util.LayoutCharacters.*; diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java --- a/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -35,7 +35,7 @@ import com.sun.tools.javac.api.DiagnosticFormatter; import com.sun.tools.javac.code.Lint.LintCategory; -import com.sun.tools.javac.parser.EndPosTable; +import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree; import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javac/util/Log.java --- a/src/share/classes/com/sun/tools/javac/util/Log.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javac/util/Log.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -37,7 +37,7 @@ import com.sun.tools.javac.api.DiagnosticFormatter; import com.sun.tools.javac.main.Main; import com.sun.tools.javac.main.Option; -import com.sun.tools.javac.parser.EndPosTable; +import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javadoc/JavadocEnter.java --- a/src/share/classes/com/sun/tools/javadoc/JavadocEnter.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javadoc/JavadocEnter.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,14 +25,17 @@ package com.sun.tools.javadoc; -import com.sun.tools.javac.util.Context; -import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; -import com.sun.tools.javac.util.List; + +import javax.tools.JavaFileObject; + import com.sun.tools.javac.code.Kinds; import com.sun.tools.javac.code.Symbol.*; import com.sun.tools.javac.comp.Enter; import com.sun.tools.javac.tree.JCTree.*; -import javax.tools.JavaFileObject; +import com.sun.tools.javac.tree.TreeInfo; +import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; +import com.sun.tools.javac.util.List; /** * Javadoc's own enter phase does a few things above and beyond that @@ -77,7 +80,7 @@ public void visitTopLevel(JCCompilationUnit tree) { super.visitTopLevel(tree); if (tree.sourcefile.isNameCompatible("package-info", JavaFileObject.Kind.SOURCE)) { - String comment = tree.docComments.get(tree); + String comment = TreeInfo.getCommentText(env, tree); docenv.makePackageDoc(tree.packge, comment, tree); } } @@ -87,7 +90,7 @@ super.visitClassDef(tree); if (tree.sym == null) return; if (tree.sym.kind == Kinds.TYP || tree.sym.kind == Kinds.ERR) { - String comment = env.toplevel.docComments.get(tree); + String comment = TreeInfo.getCommentText(env, tree); ClassSymbol c = tree.sym; docenv.makeClassDoc(c, comment, tree, env.toplevel.lineMap); } diff -r a39c99192184 -r e111e4587cca src/share/classes/com/sun/tools/javadoc/JavadocMemberEnter.java --- a/src/share/classes/com/sun/tools/javadoc/JavadocMemberEnter.java Thu Jun 21 17:08:31 2012 -0700 +++ b/src/share/classes/com/sun/tools/javadoc/JavadocMemberEnter.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -25,13 +25,14 @@ package com.sun.tools.javadoc; -import com.sun.tools.javac.util.Context; -import com.sun.tools.javac.util.Position; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.Kinds; import com.sun.tools.javac.code.Symbol.*; import com.sun.tools.javac.comp.MemberEnter; import com.sun.tools.javac.tree.JCTree.*; +import com.sun.tools.javac.tree.TreeInfo; +import com.sun.tools.javac.util.Context; +import com.sun.tools.javac.util.Position; /** * Javadoc's own memberEnter phase does a few things above and beyond that @@ -61,11 +62,12 @@ docenv = DocEnv.instance(context); } + @Override public void visitMethodDef(JCMethodDecl tree) { super.visitMethodDef(tree); MethodSymbol meth = tree.sym; if (meth == null || meth.kind != Kinds.MTH) return; - String docComment = env.toplevel.docComments.get(tree); + String docComment = TreeInfo.getCommentText(env, tree); Position.LineMap lineMap = env.toplevel.lineMap; if (meth.isConstructor()) docenv.makeConstructorDoc(meth, docComment, tree, lineMap); @@ -75,12 +77,13 @@ docenv.makeMethodDoc(meth, docComment, tree, lineMap); } + @Override public void visitVarDef(JCVariableDecl tree) { super.visitVarDef(tree); if (tree.sym != null && tree.sym.kind == Kinds.VAR && !isParameter(tree.sym)) { - String docComment = env.toplevel.docComments.get(tree); + String docComment = TreeInfo.getCommentText(env, tree); Position.LineMap lineMap = env.toplevel.lineMap; docenv.makeFieldDoc(tree.sym, docComment, tree, lineMap); } diff -r a39c99192184 -r e111e4587cca test/tools/javac/6304921/TestLog.java --- a/test/tools/javac/6304921/TestLog.java Thu Jun 21 17:08:31 2012 -0700 +++ b/test/tools/javac/6304921/TestLog.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -33,9 +33,9 @@ import javax.tools.JavaFileObject; import javax.tools.SimpleJavaFileObject; import com.sun.tools.javac.file.JavacFileManager; -import com.sun.tools.javac.parser.EndPosTable; import com.sun.tools.javac.parser.Parser; import com.sun.tools.javac.parser.ParserFactory; +import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.TreeScanner; import com.sun.tools.javac.util.Context; diff -r a39c99192184 -r e111e4587cca test/tools/javac/failover/CheckAttributedTree.java --- a/test/tools/javac/failover/CheckAttributedTree.java Thu Jun 21 17:08:31 2012 -0700 +++ b/test/tools/javac/failover/CheckAttributedTree.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -68,7 +68,7 @@ import com.sun.tools.javac.api.JavacTool; import com.sun.tools.javac.code.Symbol; import com.sun.tools.javac.code.Type; -import com.sun.tools.javac.parser.EndPosTable; +import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCImport; diff -r a39c99192184 -r e111e4587cca test/tools/javac/parser/JavacParserTest.java --- a/test/tools/javac/parser/JavacParserTest.java Thu Jun 21 17:08:31 2012 -0700 +++ b/test/tools/javac/parser/JavacParserTest.java Mon Jun 25 21:39:16 2012 -0700 @@ -23,7 +23,7 @@ /* * @test - * @bug 7073631 7159445 + * @bug 7073631 7159445 7156633 * @summary tests error and diagnostics positions * @author Jan Lahoda */ @@ -49,11 +49,17 @@ import com.sun.tools.javac.api.JavacTaskImpl; import com.sun.tools.javac.tree.JCTree; import java.io.IOException; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.Method; import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import java.util.regex.Pattern; import javax.tools.Diagnostic; import javax.tools.DiagnosticCollector; import javax.tools.DiagnosticListener; @@ -63,13 +69,15 @@ import javax.tools.ToolProvider; public class JavacParserTest extends TestCase { - final JavaCompiler tool; - public JavacParserTest(String testName) { - tool = ToolProvider.getSystemJavaCompiler(); - System.out.println("java.home=" + System.getProperty("java.home")); + static final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); + + private JavacParserTest(){} + + public static void main(String... args) throws Exception { + new JavacParserTest().run(args); } - static class MyFileObject extends SimpleJavaFileObject { + class MyFileObject extends SimpleJavaFileObject { private String text; @@ -86,11 +94,11 @@ /* * converts Windows to Unix style LFs for comparing strings */ - private String normalize(String in) { + String normalize(String in) { return in.replace(System.getProperty("line.separator"), "\n"); } - public CompilationUnitTree getCompilationUnitTree(String code) throws IOException { + CompilationUnitTree getCompilationUnitTree(String code) throws IOException { JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(code))); @@ -98,7 +106,7 @@ return cut; } - public List getErroneousTreeValues(ErroneousTree node) { + List getErroneousTreeValues(ErroneousTree node) { List values = new ArrayList<>(); if (node.getErrorTrees() != null) { @@ -112,7 +120,8 @@ return values; } - public void testPositionForSuperConstructorCalls() throws IOException { + @Test + void testPositionForSuperConstructorCalls() throws IOException { assert tool != null; String code = "package test; public class Test {public Test() {super();}}"; @@ -149,12 +158,12 @@ methodStartPos, pos.getStartPosition(cut, mit.getMethodSelect())); assertEquals("testPositionForSuperConstructorCalls", methodEndPos, pos.getEndPosition(cut, mit.getMethodSelect())); - } - public void testPositionForEnumModifiers() throws IOException { - - String code = "package test; public enum Test {A;}"; + @Test + void testPositionForEnumModifiers() throws IOException { + final String theString = "public"; + String code = "package test; " + theString + " enum Test {A;}"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(code))); @@ -163,19 +172,21 @@ ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); ModifiersTree mt = clazz.getModifiers(); - + int spos = code.indexOf(theString); + int epos = spos + theString.length(); assertEquals("testPositionForEnumModifiers", - 38 - 24, pos.getStartPosition(cut, mt)); + spos, pos.getStartPosition(cut, mt)); assertEquals("testPositionForEnumModifiers", - 44 - 24, pos.getEndPosition(cut, mt)); + epos, pos.getEndPosition(cut, mt)); } - public void testNewClassWithEnclosing() throws IOException { + @Test + void testNewClassWithEnclosing() throws IOException { - + final String theString = "Test.this.new d()"; String code = "package test; class Test { " + "class d {} private void method() { " + - "Object o = Test.this.new d(); } }"; + "Object o = " + theString + "; } }"; JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, null, Arrays.asList(new MyFileObject(code))); @@ -186,13 +197,16 @@ ExpressionTree est = ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer(); + final int spos = code.indexOf(theString); + final int epos = spos + theString.length(); assertEquals("testNewClassWithEnclosing", - 97 - 24, pos.getStartPosition(cut, est)); + spos, pos.getStartPosition(cut, est)); assertEquals("testNewClassWithEnclosing", - 114 - 24, pos.getEndPosition(cut, est)); + epos, pos.getEndPosition(cut, est)); } - public void testPreferredPositionForBinaryOp() throws IOException { + @Test + void testPreferredPositionForBinaryOp() throws IOException { String code = "package test; public class Test {" + "private void test() {" @@ -211,7 +225,581 @@ condStartPos, condJC.pos); } - public void testPositionBrokenSource126732a() throws IOException { + @Test + void testErrorRecoveryForEnhancedForLoop142381() throws IOException { + + String code = "package test; class Test { " + + "private void method() { " + + "java.util.Set s = null; for (a : s) {} } }"; + + final List> errors = + new LinkedList>(); + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, + new DiagnosticListener() { + public void report(Diagnostic diagnostic) { + errors.add(diagnostic); + } + }, null, null, Arrays.asList(new MyFileObject(code))); + + CompilationUnitTree cut = ct.parse().iterator().next(); + + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + StatementTree forStatement = + ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1); + + assertEquals("testErrorRecoveryForEnhancedForLoop142381", + Kind.ENHANCED_FOR_LOOP, forStatement.getKind()); + assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty()); + } + + @Test + void testPositionAnnotationNoPackage187551() throws IOException { + + String code = "\n@interface Test {}"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + + CompilationUnitTree cut = ct.parse().iterator().next(); + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + Trees t = Trees.instance(ct); + + assertEquals("testPositionAnnotationNoPackage187551", + 1, t.getSourcePositions().getStartPosition(cut, clazz)); + } + + @Test + void testPositionsSane1() throws IOException { + performPositionsSanityTest("package test; class Test { " + + "private void method() { " + + "java.util.List> l; " + + "} }"); + } + + @Test + void testPositionsSane2() throws IOException { + performPositionsSanityTest("package test; class Test { " + + "private void method() { " + + "java.util.List> l; " + + "} }"); + } + + @Test + void testPositionsSane3() throws IOException { + performPositionsSanityTest("package test; class Test { " + + "private void method() { " + + "java.util.List> l; } }"); + } + + private void performPositionsSanityTest(String code) throws IOException { + + final List> errors = + new LinkedList>(); + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, + new DiagnosticListener() { + + public void report(Diagnostic diagnostic) { + errors.add(diagnostic); + } + }, null, null, Arrays.asList(new MyFileObject(code))); + + final CompilationUnitTree cut = ct.parse().iterator().next(); + final Trees trees = Trees.instance(ct); + + new TreeScanner() { + + private long parentStart = 0; + private long parentEnd = Integer.MAX_VALUE; + + @Override + public Void scan(Tree node, Void p) { + if (node == null) { + return null; + } + + long start = trees.getSourcePositions().getStartPosition(cut, node); + + if (start == (-1)) { + return null; // synthetic tree + } + assertTrue(node.toString() + ":" + start + "/" + parentStart, + parentStart <= start); + + long prevParentStart = parentStart; + + parentStart = start; + + long end = trees.getSourcePositions().getEndPosition(cut, node); + + assertTrue(node.toString() + ":" + end + "/" + parentEnd, + end <= parentEnd); + + long prevParentEnd = parentEnd; + + parentEnd = end; + + super.scan(node, p); + + parentStart = prevParentStart; + parentEnd = prevParentEnd; + + return null; + } + + private void assertTrue(String message, boolean b) { + if (!b) fail(message); + } + }.scan(cut, null); + } + + @Test + void testCorrectWilcardPositions1() throws IOException { + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { List> l; } }", + + Arrays.asList("List> l;", + "List>", + "List", + "? extends List", + "List", + "List", + "? extends String", + "String")); + } + + @Test + void testCorrectWilcardPositions2() throws IOException { + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { List> l; } }", + Arrays.asList("List> l;", + "List>", + "List", + "? super List", + "List", + "List", + "? super String", + "String")); + } + + @Test + void testCorrectWilcardPositions3() throws IOException { + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { List> l; } }", + + Arrays.asList("List> l;", + "List>", + "List", + "? super List", + "List", + "List", + "?")); + } + + @Test + void testCorrectWilcardPositions4() throws IOException { + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { " + + "List>> l; } }", + + Arrays.asList("List>> l;", + "List>>", + "List", + "? extends List>", + "List>", + "List", + "? extends List", + "List", + "List", + "? extends String", + "String")); + } + + @Test + void testCorrectWilcardPositions5() throws IOException { + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { " + + "List>> l; } }", + Arrays.asList("List>> l;", + "List>>", + "List", + "? extends List>", + "List>", + "List", + "? extends List", + "List", + "List", + "? extends String", + "String")); + } + + void performWildcardPositionsTest(final String code, + List golden) throws IOException { + + final List> errors = + new LinkedList>(); + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, + new DiagnosticListener() { + public void report(Diagnostic diagnostic) { + errors.add(diagnostic); + } + }, null, null, Arrays.asList(new MyFileObject(code))); + + final CompilationUnitTree cut = ct.parse().iterator().next(); + final List content = new LinkedList(); + final Trees trees = Trees.instance(ct); + + new TreeScanner() { + @Override + public Void scan(Tree node, Void p) { + if (node == null) { + return null; + } + long start = trees.getSourcePositions().getStartPosition(cut, node); + + if (start == (-1)) { + return null; // synthetic tree + } + long end = trees.getSourcePositions().getEndPosition(cut, node); + String s = code.substring((int) start, (int) end); + content.add(s); + + return super.scan(node, p); + } + }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null); + + assertEquals("performWildcardPositionsTest",golden.toString(), + content.toString()); + } + + @Test + void testStartPositionForMethodWithoutModifiers() throws IOException { + + String code = "package t; class Test { void t() {} }"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + MethodTree mt = (MethodTree) clazz.getMembers().get(0); + Trees t = Trees.instance(ct); + int start = (int) t.getSourcePositions().getStartPosition(cut, mt); + int end = (int) t.getSourcePositions().getEndPosition(cut, mt); + + assertEquals("testStartPositionForMethodWithoutModifiers", + " void t() {}", code.substring(start, end)); + } + + @Test + void testVariableInIfThen1() throws IOException { + + String code = "package t; class Test { " + + "private static void t(String name) { " + + "if (name != null) String nn = name.trim(); } }"; + + DiagnosticCollector coll = + new DiagnosticCollector(); + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, + null, Arrays.asList(new MyFileObject(code))); + + ct.parse(); + + List codes = new LinkedList(); + + for (Diagnostic d : coll.getDiagnostics()) { + codes.add(d.getCode()); + } + + assertEquals("testVariableInIfThen1", + Arrays.asList("compiler.err.variable.not.allowed"), + codes); + } + + @Test + void testVariableInIfThen2() throws IOException { + + String code = "package t; class Test { " + + "private static void t(String name) { " + + "if (name != null) class X {} } }"; + DiagnosticCollector coll = + new DiagnosticCollector(); + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, + null, Arrays.asList(new MyFileObject(code))); + + ct.parse(); + + List codes = new LinkedList(); + + for (Diagnostic d : coll.getDiagnostics()) { + codes.add(d.getCode()); + } + + assertEquals("testVariableInIfThen2", + Arrays.asList("compiler.err.class.not.allowed"), codes); + } + + @Test + void testVariableInIfThen3() throws IOException { + + String code = "package t; class Test { "+ + "private static void t() { " + + "if (true) abstract class F {} }}"; + DiagnosticCollector coll = + new DiagnosticCollector(); + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, + null, Arrays.asList(new MyFileObject(code))); + + ct.parse(); + + List codes = new LinkedList(); + + for (Diagnostic d : coll.getDiagnostics()) { + codes.add(d.getCode()); + } + + assertEquals("testVariableInIfThen3", + Arrays.asList("compiler.err.class.not.allowed"), codes); + } + + @Test + void testVariableInIfThen4() throws IOException { + + String code = "package t; class Test { "+ + "private static void t(String name) { " + + "if (name != null) interface X {} } }"; + DiagnosticCollector coll = + new DiagnosticCollector(); + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, + null, Arrays.asList(new MyFileObject(code))); + + ct.parse(); + + List codes = new LinkedList(); + + for (Diagnostic d : coll.getDiagnostics()) { + codes.add(d.getCode()); + } + + assertEquals("testVariableInIfThen4", + Arrays.asList("compiler.err.class.not.allowed"), codes); + } + + @Test + void testVariableInIfThen5() throws IOException { + + String code = "package t; class Test { "+ + "private static void t() { " + + "if (true) } }"; + DiagnosticCollector coll = + new DiagnosticCollector(); + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, + null, Arrays.asList(new MyFileObject(code))); + + ct.parse(); + + List codes = new LinkedList(); + + for (Diagnostic d : coll.getDiagnostics()) { + codes.add(d.getCode()); + } + + assertEquals("testVariableInIfThen5", + Arrays.asList("compiler.err.illegal.start.of.stmt"), + codes); + } + + // see javac bug #6882235, NB bug #98234: + @Test + void testMissingExponent() throws IOException { + + String code = "\nclass Test { { System.err.println(0e); } }"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + + assertNotNull(ct.parse().iterator().next()); + } + + @Test + void testTryResourcePos() throws IOException { + + final String code = "package t; class Test { " + + "{ try (java.io.InputStream in = null) { } } }"; + + CompilationUnitTree cut = getCompilationUnitTree(code); + + new TreeScanner() { + @Override + public Void visitVariable(VariableTree node, Void p) { + if ("in".contentEquals(node.getName())) { + JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; + assertEquals("testTryResourcePos", "in = null) { } } }", + code.substring(var.pos)); + } + return super.visitVariable(node, p); + } + }.scan(cut, null); + } + + @Test + void testVarPos() throws IOException { + + final String code = "package t; class Test { " + + "{ java.io.InputStream in = null; } }"; + + CompilationUnitTree cut = getCompilationUnitTree(code); + + new TreeScanner() { + + @Override + public Void visitVariable(VariableTree node, Void p) { + if ("in".contentEquals(node.getName())) { + JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; + assertEquals("testVarPos","in = null; } }", + code.substring(var.pos)); + } + return super.visitVariable(node, p); + } + }.scan(cut, null); + } + + // expected erroneous tree: int x = y;(ERROR); + @Test + void testOperatorMissingError() throws IOException { + + String code = "package test; public class ErrorTest { " + + "void method() { int x = y z } }"; + CompilationUnitTree cut = getCompilationUnitTree(code); + final List values = new ArrayList<>(); + final List expectedValues = + new ArrayList<>(Arrays.asList("[z]")); + + new TreeScanner() { + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + values.add(getErroneousTreeValues(node).toString()); + return null; + + } + }.scan(cut, null); + + assertEquals("testSwitchError: The Erroneous tree " + + "error values: " + values + + " do not match expected error values: " + + expectedValues, values, expectedValues); + } + + // expected erroneous tree: String s = (ERROR); + @Test + void testMissingParenthesisError() throws IOException { + + String code = "package test; public class ErrorTest { " + + "void f() {String s = new String; } }"; + CompilationUnitTree cut = getCompilationUnitTree(code); + final List values = new ArrayList<>(); + final List expectedValues = + new ArrayList<>(Arrays.asList("[new String()]")); + + new TreeScanner() { + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + values.add(getErroneousTreeValues(node).toString()); + return null; + } + }.scan(cut, null); + + assertEquals("testSwitchError: The Erroneous tree " + + "error values: " + values + + " do not match expected error values: " + + expectedValues, values, expectedValues); + } + + // expected erroneous tree: package test; (ERROR)(ERROR) + @Test + void testMissingClassError() throws IOException { + + String code = "package Test; clas ErrorTest { " + + "void f() {String s = new String(); } }"; + CompilationUnitTree cut = getCompilationUnitTree(code); + final List values = new ArrayList<>(); + final List expectedValues = + new ArrayList<>(Arrays.asList("[, clas]", "[]")); + + new TreeScanner() { + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + values.add(getErroneousTreeValues(node).toString()); + return null; + } + }.scan(cut, null); + + assertEquals("testSwitchError: The Erroneous tree " + + "error values: " + values + + " do not match expected error values: " + + expectedValues, values, expectedValues); + } + + // expected erroneous tree: void m1(int i) {(ERROR);{(ERROR);} + @Test + void testSwitchError() throws IOException { + + String code = "package test; public class ErrorTest { " + + "int numDays; void m1(int i) { switchh {i} { case 1: " + + "numDays = 31; break; } } }"; + CompilationUnitTree cut = getCompilationUnitTree(code); + final List values = new ArrayList<>(); + final List expectedValues = + new ArrayList<>(Arrays.asList("[switchh]", "[i]")); + + new TreeScanner() { + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + values.add(getErroneousTreeValues(node).toString()); + return null; + } + }.scan(cut, null); + + assertEquals("testSwitchError: The Erroneous tree " + + "error values: " + values + + " do not match expected error values: " + + expectedValues, values, expectedValues); + } + + // expected erroneous tree: class ErrorTest {(ERROR) + @Test + void testMethodError() throws IOException { + + String code = "package Test; class ErrorTest { " + + "static final void f) {String s = new String(); } }"; + CompilationUnitTree cut = cut = getCompilationUnitTree(code); + + final List values = new ArrayList<>(); + final List expectedValues = + new ArrayList<>(Arrays.asList("[\nstatic final void f();]")); + + new TreeScanner() { + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + values.add(normalize(getErroneousTreeValues(node).toString())); + return null; + } + }.scan(cut, null); + + assertEquals("testMethodError: The Erroneous tree " + + "error value: " + values + + " does not match expected error values: " + + expectedValues, values, expectedValues); + } + + /* + * The following tests do not work just yet with nb-javac nor javac, + * they need further investigation, see CR: 7167356 + */ + + void testPositionBrokenSource126732a() throws IOException { String[] commands = new String[]{ "return Runnable()", "do { } while (true)", @@ -250,7 +838,7 @@ } } - public void testPositionBrokenSource126732b() throws IOException { + void testPositionBrokenSource126732b() throws IOException { String[] commands = new String[]{ "break", "break A", @@ -291,246 +879,7 @@ } } - public void testErrorRecoveryForEnhancedForLoop142381() throws IOException { - - String code = "package test; class Test { " + - "private void method() { " + - "java.util.Set s = null; for (a : s) {} } }"; - - final List> errors = - new LinkedList>(); - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, - new DiagnosticListener() { - public void report(Diagnostic diagnostic) { - errors.add(diagnostic); - } - }, null, null, Arrays.asList(new MyFileObject(code))); - - CompilationUnitTree cut = ct.parse().iterator().next(); - - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - StatementTree forStatement = - ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1); - - assertEquals("testErrorRecoveryForEnhancedForLoop142381", - Kind.ENHANCED_FOR_LOOP, forStatement.getKind()); - assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty()); - } - - public void testPositionAnnotationNoPackage187551() throws IOException { - - String code = "\n@interface Test {}"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - - CompilationUnitTree cut = ct.parse().iterator().next(); - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - Trees t = Trees.instance(ct); - - assertEquals("testPositionAnnotationNoPackage187551", - 1, t.getSourcePositions().getStartPosition(cut, clazz)); - } - - public void testPositionsSane() throws IOException { - performPositionsSanityTest("package test; class Test { " + - "private void method() { " + - "java.util.List> l; " + - "} }"); - performPositionsSanityTest("package test; class Test { " + - "private void method() { " + - "java.util.List> l; " + - "} }"); - performPositionsSanityTest("package test; class Test { " + - "private void method() { " + - "java.util.List> l; } }"); - } - - private void performPositionsSanityTest(String code) throws IOException { - - final List> errors = - new LinkedList>(); - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, - new DiagnosticListener() { - - public void report(Diagnostic diagnostic) { - errors.add(diagnostic); - } - }, null, null, Arrays.asList(new MyFileObject(code))); - - final CompilationUnitTree cut = ct.parse().iterator().next(); - final Trees trees = Trees.instance(ct); - - new TreeScanner() { - - private long parentStart = 0; - private long parentEnd = Integer.MAX_VALUE; - - @Override - public Void scan(Tree node, Void p) { - if (node == null) { - return null; - } - - long start = trees.getSourcePositions().getStartPosition(cut, node); - - if (start == (-1)) { - return null; //synthetic tree - } - assertTrue(node.toString() + ":" + start + "/" + parentStart, - parentStart <= start); - - long prevParentStart = parentStart; - - parentStart = start; - - long end = trees.getSourcePositions().getEndPosition(cut, node); - - assertTrue(node.toString() + ":" + end + "/" + parentEnd, - end <= parentEnd); - - long prevParentEnd = parentEnd; - - parentEnd = end; - - super.scan(node, p); - - parentStart = prevParentStart; - parentEnd = prevParentEnd; - - return null; - } - - private void assertTrue(String message, boolean b) { - if (!b) fail(message); - } - }.scan(cut, null); - } - - public void testCorrectWilcardPositions() throws IOException { - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { List> l; } }", - - Arrays.asList("List> l;", - "List>", - "List", - "? extends List", - "List", - "List", - "? extends String", - "String")); - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { List> l; } }", - - Arrays.asList("List> l;", - "List>", - "List", - "? super List", - "List", - "List", - "? super String", - "String")); - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { List> l; } }", - - Arrays.asList("List> l;", - "List>", - "List", - "? super List", - "List", - "List", - "?")); - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { " + - "List>> l; } }", - - Arrays.asList("List>> l;", - "List>>", - "List", - "? extends List>", - "List>", - "List", - "? extends List", - "List", - "List", - "? extends String", - "String")); - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { " + - "List>> l; } }", - Arrays.asList("List>> l;", - "List>>", - "List", - "? extends List>", - "List>", - "List", - "? extends List", - "List", - "List", - "? extends String", - "String")); - } - - public void performWildcardPositionsTest(final String code, - List golden) throws IOException { - - final List> errors = - new LinkedList>(); - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, - new DiagnosticListener() { - public void report(Diagnostic diagnostic) { - errors.add(diagnostic); - } - }, null, null, Arrays.asList(new MyFileObject(code))); - - final CompilationUnitTree cut = ct.parse().iterator().next(); - final List content = new LinkedList(); - final Trees trees = Trees.instance(ct); - - new TreeScanner() { - @Override - public Void scan(Tree node, Void p) { - if (node == null) { - return null; - } - long start = trees.getSourcePositions().getStartPosition(cut, node); - - if (start == (-1)) { - return null; //synthetic tree - } - long end = trees.getSourcePositions().getEndPosition(cut, node); - String s = code.substring((int) start, (int) end); - content.add(s); - - return super.scan(node, p); - } - }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null); - - assertEquals("performWildcardPositionsTest",golden.toString(), - content.toString()); - } - - public void testStartPositionForMethodWithoutModifiers() throws IOException { - - String code = "package t; class Test { void t() {} }"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - MethodTree mt = (MethodTree) clazz.getMembers().get(0); - Trees t = Trees.instance(ct); - int start = (int) t.getSourcePositions().getStartPosition(cut, mt); - int end = (int) t.getSourcePositions().getEndPosition(cut, mt); - - assertEquals("testStartPositionForMethodWithoutModifiers", - " void t() {}", code.substring(start, end)); - } - - public void testStartPositionEnumConstantInit() throws IOException { + void testStartPositionEnumConstantInit() throws IOException { String code = "package t; enum Test { AAA; }"; @@ -546,342 +895,34 @@ assertEquals("testStartPositionEnumConstantInit", -1, start); } - public void testVariableInIfThen1() throws IOException { - - String code = "package t; class Test { " + - "private static void t(String name) { " + - "if (name != null) String nn = name.trim(); } }"; - - DiagnosticCollector coll = - new DiagnosticCollector(); - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, - null, Arrays.asList(new MyFileObject(code))); - - ct.parse(); - - List codes = new LinkedList(); - - for (Diagnostic d : coll.getDiagnostics()) { - codes.add(d.getCode()); - } - - assertEquals("testVariableInIfThen1", - Arrays.asList("compiler.err.variable.not.allowed"), - codes); - } - - public void testVariableInIfThen2() throws IOException { - - String code = "package t; class Test { " + - "private static void t(String name) { " + - "if (name != null) class X {} } }"; - DiagnosticCollector coll = - new DiagnosticCollector(); - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, - null, Arrays.asList(new MyFileObject(code))); - - ct.parse(); - - List codes = new LinkedList(); - - for (Diagnostic d : coll.getDiagnostics()) { - codes.add(d.getCode()); - } - - assertEquals("testVariableInIfThen2", - Arrays.asList("compiler.err.class.not.allowed"), codes); - } - - public void testVariableInIfThen3() throws IOException { - - String code = "package t; class Test { "+ - "private static void t() { " + - "if (true) abstract class F {} }}"; - DiagnosticCollector coll = - new DiagnosticCollector(); - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, - null, Arrays.asList(new MyFileObject(code))); - - ct.parse(); - - List codes = new LinkedList(); - - for (Diagnostic d : coll.getDiagnostics()) { - codes.add(d.getCode()); - } - - assertEquals("testVariableInIfThen3", - Arrays.asList("compiler.err.class.not.allowed"), codes); - } - - public void testVariableInIfThen4() throws IOException { - - String code = "package t; class Test { "+ - "private static void t(String name) { " + - "if (name != null) interface X {} } }"; - DiagnosticCollector coll = - new DiagnosticCollector(); - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, - null, Arrays.asList(new MyFileObject(code))); - - ct.parse(); - - List codes = new LinkedList(); - - for (Diagnostic d : coll.getDiagnostics()) { - codes.add(d.getCode()); - } - - assertEquals("testVariableInIfThen4", - Arrays.asList("compiler.err.class.not.allowed"), codes); - } - - public void testVariableInIfThen5() throws IOException { - - String code = "package t; class Test { "+ - "private static void t() { " + - "if (true) } }"; - DiagnosticCollector coll = - new DiagnosticCollector(); - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, - null, Arrays.asList(new MyFileObject(code))); - - ct.parse(); - - List codes = new LinkedList(); - - for (Diagnostic d : coll.getDiagnostics()) { - codes.add(d.getCode()); - } - - assertEquals("testVariableInIfThen5", - Arrays.asList("compiler.err.illegal.start.of.stmt"), - codes); - } - - //see javac bug #6882235, NB bug #98234: - public void testMissingExponent() throws IOException { - - String code = "\nclass Test { { System.err.println(0e); } }"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - - assertNotNull(ct.parse().iterator().next()); - } - - public void testTryResourcePos() throws IOException { - - final String code = "package t; class Test { " + - "{ try (java.io.InputStream in = null) { } } }"; - - CompilationUnitTree cut = getCompilationUnitTree(code); - - new TreeScanner() { - @Override - public Void visitVariable(VariableTree node, Void p) { - if ("in".contentEquals(node.getName())) { - JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; - System.out.println(node.getName() + "," + var.pos); - assertEquals("testTryResourcePos", "in = null) { } } }", - code.substring(var.pos)); + void run(String[] args) throws Exception { + int passed = 0, failed = 0; + final Pattern p = (args != null && args.length > 0) + ? Pattern.compile(args[0]) + : null; + for (Method m : this.getClass().getDeclaredMethods()) { + boolean selected = (p == null) + ? m.isAnnotationPresent(Test.class) + : p.matcher(m.getName()).matches(); + if (selected) { + try { + m.invoke(this, (Object[]) null); + System.out.println(m.getName() + ": OK"); + passed++; + } catch (Throwable ex) { + System.out.printf("Test %s failed: %s %n", m, ex.getCause()); + failed++; } - return super.visitVariable(node, p); - } - }.scan(cut, null); - } - - public void testVarPos() throws IOException { - - final String code = "package t; class Test { " + - "{ java.io.InputStream in = null; } }"; - - CompilationUnitTree cut = getCompilationUnitTree(code); - - new TreeScanner() { - - @Override - public Void visitVariable(VariableTree node, Void p) { - if ("in".contentEquals(node.getName())) { - JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; - assertEquals("testVarPos","in = null; } }", - code.substring(var.pos)); - } - return super.visitVariable(node, p); } - }.scan(cut, null); - } - - // expected erroneous tree: int x = y;(ERROR); - public void testOperatorMissingError() throws IOException { - - String code = "package test; public class ErrorTest { " - + "void method() { int x = y z } }"; - CompilationUnitTree cut = getCompilationUnitTree(code); - final List values = new ArrayList<>(); - final List expectedValues = - new ArrayList<>(Arrays.asList("[z]")); - - new TreeScanner() { - - @Override - public Void visitErroneous(ErroneousTree node, Void p) { - - values.add(getErroneousTreeValues(node).toString()); - return null; - - } - }.scan(cut, null); - - assertEquals("testSwitchError: The Erroneous tree " - + "error values: " + values - + " do not match expected error values: " - + expectedValues, values, expectedValues); - } - - //expected erroneous tree: String s = (ERROR); - public void testMissingParenthesisError() throws IOException { - - String code = "package test; public class ErrorTest { " - + "void f() {String s = new String; } }"; - CompilationUnitTree cut = getCompilationUnitTree(code); - final List values = new ArrayList<>(); - final List expectedValues = - new ArrayList<>(Arrays.asList("[new String()]")); - - new TreeScanner() { - - @Override - public Void visitErroneous(ErroneousTree node, Void p) { - - values.add(getErroneousTreeValues(node).toString()); - return null; - } - }.scan(cut, null); - - assertEquals("testSwitchError: The Erroneous tree " - + "error values: " + values - + " do not match expected error values: " - + expectedValues, values, expectedValues); - } - - //expected erroneous tree: package test; (ERROR)(ERROR) - public void testMissingClassError() throws IOException { - - String code = "package Test; clas ErrorTest { " - + "void f() {String s = new String(); } }"; - CompilationUnitTree cut = getCompilationUnitTree(code); - final List values = new ArrayList<>(); - final List expectedValues = - new ArrayList<>(Arrays.asList("[, clas]", "[]")); - - new TreeScanner() { - - @Override - public Void visitErroneous(ErroneousTree node, Void p) { - - values.add(getErroneousTreeValues(node).toString()); - return null; - } - }.scan(cut, null); - - assertEquals("testSwitchError: The Erroneous tree " - + "error values: " + values - + " do not match expected error values: " - + expectedValues, values, expectedValues); - } - - //expected erroneous tree: void m1(int i) {(ERROR);{(ERROR);} - public void testSwitchError() throws IOException { - - String code = "package test; public class ErrorTest { " - + "int numDays; void m1(int i) { switchh {i} { case 1: " - + "numDays = 31; break; } } }"; - CompilationUnitTree cut = getCompilationUnitTree(code); - final List values = new ArrayList<>(); - final List expectedValues = - new ArrayList<>(Arrays.asList("[switchh]", "[i]")); - - new TreeScanner() { - - @Override - public Void visitErroneous(ErroneousTree node, Void p) { - - values.add(getErroneousTreeValues(node).toString()); - return null; - } - }.scan(cut, null); - - assertEquals("testSwitchError: The Erroneous tree " - + "error values: " + values - + " do not match expected error values: " - + expectedValues, values, expectedValues); - } - - //expected erroneous tree: class ErrorTest {(ERROR) - public void testMethodError() throws IOException { - - String code = "package Test; class ErrorTest { " - + "static final void f) {String s = new String(); } }"; - CompilationUnitTree cut = getCompilationUnitTree(code); - final List values = new ArrayList<>(); - final List expectedValues = - new ArrayList<>(Arrays.asList("[\nstatic final void f();]")); - - new TreeScanner() { - - @Override - public Void visitErroneous(ErroneousTree node, Void p) { - - values.add(normalize(getErroneousTreeValues(node).toString())); - return null; - } - }.scan(cut, null); - - assertEquals("testMethodError: The Erroneous tree " - + "error value: " + values - + " does not match expected error values: " - + expectedValues, values, expectedValues); - } - - void testsNotWorking() throws IOException { - - // Fails with nb-javac, needs further investigation - testPositionBrokenSource126732a(); - testPositionBrokenSource126732b(); - - // Fails, these tests yet to be addressed - testPositionForEnumModifiers(); - testStartPositionEnumConstantInit(); - } - void testPositions() throws IOException { - testPositionsSane(); - testCorrectWilcardPositions(); - testPositionAnnotationNoPackage187551(); - testPositionForSuperConstructorCalls(); - testPreferredPositionForBinaryOp(); - testStartPositionForMethodWithoutModifiers(); - testVarPos(); - testVariableInIfThen1(); - testVariableInIfThen2(); - testVariableInIfThen3(); - testVariableInIfThen4(); - testVariableInIfThen5(); - testMissingExponent(); - testTryResourcePos(); - testOperatorMissingError(); - testMissingParenthesisError(); - testMissingClassError(); - testSwitchError(); - testMethodError(); - testErrorRecoveryForEnhancedForLoop142381(); - } - - public static void main(String... args) throws IOException { - JavacParserTest jpt = new JavacParserTest("JavacParserTest"); - jpt.testPositions(); - System.out.println("PASS"); + } + System.out.printf("Passed: %d, Failed %d%n", passed, failed); + if (failed > 0) { + throw new RuntimeException("Tests failed: " + failed); + } + if (passed == 0 && failed == 0) { + throw new AssertionError("No test(s) selected: passed = " + + passed + ", failed = " + failed + " ??????????"); + } } } @@ -906,8 +947,6 @@ } void assertEquals(String message, Object o1, Object o2) { - System.out.println(o1); - System.out.println(o2); if (o1 != null && o2 != null && !o1.equals(o2)) { fail(message); } @@ -929,4 +968,11 @@ void fail(String message) { throw new RuntimeException(message); } + + /** + * Indicates that the annotated method is a test method. + */ + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.METHOD) + public @interface Test {} } diff -r a39c99192184 -r e111e4587cca test/tools/javac/tree/DocCommentToplevelTest.java --- a/test/tools/javac/tree/DocCommentToplevelTest.java Thu Jun 21 17:08:31 2012 -0700 +++ b/test/tools/javac/tree/DocCommentToplevelTest.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -29,6 +29,7 @@ import com.sun.source.tree.*; import com.sun.source.util.*; +import com.sun.tools.javac.tree.DocCommentTable; import com.sun.tools.javac.tree.JCTree; import java.net.URI; @@ -137,16 +138,16 @@ new TreeScanner() { - Map docComments; + DocCommentTable docComments; @Override public ClassTree visitCompilationUnit(CompilationUnitTree node, Void unused) { docComments = ((JCTree.JCCompilationUnit)node).docComments; boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && (pk != PackageKind.NO_PKG || ik != ImportKind.ZERO); - boolean foundComment = docComments.get(node) != null; + boolean foundComment = docComments.hasComment((JCTree) node); if (expectedComment != foundComment) { - error("Unexpected comment " + docComments.get(node) + " on toplevel"); + error("Unexpected comment " + docComments.getComment((JCTree) node) + " on toplevel"); } return super.visitCompilationUnit(node, null); } @@ -156,9 +157,9 @@ boolean expectedComment = tdk == ToplevelDocKind.HAS_DOC && pk == PackageKind.NO_PKG && ik == ImportKind.ZERO && node.getSimpleName().toString().equals("First"); - boolean foundComment = docComments.get(node) != null; + boolean foundComment = docComments.hasComment((JCTree) node); if (expectedComment != foundComment) { - error("Unexpected comment " + docComments.get(node) + " on class " + node.getSimpleName()); + error("Unexpected comment " + docComments.getComment((JCTree) node) + " on class " + node.getSimpleName()); } return super.visitClass(node, unused); } diff -r a39c99192184 -r e111e4587cca test/tools/javac/tree/TreePosTest.java --- a/test/tools/javac/tree/TreePosTest.java Thu Jun 21 17:08:31 2012 -0700 +++ b/test/tools/javac/tree/TreePosTest.java Mon Jun 25 21:39:16 2012 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -73,7 +73,7 @@ import com.sun.source.util.JavacTask; import com.sun.tools.javac.api.JavacTool; import com.sun.tools.javac.code.Flags; -import com.sun.tools.javac.parser.EndPosTable; +import com.sun.tools.javac.tree.EndPosTable; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCNewClass;