duke@1: /* ohair@554: * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: import java.io.*; duke@1: import java.util.*; duke@1: import javax.lang.model.util.*; duke@1: import javax.tools.*; duke@1: import com.sun.tools.javac.api.*; duke@1: import com.sun.source.tree.*; duke@1: import com.sun.source.util.*; duke@1: import com.sun.tools.javac.tree.JCTree; duke@1: import com.sun.tools.javac.tree.JCTree.*; duke@1: import com.sun.tools.javac.util.Position; duke@1: duke@1: /* duke@1: * Abstract class to help check the scopes in a parsed source file. duke@1: * -- parse source file duke@1: * -- scan trees looking for string literals duke@1: * -- check the scope at that point against the string, using duke@1: * boolean check(Scope s, String ref) duke@1: */ duke@1: abstract class Checker { duke@1: // parse the source file and call check(scope, string) for each string literal found duke@1: void check(String... fileNames) throws IOException { duke@1: File testSrc = new File(System.getProperty("test.src")); duke@1: duke@1: DiagnosticListener dl = new DiagnosticListener() { duke@1: public void report(Diagnostic d) { duke@1: System.err.println(d); duke@1: if (d.getKind() == Diagnostic.Kind.ERROR) duke@1: errors = true; duke@1: new Exception().printStackTrace(); duke@1: } duke@1: }; duke@1: duke@1: JavacTool tool = JavacTool.create(); duke@1: StandardJavaFileManager fm = tool.getStandardFileManager(dl, null, null); duke@1: Iterable files = duke@1: fm.getJavaFileObjectsFromFiles(getFiles(testSrc, fileNames)); duke@1: task = tool.getTask(null, fm, dl, null, null, files); duke@1: Iterable units = task.parse(); duke@1: duke@1: if (errors) duke@1: throw new AssertionError("errors occurred creating trees"); duke@1: duke@1: ScopeScanner s = new ScopeScanner(); duke@1: for (CompilationUnitTree unit: units) { duke@1: TreePath p = new TreePath(unit); duke@1: s.scan(p, getTrees()); duke@1: } duke@1: task = null; duke@1: duke@1: if (errors) duke@1: throw new AssertionError("errors occurred checking scopes"); duke@1: } duke@1: duke@1: // default impl: split ref at ";" and call checkLocal(scope, ref_segment) on scope and its enclosing scopes duke@1: protected boolean check(Scope s, String ref) { duke@1: // System.err.println("check scope: " + s); duke@1: // System.err.println("check ref: " + ref); duke@1: if (s == null && (ref == null || ref.trim().length() == 0)) duke@1: return true; duke@1: duke@1: if (s == null) { duke@1: error(s, ref, "scope missing"); duke@1: return false; duke@1: } duke@1: duke@1: if (ref == null) { duke@1: error(s, ref, "scope unexpected"); duke@1: return false; duke@1: } duke@1: duke@1: String local; duke@1: String encl; duke@1: int semi = ref.indexOf(';'); duke@1: if (semi == -1) { duke@1: local = ref; duke@1: encl = null; duke@1: } else { duke@1: local = ref.substring(0, semi); duke@1: encl = ref.substring(semi + 1); duke@1: } duke@1: duke@1: return checkLocal(s, local.trim()) duke@1: & check(s.getEnclosingScope(), encl); duke@1: } duke@1: duke@1: // override if using default check(Scope,String) duke@1: boolean checkLocal(Scope s, String ref) { duke@1: throw new IllegalStateException(); duke@1: } duke@1: duke@1: void error(Scope s, String ref, String msg) { duke@1: System.err.println("Error: " + msg); duke@1: System.err.println("Scope: " + (s == null ? null : asList(s.getLocalElements()))); duke@1: System.err.println("Expect: " + ref); duke@1: System.err.println("javac: " + (s == null ? null : ((JavacScope) s).getEnv())); duke@1: errors = true; duke@1: } duke@1: duke@1: protected Elements getElements() { duke@1: return task.getElements(); duke@1: } duke@1: duke@1: protected Trees getTrees() { duke@1: return Trees.instance(task); duke@1: } duke@1: duke@1: boolean errors = false; duke@1: protected JavacTask task; duke@1: duke@1: // scan a parse tree, and for every string literal found, call check(scope, string) with duke@1: // the string value at the scope at that point duke@1: class ScopeScanner extends TreePathScanner { duke@1: public Boolean visitLiteral(LiteralTree tree, Trees trees) { duke@1: TreePath path = getCurrentPath(); duke@1: CompilationUnitTree unit = path.getCompilationUnit(); duke@1: Position.LineMap lineMap = ((JCCompilationUnit)unit).lineMap; duke@1: // long line = lineMap.getLineNumber(((JCTree)tree).pos/*trees.getSourcePositions().getStartPosition(tree)*/); duke@1: // System.err.println(line + ": " + abbrev(tree)); duke@1: Scope s = trees.getScope(path); duke@1: if (tree.getKind() == Tree.Kind.STRING_LITERAL) duke@1: check(s, tree.getValue().toString().trim()); duke@1: return null; duke@1: } duke@1: duke@1: private String abbrev(Tree tree) { duke@1: int max = 48; duke@1: String s = tree.toString().replaceAll("[ \n]+", " "); duke@1: return (s.length() < max ? s : s.substring(0, max-3) + "..."); duke@1: } duke@1: } duke@1: duke@1: // prefix filenames with a directory duke@1: static Iterable getFiles(File dir, String... names) { duke@1: List files = new ArrayList(names.length); duke@1: for (String name: names) duke@1: files.add(new File(dir, name)); duke@1: return files; duke@1: } duke@1: duke@1: static private List asList(Iterable iter) { duke@1: List l = new ArrayList(); duke@1: for (T t: iter) duke@1: l.add(t); duke@1: return l; duke@1: } duke@1: }