Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6402516/CheckLocalElements.java
38813 views
/*1* Copyright (c) 2006, 2011, 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 640251626* @summary need Trees.getScope(TreePath)27* @build Checker CheckLocalElements28* @run main CheckLocalElements29*/3031import java.util.*;32import com.sun.source.tree.*;33import javax.lang.model.element.*;34import javax.lang.model.util.*;3536/*37* Check the local elements of a scope against the contents of string literals.38*/39public class CheckLocalElements extends Checker {40public static void main(String... args) throws Exception {41Checker chk = new CheckLocalElements();42chk.check("TestLocalElements.java");43}4445@Override46protected boolean checkLocal(Scope s, String ref) {47Iterator<? extends Element> elemIter = s.getLocalElements().iterator();48ref = ref.trim();49String[] refs = ref.length() == 0 ? new String[0] : ref.split("[ ]*,[ ]*", -1);50Iterator<String> refIter = Arrays.asList(refs).iterator();51String r = null;5253nextElem:54while (elemIter.hasNext()) {55Element e = elemIter.next();56try {57if (r == null)58r = refIter.next();5960while (r.endsWith(".*")) {61String encl = getEnclosingName(e);62String rBase = r.substring(0, r.length() - 2);63if (encl.equals(rBase) || encl.startsWith(rBase + "."))64continue nextElem;65r = refIter.next();66}6768if (r.equals("-") && (e.getSimpleName().length() == 0)69|| e.getSimpleName().toString().equals(r)) {70r = null;71continue nextElem;72}7374error(s, ref, "mismatch: " + e.getSimpleName() + " " + r);75return false;7677} catch (NoSuchElementException ex) { // from refIter.next()78error(s, null, "scope has unexpected entry: " + e.getSimpleName());79return false;80}8182}8384if (refIter.hasNext()) {85error(s, ref, "scope is missing entry: " + refIter.next());86return false;87}8889return true;90}9192private String getEnclosingName(Element e) {93Element encl = e.getEnclosingElement();94return encl == null ? "" : encl.accept(qualNameVisitor, null);95}9697private ElementVisitor<String,Void> qualNameVisitor = new SimpleElementVisitor8<String,Void>() {98protected String defaultAction(Element e, Void ignore) {99return "";100}101102public String visitPackage(PackageElement e, Void ignore) {103return e.getQualifiedName().toString();104}105106public String visitType(TypeElement e, Void ignore) {107return e.getQualifiedName().toString();108}109};110}111112113