src/share/jaxws_classes/com/sun/codemodel/internal/JOp.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.codemodel.internal;
    29 /**
    30  * JClass for generating expressions containing operators
    31  */
    33 abstract public class JOp {
    35     private JOp() {
    36     }
    39     /**
    40      * Determine whether the top level of an expression involves an
    41      * operator.
    42      */
    43     static boolean hasTopOp(JExpression e) {
    44         return (e instanceof UnaryOp) || (e instanceof BinaryOp);
    45     }
    47     /* -- Unary operators -- */
    49     static private class UnaryOp extends JExpressionImpl {
    51         protected String op;
    52         protected JExpression e;
    53         protected boolean opFirst = true;
    55         UnaryOp(String op, JExpression e) {
    56             this.op = op;
    57             this.e = e;
    58         }
    60         UnaryOp(JExpression e, String op) {
    61             this.op = op;
    62             this.e = e;
    63             opFirst = false;
    64         }
    66         public void generate(JFormatter f) {
    67             if (opFirst)
    68                 f.p('(').p(op).g(e).p(')');
    69             else
    70                 f.p('(').g(e).p(op).p(')');
    71         }
    73     }
    75     public static JExpression minus(JExpression e) {
    76         return new UnaryOp("-", e);
    77     }
    79     /**
    80      * Logical not <tt>'!x'</tt>.
    81      */
    82     public static JExpression not(JExpression e) {
    83         if (e == JExpr.TRUE) return JExpr.FALSE;
    84         if (e == JExpr.FALSE) return JExpr.TRUE;
    85         return new UnaryOp("!", e);
    86     }
    88     public static JExpression complement(JExpression e) {
    89         return new UnaryOp("~", e);
    90     }
    92     static private class TightUnaryOp extends UnaryOp {
    94         TightUnaryOp(JExpression e, String op) {
    95             super(e, op);
    96         }
    98         public void generate(JFormatter f) {
    99             if (opFirst)
   100                 f.p(op).g(e);
   101             else
   102                 f.g(e).p(op);
   103         }
   105     }
   107     public static JExpression incr(JExpression e) {
   108         return new TightUnaryOp(e, "++");
   109     }
   111     public static JExpression decr(JExpression e) {
   112         return new TightUnaryOp(e, "--");
   113     }
   116     /* -- Binary operators -- */
   118     static private class BinaryOp extends JExpressionImpl {
   120         String op;
   121         JExpression left;
   122         JGenerable right;
   124         BinaryOp(String op, JExpression left, JGenerable right) {
   125             this.left = left;
   126             this.op = op;
   127             this.right = right;
   128         }
   130         public void generate(JFormatter f) {
   131             f.p('(').g(left).p(op).g(right).p(')');
   132         }
   134     }
   136     public static JExpression plus(JExpression left, JExpression right) {
   137         return new BinaryOp("+", left, right);
   138     }
   140     public static JExpression minus(JExpression left, JExpression right) {
   141         return new BinaryOp("-", left, right);
   142     }
   144     public static JExpression mul(JExpression left, JExpression right) {
   145         return new BinaryOp("*", left, right);
   146     }
   148     public static JExpression div(JExpression left, JExpression right) {
   149         return new BinaryOp("/", left, right);
   150     }
   152     public static JExpression mod(JExpression left, JExpression right) {
   153         return new BinaryOp("%", left, right);
   154     }
   156     public static JExpression shl(JExpression left, JExpression right) {
   157         return new BinaryOp("<<", left, right);
   158     }
   160     public static JExpression shr(JExpression left, JExpression right) {
   161         return new BinaryOp(">>", left, right);
   162     }
   164     public static JExpression shrz(JExpression left, JExpression right) {
   165         return new BinaryOp(">>>", left, right);
   166     }
   168     public static JExpression band(JExpression left, JExpression right) {
   169         return new BinaryOp("&", left, right);
   170     }
   172     public static JExpression bor(JExpression left, JExpression right) {
   173         return new BinaryOp("|", left, right);
   174     }
   176     public static JExpression cand(JExpression left, JExpression right) {
   177         if (left == JExpr.TRUE) return right;
   178         if (right == JExpr.TRUE) return left;
   179         if (left == JExpr.FALSE) return left;    // JExpr.FALSE
   180         if (right == JExpr.FALSE) return right;   // JExpr.FALSE
   181         return new BinaryOp("&&", left, right);
   182     }
   184     public static JExpression cor(JExpression left, JExpression right) {
   185         if (left == JExpr.TRUE) return left;    // JExpr.TRUE
   186         if (right == JExpr.TRUE) return right;   // JExpr.FALSE
   187         if (left == JExpr.FALSE) return right;
   188         if (right == JExpr.FALSE) return left;
   189         return new BinaryOp("||", left, right);
   190     }
   192     public static JExpression xor(JExpression left, JExpression right) {
   193         return new BinaryOp("^", left, right);
   194     }
   196     public static JExpression lt(JExpression left, JExpression right) {
   197         return new BinaryOp("<", left, right);
   198     }
   200     public static JExpression lte(JExpression left, JExpression right) {
   201         return new BinaryOp("<=", left, right);
   202     }
   204     public static JExpression gt(JExpression left, JExpression right) {
   205         return new BinaryOp(">", left, right);
   206     }
   208     public static JExpression gte(JExpression left, JExpression right) {
   209         return new BinaryOp(">=", left, right);
   210     }
   212     public static JExpression eq(JExpression left, JExpression right) {
   213         return new BinaryOp("==", left, right);
   214     }
   216     public static JExpression ne(JExpression left, JExpression right) {
   217         return new BinaryOp("!=", left, right);
   218     }
   220     public static JExpression _instanceof(JExpression left, JType right) {
   221         return new BinaryOp("instanceof", left, right);
   222     }
   224     /* -- Ternary operators -- */
   226     static private class TernaryOp extends JExpressionImpl {
   228         String op1;
   229         String op2;
   230         JExpression e1;
   231         JExpression e2;
   232         JExpression e3;
   234         TernaryOp(String op1, String op2,
   235                   JExpression e1, JExpression e2, JExpression e3) {
   236             this.e1 = e1;
   237             this.op1 = op1;
   238             this.e2 = e2;
   239             this.op2 = op2;
   240             this.e3 = e3;
   241         }
   243         public void generate(JFormatter f) {
   244             f.p('(').g(e1).p(op1).g(e2).p(op2).g(e3).p(')');
   245         }
   247     }
   249     public static JExpression cond(JExpression cond,
   250                                    JExpression ifTrue, JExpression ifFalse) {
   251         return new TernaryOp("?", ":", cond, ifTrue, ifFalse);
   252     }
   254 }

mercurial