Path: blob/master/src/jdk.jcmd/share/classes/sun/tools/jstat/ExpressionResolver.java
41159 views
/*1* Copyright (c) 2004, 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. 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) {71if (debug) {72System.err.println("Warning: Unresolved Symbol: "73+ id.getName() + " substituted NaN");74}75return new Literal(e.isRequired() ? 0.0d : Double.NaN);76}77if (m.getVariability() == Variability.CONSTANT) {78if (debug) {79System.out.println("Converting constant " + id.getName()80+ " to literal with value "81+ m.getValue());82}83return new Literal(m.getValue());84}85id.setValue(m);86return id;87}8889if (e instanceof Literal) {90return e;91}9293Expression l = null;94Expression r = null;9596if (e.getLeft() != null) {97l = (Expression)evaluate(e.getLeft());98}99if (e.getRight() != null) {100r = (Expression)evaluate(e.getRight());101}102103if (l != null && r != null) {104if ((l instanceof Literal) && (r instanceof Literal)) {105Literal ll = (Literal)l;106Literal rl = (Literal)r;107boolean warn = false;108109Double nan = Double.valueOf(Double.NaN);110if (ll.getValue() instanceof String) {111warn = true; ll.setValue(nan);112}113if (rl.getValue() instanceof String) {114warn = true; rl.setValue(nan);115}116if (debug && warn) {117System.out.println("Warning: String literal in "118+ "numerical expression: "119+ "substitutied NaN");120}121122// perform the operation123Number ln = (Number)ll.getValue();124Number rn = (Number)rl.getValue();125double result = e.getOperator().eval(ln.doubleValue(),126rn.doubleValue());127if (debug) {128System.out.println("Converting expression " + e129+ " (left = " + ln.doubleValue() + ")"130+ " (right = " + rn.doubleValue() + ")"131+ " to literal value " + result);132}133var literal = new Literal(result);134literal.setRequired(e.isRequired());135return literal;136}137}138139if (l != null && r == null) {140return l;141}142143e.setLeft(l);144e.setRight(r);145146return e;147}148}149150151