Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/mlvm/tools/StratumAPTreeVisitor.java
41155 views
/*1* Copyright (c) 2011, 2018, 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*/2223package vm.mlvm.tools;2425import java.util.HashMap;26import java.util.Map;27import java.util.Set;28import java.util.TreeSet;2930import com.sun.source.tree.CompilationUnitTree;31import com.sun.source.tree.LabeledStatementTree;32import com.sun.source.util.TreePathScanner;33import com.sun.source.util.Trees;3435public class StratumAPTreeVisitor extends TreePathScanner<Object, Trees> {36public static final String LABEL_PREFIX = "Stratum_";3738public static class StratumLineInfo implements Comparable<StratumLineInfo> {39String stratumName;40int stratumLineOrder;41String stratumLine;42int javaLineNum;4344public StratumLineInfo(String stratumName, int stratumLineOrder, String stratumLine, int javaLineNum) {45this.stratumName = stratumName;46this.stratumLineOrder = stratumLineOrder;47this.stratumLine = stratumLine;48this.javaLineNum = javaLineNum;49}5051public String getStratumName() {52return stratumName;53}5455public int getStratumLineOrder() {56return stratumLineOrder;57}5859public String getStratumSourceLine() {60return stratumLine;61}6263public int getJavaLineNum() {64return javaLineNum;65}6667@Override68public int compareTo(StratumLineInfo o) {69int i;70if ( (i = getStratumName().compareTo(o.getStratumName())) != 0 )71return i;7273if ( (i = getStratumLineOrder() - o.getStratumLineOrder()) != 0 )74return i;7576return 0;77}7879@Override80public String toString() {81return getStratumName() + ":" + getStratumLineOrder()82+ " => Java:" + getJavaLineNum()83+ " [" + getStratumSourceLine() + "]";84}85}8687public Map<String, Set<StratumLineInfo>> strata = new HashMap<String, Set<StratumLineInfo>>();8889@Override90public Object visitLabeledStatement(LabeledStatementTree node, Trees p) {91processLabel(node, p);92return super.visitLabeledStatement(node, p);93}9495private void processLabel(LabeledStatementTree node, Trees p) {96String label = node.getLabel().toString();9798if ( ! label.startsWith(LABEL_PREFIX) )99return;100101int stratumNameEndPos = label.indexOf('_', LABEL_PREFIX.length());102if ( stratumNameEndPos == -1 )103return;104105String stratumName = label.substring(LABEL_PREFIX.length(), stratumNameEndPos);106107int stratumLineEndPos = label.indexOf('_', stratumNameEndPos + 1);108if ( stratumLineEndPos == -1 )109return;110111String stratumLineNumStr = label.substring(stratumNameEndPos + 1, stratumLineEndPos);112int stratumLineNum = Integer.parseInt(stratumLineNumStr);113114String stratumLine = label.substring(stratumLineEndPos + 1);115116CompilationUnitTree unit = getCurrentPath().getCompilationUnit();117int javaLineNum = (int) unit.getLineMap().getLineNumber(p.getSourcePositions().getStartPosition(unit, node));118119Set<StratumLineInfo> stratumLines = this.strata.get(stratumName);120if ( stratumLines == null ) {121stratumLines = new TreeSet<StratumLineInfo>();122this.strata.put(stratumName, stratumLines);123}124125stratumLines.add(new StratumLineInfo(stratumName, stratumLineNum, stratumLine, javaLineNum));126}127}128129130