Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/jstat/ExpressionResolver.java
38918 views
/*1* Copyright (c) 2004, 2010, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.jstat;2627import sun.jvmstat.monitor.*;2829/**30* A class implementing the ExpressionEvaluator to resolve unresolved31* symbols in an Expression in the context of the available monitoring data.32* This class also performs some minimal optimizations of the expressions,33* such as simplification of constant subexpressions.34*35* @author Brian Doherty36* @since 1.537*/38public class ExpressionResolver implements ExpressionEvaluator {39private static boolean debug = Boolean.getBoolean("ExpressionResolver.debug");40private MonitoredVm vm;4142ExpressionResolver(MonitoredVm vm) {43this.vm = vm;44}4546/*47* evaluate the given expression. evaluation in this case means48* to resolve the counter names in the expression49*/50public Object evaluate(Expression e) throws MonitorException {5152if (e == null) {53return null;54}5556if (debug) {57System.out.println("Resolving Expression:" + e);58}5960if (e instanceof Identifier) {61Identifier id = (Identifier)e;6263// check if it's already resolved64if (id.isResolved()) {65return id;66}6768// look it up69Monitor m = vm.findByName(id.getName());70if (m == null) {71System.err.println("Warning: Unresolved Symbol: "72+ id.getName() + " substituted NaN");73return new Literal(new Double(Double.NaN));74}75if (m.getVariability() == Variability.CONSTANT) {76if (debug) {77System.out.println("Converting constant " + id.getName()78+ " to literal with value "79+ m.getValue());80}81return new Literal(m.getValue());82}83id.setValue(m);84return id;85}8687if (e instanceof Literal) {88return e;89}9091Expression l = null;92Expression r = null;9394if (e.getLeft() != null) {95l = (Expression)evaluate(e.getLeft());96}97if (e.getRight() != null) {98r = (Expression)evaluate(e.getRight());99}100101if (l != null && r != null) {102if ((l instanceof Literal) && (r instanceof Literal)) {103Literal ll = (Literal)l;104Literal rl = (Literal)r;105boolean warn = false;106107Double nan = new Double(Double.NaN);108if (ll.getValue() instanceof String) {109warn = true; ll.setValue(nan);110}111if (rl.getValue() instanceof String) {112warn = true; rl.setValue(nan);113}114if (debug && warn) {115System.out.println("Warning: String literal in "116+ "numerical expression: "117+ "substitutied NaN");118}119120// perform the operation121Number ln = (Number)ll.getValue();122Number rn = (Number)rl.getValue();123double result = e.getOperator().eval(ln.doubleValue(),124rn.doubleValue());125if (debug) {126System.out.println("Converting expression " + e127+ " (left = " + ln.doubleValue() + ")"128+ " (right = " + rn.doubleValue() + ")"129+ " to literal value " + result);130}131return new Literal(new Double(result));132}133}134135if (l != null && r == null) {136return l;137}138139e.setLeft(l);140e.setRight(r);141142return e;143}144}145146147