jdk/src/jdk.jcmd/share/classes/sun/tools/jstat/ExpressionResolver.java
author vinnie
Mon, 06 Oct 2014 16:44:57 +0100
changeset 26958 cb5cbde6e7a4
parent 25859 3317bb8137f4
child 37521 b6e0f285c998
permissions -rw-r--r--
8059627: Enable PKCS11 tests on Mac Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5627
diff changeset
     2
 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jstat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.jvmstat.monitor.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A class implementing the ExpressionEvaluator to resolve unresolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * symbols in an Expression in the context of the available monitoring data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This class also performs some minimal optimizations of the expressions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * such as simplification of constant subexpressions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Brian Doherty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class ExpressionResolver implements ExpressionEvaluator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static boolean debug = Boolean.getBoolean("ExpressionResolver.debug");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private MonitoredVm vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    ExpressionResolver(MonitoredVm vm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        this.vm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * evaluate the given expression. evaluation in this case means
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * to resolve the counter names in the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public Object evaluate(Expression e) throws MonitorException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            System.out.println("Resolving Expression:" + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (e instanceof Identifier) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            Identifier id = (Identifier)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // check if it's already resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (id.isResolved()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            // look it up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            Monitor m = vm.findByName(id.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (m == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                System.err.println("Warning: Unresolved Symbol: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                   + id.getName() + " substituted NaN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                return new Literal(new Double(Double.NaN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (m.getVariability() == Variability.CONSTANT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    System.out.println("Converting constant " + id.getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                       + " to literal with value "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                       + m.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                return new Literal(m.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            id.setValue(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (e instanceof Literal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Expression l = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Expression r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (e.getLeft() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            l = (Expression)evaluate(e.getLeft());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (e.getRight() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            r = (Expression)evaluate(e.getRight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (l != null && r != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if ((l instanceof Literal) && (r instanceof Literal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                Literal ll = (Literal)l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                Literal rl = (Literal)r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                boolean warn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                Double nan = new Double(Double.NaN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (ll.getValue() instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    warn = true; ll.setValue(nan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (rl.getValue() instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    warn = true; rl.setValue(nan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (debug && warn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                     System.out.println("Warning: String literal in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                        + "numerical expression: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                        + "substitutied NaN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // perform the operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                Number ln = (Number)ll.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                Number rn = (Number)rl.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                double result = e.getOperator().eval(ln.doubleValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                                     rn.doubleValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    System.out.println("Converting expression " + e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                                       + " (left = " + ln.doubleValue() + ")"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                                       + " (right = " + rn.doubleValue() + ")"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                       + " to literal value " + result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                return new Literal(new Double(result));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (l != null && r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        e.setLeft(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        e.setRight(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
}