1 /* 2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4994049 27 * @summary Unit test for SourcePosition.column with respect to tab expansion 28 * @author Peter von der Ah\u00e9 29 * @modules jdk.javadoc 30 * @run main T4994049 FileWithTabs.java 31 */ 32 33 import java.io.*; 34 import java.util.ArrayList; 35 import java.util.Collections; 36 import java.util.List; 37 import java.util.Locale; 38 import java.util.Set; 39 40 import javax.lang.model.SourceVersion; 41 import javax.lang.model.element.ElementKind; 42 import javax.lang.model.element.ExecutableElement; 43 import javax.lang.model.element.TypeElement; 44 45 import com.sun.source.tree.CompilationUnitTree; 46 import com.sun.source.tree.LineMap; 47 import com.sun.source.util.DocTrees; 48 import com.sun.source.util.SourcePositions; 49 import com.sun.source.util.TreePath; 50 import jdk.javadoc.doclet.Doclet; 51 import jdk.javadoc.doclet.Reporter; 52 import jdk.javadoc.doclet.DocletEnvironment; 53 54 import static jdk.javadoc.internal.tool.Main.execute; 55 56 public class T4994049 implements Doclet { 57 58 public boolean run(DocletEnvironment root) { 59 DocTrees trees = root.getDocTrees(); 60 61 SourcePositions sourcePositions = trees.getSourcePositions(); 62 for (TypeElement klass : root.getIncludedClasses()) { 63 for (ExecutableElement method : getMethods(klass)) { 64 if (method.getSimpleName().toString().equals("tabbedMethod")) { 65 TreePath path = trees.getPath(method); 66 CompilationUnitTree cu = path.getCompilationUnit(); 67 long pos = sourcePositions.getStartPosition(cu, path.getLeaf()); 68 LineMap lineMap = cu.getLineMap(); 69 long columnNumber = lineMap.getColumnNumber(pos); 70 if (columnNumber == 9) { 71 System.out.println(columnNumber + ": OK!"); 72 return true; 73 } else { 74 System.err.println(columnNumber + ": wrong tab expansion"); 75 return false; 76 } 77 } 78 } 79 } 80 return false; 81 } 82 83 public static void main(String... args) throws Exception { 84 File testSrc = new File(System.getProperty("test.src")); 85 File tmpSrc = new File("tmpSrc"); 86 initTabs(testSrc, tmpSrc); 87 88 89 for (String file : args) { 90 File source = new File(tmpSrc, file); 91 String[] array = { 92 "-docletpath", System.getProperty("test.classes", "."), 93 "-doclet", "T4994049", 94 source.getPath() 95 }; 96 int rc = execute(array); 97 if (rc != 0) 98 throw new Error("Unexpected return code from javadoc: " + rc); 99 } 100 } 101 102 static void initTabs(File from, File to) throws IOException { 103 for (File f: from.listFiles()) { 104 File t = new File(to, f.getName()); 105 if (f.isDirectory()) { 106 initTabs(f, t); 107 } else if (f.getName().endsWith(".java")) { 108 write(t, read(f).replace("\\t", "\t")); 109 } 110 } 111 } 112 113 static String read(File f) throws IOException { 114 StringBuilder sb = new StringBuilder(); 115 try (BufferedReader in = new BufferedReader(new FileReader(f))) { 116 String line; 117 while ((line = in.readLine()) != null) { 118 sb.append(line); 119 sb.append("\n"); 120 } 121 } 122 return sb.toString(); 123 } 124 125 static void write(File f, String s) throws IOException { 126 f.getParentFile().mkdirs(); 127 try (Writer out = new FileWriter(f)) { 128 out.write(s); 129 } 130 } 131 132 List<ExecutableElement> getMethods(TypeElement klass) { 133 List<ExecutableElement> methods = new ArrayList<>(); 134 klass.getEnclosedElements() 135 .stream() 136 .filter((e) -> (e.getKind() == ElementKind.METHOD)) 137 .forEach((e) -> { 138 methods.add((ExecutableElement) e); 139 }); 140 return methods; 141 } 142 143 @Override 144 public String getName() { 145 return "Test"; 146 } 147 148 @Override 149 public Set<Option> getSupportedOptions() { 150 return Collections.emptySet(); 151 } 152 153 @Override 154 public SourceVersion getSupportedSourceVersion() { 155 return SourceVersion.latest(); 156 } 157 158 @Override 159 public void init(Locale locale, Reporter reporter) { 160 return; 161 } 162 }