Package org.openquark.cal.internal.javamodel

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


                    String javaFieldName = (String)typeConstructorInfo.fieldJavaNames.get(fieldName);
                    Set<JavaTypeName> fieldTypes = typeConstructorInfo.allFieldTypeNames.get(fieldName);
                   
                    for (JavaTypeName type  : fieldTypes) {
                        String accessorMethodName = (String)typeConstructorInfo.fieldJavaAccessorMethodNames.get(fieldName);
                        JavaMethod getter = createMethod_getField (
                                accessorMethodName,
                                javaFieldName,
                                type,
                                true);
                       
                        javaClassRep.addMethod(getter);
                       
                        getter.setJavaDocComment(new JavaDocComment ("@return " + fieldName));
                    }
                }
               
   
                javaClassRep.addMethod(createMethod_getDCName(typeConstructorInfo));
               
                javaClassRep.addMethod(createMethod_getDCOrdinal(typeConstructorInfo));
               
                JavaMethod toString = createMethod_toString();
                if (toString != null) {
                    javaClassRep.addMethod (toString);
                }
               
                if (typeConstructorInfo.isEnumerationType) {
                    JavaMethod equals = JavaDataClassGenerator.this.createMethod_equals(
                            typeConstructorClassName,
                            typeConstructorInfo.commonFieldNames,
                            typeConstructorInfo.commonFieldTypes);

                    javaClassRep.addMethod (equals);
View Full Code Here


           
            private void createMethod_hashCode (JavaClassRep javaClassRep) {
           
                if (typeConstructorInfo.isEnumerationType) {
                    // We can just return the ordinal.
                    JavaMethod hashCode =
                        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);
                    hashCode.addStatement(new ReturnStatement (call_getOrdinal));
                   
                    javaClassRep.addMethod(hashCode);
                }
            }
View Full Code Here

                }
            }
           
            private JavaMethod createMethod_toString() {

                JavaMethod toString;
               
                if (typeConstructorInfo.isEnumerationType) {
                    toString =
                        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);
                   
                    toString.addStatement(new ReturnStatement (getDCName));
                    return toString;
                } else {
                    toString = null;
                }

                if (toString != null) {
                    JavaDocComment jdc =
                        new JavaDocComment ("@return a String representing this instance of " + typeConstructorInfo.javaClassName);
                   
                    toString.setJavaDocComment(jdc);
                }
               
                return toString;
            }
View Full Code Here

               
                return toString;
            }
           
            private JavaMethod createMethod_fromOrdinal (JavaTypeName dataType_TypeName) {
                JavaMethod fromOrdinal =
                    new JavaMethod(
                            Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL,
                            dataType_TypeName,
                            "ordinal",
                            JavaTypeName.INT,
                            true,
                            "fromOrdinal");
               
                // build up a switch.
                JavaStatement.SwitchStatement switchStatement =
                    new JavaStatement.SwitchStatement(new MethodVariable("ordinal"));
               
                for (int i = 0, n = typeConstructorInfo.typeConstructor.getNDataConstructors(); i < n; ++i) {
                    DataConstructor dc = typeConstructorInfo.typeConstructor.getNthDataConstructor(i);
                   
                    String enumFieldName = createEnumFieldName(dc.getName().getUnqualifiedName());
                    JavaField field = new JavaField.Static(dataType_TypeName, enumFieldName, dataType_TypeName);
                   
                    JavaStatement.SwitchStatement.IntCaseGroup intCase =
                        new SwitchStatement.IntCaseGroup(
                                dc.getOrdinal(),
                                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);
               
                JavaStatement throwStatement = new JavaStatement.ThrowStatement(createException);

                SwitchStatement.DefaultCase defaultCase =
                    new SwitchStatement.DefaultCase(throwStatement);
                switchStatement.addCase(defaultCase);
               
                fromOrdinal.addStatement(switchStatement);
               
                JavaDocComment jdc =
                    new JavaDocComment ("@param ordinal");
                jdc.addLine("@return the instance of " + typeConstructorInfo.javaClassName + " corresponding to the given ordinal.");
                fromOrdinal.setJavaDocComment(jdc);
               
                return fromOrdinal;
            }
View Full Code Here

           
            private final JavaMethod createMethod_getDCOrdinal(TypeConstructorInfo typeConstructorInfo) {
               
                int modifiers = Modifier.PUBLIC;
               
                JavaMethod javaMethod = new JavaMethod(modifiers, JavaTypeName.INT, GET_DC_ORDINAL_METHOD_NAME);
               
                if (typeConstructorInfo.isEnumerationType) {
                    // return instance field.
                    javaMethod.addStatement (new ReturnStatement (new JavaField.Instance (null, ORDINAL_FIELD_NAME, JavaTypeName.INT)));
                } else {
                    javaMethod.addStatement (new ReturnStatement(LiteralWrapper.make (new Integer(-1))));
                }
               
                JavaDocComment jdc =
                    new JavaDocComment ("@return the ordinal of this instance of " + typeConstructorInfo.javaClassName);
                javaMethod.setJavaDocComment(jdc);
               
                return javaMethod;
            }
View Full Code Here

           
            private final JavaMethod createMethod_getDCName(TypeConstructorInfo typeConstructorInfo) {
               
                int modifiers = Modifier.PUBLIC;
               
                JavaMethod javaMethod = new JavaMethod(modifiers, JavaTypeName.STRING, GET_DC_NAME_METHOD_NAME);
               
                if (typeConstructorInfo.isEnumerationType) {
                    // return the name$ field
                    javaMethod.addStatement (new ReturnStatement (new JavaField.Instance (null, DC_NAME_FIELD_NAME, JavaTypeName.STRING)));
                } else {
                    javaMethod.addStatement (new ReturnStatement(LiteralWrapper.NULL));
                }
               
                JavaDocComment jdc =
                    new JavaDocComment ("@return the name of the data constructor corresponding to this instance of " + typeConstructorInfo.javaClassName);
                javaMethod.setJavaDocComment(jdc);

                return javaMethod;

            }
View Full Code Here

            if (implementAtThisLevel) {
                modifiers = modifiers | Modifier.FINAL;
            }
   
            // Create the method.
            JavaMethod javaMethod = new JavaMethod(modifiers, fieldType, methodName);
   
            if (implementAtThisLevel) {
                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);
               
                javaMethod.addStatement(new JavaStatement.ThrowStatement(createException));
            }
           
            return javaMethod;
        }
View Full Code Here

   
        private final JavaMethod createMethod_hashCode (
                Set<FieldName> fieldNames,
                Map<FieldName, JavaTypeName> fieldNameToType) {
       
            JavaMethod hashCode =
                new JavaMethod (
                        Modifier.PUBLIC | Modifier.FINAL,
                        JavaTypeName.INT,
                        "hashCode");
           
            JavaField hashCodeField =
                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));
           
            return hashCode;
        }
View Full Code Here

                Set<FieldName> fieldNames, 
                Map<FieldName, JavaTypeName> fieldNameToType) {
           
            String argName = "object";
           
            JavaMethod equals =
                new JavaMethod(
                        Modifier.PUBLIC | Modifier.FINAL,
                        JavaTypeName.BOOLEAN,
                        argName,
                        JavaTypeName.OBJECT,
                        false,
                        "equals");

            MethodVariable objectArg = new MethodVariable (argName);
            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));
            } else {
                equals.addStatement(new ReturnStatement(LiteralWrapper.TRUE));
            }
           
            return equals;
        }
View Full Code Here

            }
        }
       
        private final JavaMethod createMethod_toString (Set<FieldName> fieldNames, Map<FieldName, JavaTypeName> fieldTypes) {
           
            JavaMethod toString =
                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 =
                        new MethodInvocation.Instance(field, "toString", JavaTypeName.STRING, MethodInvocation.InvocationType.VIRTUAL);
                }

                message =
                    new JavaExpression.OperatorExpression.Binary(
                            JavaOperator.STRING_CONCATENATION,
                            message,
                            LiteralWrapper.make("    " + fn.toString() + " = "));

                message =
                    new JavaExpression.OperatorExpression.Binary(
                            JavaOperator.STRING_CONCATENATION,
                            message,
                            fieldString);

                message =
                    new JavaExpression.OperatorExpression.Binary(
                            JavaOperator.STRING_CONCATENATION,
                            message,
                            LiteralWrapper.make("\n"));
            }
           
            toString.addStatement (new ReturnStatement (message));
           
            return toString;
        }
View Full Code Here

TOP

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

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.