Path: blob/master/test/langtools/jdk/javadoc/tool/T4994049/T4994049.java
40973 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 499404926* @summary Unit test for SourcePosition.column with respect to tab expansion27* @modules jdk.javadoc/jdk.javadoc.internal.tool28* @run main T4994049 FileWithTabs.java29*/3031import java.io.*;32import java.util.ArrayList;33import java.util.Collections;34import java.util.List;35import java.util.Locale;36import java.util.Set;3738import javax.lang.model.SourceVersion;39import javax.lang.model.element.ElementKind;40import javax.lang.model.element.ExecutableElement;41import javax.lang.model.element.TypeElement;42import javax.lang.model.util.ElementFilter;4344import com.sun.source.tree.CompilationUnitTree;45import com.sun.source.tree.LineMap;46import com.sun.source.util.DocTrees;47import com.sun.source.util.SourcePositions;48import com.sun.source.util.TreePath;49import jdk.javadoc.doclet.Doclet;50import jdk.javadoc.doclet.Reporter;51import jdk.javadoc.doclet.DocletEnvironment;5253import static jdk.javadoc.internal.tool.Main.execute;5455public class T4994049 implements Doclet {5657public boolean run(DocletEnvironment root) {58DocTrees trees = root.getDocTrees();5960SourcePositions sourcePositions = trees.getSourcePositions();61for (TypeElement klass : ElementFilter.typesIn(root.getIncludedElements())) {62for (ExecutableElement method : getMethods(klass)) {63if (method.getSimpleName().toString().equals("tabbedMethod")) {64TreePath path = trees.getPath(method);65CompilationUnitTree cu = path.getCompilationUnit();66long pos = sourcePositions.getStartPosition(cu, path.getLeaf());67LineMap lineMap = cu.getLineMap();68long columnNumber = lineMap.getColumnNumber(pos);69if (columnNumber == 9) {70System.out.println(columnNumber + ": OK!");71return true;72} else {73System.err.println(columnNumber + ": wrong tab expansion");74return false;75}76}77}78}79return false;80}8182public static void main(String... args) throws Exception {83File testSrc = new File(System.getProperty("test.src"));84File tmpSrc = new File("tmpSrc");85initTabs(testSrc, tmpSrc);868788for (String file : args) {89File source = new File(tmpSrc, file);90String[] array = {91"-docletpath", System.getProperty("test.classes", "."),92"-doclet", "T4994049",93source.getPath()94};95int rc = execute(array);96if (rc != 0)97throw new Error("Unexpected return code from javadoc: " + rc);98}99}100101static void initTabs(File from, File to) throws IOException {102for (File f: from.listFiles()) {103File t = new File(to, f.getName());104if (f.isDirectory()) {105initTabs(f, t);106} else if (f.getName().endsWith(".java")) {107write(t, read(f).replace("\\t", "\t"));108}109}110}111112static String read(File f) throws IOException {113StringBuilder sb = new StringBuilder();114try (BufferedReader in = new BufferedReader(new FileReader(f))) {115String line;116while ((line = in.readLine()) != null) {117sb.append(line);118sb.append("\n");119}120}121return sb.toString();122}123124static void write(File f, String s) throws IOException {125f.getParentFile().mkdirs();126try (Writer out = new FileWriter(f)) {127out.write(s);128}129}130131List<ExecutableElement> getMethods(TypeElement klass) {132List<ExecutableElement> methods = new ArrayList<>();133klass.getEnclosedElements()134.stream()135.filter((e) -> (e.getKind() == ElementKind.METHOD))136.forEach((e) -> {137methods.add((ExecutableElement) e);138});139return methods;140}141142@Override143public String getName() {144return "Test";145}146147@Override148public Set<Option> getSupportedOptions() {149return Collections.emptySet();150}151152@Override153public SourceVersion getSupportedSourceVersion() {154return SourceVersion.latest();155}156157@Override158public void init(Locale locale, Reporter reporter) {159return;160}161}162163164