Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6402516/Checker.java
38813 views
/*1* Copyright (c) 2006, 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*/2223import java.io.*;24import java.util.*;25import javax.lang.model.util.*;26import javax.tools.*;27import com.sun.tools.javac.api.*;28import com.sun.source.tree.*;29import com.sun.source.util.*;30import com.sun.tools.javac.tree.JCTree;31import com.sun.tools.javac.tree.JCTree.*;32import com.sun.tools.javac.util.Position;3334/*35* Abstract class to help check the scopes in a parsed source file.36* -- parse source file37* -- scan trees looking for string literals38* -- check the scope at that point against the string, using39* boolean check(Scope s, String ref)40*/41abstract class Checker {42// parse the source file and call check(scope, string) for each string literal found43void check(String... fileNames) throws IOException {44File testSrc = new File(System.getProperty("test.src"));4546DiagnosticListener<JavaFileObject> dl = new DiagnosticListener<JavaFileObject>() {47public void report(Diagnostic d) {48System.err.println(d);49if (d.getKind() == Diagnostic.Kind.ERROR)50errors = true;51new Exception().printStackTrace();52}53};5455JavacTool tool = JavacTool.create();56StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null);57Iterable<? extends JavaFileObject> files =58fm.getJavaFileObjectsFromFiles(getFiles(testSrc, fileNames));59task = tool.getTask(null, fm, dl, null, null, files);60Iterable<? extends CompilationUnitTree> units = task.parse();6162if (errors)63throw new AssertionError("errors occurred creating trees");6465ScopeScanner s = new ScopeScanner();66for (CompilationUnitTree unit: units) {67TreePath p = new TreePath(unit);68s.scan(p, getTrees());69}70task = null;7172if (errors)73throw new AssertionError("errors occurred checking scopes");74}7576// default impl: split ref at ";" and call checkLocal(scope, ref_segment) on scope and its enclosing scopes77protected boolean check(Scope s, String ref) {78// System.err.println("check scope: " + s);79// System.err.println("check ref: " + ref);80if (s == null && (ref == null || ref.trim().length() == 0))81return true;8283if (s == null) {84error(s, ref, "scope missing");85return false;86}8788if (ref == null) {89error(s, ref, "scope unexpected");90return false;91}9293String local;94String encl;95int semi = ref.indexOf(';');96if (semi == -1) {97local = ref;98encl = null;99} else {100local = ref.substring(0, semi);101encl = ref.substring(semi + 1);102}103104return checkLocal(s, local.trim())105& check(s.getEnclosingScope(), encl);106}107108// override if using default check(Scope,String)109boolean checkLocal(Scope s, String ref) {110throw new IllegalStateException();111}112113void error(Scope s, String ref, String msg) {114System.err.println("Error: " + msg);115System.err.println("Scope: " + (s == null ? null : asList(s.getLocalElements())));116System.err.println("Expect: " + ref);117System.err.println("javac: " + (s == null ? null : ((JavacScope) s).getEnv()));118errors = true;119}120121protected Elements getElements() {122return task.getElements();123}124125protected Trees getTrees() {126return Trees.instance(task);127}128129boolean errors = false;130protected JavacTask task;131132// scan a parse tree, and for every string literal found, call check(scope, string) with133// the string value at the scope at that point134class ScopeScanner extends TreePathScanner<Boolean,Trees> {135public Boolean visitLiteral(LiteralTree tree, Trees trees) {136TreePath path = getCurrentPath();137CompilationUnitTree unit = path.getCompilationUnit();138Position.LineMap lineMap = ((JCCompilationUnit)unit).lineMap;139// long line = lineMap.getLineNumber(((JCTree)tree).pos/*trees.getSourcePositions().getStartPosition(tree)*/);140// System.err.println(line + ": " + abbrev(tree));141Scope s = trees.getScope(path);142if (tree.getKind() == Tree.Kind.STRING_LITERAL)143check(s, tree.getValue().toString().trim());144return null;145}146147private String abbrev(Tree tree) {148int max = 48;149String s = tree.toString().replaceAll("[ \n]+", " ");150return (s.length() < max ? s : s.substring(0, max-3) + "...");151}152}153154// prefix filenames with a directory155static Iterable<File> getFiles(File dir, String... names) {156List<File> files = new ArrayList<File>(names.length);157for (String name: names)158files.add(new File(dir, name));159return files;160}161162static private <T> List<T> asList(Iterable<T> iter) {163List<T> l = new ArrayList<T>();164for (T t: iter)165l.add(t);166return l;167}168}169170171