325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.codemodel.internal;
325N/A
325N/A
325N/A/**
325N/A * Factory methods that generate various {@link JExpression}s.
325N/A */
325N/Apublic abstract class JExpr {
325N/A
325N/A /**
325N/A * This class is not instanciable.
325N/A */
325N/A private JExpr() { }
325N/A
325N/A public static JExpression assign(JAssignmentTarget lhs, JExpression rhs) {
325N/A return new JAssignment(lhs, rhs);
325N/A }
325N/A
325N/A public static JExpression assignPlus(JAssignmentTarget lhs, JExpression rhs) {
325N/A return new JAssignment(lhs, rhs, "+");
325N/A }
325N/A
325N/A public static JInvocation _new(JClass c) {
325N/A return new JInvocation(c);
325N/A }
325N/A
325N/A public static JInvocation _new(JType t) {
325N/A return new JInvocation(t);
325N/A }
325N/A
325N/A public static JInvocation invoke(String method) {
325N/A return new JInvocation((JExpression)null, method);
325N/A }
325N/A
325N/A public static JInvocation invoke(JMethod method) {
325N/A return new JInvocation((JExpression)null,method);
325N/A }
325N/A
325N/A public static JInvocation invoke(JExpression lhs, JMethod method) {
325N/A return new JInvocation(lhs, method);
325N/A }
325N/A
325N/A public static JInvocation invoke(JExpression lhs, String method) {
325N/A return new JInvocation(lhs, method);
325N/A }
325N/A
325N/A public static JFieldRef ref(String field) {
325N/A return new JFieldRef((JExpression)null, field);
325N/A }
325N/A
325N/A public static JFieldRef ref(JExpression lhs, JVar field) {
325N/A return new JFieldRef(lhs,field);
325N/A }
325N/A
325N/A public static JFieldRef ref(JExpression lhs, String field) {
325N/A return new JFieldRef(lhs, field);
325N/A }
325N/A
325N/A public static JFieldRef refthis(String field) {
325N/A return new JFieldRef(null, field, true);
325N/A }
325N/A
325N/A public static JExpression dotclass(final JClass cl) {
325N/A return new JExpressionImpl() {
325N/A public void generate(JFormatter f) {
325N/A JClass c;
325N/A if(cl instanceof JNarrowedClass)
325N/A c = ((JNarrowedClass)cl).basis;
325N/A else
325N/A c = cl;
325N/A f.g(c).p(".class");
325N/A }
325N/A };
325N/A }
325N/A
325N/A public static JArrayCompRef component(JExpression lhs, JExpression index) {
325N/A return new JArrayCompRef(lhs, index);
325N/A }
325N/A
325N/A public static JCast cast(JType type, JExpression expr) {
325N/A return new JCast(type, expr);
325N/A }
325N/A
325N/A public static JArray newArray(JType type) {
325N/A return newArray(type,null);
325N/A }
325N/A
325N/A /**
325N/A * Generates {@code new T[size]}.
325N/A *
325N/A * @param type
325N/A * The type of the array component. 'T' or {@code new T[size]}.
325N/A */
325N/A public static JArray newArray(JType type, JExpression size) {
325N/A // you cannot create an array whose component type is a generic
325N/A return new JArray(type.erasure(), size);
325N/A }
325N/A
325N/A /**
325N/A * Generates {@code new T[size]}.
325N/A *
325N/A * @param type
325N/A * The type of the array component. 'T' or {@code new T[size]}.
325N/A */
325N/A public static JArray newArray(JType type, int size) {
325N/A return newArray(type,lit(size));
325N/A }
325N/A
325N/A
325N/A private static final JExpression __this = new JAtom("this");
325N/A /**
325N/A * Returns a reference to "this", an implicit reference
325N/A * to the current object.
325N/A */
325N/A public static JExpression _this() { return __this; }
325N/A
325N/A private static final JExpression __super = new JAtom("super");
325N/A /**
325N/A * Returns a reference to "super", an implicit reference
325N/A * to the super class.
325N/A */
325N/A public static JExpression _super() { return __super; }
325N/A
325N/A
325N/A /* -- Literals -- */
325N/A
325N/A private static final JExpression __null = new JAtom("null");
325N/A public static JExpression _null() {
325N/A return __null;
325N/A }
325N/A
325N/A /**
325N/A * Boolean constant that represents <code>true</code>
325N/A */
325N/A public static final JExpression TRUE = new JAtom("true");
325N/A
325N/A /**
325N/A * Boolean constant that represents <code>false</code>
325N/A */
325N/A public static final JExpression FALSE = new JAtom("false");
325N/A
325N/A public static JExpression lit(boolean b) {
325N/A return b?TRUE:FALSE;
325N/A }
325N/A
325N/A public static JExpression lit(int n) {
325N/A return new JAtom(Integer.toString(n));
325N/A }
325N/A
325N/A public static JExpression lit(long n) {
325N/A return new JAtom(Long.toString(n) + "L");
325N/A }
325N/A
325N/A public static JExpression lit(float f) {
325N/A if (f == Float.NEGATIVE_INFINITY)
325N/A {
325N/A return new JAtom("java.lang.Float.NEGATIVE_INFINITY");
325N/A }
325N/A else if (f == Float.POSITIVE_INFINITY)
325N/A {
325N/A return new JAtom("java.lang.Float.POSITIVE_INFINITY");
325N/A }
325N/A else if (Float.isNaN(f))
325N/A {
325N/A return new JAtom("java.lang.Float.NaN");
325N/A }
325N/A else
325N/A {
325N/A return new JAtom(Float.toString(f) + "F");
325N/A }
325N/A }
325N/A
325N/A public static JExpression lit(double d) {
325N/A if (d == Double.NEGATIVE_INFINITY)
325N/A {
325N/A return new JAtom("java.lang.Double.NEGATIVE_INFINITY");
325N/A }
325N/A else if (d == Double.POSITIVE_INFINITY)
325N/A {
325N/A return new JAtom("java.lang.Double.POSITIVE_INFINITY");
325N/A }
325N/A else if (Double.isNaN(d))
325N/A {
325N/A return new JAtom("java.lang.Double.NaN");
325N/A }
325N/A else
325N/A {
325N/A return new JAtom(Double.toString(d) + "D");
325N/A }
325N/A }
325N/A
325N/A static final String charEscape = "\b\t\n\f\r\"\'\\";
325N/A static final String charMacro = "btnfr\"'\\";
325N/A
325N/A /**
325N/A * Escapes the given string, then surrounds it by the specified
325N/A * quotation mark.
325N/A */
325N/A public static String quotify(char quote, String s) {
325N/A int n = s.length();
325N/A StringBuilder sb = new StringBuilder(n + 2);
325N/A sb.append(quote);
325N/A for (int i = 0; i < n; i++) {
325N/A char c = s.charAt(i);
325N/A int j = charEscape.indexOf(c);
325N/A if(j>=0) {
325N/A if((quote=='"' && c=='\'') || (quote=='\'' && c=='"')) {
325N/A sb.append(c);
325N/A } else {
325N/A sb.append('\\');
325N/A sb.append(charMacro.charAt(j));
325N/A }
325N/A } else {
325N/A // technically Unicode escape shouldn't be done here,
325N/A // for it's a lexical level handling.
325N/A //
325N/A // However, various tools are so broken around this area,
325N/A // so just to be on the safe side, it's better to do
325N/A // the escaping here (regardless of the actual file encoding)
325N/A //
325N/A // see bug
325N/A if( c<0x20 || 0x7E<c ) {
325N/A // not printable. use Unicode escape
325N/A sb.append("\\u");
325N/A String hex = Integer.toHexString(((int)c)&0xFFFF);
325N/A for( int k=hex.length(); k<4; k++ )
325N/A sb.append('0');
325N/A sb.append(hex);
325N/A } else {
325N/A sb.append(c);
325N/A }
325N/A }
325N/A }
325N/A sb.append(quote);
325N/A return sb.toString();
325N/A }
325N/A
325N/A public static JExpression lit(char c) {
325N/A return new JAtom(quotify('\'', "" + c));
325N/A }
325N/A
325N/A public static JExpression lit(String s) {
325N/A return new JStringLiteral(s);
325N/A }
325N/A
325N/A /**
325N/A * Creates an expression directly from a source code fragment.
325N/A *
325N/A * <p>
325N/A * This method can be used as a short-cut to create a JExpression.
325N/A * For example, instead of <code>_a.gt(_b)</code>, you can write
325N/A * it as: <code>JExpr.direct("a>b")</code>.
325N/A *
325N/A * <p>
325N/A * Be warned that there is a danger in using this method,
325N/A * as it obfuscates the object model.
325N/A */
325N/A public static JExpression direct( final String source ) {
325N/A return new JExpressionImpl(){
325N/A public void generate( JFormatter f ) {
325N/A f.p('(').p(source).p(')');
325N/A }
325N/A };
325N/A }
325N/A}