Thu, 12 Oct 2017 19:44:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.codemodel.internal; |
aoqi@0 | 27 | |
aoqi@0 | 28 | |
aoqi@0 | 29 | /** |
aoqi@0 | 30 | * Factory methods that generate various {@link JExpression}s. |
aoqi@0 | 31 | */ |
aoqi@0 | 32 | public abstract class JExpr { |
aoqi@0 | 33 | |
aoqi@0 | 34 | /** |
aoqi@0 | 35 | * This class is not instanciable. |
aoqi@0 | 36 | */ |
aoqi@0 | 37 | private JExpr() { } |
aoqi@0 | 38 | |
aoqi@0 | 39 | public static JExpression assign(JAssignmentTarget lhs, JExpression rhs) { |
aoqi@0 | 40 | return new JAssignment(lhs, rhs); |
aoqi@0 | 41 | } |
aoqi@0 | 42 | |
aoqi@0 | 43 | public static JExpression assignPlus(JAssignmentTarget lhs, JExpression rhs) { |
aoqi@0 | 44 | return new JAssignment(lhs, rhs, "+"); |
aoqi@0 | 45 | } |
aoqi@0 | 46 | |
aoqi@0 | 47 | public static JInvocation _new(JClass c) { |
aoqi@0 | 48 | return new JInvocation(c); |
aoqi@0 | 49 | } |
aoqi@0 | 50 | |
aoqi@0 | 51 | public static JInvocation _new(JType t) { |
aoqi@0 | 52 | return new JInvocation(t); |
aoqi@0 | 53 | } |
aoqi@0 | 54 | |
aoqi@0 | 55 | public static JInvocation invoke(String method) { |
aoqi@0 | 56 | return new JInvocation((JExpression)null, method); |
aoqi@0 | 57 | } |
aoqi@0 | 58 | |
aoqi@0 | 59 | public static JInvocation invoke(JMethod method) { |
aoqi@0 | 60 | return new JInvocation((JExpression)null,method); |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | public static JInvocation invoke(JExpression lhs, JMethod method) { |
aoqi@0 | 64 | return new JInvocation(lhs, method); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | |
aoqi@0 | 67 | public static JInvocation invoke(JExpression lhs, String method) { |
aoqi@0 | 68 | return new JInvocation(lhs, method); |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | public static JFieldRef ref(String field) { |
aoqi@0 | 72 | return new JFieldRef((JExpression)null, field); |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | public static JFieldRef ref(JExpression lhs, JVar field) { |
aoqi@0 | 76 | return new JFieldRef(lhs,field); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | public static JFieldRef ref(JExpression lhs, String field) { |
aoqi@0 | 80 | return new JFieldRef(lhs, field); |
aoqi@0 | 81 | } |
aoqi@0 | 82 | |
aoqi@0 | 83 | public static JFieldRef refthis(String field) { |
aoqi@0 | 84 | return new JFieldRef(null, field, true); |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | public static JExpression dotclass(final JClass cl) { |
aoqi@0 | 88 | return new JExpressionImpl() { |
aoqi@0 | 89 | public void generate(JFormatter f) { |
aoqi@0 | 90 | JClass c; |
aoqi@0 | 91 | if(cl instanceof JNarrowedClass) |
aoqi@0 | 92 | c = ((JNarrowedClass)cl).basis; |
aoqi@0 | 93 | else |
aoqi@0 | 94 | c = cl; |
aoqi@0 | 95 | f.g(c).p(".class"); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | }; |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | public static JArrayCompRef component(JExpression lhs, JExpression index) { |
aoqi@0 | 101 | return new JArrayCompRef(lhs, index); |
aoqi@0 | 102 | } |
aoqi@0 | 103 | |
aoqi@0 | 104 | public static JCast cast(JType type, JExpression expr) { |
aoqi@0 | 105 | return new JCast(type, expr); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | public static JArray newArray(JType type) { |
aoqi@0 | 109 | return newArray(type,null); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | /** |
aoqi@0 | 113 | * Generates {@code new T[size]}. |
aoqi@0 | 114 | * |
aoqi@0 | 115 | * @param type |
aoqi@0 | 116 | * The type of the array component. 'T' or {@code new T[size]}. |
aoqi@0 | 117 | */ |
aoqi@0 | 118 | public static JArray newArray(JType type, JExpression size) { |
aoqi@0 | 119 | // you cannot create an array whose component type is a generic |
aoqi@0 | 120 | return new JArray(type.erasure(), size); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | /** |
aoqi@0 | 124 | * Generates {@code new T[size]}. |
aoqi@0 | 125 | * |
aoqi@0 | 126 | * @param type |
aoqi@0 | 127 | * The type of the array component. 'T' or {@code new T[size]}. |
aoqi@0 | 128 | */ |
aoqi@0 | 129 | public static JArray newArray(JType type, int size) { |
aoqi@0 | 130 | return newArray(type,lit(size)); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | |
aoqi@0 | 134 | private static final JExpression __this = new JAtom("this"); |
aoqi@0 | 135 | /** |
aoqi@0 | 136 | * Returns a reference to "this", an implicit reference |
aoqi@0 | 137 | * to the current object. |
aoqi@0 | 138 | */ |
aoqi@0 | 139 | public static JExpression _this() { return __this; } |
aoqi@0 | 140 | |
aoqi@0 | 141 | private static final JExpression __super = new JAtom("super"); |
aoqi@0 | 142 | /** |
aoqi@0 | 143 | * Returns a reference to "super", an implicit reference |
aoqi@0 | 144 | * to the super class. |
aoqi@0 | 145 | */ |
aoqi@0 | 146 | public static JExpression _super() { return __super; } |
aoqi@0 | 147 | |
aoqi@0 | 148 | |
aoqi@0 | 149 | /* -- Literals -- */ |
aoqi@0 | 150 | |
aoqi@0 | 151 | private static final JExpression __null = new JAtom("null"); |
aoqi@0 | 152 | public static JExpression _null() { |
aoqi@0 | 153 | return __null; |
aoqi@0 | 154 | } |
aoqi@0 | 155 | |
aoqi@0 | 156 | /** |
aoqi@0 | 157 | * Boolean constant that represents <code>true</code> |
aoqi@0 | 158 | */ |
aoqi@0 | 159 | public static final JExpression TRUE = new JAtom("true"); |
aoqi@0 | 160 | |
aoqi@0 | 161 | /** |
aoqi@0 | 162 | * Boolean constant that represents <code>false</code> |
aoqi@0 | 163 | */ |
aoqi@0 | 164 | public static final JExpression FALSE = new JAtom("false"); |
aoqi@0 | 165 | |
aoqi@0 | 166 | public static JExpression lit(boolean b) { |
aoqi@0 | 167 | return b?TRUE:FALSE; |
aoqi@0 | 168 | } |
aoqi@0 | 169 | |
aoqi@0 | 170 | public static JExpression lit(int n) { |
aoqi@0 | 171 | return new JAtom(Integer.toString(n)); |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | public static JExpression lit(long n) { |
aoqi@0 | 175 | return new JAtom(Long.toString(n) + "L"); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | public static JExpression lit(float f) { |
aoqi@0 | 179 | if (f == Float.NEGATIVE_INFINITY) |
aoqi@0 | 180 | { |
aoqi@0 | 181 | return new JAtom("java.lang.Float.NEGATIVE_INFINITY"); |
aoqi@0 | 182 | } |
aoqi@0 | 183 | else if (f == Float.POSITIVE_INFINITY) |
aoqi@0 | 184 | { |
aoqi@0 | 185 | return new JAtom("java.lang.Float.POSITIVE_INFINITY"); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | else if (Float.isNaN(f)) |
aoqi@0 | 188 | { |
aoqi@0 | 189 | return new JAtom("java.lang.Float.NaN"); |
aoqi@0 | 190 | } |
aoqi@0 | 191 | else |
aoqi@0 | 192 | { |
aoqi@0 | 193 | return new JAtom(Float.toString(f) + "F"); |
aoqi@0 | 194 | } |
aoqi@0 | 195 | } |
aoqi@0 | 196 | |
aoqi@0 | 197 | public static JExpression lit(double d) { |
aoqi@0 | 198 | if (d == Double.NEGATIVE_INFINITY) |
aoqi@0 | 199 | { |
aoqi@0 | 200 | return new JAtom("java.lang.Double.NEGATIVE_INFINITY"); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | else if (d == Double.POSITIVE_INFINITY) |
aoqi@0 | 203 | { |
aoqi@0 | 204 | return new JAtom("java.lang.Double.POSITIVE_INFINITY"); |
aoqi@0 | 205 | } |
aoqi@0 | 206 | else if (Double.isNaN(d)) |
aoqi@0 | 207 | { |
aoqi@0 | 208 | return new JAtom("java.lang.Double.NaN"); |
aoqi@0 | 209 | } |
aoqi@0 | 210 | else |
aoqi@0 | 211 | { |
aoqi@0 | 212 | return new JAtom(Double.toString(d) + "D"); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | } |
aoqi@0 | 215 | |
aoqi@0 | 216 | static final String charEscape = "\b\t\n\f\r\"\'\\"; |
aoqi@0 | 217 | static final String charMacro = "btnfr\"'\\"; |
aoqi@0 | 218 | |
aoqi@0 | 219 | /** |
aoqi@0 | 220 | * Escapes the given string, then surrounds it by the specified |
aoqi@0 | 221 | * quotation mark. |
aoqi@0 | 222 | */ |
aoqi@0 | 223 | public static String quotify(char quote, String s) { |
aoqi@0 | 224 | int n = s.length(); |
aoqi@0 | 225 | StringBuilder sb = new StringBuilder(n + 2); |
aoqi@0 | 226 | sb.append(quote); |
aoqi@0 | 227 | for (int i = 0; i < n; i++) { |
aoqi@0 | 228 | char c = s.charAt(i); |
aoqi@0 | 229 | int j = charEscape.indexOf(c); |
aoqi@0 | 230 | if(j>=0) { |
aoqi@0 | 231 | if((quote=='"' && c=='\'') || (quote=='\'' && c=='"')) { |
aoqi@0 | 232 | sb.append(c); |
aoqi@0 | 233 | } else { |
aoqi@0 | 234 | sb.append('\\'); |
aoqi@0 | 235 | sb.append(charMacro.charAt(j)); |
aoqi@0 | 236 | } |
aoqi@0 | 237 | } else { |
aoqi@0 | 238 | // technically Unicode escape shouldn't be done here, |
aoqi@0 | 239 | // for it's a lexical level handling. |
aoqi@0 | 240 | // |
aoqi@0 | 241 | // However, various tools are so broken around this area, |
aoqi@0 | 242 | // so just to be on the safe side, it's better to do |
aoqi@0 | 243 | // the escaping here (regardless of the actual file encoding) |
aoqi@0 | 244 | // |
aoqi@0 | 245 | // see bug |
aoqi@0 | 246 | if( c<0x20 || 0x7E<c ) { |
aoqi@0 | 247 | // not printable. use Unicode escape |
aoqi@0 | 248 | sb.append("\\u"); |
aoqi@0 | 249 | String hex = Integer.toHexString(((int)c)&0xFFFF); |
aoqi@0 | 250 | for( int k=hex.length(); k<4; k++ ) |
aoqi@0 | 251 | sb.append('0'); |
aoqi@0 | 252 | sb.append(hex); |
aoqi@0 | 253 | } else { |
aoqi@0 | 254 | sb.append(c); |
aoqi@0 | 255 | } |
aoqi@0 | 256 | } |
aoqi@0 | 257 | } |
aoqi@0 | 258 | sb.append(quote); |
aoqi@0 | 259 | return sb.toString(); |
aoqi@0 | 260 | } |
aoqi@0 | 261 | |
aoqi@0 | 262 | public static JExpression lit(char c) { |
aoqi@0 | 263 | return new JAtom(quotify('\'', "" + c)); |
aoqi@0 | 264 | } |
aoqi@0 | 265 | |
aoqi@0 | 266 | public static JExpression lit(String s) { |
aoqi@0 | 267 | return new JStringLiteral(s); |
aoqi@0 | 268 | } |
aoqi@0 | 269 | |
aoqi@0 | 270 | /** |
aoqi@0 | 271 | * Creates an expression directly from a source code fragment. |
aoqi@0 | 272 | * |
aoqi@0 | 273 | * <p> |
aoqi@0 | 274 | * This method can be used as a short-cut to create a JExpression. |
aoqi@0 | 275 | * For example, instead of <code>_a.gt(_b)</code>, you can write |
aoqi@0 | 276 | * it as: <code>JExpr.direct("a>b")</code>. |
aoqi@0 | 277 | * |
aoqi@0 | 278 | * <p> |
aoqi@0 | 279 | * Be warned that there is a danger in using this method, |
aoqi@0 | 280 | * as it obfuscates the object model. |
aoqi@0 | 281 | */ |
aoqi@0 | 282 | public static JExpression direct( final String source ) { |
aoqi@0 | 283 | return new JExpressionImpl(){ |
aoqi@0 | 284 | public void generate( JFormatter f ) { |
aoqi@0 | 285 | f.p('(').p(source).p(')'); |
aoqi@0 | 286 | } |
aoqi@0 | 287 | }; |
aoqi@0 | 288 | } |
aoqi@0 | 289 | } |