Mon, 27 Sep 2010 14:20:39 -0700
6877202: Elements.getDocComment() is not getting JavaDocComments
6861094: javac -Xprint <file> does not print comments
6985205: access to tree positions and doc comments may be lost across annotation processing rounds
Reviewed-by: darcy
1.1 --- a/src/share/classes/com/sun/tools/apt/main/JavaCompiler.java Mon Sep 27 14:05:33 2010 -0700 1.2 +++ b/src/share/classes/com/sun/tools/apt/main/JavaCompiler.java Mon Sep 27 14:20:39 2010 -0700 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2004, 2009, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -99,9 +99,6 @@ 1.11 private static Context preRegister(Context context) { 1.12 Bark.preRegister(context); 1.13 1.14 - // force the use of the scanner that captures Javadoc comments 1.15 - DocCommentScanner.Factory.preRegister(context); 1.16 - 1.17 if (context.get(JavaFileManager.class) == null) 1.18 JavacFileManager.preRegister(context); 1.19
2.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Mon Sep 27 14:05:33 2010 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Mon Sep 27 14:20:39 2010 -0700 2.3 @@ -1,5 +1,5 @@ 2.4 /* 2.5 - * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 2.6 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 2.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.8 * 2.9 * This code is free software; you can redistribute it and/or modify it 2.10 @@ -96,9 +96,6 @@ 2.11 args.getClass(); 2.12 context.getClass(); 2.13 fileObjects.getClass(); 2.14 - 2.15 - // force the use of the scanner that captures Javadoc comments 2.16 - com.sun.tools.javac.parser.DocCommentScanner.Factory.preRegister(context); 2.17 } 2.18 2.19 JavacTaskImpl(JavacTool tool, 2.20 @@ -337,9 +334,13 @@ 2.21 2.22 ListBuffer<TypeElement> elements = new ListBuffer<TypeElement>(); 2.23 for (JCCompilationUnit unit : units) { 2.24 - for (JCTree node : unit.defs) 2.25 - if (node.getTag() == JCTree.CLASSDEF) 2.26 - elements.append(((JCTree.JCClassDecl) node).sym); 2.27 + for (JCTree node : unit.defs) { 2.28 + if (node.getTag() == JCTree.CLASSDEF) { 2.29 + JCClassDecl cdef = (JCClassDecl) node; 2.30 + if (cdef.sym != null) // maybe null if errors in anno processing 2.31 + elements.append(cdef.sym); 2.32 + } 2.33 + } 2.34 } 2.35 return elements.toList(); 2.36 }
3.1 --- a/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java Mon Sep 27 14:05:33 2010 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java Mon Sep 27 14:20:39 2010 -0700 3.3 @@ -1,5 +1,5 @@ 3.4 /* 3.5 - * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. 3.6 + * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 3.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.8 * 3.9 * This code is free software; you can redistribute it and/or modify it 3.10 @@ -62,9 +62,6 @@ 3.11 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*; 3.12 import static com.sun.tools.javac.util.ListBuffer.lb; 3.13 3.14 -// TEMP, until we have a more efficient way to save doc comment info 3.15 -import com.sun.tools.javac.parser.DocCommentScanner; 3.16 - 3.17 import java.util.HashMap; 3.18 import java.util.Queue; 3.19 import javax.lang.model.SourceVersion; 3.20 @@ -964,11 +961,10 @@ 3.21 processAnnotations = procEnvImpl.atLeastOneProcessor(); 3.22 3.23 if (processAnnotations) { 3.24 - if (context.get(Scanner.Factory.scannerFactoryKey) == null) 3.25 - DocCommentScanner.Factory.preRegister(context); 3.26 options.put("save-parameter-names", "save-parameter-names"); 3.27 reader.saveParameterNames = true; 3.28 keepComments = true; 3.29 + genEndPos = true; 3.30 if (taskListener != null) 3.31 taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING)); 3.32 log.deferDiagnostics = true; 3.33 @@ -1587,6 +1583,7 @@ 3.34 } 3.35 3.36 public void initRound(JavaCompiler prev) { 3.37 + genEndPos = prev.genEndPos; 3.38 keepComments = prev.keepComments; 3.39 start_msec = prev.start_msec; 3.40 hasBeenUsed = true;
4.1 --- a/src/share/classes/com/sun/tools/javac/parser/DocCommentScanner.java Mon Sep 27 14:05:33 2010 -0700 4.2 +++ b/src/share/classes/com/sun/tools/javac/parser/DocCommentScanner.java Mon Sep 27 14:20:39 2010 -0700 4.3 @@ -1,5 +1,5 @@ 4.4 /* 4.5 - * Copyright (c) 2004, 2008, Oracle and/or its affiliates. All rights reserved. 4.6 + * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 4.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.8 * 4.9 * This code is free software; you can redistribute it and/or modify it 4.10 @@ -42,50 +42,17 @@ 4.11 */ 4.12 public class DocCommentScanner extends Scanner { 4.13 4.14 - /** A factory for creating scanners. */ 4.15 - public static class Factory extends Scanner.Factory { 4.16 - 4.17 - public static void preRegister(final Context context) { 4.18 - context.put(scannerFactoryKey, new Context.Factory<Scanner.Factory>() { 4.19 - public Factory make() { 4.20 - return new Factory(context); 4.21 - } 4.22 - }); 4.23 - } 4.24 - 4.25 - /** Create a new scanner factory. */ 4.26 - protected Factory(Context context) { 4.27 - super(context); 4.28 - } 4.29 - 4.30 - @Override 4.31 - public Scanner newScanner(CharSequence input) { 4.32 - if (input instanceof CharBuffer) { 4.33 - return new DocCommentScanner(this, (CharBuffer)input); 4.34 - } else { 4.35 - char[] array = input.toString().toCharArray(); 4.36 - return newScanner(array, array.length); 4.37 - } 4.38 - } 4.39 - 4.40 - @Override 4.41 - public Scanner newScanner(char[] input, int inputLength) { 4.42 - return new DocCommentScanner(this, input, inputLength); 4.43 - } 4.44 - } 4.45 - 4.46 - 4.47 /** Create a scanner from the input buffer. buffer must implement 4.48 * array() and compact(), and remaining() must be less than limit(). 4.49 */ 4.50 - protected DocCommentScanner(Factory fac, CharBuffer buffer) { 4.51 + protected DocCommentScanner(ScannerFactory fac, CharBuffer buffer) { 4.52 super(fac, buffer); 4.53 } 4.54 4.55 /** Create a scanner from the input array. The array must have at 4.56 * least a single character of extra space. 4.57 */ 4.58 - protected DocCommentScanner(Factory fac, char[] input, int inputLength) { 4.59 + protected DocCommentScanner(ScannerFactory fac, char[] input, int inputLength) { 4.60 super(fac, input, inputLength); 4.61 } 4.62
5.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Sep 27 14:05:33 2010 -0700 5.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon Sep 27 14:20:39 2010 -0700 5.3 @@ -1,5 +1,5 @@ 5.4 /* 5.5 - * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. 5.6 + * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 5.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.8 * 5.9 * This code is free software; you can redistribute it and/or modify it 5.10 @@ -2473,6 +2473,11 @@ 5.11 defs.append(importDeclaration()); 5.12 } else { 5.13 JCTree def = typeDeclaration(mods); 5.14 + if (keepDocComments && dc != null && docComments.get(def) == dc) { 5.15 + // If the first type declaration has consumed the first doc 5.16 + // comment, then don't use it for the top level comment as well. 5.17 + dc = null; 5.18 + } 5.19 if (def instanceof JCExpressionStatement) 5.20 def = ((JCExpressionStatement)def).expr; 5.21 defs.append(def);
6.1 --- a/src/share/classes/com/sun/tools/javac/parser/ParserFactory.java Mon Sep 27 14:05:33 2010 -0700 6.2 +++ b/src/share/classes/com/sun/tools/javac/parser/ParserFactory.java Mon Sep 27 14:20:39 2010 -0700 6.3 @@ -1,5 +1,5 @@ 6.4 /* 6.5 - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. 6.6 + * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 6.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.8 * 6.9 * This code is free software; you can redistribute it and/or modify it 6.10 @@ -59,7 +59,7 @@ 6.11 final Source source; 6.12 final Names names; 6.13 final Options options; 6.14 - final Scanner.Factory scannerFactory; 6.15 + final ScannerFactory scannerFactory; 6.16 6.17 protected ParserFactory(Context context) { 6.18 super(); 6.19 @@ -70,11 +70,11 @@ 6.20 this.keywords = Keywords.instance(context); 6.21 this.source = Source.instance(context); 6.22 this.options = Options.instance(context); 6.23 - this.scannerFactory = Scanner.Factory.instance(context); 6.24 + this.scannerFactory = ScannerFactory.instance(context); 6.25 } 6.26 6.27 public Parser newParser(CharSequence input, boolean keepDocComments, boolean keepEndPos, boolean keepLineMap) { 6.28 - Lexer lexer = scannerFactory.newScanner(input); 6.29 + Lexer lexer = scannerFactory.newScanner(input, keepDocComments); 6.30 if (keepEndPos) { 6.31 return new EndPosParser(this, lexer, keepDocComments, keepLineMap); 6.32 } else {
7.1 --- a/src/share/classes/com/sun/tools/javac/parser/Scanner.java Mon Sep 27 14:05:33 2010 -0700 7.2 +++ b/src/share/classes/com/sun/tools/javac/parser/Scanner.java Mon Sep 27 14:20:39 2010 -0700 7.3 @@ -1,5 +1,5 @@ 7.4 /* 7.5 - * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. 7.6 + * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 7.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 7.8 * 7.9 * This code is free software; you can redistribute it and/or modify it 7.10 @@ -47,48 +47,6 @@ 7.11 7.12 private static boolean scannerDebug = false; 7.13 7.14 - /** A factory for creating scanners. */ 7.15 - public static class Factory { 7.16 - /** The context key for the scanner factory. */ 7.17 - public static final Context.Key<Scanner.Factory> scannerFactoryKey = 7.18 - new Context.Key<Scanner.Factory>(); 7.19 - 7.20 - /** Get the Factory instance for this context. */ 7.21 - public static Factory instance(Context context) { 7.22 - Factory instance = context.get(scannerFactoryKey); 7.23 - if (instance == null) 7.24 - instance = new Factory(context); 7.25 - return instance; 7.26 - } 7.27 - 7.28 - final Log log; 7.29 - final Names names; 7.30 - final Source source; 7.31 - final Keywords keywords; 7.32 - 7.33 - /** Create a new scanner factory. */ 7.34 - protected Factory(Context context) { 7.35 - context.put(scannerFactoryKey, this); 7.36 - this.log = Log.instance(context); 7.37 - this.names = Names.instance(context); 7.38 - this.source = Source.instance(context); 7.39 - this.keywords = Keywords.instance(context); 7.40 - } 7.41 - 7.42 - public Scanner newScanner(CharSequence input) { 7.43 - if (input instanceof CharBuffer) { 7.44 - return new Scanner(this, (CharBuffer)input); 7.45 - } else { 7.46 - char[] array = input.toString().toCharArray(); 7.47 - return newScanner(array, array.length); 7.48 - } 7.49 - } 7.50 - 7.51 - public Scanner newScanner(char[] input, int inputLength) { 7.52 - return new Scanner(this, input, inputLength); 7.53 - } 7.54 - } 7.55 - 7.56 /* Output variables; set by nextToken(): 7.57 */ 7.58 7.59 @@ -177,7 +135,7 @@ 7.60 private final Keywords keywords; 7.61 7.62 /** Common code for constructors. */ 7.63 - private Scanner(Factory fac) { 7.64 + private Scanner(ScannerFactory fac) { 7.65 log = fac.log; 7.66 names = fac.names; 7.67 keywords = fac.keywords; 7.68 @@ -201,7 +159,7 @@ 7.69 /** Create a scanner from the input buffer. buffer must implement 7.70 * array() and compact(), and remaining() must be less than limit(). 7.71 */ 7.72 - protected Scanner(Factory fac, CharBuffer buffer) { 7.73 + protected Scanner(ScannerFactory fac, CharBuffer buffer) { 7.74 this(fac, JavacFileManager.toArray(buffer), buffer.limit()); 7.75 } 7.76 7.77 @@ -216,7 +174,7 @@ 7.78 * @param inputLength the size of the input. 7.79 * Must be positive and less than or equal to input.length. 7.80 */ 7.81 - protected Scanner(Factory fac, char[] input, int inputLength) { 7.82 + protected Scanner(ScannerFactory fac, char[] input, int inputLength) { 7.83 this(fac); 7.84 eofPos = inputLength; 7.85 if (inputLength == input.length) {
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/src/share/classes/com/sun/tools/javac/parser/ScannerFactory.java Mon Sep 27 14:20:39 2010 -0700 8.3 @@ -0,0 +1,90 @@ 8.4 +/* 8.5 + * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. Oracle designates this 8.11 + * particular file as subject to the "Classpath" exception as provided 8.12 + * by Oracle in the LICENSE file that accompanied this code. 8.13 + * 8.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.17 + * version 2 for more details (a copy is included in the LICENSE file that 8.18 + * accompanied this code). 8.19 + * 8.20 + * You should have received a copy of the GNU General Public License version 8.21 + * 2 along with this work; if not, write to the Free Software Foundation, 8.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.23 + * 8.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.25 + * or visit www.oracle.com if you need additional information or have any 8.26 + * questions. 8.27 + */ 8.28 + 8.29 +package com.sun.tools.javac.parser; 8.30 + 8.31 +import java.nio.CharBuffer; 8.32 + 8.33 +import com.sun.tools.javac.code.Source; 8.34 +import com.sun.tools.javac.util.Context; 8.35 +import com.sun.tools.javac.util.Log; 8.36 +import com.sun.tools.javac.util.Names; 8.37 + 8.38 + 8.39 +/** 8.40 + * A factory for creating scanners. 8.41 + * 8.42 + * <p><b>This is NOT part of any supported API. 8.43 + * If you write code that depends on this, you do so at your own 8.44 + * risk. This code and its internal interfaces are subject to change 8.45 + * or deletion without notice.</b> 8.46 + */ 8.47 +public class ScannerFactory { 8.48 + /** The context key for the scanner factory. */ 8.49 + public static final Context.Key<ScannerFactory> scannerFactoryKey = 8.50 + new Context.Key<ScannerFactory>(); 8.51 + 8.52 + /** Get the Factory instance for this context. */ 8.53 + public static ScannerFactory instance(Context context) { 8.54 + ScannerFactory instance = context.get(scannerFactoryKey); 8.55 + if (instance == null) 8.56 + instance = new ScannerFactory(context); 8.57 + return instance; 8.58 + } 8.59 + 8.60 + final Log log; 8.61 + final Names names; 8.62 + final Source source; 8.63 + final Keywords keywords; 8.64 + 8.65 + /** Create a new scanner factory. */ 8.66 + protected ScannerFactory(Context context) { 8.67 + context.put(scannerFactoryKey, this); 8.68 + this.log = Log.instance(context); 8.69 + this.names = Names.instance(context); 8.70 + this.source = Source.instance(context); 8.71 + this.keywords = Keywords.instance(context); 8.72 + } 8.73 + 8.74 + public Scanner newScanner(CharSequence input, boolean keepDocComments) { 8.75 + if (input instanceof CharBuffer) { 8.76 + CharBuffer buf = (CharBuffer) input; 8.77 + if (keepDocComments) 8.78 + return new DocCommentScanner(this, buf); 8.79 + else 8.80 + return new Scanner(this, buf); 8.81 + } else { 8.82 + char[] array = input.toString().toCharArray(); 8.83 + return newScanner(array, array.length, keepDocComments); 8.84 + } 8.85 + } 8.86 + 8.87 + public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { 8.88 + if (keepDocComments) 8.89 + return new DocCommentScanner(this, input, inputLength); 8.90 + else 8.91 + return new Scanner(this, input, inputLength); 8.92 + } 8.93 +}
9.1 --- a/src/share/classes/com/sun/tools/javadoc/JavadocTool.java Mon Sep 27 14:05:33 2010 -0700 9.2 +++ b/src/share/classes/com/sun/tools/javadoc/JavadocTool.java Mon Sep 27 14:20:39 2010 -0700 9.3 @@ -1,5 +1,5 @@ 9.4 /* 9.5 - * Copyright (c) 2001, 2009, Oracle and/or its affiliates. All rights reserved. 9.6 + * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. 9.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.8 * 9.9 * This code is free software; you can redistribute it and/or modify it 9.10 @@ -107,9 +107,6 @@ 9.11 // force the use of Messager as a Log 9.12 messager = Messager.instance0(context); 9.13 9.14 - // force the use of the scanner that captures Javadoc comments 9.15 - DocCommentScanner.Factory.preRegister(context); 9.16 - 9.17 return new JavadocTool(context); 9.18 } catch (CompletionFailure ex) { 9.19 messager.error(Position.NOPOS, ex.getMessage());
10.1 --- a/test/tools/javac/6302184/T6302184.out Mon Sep 27 14:05:33 2010 -0700 10.2 +++ b/test/tools/javac/6302184/T6302184.out Mon Sep 27 14:20:39 2010 -0700 10.3 @@ -1,4 +1,7 @@ 10.4 10.5 +/** 10.6 + * This is a test that uses ISO 8859 encoding. 10.7 + */ 10.8 class T6302184 { 10.9 10.10 T6302184() {
11.1 --- a/test/tools/javac/6304921/TestLog.java Mon Sep 27 14:05:33 2010 -0700 11.2 +++ b/test/tools/javac/6304921/TestLog.java Mon Sep 27 14:20:39 2010 -0700 11.3 @@ -1,5 +1,5 @@ 11.4 /* 11.5 - * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. 11.6 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 11.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 11.8 * 11.9 * This code is free software; you can redistribute it and/or modify it 11.10 @@ -35,7 +35,6 @@ 11.11 import com.sun.tools.javac.file.JavacFileManager; 11.12 import com.sun.tools.javac.parser.Parser; 11.13 import com.sun.tools.javac.parser.ParserFactory; 11.14 -import com.sun.tools.javac.parser.Scanner; 11.15 import com.sun.tools.javac.tree.JCTree; 11.16 import com.sun.tools.javac.tree.TreeScanner; 11.17 import com.sun.tools.javac.util.Context; 11.18 @@ -60,7 +59,6 @@ 11.19 log.multipleErrors = true; 11.20 11.21 JavacFileManager.preRegister(context); 11.22 - Scanner.Factory sfac = Scanner.Factory.instance(context); 11.23 ParserFactory pfac = ParserFactory.instance(context); 11.24 11.25 final String text =
12.1 --- a/test/tools/javac/api/TestJavacTaskScanner.java Mon Sep 27 14:05:33 2010 -0700 12.2 +++ b/test/tools/javac/api/TestJavacTaskScanner.java Mon Sep 27 14:20:39 2010 -0700 12.3 @@ -1,5 +1,5 @@ 12.4 /* 12.5 - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 12.6 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 12.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 12.8 * 12.9 * This code is free software; you can redistribute it and/or modify it 12.10 @@ -31,8 +31,8 @@ 12.11 */ 12.12 12.13 import com.sun.tools.javac.api.JavacTaskImpl; 12.14 -import com.sun.tools.javac.parser.*; // XXX 12.15 -import com.sun.tools.javac.util.*; // XXX 12.16 +import com.sun.tools.javac.parser.*; 12.17 +import com.sun.tools.javac.util.*; 12.18 import java.io.*; 12.19 import java.net.*; 12.20 import java.nio.*; 12.21 @@ -65,7 +65,7 @@ 12.22 fm.getJavaFileObjects(new File[] {file}); 12.23 StandardJavaFileManager fm = getLocalFileManager(tool, null, null); 12.24 task = (JavacTaskImpl)tool.getTask(null, fm, null, null, null, compilationUnits); 12.25 - task.getContext().put(Scanner.Factory.scannerFactoryKey, 12.26 + task.getContext().put(ScannerFactory.scannerFactoryKey, 12.27 new MyScanner.Factory(task.getContext(), this)); 12.28 elements = task.getElements(); 12.29 types = task.getTypes(); 12.30 @@ -170,34 +170,36 @@ 12.31 12.32 class MyScanner extends Scanner { 12.33 12.34 - public static class Factory extends Scanner.Factory { 12.35 + public static class Factory extends ScannerFactory { 12.36 public Factory(Context context, TestJavacTaskScanner test) { 12.37 super(context); 12.38 this.test = test; 12.39 } 12.40 12.41 @Override 12.42 - public Scanner newScanner(CharSequence input) { 12.43 + public Scanner newScanner(CharSequence input, boolean keepDocComments) { 12.44 + assert !keepDocComments; 12.45 if (input instanceof CharBuffer) { 12.46 return new MyScanner(this, (CharBuffer)input, test); 12.47 } else { 12.48 char[] array = input.toString().toCharArray(); 12.49 - return newScanner(array, array.length); 12.50 + return newScanner(array, array.length, keepDocComments); 12.51 } 12.52 } 12.53 12.54 @Override 12.55 - public Scanner newScanner(char[] input, int inputLength) { 12.56 + public Scanner newScanner(char[] input, int inputLength, boolean keepDocComments) { 12.57 + assert !keepDocComments; 12.58 return new MyScanner(this, input, inputLength, test); 12.59 } 12.60 12.61 private TestJavacTaskScanner test; 12.62 } 12.63 - protected MyScanner(Factory fac, CharBuffer buffer, TestJavacTaskScanner test) { 12.64 + protected MyScanner(ScannerFactory fac, CharBuffer buffer, TestJavacTaskScanner test) { 12.65 super(fac, buffer); 12.66 this.test = test; 12.67 } 12.68 - protected MyScanner(Factory fac, char[] input, int inputLength, TestJavacTaskScanner test) { 12.69 + protected MyScanner(ScannerFactory fac, char[] input, int inputLength, TestJavacTaskScanner test) { 12.70 super(fac, input, inputLength); 12.71 this.test = test; 12.72 }
13.1 --- a/test/tools/javac/processing/Xprint.java Mon Sep 27 14:05:33 2010 -0700 13.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 13.3 @@ -1,42 +0,0 @@ 13.4 -/* 13.5 - * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 13.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 13.7 - * 13.8 - * This code is free software; you can redistribute it and/or modify it 13.9 - * under the terms of the GNU General Public License version 2 only, as 13.10 - * published by the Free Software Foundation. 13.11 - * 13.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 13.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13.15 - * version 2 for more details (a copy is included in the LICENSE file that 13.16 - * accompanied this code). 13.17 - * 13.18 - * You should have received a copy of the GNU General Public License version 13.19 - * 2 along with this work; if not, write to the Free Software Foundation, 13.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 13.21 - * 13.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 13.23 - * or visit www.oracle.com if you need additional information or have any 13.24 - * questions. 13.25 - */ 13.26 - 13.27 -/* 13.28 - * @test 13.29 - * @bug 6266828 13.30 - * @summary JSR 269: Java Language Model API 13.31 - * @author Peter von der Ah\u00e9 13.32 - */ 13.33 -import javax.tools.JavaCompiler; 13.34 -import javax.tools.ToolProvider; 13.35 - 13.36 -public class Xprint { 13.37 - public static void main(String[] args) { 13.38 - JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); 13.39 - javac.run(System.in, null, null, 13.40 - "-Xprint", 13.41 - "com.sun.tools.javac.code.Types", 13.42 - "com.sun.tools.javac.parser.Parser", 13.43 - "java.util.EnumSet"); 13.44 - } 13.45 -}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/test/tools/javac/processing/model/util/elements/doccomments/TestDocComments.java Mon Sep 27 14:20:39 2010 -0700 14.3 @@ -0,0 +1,309 @@ 14.4 +/* 14.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 14.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 14.7 + * 14.8 + * This code is free software; you can redistribute it and/or modify it 14.9 + * under the terms of the GNU General Public License version 2 only, as 14.10 + * published by the Free Software Foundation. 14.11 + * 14.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 14.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14.15 + * version 2 for more details (a copy is included in the LICENSE file that 14.16 + * accompanied this code). 14.17 + * 14.18 + * You should have received a copy of the GNU General Public License version 14.19 + * 2 along with this work; if not, write to the Free Software Foundation, 14.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 14.21 + * 14.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 14.23 + * or visit www.oracle.com if you need additional information or have any 14.24 + * questions. 14.25 + */ 14.26 + 14.27 +/* 14.28 + * @test 14.29 + * @bug 6877202 14.30 + * @summary Elements.getDocComment() is not getting JavaDocComments 14.31 + */ 14.32 + 14.33 +import com.sun.source.tree.*; 14.34 +import com.sun.source.util.*; 14.35 +import java.io.*; 14.36 +import java.util.*; 14.37 +import javax.annotation.processing.*; 14.38 +import javax.lang.model.*; 14.39 +import javax.lang.model.element.*; 14.40 +import javax.lang.model.util.*; 14.41 +import javax.tools.*; 14.42 + 14.43 +/* 14.44 + * For a mixture of pre-existing and generated source files, ensure that we can 14.45 + * get the doc comments. 14.46 + * The test uses both a standard ElementScanner to find all the elements being 14.47 + * processed, and a TreeScanner to find all the local and anonymous inner classes 14.48 + * as well. 14.49 + * And, because the relevant code paths in the compiler are different for 14.50 + * command line and JSR 199 invocation, the test covers both ways of invoking the 14.51 + * compiler. 14.52 + */ 14.53 + 14.54 +@SupportedOptions("scan") 14.55 +@SupportedAnnotationTypes("*") 14.56 +public class TestDocComments extends AbstractProcessor { 14.57 + enum CompileKind { API, CMD }; 14.58 + enum ScanKind { TREE, ELEMENT }; 14.59 + 14.60 + // ----- Main test driver: invoke compiler for the various test cases ------ 14.61 + 14.62 + public static void main(String... args) throws Exception { 14.63 + for (CompileKind ck: CompileKind.values()) { 14.64 + for (ScanKind sk: ScanKind.values()) { 14.65 + try { 14.66 + test(ck, sk); 14.67 + } catch (IOException e) { 14.68 + error(e.toString()); 14.69 + } 14.70 + } 14.71 + } 14.72 + 14.73 + if (errors > 0) 14.74 + throw new Exception(errors + " errors occurred"); 14.75 + } 14.76 + 14.77 + static void test(CompileKind ck, ScanKind sk) throws IOException { 14.78 + String testClasses = System.getProperty("test.classes"); 14.79 + String testSrc = System.getProperty("test.src"); 14.80 + File testDir = new File("test." + ck + "." + sk); 14.81 + testDir.mkdirs(); 14.82 + String[] opts = { 14.83 + "-d", testDir.getPath(), 14.84 + "-implicit:none", 14.85 + "-processor", TestDocComments.class.getName(), 14.86 + "-processorpath", testClasses, 14.87 + //"-XprintRounds", 14.88 + "-Ascan=" + sk 14.89 + }; 14.90 + File[] files = { 14.91 + new File(testSrc, "a/First.java") 14.92 + }; 14.93 + 14.94 + if (ck == CompileKind.API) 14.95 + test_javac_api(opts, files); 14.96 + else 14.97 + test_javac_cmd(opts, files); 14.98 + } 14.99 + 14.100 + static void test_javac_api(String[] opts, File[] files) throws IOException { 14.101 + System.err.println("test javac api: " + Arrays.asList(opts) + " " + Arrays.asList(files)); 14.102 + DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() { 14.103 + public void report(Diagnostic diagnostic) { 14.104 + error(diagnostic.toString()); 14.105 + } 14.106 + }; 14.107 + JavaCompiler c = ToolProvider.getSystemJavaCompiler(); 14.108 + StandardJavaFileManager fm = c.getStandardFileManager(null, null, null); 14.109 + Iterable<? extends JavaFileObject> units = fm.getJavaFileObjects(files); 14.110 + JavacTask t = (JavacTask) c.getTask(null, fm, dl, Arrays.asList(opts), null, units); 14.111 + t.parse(); 14.112 + t.analyze(); 14.113 + } 14.114 + 14.115 + static void test_javac_cmd(String[] opts, File[] files) { 14.116 + System.err.println("test javac cmd: " + Arrays.asList(opts) + " " + Arrays.asList(files)); 14.117 + StringWriter sw = new StringWriter(); 14.118 + PrintWriter pw = new PrintWriter(sw); 14.119 + List<String> args = new ArrayList<String>(Arrays.asList(opts)); 14.120 + for (File f: files) 14.121 + args.add(f.getPath()); 14.122 + int rc = com.sun.tools.javac.Main.compile(args.toArray(new String[args.size()]), pw); 14.123 + pw.close(); 14.124 + String out = sw.toString(); 14.125 + if (out.length() > 0) 14.126 + System.err.println(out); 14.127 + if (rc > 0) 14.128 + error("Compilation failed: rc=" + rc); 14.129 + } 14.130 + 14.131 + static void error(String msg) { 14.132 + System.err.println(msg); 14.133 + errors++; 14.134 + //throw new Error(msg); 14.135 + } 14.136 + 14.137 + static int errors; 14.138 + 14.139 + // ----- Annotation processor: scan for elements and check doc comments ---- 14.140 + 14.141 + Map<String,String> options; 14.142 + Filer filer; 14.143 + Messager messager; 14.144 + Elements elements; 14.145 + ScanKind skind; 14.146 + 14.147 + int round = 0; 14.148 + 14.149 + @Override 14.150 + public SourceVersion getSupportedSourceVersion() { 14.151 + return SourceVersion.latest(); 14.152 + } 14.153 + 14.154 + @Override 14.155 + public void init(ProcessingEnvironment pEnv) { 14.156 + super.init(pEnv); 14.157 + options = pEnv.getOptions(); 14.158 + filer = pEnv.getFiler(); 14.159 + messager = pEnv.getMessager(); 14.160 + elements = pEnv.getElementUtils(); 14.161 + skind = ScanKind.valueOf(options.get("scan")); 14.162 + } 14.163 + 14.164 + @Override 14.165 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 14.166 + round++; 14.167 + 14.168 + // Scan elements using an appropriate scanner, and for each element found, 14.169 + // call check(Element e) to verify the doc comment on that element 14.170 + for (Element e: roundEnv.getRootElements()) { 14.171 + System.err.println("scan " + skind + " " + e.getKind() + " " + e.getSimpleName()); 14.172 + if (skind == ScanKind.TREE) { 14.173 + Trees trees = Trees.instance(processingEnv); // cannot cache this across rounds 14.174 + new TestTreeScanner().scan(trees.getPath(e), trees); 14.175 + } else 14.176 + new TestElementScanner().scan(e); 14.177 + } 14.178 + 14.179 + // For a few rounds, generate new source files, so that we can check whether 14.180 + // doc comments are correctly handled in subsequent processing rounds 14.181 + final int MAX_ROUNDS = 3; 14.182 + if (round <= MAX_ROUNDS) { 14.183 + String pkg = "p"; 14.184 + String currClass = "Gen" + round; 14.185 + String curr = pkg + "." + currClass; 14.186 + String next = (round < MAX_ROUNDS) ? (pkg + ".Gen" + (round + 1)) : "z.Last"; 14.187 + StringBuilder text = new StringBuilder(); 14.188 + text.append("package ").append(pkg).append(";\n"); 14.189 + text.append("/** CLASS ").append(currClass).append(" */\n"); 14.190 + text.append("public class ").append(currClass).append(" {\n"); 14.191 + text.append(" /** CONSTRUCTOR <init> **/\n"); 14.192 + text.append(" ").append(currClass).append("() { }\n"); 14.193 + text.append(" /** FIELD x */\n"); 14.194 + text.append(" ").append(next).append(" x;\n"); 14.195 + text.append(" /** METHOD m */\n"); 14.196 + text.append(" void m() { }\n"); 14.197 + text.append("}\n"); 14.198 + 14.199 + try { 14.200 + JavaFileObject fo = filer.createSourceFile(curr); 14.201 + Writer out = fo.openWriter(); 14.202 + try { 14.203 + out.write(text.toString()); 14.204 + } finally { 14.205 + out.close(); 14.206 + } 14.207 + } catch (IOException e) { 14.208 + throw new Error(e); 14.209 + } 14.210 + } 14.211 + 14.212 + return true; 14.213 + } 14.214 + 14.215 + /* 14.216 + * Check that the doc comment on an element is as expected. 14.217 + * This method is invoked for each element found by the scanners run by process. 14.218 + */ 14.219 + void check(Element e) { 14.220 + System.err.println("Checking " + e); 14.221 + 14.222 + String dc = elements.getDocComment(e); 14.223 + System.err.println(" found " + dc); 14.224 + 14.225 + String expect = (e.getKind() + " " + e.getSimpleName()); // default 14.226 + 14.227 + Name name = e.getSimpleName(); 14.228 + Element encl = e.getEnclosingElement(); 14.229 + Name enclName = encl.getSimpleName(); 14.230 + ElementKind enclKind = encl.getKind(); 14.231 + switch (e.getKind()) { 14.232 + case PARAMETER: 14.233 + case LOCAL_VARIABLE: 14.234 + // doc comments not retained for these elements 14.235 + expect = null; 14.236 + break; 14.237 + 14.238 + case CONSTRUCTOR: 14.239 + if (enclName.length() == 0 || enclKind == ElementKind.ENUM) { 14.240 + // Enum constructor is synthetic 14.241 + expect = null; 14.242 + } 14.243 + break; 14.244 + 14.245 + case METHOD: 14.246 + if (enclKind == ElementKind.ENUM 14.247 + && (name.contentEquals("values") || name.contentEquals("valueOf"))) { 14.248 + // synthetic enum methods 14.249 + expect = null; 14.250 + } 14.251 + break; 14.252 + 14.253 + case CLASS: 14.254 + if (e.getSimpleName().length() == 0) { 14.255 + // anon inner class 14.256 + expect = null; 14.257 + } 14.258 + break; 14.259 + } 14.260 + 14.261 + System.err.println(" expect " + expect); 14.262 + 14.263 + if (dc == null ? expect == null : dc.trim().equals(expect)) 14.264 + return; 14.265 + 14.266 + if (dc == null) 14.267 + messager.printMessage(Diagnostic.Kind.ERROR, "doc comment is null", e); 14.268 + else { 14.269 + messager.printMessage(Diagnostic.Kind.ERROR, 14.270 + "unexpected comment: \"" + dc + "\", expected \"" + expect + "\"", e); 14.271 + } 14.272 + } 14.273 + 14.274 + // ----- Scanners to find elements ----------------------------------------- 14.275 + 14.276 + class TestElementScanner extends ElementScanner7<Void, Void> { 14.277 + @Override 14.278 + public Void visitExecutable(ExecutableElement e, Void _) { 14.279 + check(e); 14.280 + return super.visitExecutable(e, _); 14.281 + } 14.282 + @Override 14.283 + public Void visitType(TypeElement e, Void _) { 14.284 + check(e); 14.285 + return super.visitType(e, _); 14.286 + } 14.287 + @Override 14.288 + public Void visitVariable(VariableElement e, Void _) { 14.289 + check(e); 14.290 + return super.visitVariable(e, _); 14.291 + } 14.292 + } 14.293 + 14.294 + class TestTreeScanner extends TreePathScanner<Void,Trees> { 14.295 + @Override 14.296 + public Void visitClass(ClassTree tree, Trees trees) { 14.297 + check(trees.getElement(getCurrentPath())); 14.298 + return super.visitClass(tree, trees); 14.299 + } 14.300 + @Override 14.301 + public Void visitMethod(MethodTree tree, Trees trees) { 14.302 + check(trees.getElement(getCurrentPath())); 14.303 + return super.visitMethod(tree, trees); 14.304 + } 14.305 + @Override 14.306 + public Void visitVariable(VariableTree tree, Trees trees) { 14.307 + check(trees.getElement(getCurrentPath())); 14.308 + return super.visitVariable(tree, trees); 14.309 + } 14.310 + } 14.311 + 14.312 +}
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/test/tools/javac/processing/model/util/elements/doccomments/a/First.java Mon Sep 27 14:20:39 2010 -0700 15.3 @@ -0,0 +1,54 @@ 15.4 +/* 15.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 15.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 15.7 + * 15.8 + * This code is free software; you can redistribute it and/or modify it 15.9 + * under the terms of the GNU General Public License version 2 only, as 15.10 + * published by the Free Software Foundation. 15.11 + * 15.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 15.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15.15 + * version 2 for more details (a copy is included in the LICENSE file that 15.16 + * accompanied this code). 15.17 + * 15.18 + * You should have received a copy of the GNU General Public License version 15.19 + * 2 along with this work; if not, write to the Free Software Foundation, 15.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 15.21 + * 15.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 15.23 + * or visit www.oracle.com if you need additional information or have any 15.24 + * questions. 15.25 + */ 15.26 + 15.27 +package a; 15.28 + 15.29 +/** CLASS First */ 15.30 +public class First { 15.31 + /** CONSTRUCTOR <init> */ 15.32 + First() { } 15.33 + 15.34 + /** FIELD x */ 15.35 + p.Gen1 x; 15.36 + 15.37 + /** METHOD m **/ 15.38 + void m(int i) { 15.39 + /** CLASS Local */ 15.40 + class Local { 15.41 + /** CONSTRUCTOR <init> */ 15.42 + Local() { } 15.43 + } 15.44 + 15.45 + Runnable r = new Runnable() { 15.46 + /** METHOD run **/ 15.47 + public void run() { } 15.48 + }; 15.49 + 15.50 + } 15.51 + 15.52 + /** ENUM E */ 15.53 + enum E { 15.54 + /** ENUM_CONSTANT e1 */ 15.55 + e1 15.56 + } 15.57 +}
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/test/tools/javac/processing/model/util/elements/doccomments/z/Last.java Mon Sep 27 14:20:39 2010 -0700 16.3 @@ -0,0 +1,30 @@ 16.4 +/* 16.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 16.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 16.7 + * 16.8 + * This code is free software; you can redistribute it and/or modify it 16.9 + * under the terms of the GNU General Public License version 2 only, as 16.10 + * published by the Free Software Foundation. 16.11 + * 16.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 16.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16.15 + * version 2 for more details (a copy is included in the LICENSE file that 16.16 + * accompanied this code). 16.17 + * 16.18 + * You should have received a copy of the GNU General Public License version 16.19 + * 2 along with this work; if not, write to the Free Software Foundation, 16.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 16.21 + * 16.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 16.23 + * or visit www.oracle.com if you need additional information or have any 16.24 + * questions. 16.25 + */ 16.26 + 16.27 +package z; 16.28 + 16.29 +// This class should be read last, implicitly. Therefore it should not 16.30 +// be subject to anno processing. If it is, the lack of doc comments should 16.31 +// be detected and will flag an error. 16.32 +public class Last { 16.33 +}
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 17.2 +++ b/test/tools/javac/processing/options/Xprint.java Mon Sep 27 14:20:39 2010 -0700 17.3 @@ -0,0 +1,42 @@ 17.4 +/* 17.5 + * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 17.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 17.7 + * 17.8 + * This code is free software; you can redistribute it and/or modify it 17.9 + * under the terms of the GNU General Public License version 2 only, as 17.10 + * published by the Free Software Foundation. 17.11 + * 17.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 17.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 17.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 17.15 + * version 2 for more details (a copy is included in the LICENSE file that 17.16 + * accompanied this code). 17.17 + * 17.18 + * You should have received a copy of the GNU General Public License version 17.19 + * 2 along with this work; if not, write to the Free Software Foundation, 17.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17.21 + * 17.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 17.23 + * or visit www.oracle.com if you need additional information or have any 17.24 + * questions. 17.25 + */ 17.26 + 17.27 +/* 17.28 + * @test 17.29 + * @bug 6266828 17.30 + * @summary JSR 269: Java Language Model API 17.31 + * @author Peter von der Ah\u00e9 17.32 + */ 17.33 +import javax.tools.JavaCompiler; 17.34 +import javax.tools.ToolProvider; 17.35 + 17.36 +public class Xprint { 17.37 + public static void main(String[] args) { 17.38 + JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); 17.39 + javac.run(System.in, null, null, 17.40 + "-Xprint", 17.41 + "com.sun.tools.javac.code.Types", 17.42 + "com.sun.tools.javac.parser.Parser", 17.43 + "java.util.EnumSet"); 17.44 + } 17.45 +}
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 18.2 +++ b/test/tools/javac/processing/options/XprintDocComments.java Mon Sep 27 14:20:39 2010 -0700 18.3 @@ -0,0 +1,39 @@ 18.4 +/* 18.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 18.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 18.7 + * 18.8 + * This code is free software; you can redistribute it and/or modify it 18.9 + * under the terms of the GNU General Public License version 2 only, as 18.10 + * published by the Free Software Foundation. 18.11 + * 18.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 18.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 18.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 18.15 + * version 2 for more details (a copy is included in the LICENSE file that 18.16 + * accompanied this code). 18.17 + * 18.18 + * You should have received a copy of the GNU General Public License version 18.19 + * 2 along with this work; if not, write to the Free Software Foundation, 18.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18.21 + * 18.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 18.23 + * or visit www.oracle.com if you need additional information or have any 18.24 + * questions. 18.25 + */ 18.26 + 18.27 +/* 18.28 + * @test 18.29 + * @bug 6861094 18.30 + * @summary javac -Xprint <file> does not print comments 18.31 + * @compile/ref=XprintDocComments.out -Xprint XprintDocComments.java 18.32 + */ 18.33 + 18.34 +/** 18.35 + * CLASS XprintDocComments 18.36 + */ 18.37 +class XPrintDocComments { 18.38 + /** 18.39 + * FIELD i; 18.40 + */ 18.41 + int i; 18.42 +}
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 19.2 +++ b/test/tools/javac/processing/options/XprintDocComments.out Mon Sep 27 14:20:39 2010 -0700 19.3 @@ -0,0 +1,12 @@ 19.4 + 19.5 +/** 19.6 + * CLASS XprintDocComments 19.7 + */ 19.8 +class XPrintDocComments { 19.9 + 19.10 + XPrintDocComments(); 19.11 + /** 19.12 + * FIELD i; 19.13 + */ 19.14 + int i; 19.15 +}
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 20.2 +++ b/test/tools/javac/tree/TreePosRoundsTest.java Mon Sep 27 14:20:39 2010 -0700 20.3 @@ -0,0 +1,204 @@ 20.4 +/* 20.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 20.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 20.7 + * 20.8 + * This code is free software; you can redistribute it and/or modify it 20.9 + * under the terms of the GNU General Public License version 2 only, as 20.10 + * published by the Free Software Foundation. 20.11 + * 20.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 20.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 20.15 + * version 2 for more details (a copy is included in the LICENSE file that 20.16 + * accompanied this code). 20.17 + * 20.18 + * You should have received a copy of the GNU General Public License version 20.19 + * 2 along with this work; if not, write to the Free Software Foundation, 20.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20.21 + * 20.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20.23 + * or visit www.oracle.com if you need additional information or have any 20.24 + * questions. 20.25 + */ 20.26 + 20.27 +/* 20.28 + * @test 20.29 + * @bug 6985205 20.30 + * @summary access to tree positions and doc comments may be lost across annotation processing rounds 20.31 + * @build TreePosRoundsTest 20.32 + * @compile -proc:only -processor TreePosRoundsTest TreePosRoundsTest.java 20.33 + * @run main TreePosRoundsTest 20.34 + */ 20.35 + 20.36 +import java.io.*; 20.37 +import java.util.*; 20.38 +import javax.annotation.processing.*; 20.39 +import javax.lang.model.*; 20.40 +import javax.lang.model.element.*; 20.41 +import javax.tools.*; 20.42 + 20.43 +import com.sun.source.tree.*; 20.44 +import com.sun.source.util.*; 20.45 +import javax.tools.JavaCompiler.CompilationTask; 20.46 + 20.47 +// This test is an annotation processor that performs multiple rounds of 20.48 +// processing, and on each round, it checks that source positions are 20.49 +// available and correct. 20.50 +// 20.51 +// The test can be run directly as a processor from the javac command line 20.52 +// or via JSR 199 by invoking the main program. 20.53 + 20.54 +@SupportedAnnotationTypes("*") 20.55 +public class TreePosRoundsTest extends AbstractProcessor { 20.56 + public static void main(String... args) throws Exception { 20.57 + String testSrc = System.getProperty("test.src"); 20.58 + String testClasses = System.getProperty("test.classes"); 20.59 + JavaCompiler c = ToolProvider.getSystemJavaCompiler(); 20.60 + StandardJavaFileManager fm = c.getStandardFileManager(null, null, null); 20.61 + String thisName = TreePosRoundsTest.class.getName(); 20.62 + File thisFile = new File(testSrc, thisName + ".java"); 20.63 + Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(thisFile); 20.64 + List<String> options = Arrays.asList( 20.65 + "-proc:only", 20.66 + "-processor", thisName, 20.67 + "-processorpath", testClasses); 20.68 + CompilationTask t = c.getTask(null, fm, null, options, null, files); 20.69 + boolean ok = t.call(); 20.70 + if (!ok) 20.71 + throw new Exception("processing failed"); 20.72 + } 20.73 + 20.74 + Filer filer; 20.75 + Messager messager; 20.76 + 20.77 + @Override 20.78 + public SourceVersion getSupportedSourceVersion() { 20.79 + return SourceVersion.latest(); 20.80 + } 20.81 + 20.82 + @Override 20.83 + public void init(ProcessingEnvironment pEnv) { 20.84 + super.init(pEnv); 20.85 + filer = pEnv.getFiler(); 20.86 + messager = pEnv.getMessager(); 20.87 + } 20.88 + 20.89 + int round = 0; 20.90 + 20.91 + @Override 20.92 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { 20.93 + round++; 20.94 + 20.95 + // Scan trees for elements, verifying source tree positions 20.96 + for (Element e: roundEnv.getRootElements()) { 20.97 + try { 20.98 + Trees trees = Trees.instance(processingEnv); // cannot cache this across rounds 20.99 + TreePath p = trees.getPath(e); 20.100 + new TestTreeScanner(p.getCompilationUnit(), trees).scan(trees.getPath(e), null); 20.101 + } catch (IOException ex) { 20.102 + messager.printMessage(Diagnostic.Kind.ERROR, 20.103 + "Cannot get source: " + ex, e); 20.104 + } 20.105 + } 20.106 + 20.107 + final int MAXROUNDS = 3; 20.108 + if (round < MAXROUNDS) 20.109 + generateSource("Gen" + round); 20.110 + 20.111 + return true; 20.112 + } 20.113 + 20.114 + void generateSource(String name) { 20.115 + StringBuilder text = new StringBuilder(); 20.116 + text.append("class ").append(name).append("{\n"); 20.117 + text.append(" int one = 1;\n"); 20.118 + text.append(" int two = 2;\n"); 20.119 + text.append(" int three = one + two;\n"); 20.120 + text.append("}\n"); 20.121 + 20.122 + try { 20.123 + JavaFileObject fo = filer.createSourceFile(name); 20.124 + Writer out = fo.openWriter(); 20.125 + try { 20.126 + out.write(text.toString()); 20.127 + } finally { 20.128 + out.close(); 20.129 + } 20.130 + } catch (IOException e) { 20.131 + throw new Error(e); 20.132 + } 20.133 + } 20.134 + 20.135 + class TestTreeScanner extends TreePathScanner<Void,Void> { 20.136 + TestTreeScanner(CompilationUnitTree unit, Trees trees) throws IOException { 20.137 + this.unit = unit; 20.138 + JavaFileObject sf = unit.getSourceFile(); 20.139 + source = sf.getCharContent(true).toString(); 20.140 + sourcePositions = trees.getSourcePositions(); 20.141 + } 20.142 + 20.143 + @Override 20.144 + public Void visitVariable(VariableTree tree, Void _) { 20.145 + check(getCurrentPath()); 20.146 + return super.visitVariable(tree, _); 20.147 + } 20.148 + 20.149 + void check(TreePath tp) { 20.150 + Tree tree = tp.getLeaf(); 20.151 + 20.152 + String expect = tree.toString(); 20.153 + if (tree.getKind() == Tree.Kind.VARIABLE) { 20.154 + // tree.toString() does not know enough context to add ";", 20.155 + // so deal with that manually... 20.156 + Tree.Kind enclKind = tp.getParentPath().getLeaf().getKind(); 20.157 + //System.err.println(" encl: " +enclKind); 20.158 + if (enclKind == Tree.Kind.CLASS || enclKind == Tree.Kind.BLOCK) 20.159 + expect += ";"; 20.160 + } 20.161 + //System.err.println("expect: " + expect); 20.162 + 20.163 + int start = (int)sourcePositions.getStartPosition(unit, tree); 20.164 + if (start == Diagnostic.NOPOS) { 20.165 + messager.printMessage(Diagnostic.Kind.ERROR, "start pos not set for " + trim(tree)); 20.166 + return; 20.167 + } 20.168 + 20.169 + int end = (int)sourcePositions.getEndPosition(unit, tree); 20.170 + if (end == Diagnostic.NOPOS) { 20.171 + messager.printMessage(Diagnostic.Kind.ERROR, "end pos not set for " + trim(tree)); 20.172 + return; 20.173 + } 20.174 + 20.175 + String found = source.substring(start, end); 20.176 + //System.err.println(" found: " + found); 20.177 + 20.178 + // allow for long lines, in which case just compare beginning and 20.179 + // end of the strings 20.180 + boolean equal; 20.181 + if (found.contains("\n")) { 20.182 + String head = found.substring(0, found.indexOf("\n")); 20.183 + String tail = found.substring(found.lastIndexOf("\n")).trim(); 20.184 + equal = expect.startsWith(head) && expect.endsWith(tail); 20.185 + } else { 20.186 + equal = expect.equals(found); 20.187 + } 20.188 + 20.189 + if (!equal) { 20.190 + messager.printMessage(Diagnostic.Kind.ERROR, 20.191 + "unexpected value found: '" + found + "'; expected: '" + expect + "'"); 20.192 + } 20.193 + } 20.194 + 20.195 + String trim(Tree tree) { 20.196 + final int MAXLEN = 32; 20.197 + String s = tree.toString().replaceAll("\\s+", " ").trim(); 20.198 + return (s.length() < MAXLEN) ? s : s.substring(0, MAXLEN); 20.199 + 20.200 + } 20.201 + 20.202 + CompilationUnitTree unit; 20.203 + SourcePositions sourcePositions; 20.204 + String source; 20.205 + } 20.206 + 20.207 +}