Path: blob/master/test/langtools/tools/javac/6402516/CheckLocalElements.java
41152 views
/*1* Copyright (c) 2006, 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 6402516 803156926* @summary need Trees.getScope(TreePath)27* @modules jdk.compiler/com.sun.tools.javac.api28* jdk.compiler/com.sun.tools.javac.comp29* jdk.compiler/com.sun.tools.javac.file30* jdk.compiler/com.sun.tools.javac.tree31* jdk.compiler/com.sun.tools.javac.util32* @build Checker CheckLocalElements33* @run main CheckLocalElements34*/3536import java.io.IOException;37import java.util.*;38import java.util.regex.*;3940import javax.lang.model.element.*;41import javax.lang.model.util.*;4243import com.sun.source.tree.*;44import com.sun.source.util.*;4546/*47* Check the local elements of a scope against the contents of string literals and top-level comment.48*/49public class CheckLocalElements extends Checker {50public static void main(String... args) throws Exception {51Checker chk = new CheckLocalElements();52chk.check("TestLocalElements.java");53}5455@Override56protected boolean checkLocal(Scope s, String ref) {57Iterator<? extends Element> elemIter = s.getLocalElements().iterator();58ref = ref.trim();59String[] refs = ref.length() == 0 ? new String[0] : ref.split("[ ]*,[ ]*", -1);60Iterator<String> refIter = Arrays.asList(refs).iterator();61String r = null;6263nextElem:64while (elemIter.hasNext()) {65Element e = elemIter.next();66try {67if (r == null)68r = refIter.next();6970while (r.endsWith(".*")) {71String encl = getEnclosingName(e);72String rBase = r.substring(0, r.length() - 2);73if (encl.equals(rBase) || encl.startsWith(rBase + "."))74continue nextElem;75r = refIter.next();76}7778if (r.equals("-") && (e.getSimpleName().length() == 0)79|| e.getSimpleName().toString().equals(r)) {80r = null;81continue nextElem;82}8384error(s, ref, "mismatch: " + e.getSimpleName() + " " + r);85return false;8687} catch (NoSuchElementException ex) { // from refIter.next()88error(s, null, "scope has unexpected entry: " + e.getSimpleName());89return false;90}9192}9394if (refIter.hasNext()) {95error(s, ref, "scope is missing entry: " + refIter.next());96return false;97}9899return true;100}101102@Override103void additionalChecks(Trees trees, CompilationUnitTree topLevel) throws IOException {104Matcher m = TOPLEVEL_SCOPE_DEF.matcher(topLevel.getSourceFile().getCharContent(false));105if (!m.find())106throw new AssertionError("Should have top-level scope def!");107check(trees.getScope(new TreePath(topLevel)), m.group(1));108}109//where:110Pattern TOPLEVEL_SCOPE_DEF = Pattern.compile("TOPLEVEL_SCOPE:(.*)");111112private String getEnclosingName(Element e) {113Element encl = e.getEnclosingElement();114return encl == null ? "" : encl.accept(qualNameVisitor, null);115}116117private ElementVisitor<String,Void> qualNameVisitor = new SimpleElementVisitor14<String,Void>() {118protected String defaultAction(Element e, Void ignore) {119return "";120}121122public String visitPackage(PackageElement e, Void ignore) {123return e.getQualifiedName().toString();124}125126public String visitType(TypeElement e, Void ignore) {127return e.getQualifiedName().toString();128}129};130}131132133