Package org.openquark.cal.internal.javamodel

Examples of org.openquark.cal.internal.javamodel.JavaExpression


                JavaField field = new JavaField.Instance (null, fieldName, fieldType);
                javaMethod.addStatement(new ReturnStatement(field));
            } else {
                // This class should throw an error for any access.  The methods will
                // be overridden by derived classes for each data constructor.
                JavaExpression getDCName =
                    new MethodInvocation.Instance(null, GET_DC_NAME_METHOD_NAME, JavaTypeName.STRING, MethodInvocation.InvocationType.VIRTUAL);
               
                JavaExpression message =
                    new JavaExpression.OperatorExpression.Binary(
                            JavaOperator.STRING_CONCATENATION,
                            LiteralWrapper.make ("Cannot access field " + fieldName + " on an instance of "),
                            getDCName);
               
                JavaExpression createException =
                    new JavaExpression.ClassInstanceCreationExpression (
                            UNSUPPORTED_OPERATION_TYPE_NAME,
                            message,
                            JavaTypeName.STRING);
               
View Full Code Here


                new JavaField.Instance (
                        null,
                        HASH_CODE_FIELD_NAME,
                        JavaTypeName.INT);
           
            JavaExpression condition =
                new OperatorExpression.Binary (
                        JavaOperator.EQUALS_INT,
                        hashCodeField,
                        LiteralWrapper.make(new Integer(0)));
           
            JavaStatement.Block thenBlock = new JavaStatement.Block();
           
            IfThenElseStatement ifThen =
                new IfThenElseStatement (condition, thenBlock);
           
            hashCode.addStatement (ifThen);
           
            // build up the hashcode value.
            JavaExpression.LocalVariable result =
                new LocalVariable ("result", JavaTypeName.INT);
            LocalVariableDeclaration localVarDecl =
                new LocalVariableDeclaration(result, LiteralWrapper.make(new Integer(17)));
            thenBlock.addStatement(localVarDecl);
           
            LiteralWrapper thirtySeven = LiteralWrapper.make (new Integer(37));
            JavaExpression thirtySevenTimesResult =
                new OperatorExpression.Binary(JavaOperator.MULTIPLY_INT, thirtySeven, result);

            LiteralWrapper zero = LiteralWrapper.make (new Integer (0));
            LiteralWrapper one  = LiteralWrapper.make (new Integer (1));

            // Start by including dc name in the hashcode.
            // get objects hashcode
            JavaExpression nameExpression =
                new MethodInvocation.Instance (
                        new MethodInvocation.Instance(
                                null,
                                GET_DC_NAME_METHOD_NAME,
                                JavaTypeName.STRING,
                                MethodInvocation.InvocationType.VIRTUAL),
                        "hashCode",
                        JavaTypeName.INT,
                        MethodInvocation.InvocationType.VIRTUAL);

            JavaExpression newResult =
                new OperatorExpression.Binary (JavaOperator.ADD_INT, thirtySevenTimesResult, nameExpression);
            JavaExpression assignResult = new Assignment (result, newResult);
            thenBlock.addStatement(new ExpressionStatement (assignResult));
           
            // Now get the contribution from each dc field.
            for (FieldName fn : fieldNames) {
                String javaFieldName = typeConstructorInfo.fieldJavaNames.get (fn);
                JavaTypeName fieldType = fieldNameToType.get (fn);
                JavaField field = new JavaField.Instance(null, javaFieldName, fieldType);
               
                JavaExpression fieldExpression;
                if (fieldType instanceof JavaTypeName.Primitive) {
                    if (fieldType instanceof JavaTypeName.Primitive.Boolean) {
                        fieldExpression =
                            new OperatorExpression.Ternary (field, zero, one);
                    } else if (fieldType instanceof JavaTypeName.Primitive.Byte ||
                            fieldType instanceof JavaTypeName.Primitive.Char ||
                            fieldType instanceof JavaTypeName.Primitive.Short) {
                        fieldExpression =
                            new CastExpression(JavaTypeName.INT, field);
                    }else if (fieldType instanceof JavaTypeName.Primitive.Double) {
                        // long f = Double.doubleToLongBits(f);
                        // result = (int) (f ^ (f >>> 32));
                        JavaExpression.LocalVariable f =
                            new LocalVariable ("f", JavaTypeName.LONG);
                        JavaExpression initializeF =
                            new MethodInvocation.Static (
                                    JavaTypeName.DOUBLE_OBJECT,
                                    "doubleToLongBits",
                                    field,
                                    JavaTypeName.DOUBLE,
                                    JavaTypeName.LONG);
                        LocalVariableDeclaration fVarDecl =
                            new LocalVariableDeclaration(f, initializeF);
                        thenBlock.addStatement(fVarDecl);
                       
                        fieldExpression =
                            new OperatorExpression.Binary (
                                JavaOperator.SHIFTR_UNSIGNED_LONG,
                                f,
                                LiteralWrapper.make(new Integer (32)));
                        fieldExpression =
                            new OperatorExpression.Binary (
                                    JavaOperator.BITWISE_XOR_LONG,
                                    f,
                                    fieldExpression);
                        fieldExpression =
                            new CastExpression (JavaTypeName.INT, fieldExpression);
                       
                    } else if (fieldType instanceof JavaTypeName.Primitive.Float) {
                        fieldExpression =
                            new MethodInvocation.Static (
                                    JavaTypeName.FLOAT_OBJECT,
                                    "floatToIntBits",
                                    field,
                                    JavaTypeName.FLOAT,
                                    JavaTypeName.INT);
                    } else if (fieldType instanceof JavaTypeName.Primitive.Int) {
                        fieldExpression = field;
                    } else if (fieldType instanceof JavaTypeName.Primitive.Long) {
                        fieldExpression =
                            new OperatorExpression.Binary (
                                JavaOperator.SHIFTR_UNSIGNED_LONG,
                                field,
                                LiteralWrapper.make(new Integer (32)));
                        fieldExpression =
                            new OperatorExpression.Binary (
                                    JavaOperator.BITWISE_XOR_LONG,
                                    field,
                                    fieldExpression);
                        fieldExpression =
                            new CastExpression (JavaTypeName.INT, fieldExpression);
                    } else {
                        fieldExpression =
                            new MethodInvocation.Instance (field, "hashCode", JavaTypeName.INT, MethodInvocation.InvocationType.VIRTUAL);
                    }
                   
                } else {
                    // get objects hashcode
                    fieldExpression =
                        new MethodInvocation.Instance (field, "hashCode", JavaTypeName.INT, MethodInvocation.InvocationType.VIRTUAL);
                }

                newResult =
                    new OperatorExpression.Binary (JavaOperator.ADD_INT, thirtySevenTimesResult, fieldExpression);
               
                assignResult = new Assignment (result, newResult);
                thenBlock.addStatement(new ExpressionStatement (assignResult));
            }
           
            // Assign the hash code value to the hashCode field.
            JavaExpression assign = new JavaExpression.Assignment(hashCodeField, result);
            thenBlock.addStatement(new ExpressionStatement (assign));
           
            // Return the initialized hashCode field.
            hashCode.addStatement (new ReturnStatement (hashCodeField));
           
View Full Code Here

            JavaField thisField = new JavaField.This(typeName);
           
            // if (object == this) {
            //     return true;
            // }
            JavaExpression condition =
                new JavaExpression.OperatorExpression.Binary(
                        JavaOperator.EQUALS_OBJECT,
                        thisField,
                        objectArg);
            JavaStatement.IfThenElseStatement ifThen =
                new IfThenElseStatement(
                        condition,
                        new ReturnStatement(LiteralWrapper.TRUE));
           
            equals.addStatement(ifThen);

            // if (object == null) {
            //     return false;
            // }
            condition =
                new JavaExpression.OperatorExpression.Binary(JavaOperator.EQUALS_OBJECT, objectArg, LiteralWrapper.NULL);
            ifThen =
                new IfThenElseStatement (condition, new ReturnStatement(LiteralWrapper.FALSE));
            equals.addStatement(ifThen);
           
            // if (!(object instanceOf ThisType)) {
            //     return false;
            // }
            condition =
                new JavaExpression.InstanceOf(objectArg, typeName);
            condition =
                new OperatorExpression.Unary(JavaOperator.LOGICAL_NEGATE, condition);
            ifThen =
                new IfThenElseStatement (
                        condition,
                        new ReturnStatement(LiteralWrapper.FALSE));
            equals.addStatement(ifThen);
           
            // ThisType castObject = (ThisType)object;
            LocalVariable localVar =
                new LocalVariable ("cast" + argName, typeName);
           
            JavaStatement localVarDecl =
                new JavaStatement.LocalVariableDeclaration (
                        localVar,
                        new JavaExpression.CastExpression(typeName, objectArg));
            equals.addStatement (localVarDecl);
           

            // Check DC name
            // if (!getDCName().equals(castObject.getDCName())) {
            //     return false;
            // }
            JavaExpression thisGetDCName =
                new MethodInvocation.Instance(null, GET_DC_NAME_METHOD_NAME, JavaTypeName.STRING, MethodInvocation.InvocationType.VIRTUAL);
            JavaExpression otherGetDCName =
                new MethodInvocation.Instance(localVar, GET_DC_NAME_METHOD_NAME, JavaTypeName.STRING, MethodInvocation.InvocationType.VIRTUAL);
            JavaExpression compareDCNames =
                new MethodInvocation.Instance(thisGetDCName, "equals", otherGetDCName, JavaTypeName.OBJECT, JavaTypeName.BOOLEAN, MethodInvocation.InvocationType.VIRTUAL);
            compareDCNames = new OperatorExpression.Unary(JavaOperator.LOGICAL_NEGATE, compareDCNames);

            ifThen = new IfThenElseStatement(compareDCNames, new ReturnStatement(LiteralWrapper.FALSE));
            equals.addStatement(ifThen);
           
            if (fieldNames != null && fieldNames.size() > 0) {
                // Now we need to compare equality for the various fields.
                Iterator<FieldName> fields = fieldNames.iterator();
                FieldName fn = fields.next ();
                JavaTypeName fieldType = (JavaTypeName)fieldNameToType.get(fn);
                JavaExpression compare = makeFieldComparison (fn, fieldType, localVar);
               
                while (fields.hasNext()) {
                    fn = fields.next ();
                    fieldType = (JavaTypeName)fieldNameToType.get(fn);
                    JavaExpression nextCompare = makeFieldComparison (fn, fieldType, localVar);
                   
                    compare =
                        new OperatorExpression.Binary (JavaOperator.CONDITIONAL_AND, compare, nextCompare);
                }
                equals.addStatement(new ReturnStatement(compare));
View Full Code Here

                JavaTypeName fieldType,
                JavaExpression other) {

            String javaFieldName = (String)typeConstructorInfo.fieldJavaNames.get(fieldName);

            JavaExpression thisField = new JavaField.Instance (null, javaFieldName, fieldType);
            JavaExpression otherField = new JavaField.Instance (other, javaFieldName, fieldType);
           
            if (fieldType instanceof JavaTypeName.Primitive) {
                // We need to use the correct version of the
                // == operator.
                // this.field == other.field
               
                JavaOperator operator;
               
                if (fieldType instanceof JavaTypeName.Primitive.Boolean) {
                    operator = JavaOperator.CONDITIONAL_AND;
                } else if (fieldType instanceof JavaTypeName.Primitive.Byte) {
                    operator = JavaOperator.EQUALS_BYTE;
                } else if (fieldType instanceof JavaTypeName.Primitive.Char) {
                    operator = JavaOperator.EQUALS_CHAR;
                } else if (fieldType instanceof JavaTypeName.Primitive.Double) {
                    operator = JavaOperator.EQUALS_BYTE;
                } else if (fieldType instanceof JavaTypeName.Primitive.Float) {
                    operator = JavaOperator.EQUALS_FLOAT;
                } else if (fieldType instanceof JavaTypeName.Primitive.Int) {
                    operator = JavaOperator.EQUALS_INT;
                } else if (fieldType instanceof JavaTypeName.Primitive.Long) {
                    operator = JavaOperator.EQUALS_LONG;
                } else if (fieldType instanceof JavaTypeName.Primitive.Short) {
                    operator = JavaOperator.EQUALS_SHORT;
                }else {
                    operator = JavaOperator.EQUALS_OBJECT;
                }
               
                JavaExpression compare =
                    new OperatorExpression.Binary (operator, thisField, otherField);
               
                return compare;
            } else {
                // Invoke the equals method.
                // (this.field == null) ? (other.field == null) | this.field.equals(other.field)
               
               
                JavaExpression condition =
                    new OperatorExpression.Binary (
                            JavaOperator.EQUALS_OBJECT,
                            thisField,
                            LiteralWrapper.NULL);
               
                JavaExpression then =
                    new OperatorExpression.Binary (
                            JavaOperator.EQUALS_OBJECT,
                            otherField,
                            LiteralWrapper.NULL);
               
                JavaExpression elsePart =
                    new MethodInvocation.Instance (
                            thisField,
                            "equals",
                            otherField,
                            JavaTypeName.OBJECT,
                            JavaTypeName.BOOLEAN,
                            MethodInvocation.InvocationType.VIRTUAL);
               
                JavaExpression ternary =
                    new OperatorExpression.Ternary (
                            condition,
                            then,
                            elsePart);
               
View Full Code Here

                new JavaMethod(
                        Modifier.PUBLIC | Modifier.FINAL,
                        JavaTypeName.STRING,
                        "toString");
           
            JavaExpression getDCName =
                new JavaExpression.MethodInvocation.Instance(
                        null,
                        GET_DC_NAME_METHOD_NAME,
                        JavaTypeName.STRING,
                        MethodInvocation.InvocationType.VIRTUAL);
           
            JavaExpression message =
                new JavaExpression.OperatorExpression.Binary(
                        JavaOperator.STRING_CONCATENATION,
                        getDCName,
                        LiteralWrapper.make("\n"));
           
            for (FieldName fn : fieldNames) {
                // There will only be one field type.
                JavaTypeName fieldType = (JavaTypeName)fieldTypes.get(fn);
                String javaFieldName = (String)typeConstructorInfo.fieldJavaNames.get(fn);
                JavaExpression field =
                    new JavaField.Instance(null, javaFieldName, fieldType);
               
                JavaExpression fieldString;
                if (fieldType instanceof JavaTypeName.Primitive ||
                        fieldType.equals(JavaTypeName.STRING)) {
                    fieldString = field;
                } else {
                    fieldString =
View Full Code Here

                        new JavaMethod(
                                Modifier.PUBLIC | Modifier.FINAL,
                                JavaTypeName.INT,
                                "hashCode");
                   
                    JavaExpression call_getOrdinal =
                        new MethodInvocation.Instance(
                                null,
                                GET_DC_ORDINAL_METHOD_NAME,
                                JavaTypeName.INT,
                                MethodInvocation.InvocationType.VIRTUAL);
View Full Code Here

                        new JavaMethod(
                                Modifier.PUBLIC | Modifier.FINAL,
                                JavaTypeName.STRING,
                                "toString");
                   
                    JavaExpression getDCName =
                        new JavaExpression.MethodInvocation.Instance(
                                null,
                                GET_DC_NAME_METHOD_NAME,
                                JavaTypeName.STRING,
                                MethodInvocation.InvocationType.VIRTUAL);
View Full Code Here

                                new ReturnStatement(field));
                    switchStatement.addCase(intCase);
                }
               
                // Throw an error if ordinal is outside accepted range.
                JavaExpression message =
                    new JavaExpression.OperatorExpression.Binary(
                            JavaOperator.STRING_CONCATENATION,
                            LiteralWrapper.make ("Invalid ordinal " ),
                            new MethodVariable("ordinal"));
               
                message =
                    new JavaExpression.OperatorExpression.Binary(
                            JavaOperator.STRING_CONCATENATION,
                            message,
                            LiteralWrapper.make(" for data type " + typeConstructorInfo.typeConstructor.getName().getUnqualifiedName()));
               
                JavaExpression createException =
                    new JavaExpression.ClassInstanceCreationExpression (
                            UNSUPPORTED_OPERATION_TYPE_NAME,
                            message,
                            JavaTypeName.STRING);
               
View Full Code Here

                    argNames[i] = argName;
                    argTypes[i] = type;
                   
                    JavaExpression.JavaField.Instance field =
                        new JavaExpression.JavaField.Instance(null, fieldName, type);
                    JavaExpression assign = new Assignment (field, new MethodVariable(argName));
                    constructorBody.addStatement(new ExpressionStatement(assign));
                    i++;
                }
               
                String constructorName = className.getUnqualifiedJavaSourceName();
View Full Code Here

                                Modifier.PRIVATE,
                                new String[]{"ordinal", "name"},
                                new JavaTypeName[]{JavaTypeName.INT, JavaTypeName.STRING},
                                className.getUnqualifiedJavaSourceName());
                   
                    JavaExpression assignOrdinal =
                        new JavaExpression.Assignment (
                                new JavaField.Instance(null, ORDINAL_FIELD_NAME, JavaTypeName.INT),
                                new MethodVariable("ordinal"));
                    constructor.addStatement(new ExpressionStatement(assignOrdinal));
                   
                    JavaExpression assignName =
                        new JavaExpression.Assignment (
                                new JavaField.Instance(null, DC_NAME_FIELD_NAME, JavaTypeName.STRING),
                                new MethodVariable("name"));
                    constructor.addStatement(new ExpressionStatement(assignName));
                   
View Full Code Here

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaExpression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.