Package org.openquark.cal.internal.javamodel.JavaStatement

Examples of org.openquark.cal.internal.javamodel.JavaStatement.JavaDocComment


            JavaClassRep innerClass = classRep.getInnerClass(i);
            JavaClassRep newInnerClass = (JavaClassRep)innerClass.accept(this, arg);
            newClassRep.addInnerClass(newInnerClass);
        }
       
        JavaDocComment jdc = classRep.getJavaDoc();
        if (jdc != null) {
            JavaDocComment newJDC = (JavaDocComment)jdc.accept(this, arg);
            newClassRep.setJavaDoc(newJDC);
        }
       
        MultiLineComment mlc = classRep.getComment();
        if (mlc != null) {
View Full Code Here


     * @throws JavaGenerationException
     */
    private static String getSource(JavaFieldDeclaration fieldDeclaration, GenerationContext context, int indent) throws JavaGenerationException {
        StringBuilder block = new StringBuilder();

        JavaDocComment jdc = fieldDeclaration.getJavaDoc();
        if (jdc != null) {
            block.append(getSource(jdc, context, indent));
        }

        String fieldName = fieldDeclaration.getFieldName();
View Full Code Here

        for (int i = 0, n = classRep.getNInnerClasses(); i < n && continueTraversal; ++i) {
            final JavaClassRep innerClass = classRep.getInnerClass(i);
            innerClass.accept(this, arg);
        }
       
        final JavaDocComment jdc = classRep.getJavaDoc();
        if (jdc != null && continueTraversal) {
            jdc.accept(this, arg);
        }
       
        final MultiLineComment mlc = classRep.getComment();
        if (mlc != null && continueTraversal) {
            mlc.accept(this, arg);
View Full Code Here

            } else {
                lines.add("@return SourceModel.Expr");
            }
        }
       
        return new JavaDocComment(lines);
    }
View Full Code Here

                    PUBLIC_STATIC_FINAL,
                    EMPTY_TYPE_NAME_ARRAY);
       
       
        // Create the JavaDoc for the inner class.
        JavaDocComment jdc = new JavaDocComment("This inner class (" + TYPECONSTRUCTOR_CLASS_NAME + ") contains constants");
        jdc.addLine("and methods related to binding to CAL TypeConstructors in the " + moduleTypeInfo.getModuleName() + " module.");
        typeConstructorsClass.setJavaDoc(jdc);

        // Build up a list of type constructors and sort by name.
        List<String> typeConstructorNames = new ArrayList<String>();
        for (int i = 0, n = moduleTypeInfo.getNTypeConstructors(); i < n; ++i) {
            TypeConstructor typeCons = moduleTypeInfo.getNthTypeConstructor(i);
            if (typeCons.getScope().isPublic() != publicEntities) {
                continue;
            }
            typeConstructorNames.add(typeCons.getName().getUnqualifiedName());
        }
        Collections.sort(typeConstructorNames);
       
        if (typeConstructorNames.size() == 0) {
            return;
        }
       
        bindingClass.addInnerClass(typeConstructorsClass);
       
        // For each type constructor create a QualifiedName field.
        // The field will have the same name as the type constructor.
        for (final String typeConstructorName : typeConstructorNames) {
           
            TypeConstructor typeCons = moduleTypeInfo.getTypeConstructor(typeConstructorName);
            // Add a field for the typeConstructor name.
            // 'static final QualifiedName typeConstructorName = QualifiedName.make(moduleName, typeConstructorName);
            // We need to check for conflict between the functionName
            // and java keywords.  It is valid to have a CAL
            // function called assert but not valid to have java code
            // 'static final String assert = "assert";
            String fieldName = fixupVarName(typeConstructorName);
           
            // Since TypeConsApp names are capitalized it is possible to have
            // a conflict between the name of the field and the name of the top
            // level class.
            if (fieldName.equals(this.bindingClassName)) {
                fieldName = fieldName + "_";
            }
           
            JavaFieldDeclaration jfd =
                makeQualifiedNameDeclaration(fieldName, typeConstructorName);

            // Add JavaDoc for the field declaration.  We use the CALDoc if there is any.
            JavaDocComment typeConstructorComment;
            CALDocComment cdc = typeCons.getCALDocComment();
            if (cdc != null) {
                typeConstructorComment = new JavaDocComment(calDocCommentToJavaComment(cdc, null, false));
            } else {
                typeConstructorComment = new JavaDocComment("/** Name binding for TypeConsApp: " + typeConstructorName + ". */");
            }
            jfd.setJavaDoc(typeConstructorComment);
           
            typeConstructorsClass.addFieldDeclaration(jfd);
           
View Full Code Here

                    EMPTY_TYPE_NAME_ARRAY);
              

       
        // Add the class JavaDoc.
        JavaDocComment jdc = new JavaDocComment("/**");
        jdc.addLine(" * This inner class (" + DATA_CONSTRUCTOR_CLASS_NAME + ") contains constants");
        jdc.addLine(" * and methods related to binding to CAL DataConstructors in the " + moduleTypeInfo.getModuleName() + " module.");
        jdc.addLine(" */");
        dataConstructorsClass.setJavaDoc(jdc);

        // We want to build up and sort a list of type constructor names.
        List<String> typeConstructorNames = new ArrayList<String>();
        for (int i = 0, n = moduleTypeInfo.getNTypeConstructors(); i < n; ++i) {
            TypeConstructor typeCons = moduleTypeInfo.getNthTypeConstructor(i);
            typeConstructorNames.add(typeCons.getName().getUnqualifiedName());
        }
        Collections.sort(typeConstructorNames);

       
        /*
         * Create a method to generate a SourceModel expression which will result
         * in the creation of an instance of the DC.
         * Create a QualifiedName field for the name of the DC.
         *
         * The ordering will be by name of the type constructor and then by dc ordinal.
         */
        boolean addClass = false;
       
        // Set of String used to avoid naming conflicts in the generated fields.
        // We want to build up a set of all the names that will be used for the methods and
        // QualifiedName fields.  This will allow us to avoid naming conflicts when creating
        // the ordinal fields.
        Set<String> javaNames = new HashSet<String>();
        for (final String typeConstructorName : typeConstructorNames) {
            TypeConstructor typeCons = moduleTypeInfo.getTypeConstructor(typeConstructorName);
            for (int i = 0, n = typeCons.getNDataConstructors(); i < n; ++i) {
                DataConstructor dc = typeCons.getNthDataConstructor(i);
               
                // If we are showing public entities we only car about public data constructors.
                if (dc.getScope().isPublic() != publicEntities) {
                    continue;
                }

                // This is the name used to name the java function and the QualfiedName field
                // associated with this data constructor.
                String javaFuncName = fixupVarName(dc.getName().getUnqualifiedName());
                javaNames.add(javaFuncName);
            }
        }
       
        for (final String typeConstructorName : typeConstructorNames) {
            TypeConstructor typeCons = moduleTypeInfo.getTypeConstructor(typeConstructorName);
            boolean commentAdded = false;
            for (int i = 0, n = typeCons.getNDataConstructors(); i < n; ++i) {
                DataConstructor dc = typeCons.getNthDataConstructor(i);
               
                // If we are showing public entities we only car about public data constructors.
                if (dc.getScope().isPublic() != publicEntities) {
                    continue;
                }
               
                addClass = true;
               
                // If this is the first DC for the data type we want a non JavaDoc comment
                // indicating the data type.
                if (!commentAdded) {
                    MultiLineComment mlc = new MultiLineComment("DataConstructors for the " + typeCons.getName().getQualifiedName() + " data type.");
                    dataConstructorsClass.addComment(mlc);
                    commentAdded = true;
                }

                // Get the types of the data constructor fields.
                TypeExpr[] fieldTypes = getFieldTypesForDC(dc);
               
               
                String javaFuncName = fixupVarName(dc.getName().getUnqualifiedName());
               
                // Since data constructors are capitalized its possible to have a conflict between the name
                // of our field/helper function and the name of the containing class.
                if (javaFuncName.equals(this.bindingClassName)) {
                    javaFuncName = javaFuncName + "_";
                }
               
                // First generate a method that takes SourceModel.Expr instances for
                // each field value.
               
                // Build up the argument names and types.
                int nArgs = dc.getArity();
                JavaTypeName argTypes[] = new JavaTypeName[nArgs];
                Arrays.fill(argTypes, SOURCE_MODEL_EXPR_TYPE_NAME);
                String argNames[] = new String[nArgs];
                String origArgNames[] = new String[nArgs];
                for (int j = 0, k = argNames.length; j < k; ++j) {
                    argNames[j] = fixupVarName(dc.getArgumentName(j));
                    origArgNames[j] = dc.getArgumentName(j);
                }
               
                // Create the method.
                JavaMethod bindingFunction =
                    new JavaMethod(PUBLIC_STATIC_FINAL,
                                   SOURCE_MODEL_EXPR_TYPE_NAME,
                                   argNames,
                                   argTypes,
                                   null, javaFuncName);
                dataConstructorsClass.addMethod(bindingFunction);
               
                // Add JavaDoc for the method.
                JavaDocComment funcComment;
                CALDocComment cdc = dc.getCALDocComment();
                if (cdc != null) {
                    funcComment = new JavaDocComment(calDocCommentToJavaComment(cdc, dc, false, argNames));
                    funcComment = fixupJavaDoc(funcComment, origArgNames, argNames);
                } else {
                    funcComment = new JavaDocComment("Binding for DataConstructor: " + dc.getName().getQualifiedName() + ".");
                    for (int iName = 0; iName < argNames.length; ++iName) {
                        funcComment.addLine("@param " + argNames[iName]);
                    }
                    funcComment.addLine("@return the SourceModule.Expr representing an application of " + dc.getName().getQualifiedName());
                }
                bindingFunction.setJavaDocComment(funcComment);
               
                // Now we need to fill in the body.
               
                // Create an instance of SourceModel.Expr.DataCons for the data constructor.
                JavaField nameField = new JavaField.Static(dataConstructorsClassTypeName, javaFuncName, QUALIFIED_NAME_TYPE_NAME);
                JavaExpression sourceModelDataConsCreation =
                    new MethodInvocation.Static(
                            SOURCE_MODEL_EXPR_DATA_CONS_TYPE_NAME,
                            "make",
                            nameField,
                            QUALIFIED_NAME_TYPE_NAME,
                            SOURCE_MODEL_EXPR_DATA_CONS_TYPE_NAME);
               
                // Build up an @see tag for the function just created.
                String atSee = "@see #" + javaFuncName + "(";
                for (int iArg = 0; iArg < argNames.length; ++iArg) {
                    if (iArg == 0) {
                        atSee = atSee + argTypes[iArg].getFullJavaSourceName();
                    } else {
                        atSee = atSee + ", " + argTypes[iArg].getFullJavaSourceName();
                    }
                }
                atSee = atSee + ")";
               
                if (dc.getArity() == 0) {
                    // Simply return the Expr.DataCons.
                    bindingFunction.addStatement(new ReturnStatement(sourceModelDataConsCreation));
                } else {
                    // Need to build up an application.
                   
                    // Create an array of SourceModel.Expr where the first element is the
                    // SourceModel.Expr.DataCons instance and the following elements are
                    // the function arguments.
                    JavaExpression arrayElements[] = new JavaExpression[dc.getArity() + 1];
                    arrayElements[0] = sourceModelDataConsCreation;
                   
                    for (int j = 1; j <= argNames.length; ++j) {
                        arrayElements[j] = new JavaExpression.MethodVariable(argNames[j-1]);
                    }
                   
                    JavaExpression arrayCreation =
                        new JavaExpression.ArrayCreationExpression(SOURCE_MODEL_EXPR_TYPE_NAME, arrayElements);
                   
                    // Invoke SourceModle.Expr.Application.make()
                    MethodInvocation makeApply =
                        new MethodInvocation.Static(
                                SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME,
                                "make",
                                arrayCreation,
                                JavaTypeName.makeArrayType(SOURCE_MODEL_EXPR_TYPE_NAME),
                                SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME);
                   
                    bindingFunction.addStatement(new ReturnStatement (makeApply));
               
                    // If any of the argument types correspond to a Java type.
                    // (eg. Prelude.Int, Prelude.Long, etc. we can generate a version
                    // of the binding function that takes these argument types.
                    boolean primArgs = false;
                    for (int j = 0, k = fieldTypes.length; j < k; ++j) {
                        if (canTypeBeUnboxed(fieldTypes[j])) {
                            primArgs = true;
                            // Update the type of the argument.
                            argTypes[j] = typeExprToTypeName(fieldTypes[j]);
                           
                            // The argument to Application.make needs to be updated.
                            // We need to wrap the raw value (i.e. int, boolean, etc.)
                            // in the appropriate SourceModel construct.
                            arrayElements[j+1] = wrapArgument(argNames[j], fieldTypes[j]);
                        }
                    }

                   
                    if (primArgs) {
                        bindingFunction =
                            new JavaMethod(PUBLIC_STATIC_FINAL,
                                           SOURCE_MODEL_EXPR_TYPE_NAME,
                                           argNames,
                                           argTypes,
                                           null, javaFuncName);
                        dataConstructorsClass.addMethod(bindingFunction);
                       
                        // For the comment for this method we want an @see referring to the previous method.
                       
                        JavaStatement.JavaDocComment comment = new JavaStatement.JavaDocComment(atSee);
                        for (int iArg = 0; iArg < argNames.length; ++iArg) {
                            comment.addLine("@param " + argNames[iArg]);
                        }                       
                        comment.addLine("@return " + SOURCE_MODEL_EXPR_TYPE_NAME.getFullJavaSourceName());
                        bindingFunction.setJavaDocComment(comment);
                       
                        arrayCreation =
                            new JavaExpression.ArrayCreationExpression(SOURCE_MODEL_EXPR_TYPE_NAME, arrayElements);
                       
                        makeApply =
                            new MethodInvocation.Static(
                                    SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME,
                                    "make",
                                    arrayCreation,
                                    JavaTypeName.makeArrayType(SOURCE_MODEL_EXPR_TYPE_NAME),
                                    SOURCE_MODEL_EXPR_APPLICATION_TYPE_NAME);
                       
                        bindingFunction.addStatement(new ReturnStatement (makeApply));
       
                    }
                   
                }
               
                // Create a field declaration for the QualifiedName field.
                JavaFieldDeclaration jfd =
                    makeQualifiedNameDeclaration(javaFuncName, dc.getName().getUnqualifiedName());
               
                // If there is CALDoc for the DC add it as JavaDoc.
                JavaDocComment comment = new JavaDocComment("Name binding for DataConstructor: " + dc.getName().getQualifiedName() + ".");
                comment.addLine(atSee);
               
                jfd.setJavaDoc(comment);
               
                dataConstructorsClass.addFieldDeclaration(jfd);               
            
                // Now add an int field which is the ordinal of the data constructor.
                String ordinalFieldName = javaFuncName + "_ordinal";
                while (javaNames.contains(ordinalFieldName)) {
                    ordinalFieldName = ordinalFieldName + "_";
                }
                JavaFieldDeclaration ordinalFieldDec =
                    new JavaFieldDeclaration(PUBLIC_STATIC_FINAL, JavaTypeName.INT, ordinalFieldName, JavaExpression.LiteralWrapper.make(Integer.valueOf(dc.getOrdinal())));
                javaNames.add(ordinalFieldName);
                JavaDocComment ordinalComment = new JavaDocComment("Ordinal of DataConstructor " + dc.getName().getQualifiedName() + ".");
                ordinalComment.addLine(atSee);
                ordinalFieldDec.setJavaDoc(ordinalComment);
               
                dataConstructorsClass.addFieldDeclaration(ordinalFieldDec);
            }
        }
View Full Code Here

                    PUBLIC_STATIC_FINAL,
                    EMPTY_TYPE_NAME_ARRAY);
               
       
        // Set the JavaDoc for the class.
        JavaDocComment jdc = new JavaDocComment("This inner class (" + TYPECLASS_CLASS_NAME + ") contains constants");
        jdc.addLine("and methods related to binding to CAL TypeClasses in the " + moduleTypeInfo.getModuleName() + " module.");
        typeClassClass.setJavaDoc(jdc);

        // Build up a list of TypeClass names and sort.
        List<String> typeClassNames = new ArrayList<String>();
        for (int i = 0, n = moduleTypeInfo.getNTypeClasses(); i < n; ++i) {
            TypeClass tc = moduleTypeInfo.getNthTypeClass(i);
            if (tc.getScope().isPublic() != publicEntities) {
                continue;
            }
            typeClassNames.add(tc.getName().getUnqualifiedName());
        }
        Collections.sort(typeClassNames);
       
        if (typeClassNames.size() == 0) {
            return;
        }
       
        bindingClass.addInnerClass(typeClassClass);

        // Generate a QualifiedName field for each type class.
        for (final String typeClassName : typeClassNames) {
            TypeClass tc = moduleTypeInfo.getTypeClass(typeClassName);
            // Add a field for the TypeClass name.
            // 'static final String typeClassName = "typeClassName";'
            // We need to check for conflict between the functionName
            // and java keywords.  It is valid to have a CAL
            // function called assert but not valid to have java code
            // 'static final QualifiedName assert = ...
            String fieldName = fixupVarName(typeClassName);
           
            // Since TypeClass names are capitalized it's possible to have a conflict
            // between the name of the field and the name of the top level class.
            if (fieldName.equals(this.bindingClassName)) {
                fieldName = fieldName + "_";
            }
           
            JavaFieldDeclaration jfd =
                makeQualifiedNameDeclaration(fieldName, typeClassName);

            // Add JavaDoc.  We use any CALDoc for the type class if available.
            JavaDocComment comment;
            CALDocComment cdc = tc.getCALDocComment();
            if (cdc != null) {
                comment = new JavaDocComment(calDocCommentToJavaComment(cdc, null, false));
            } else {
                comment = new JavaDocComment("/** Name binding for TypeClass: " + tc.getName().getQualifiedName() + ". */");
            }
            jfd.setJavaDoc(comment);
           
            typeClassClass.addFieldDeclaration(jfd);
        }
View Full Code Here

                mlc.addLine(" ");
               
                javaClassRep.setComment(mlc);
               
                // We want to insert the CALDoc comment for the type as a JavaDoc comment for the class.
                JavaDocComment jdc =
                    new JavaDocComment ("This class (" + javaClassRep.getClassName().getUnqualifiedJavaSourceName() + ") provides a Java data class corresponding to");
                    jdc.addLine("the CAL type constructor " + typeConstructorInfo.typeConstructor.getName() + ".");
                    jdc.addLine("");
                   
                if (typeConstructorInfo.isEnumerationType) {
                    jdc.addLine("This type constructor is an enumeration. (i.e. all data constructors have no fields)");
                    jdc.addLine("The individual data constructors are represented by instances of " + javaClassRep.getClassName().getUnqualifiedJavaSourceName() + " held ");
                    jdc.addLine("in static final fields.");
                } else {
                    jdc.addLine("Because the type constructor has only one data constructor, with the same name");
                    jdc.addLine("as the type constructor this class also represents instances of the data constructor.");
                }
               
                javaClassRep.setJavaDoc(jdc);
               
                // Generate fields in this class for any fields which are common to all data constructors.
                createFields(javaClassRep, typeConstructorInfo);
       
                if (typeConstructorInfo.isEnumerationType) {
                    // Add instance fields for name and ordinal
                    // Add static final fields for each data constructor
                    createFields_Enumeration(typeConstructorInfo.typeConstructor, javaClassRep, typeConstructorClassName);

                    // Add a function which takes an int value for the
                    // ordinal and returns the corresponding enum instance.
                    javaClassRep.addMethod(createMethod_fromOrdinal (typeConstructorClassName));
                }
               
                if (typeConstructorInfo.commonFieldNames.size() > 0) {
                    javaClassRep.addConstructor(createConstructor(typeConstructorInfo, typeConstructorClassName));
                } else {
                    javaClassRep.addConstructor(createConstructor_default(typeConstructorClassName, typeConstructorInfo.isEnumerationType));
                   
                }
               
                // Create a get_FieldName method for each unique field name in the set of
                // data constructors. 
                // If the field is common to all DCs it will be implemented at this
                // level.  Otherwise the implementation at this level throws an error
                // and the DC classes are expected to override it.
                for (FieldName fieldName : typeConstructorInfo.commonFieldNames) {
                    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));
View Full Code Here

                } 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

                    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

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaStatement.JavaDocComment

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.