Examples of JSourceCode


Examples of org.exolab.javasource.JSourceCode

            final JClass jClass, final boolean useJava50) {
        JMethod method = new JMethod("enumerate" + fieldInfo.getMethodSuffix(),
                SGTypes.createEnumeration(fieldInfo.getContentType().getJType(), useJava50, true),
                "an Enumeration over all elements of this collection");

        JSourceCode sourceCode = method.getSourceCode();
        sourceCode.add("java.util.Vector v = new java.util.Vector();"); // ODMG 3.0
        sourceCode.add("java.util.Iterator i = ");
        sourceCode.append(fieldInfo.getName());
        sourceCode.append(".iterator();");
        sourceCode.add("");
        sourceCode.add("while (i.hasNext()) {");
        sourceCode.indent();
        sourceCode.add("v.add(i.next());");
        sourceCode.unindent();
        sourceCode.add("");
        sourceCode.add("return v.elements();");

        jClass.addMethod(method);
    }
View Full Code Here

Examples of org.exolab.javasource.JSourceCode

           new JDODescriptorJClass(_config, descriptorClassName, jClass);
       JDOClassInfoNature cNature = new JDOClassInfoNature(classInfo);

       //-- get handle to default constructor
       JConstructor ctor   = classDesc.getConstructor(0);
       JSourceCode jsc     = ctor.getSourceCode();

       jsc = createClassInfoPart(classInfo, jsc);

       //=================
       // FieldDescriptors
       //=================

       for (int i = 0; i < classInfo.getElementFields().length; i++) {
           FieldInfo fInfo = classInfo.getElementFields()[i];
           if (checkFieldInfoNatures(fInfo)) {
              
               if (fInfo.hasNature(JDOOneToOneNature.class.getName())) {
                   jsc = createOneToOneFieldInfoPart(fInfo, jsc);
                  
               } else if (fInfo.hasNature(JDOOneToManyNature.class.getName())) {
                   jsc = createOneToManyFieldInfoPart(fInfo, jsc);
               } else {
                   jsc = createEntityFieldInfoPart(fInfo, jsc);
               }
           }
       }


       _fields = setFields(classInfo.getElementFields());
       _identities = setIdentities(cNature.getPrimaryKeys());

       jsc.add("");

       jsc.add("setFields(new FieldDescriptor[] {" + _fields + "});");
       jsc.add("setIdentities(new FieldDescriptor[] {" + _identities + "});");

       return classDesc;
   }
View Full Code Here

Examples of org.exolab.javasource.JSourceCode

     * Adds our default constructor.
     */
    private void addDefaultConstructor() {
        addConstructor(createConstructor());
        JConstructor cons = getConstructor(0);
        JSourceCode jsc = cons.getSourceCode();
        jsc.add("super();");
        jsc.add("ClassMapping mapping = new ClassMapping();");
        jsc.add("ClassChoice choice = new ClassChoice();");
        jsc.add("MapTo mapTo = new MapTo();");
    }
View Full Code Here

Examples of org.exolab.javasource.JSourceCode

     * Creates the Validate methods for the given JClass.
     * @param jClass the JClass to create the Validate methods for
     */
    private void createValidateMethods(final JClass jClass) {
        JMethod     jMethod = null;
        JSourceCode jsc     = null;

        //-- #validate
        jMethod = new JMethod("validate");
        jMethod.addException(SGTypes.VALIDATION_EXCEPTION,
                             "if this object is an invalid instance according to the schema");

        jClass.addMethod(jMethod);
        jsc = jMethod.getSourceCode();
        jsc.add("org.exolab.castor.xml.Validator validator = new ");
        jsc.append("org.exolab.castor.xml.Validator();");
        jsc.add("validator.validate(this);");

        //-- #isValid
        jMethod  = new JMethod("isValid", JType.BOOLEAN,
                               "true if this object is valid according to the schema");
        jsc = jMethod.getSourceCode();
        jsc.add("try {");
        jsc.indent();
        jsc.add("validate();");
        jsc.unindent();
        jsc.add("} catch (org.exolab.castor.xml.ValidationException vex) {");
        jsc.indent();
        jsc.add("return false;");
        jsc.unindent();
        jsc.add("}");
        jsc.add("return true;");
        jClass.addMethod(jMethod);
    } //-- createValidateMethods
View Full Code Here

Examples of org.exolab.javasource.JSourceCode

                        jClass.removeMethod(method);

                        //-- update setter method
                        mname = fieldInfo.getWriteMethodName();
                        method = jClass.getMethod(mname, 0);
                        JSourceCode jsc = method.getSourceCode();
                        jsc.add("super.");
                        jsc.append(mname);
                        jsc.append("(this.");
                        jsc.append(fieldInfo.getName());
                        jsc.append(".toString());");
                    }
                    //-- else just use superclass setters/getters
                    //-- do nothing
                } else {
                    //
View Full Code Here

Examples of org.exolab.javasource.JSourceCode

            }
        }
       
        processAppInfo(component.getAnnotated(), fieldInfo);
       
        JSourceCode scInitializer = state.getJClass().getConstructor(0).getSourceCode();

        ClassInfo base = state.getClassInfo().getBaseClass();
        boolean present = false;
        if (base != null) {
            switch (new XMLInfoNature(fieldInfo).getNodeType()) {
View Full Code Here

Examples of org.exolab.javasource.JSourceCode

     *            true if source code is supposed to be generated for Java 5
     */
    private void createGetterMethod(final FieldInfo fieldInfo,
            final JClass jClass, final boolean useJava50, AnnotationBuilder[] annotationBuilders) {
        JMethod method    = null;
        JSourceCode jsc   = null;

        String mname = fieldInfo.getMethodSuffix();

        XSType xsType = new XMLInfoNature(fieldInfo).getSchemaType();
        JType jType  = xsType.getJType();

        //-- create get method
        method = new JMethod(fieldInfo.getReadMethodName(), jType,
                             "the value of field '" + mname + "'.");
//        if (useJava50) {
//            Java5HacksHelper.addOverrideAnnotations(method.getSignature());
//        }
       
        for (int i = 0; i < annotationBuilders.length; i++) {
            AnnotationBuilder annotationBuilder = annotationBuilders[i];
            annotationBuilder.addFieldGetterAnnotations(fieldInfo, method);
        }
       
        jClass.addMethod(method);
        createGetterComment(fieldInfo, method.getJDocComment());
        jsc = method.getSourceCode();
        jsc.add("return this.");
        jsc.append(fieldInfo.getName());
        jsc.append(";");

        if (xsType.getType() == XSType.BOOLEAN_TYPE) {

            // -- create is<Property>t method
            method = new JMethod(fieldInfo.getIsMethodName(), jType,
                    "the value of field '" + mname + "'.");
//            if (useJava50) {
//                Java5HacksHelper.addOverrideAnnotations(method.getSignature());
//            }
            jClass.addMethod(method);
            createGetterComment(fieldInfo, method.getJDocComment());
            jsc = method.getSourceCode();
            jsc.add("return this.");
            jsc.append(fieldInfo.getName());
            jsc.append(";");

        }

    } //-- createGetterMethod
View Full Code Here

Examples of org.exolab.javasource.JSourceCode

     * @param jClass the JClass to add the methods to
     */
    private void createHasAndDeleteMethods(final FieldInfo fieldInfo,
            final JClass jClass) {
        JMethod method    = null;
        JSourceCode jsc   = null;

        String mname = fieldInfo.getMethodSuffix();

        XSType xsType = new XMLInfoNature(fieldInfo).getSchemaType();
        xsType.getJType();

        //-- create hasMethod
        method = new JMethod(fieldInfo.getHasMethodName(), JType.BOOLEAN,
                             "true if at least one " + mname + " has been added");
        jClass.addMethod(method);
        jsc = method.getSourceCode();
        jsc.add("return this._has");
        String fieldName = fieldInfo.getName();
        jsc.append(fieldName);
        jsc.append(";");

        //-- create delete method
        method = new JMethod(fieldInfo.getDeleteMethodName());
        jClass.addMethod(method);
        jsc = method.getSourceCode();
        jsc.add("this._has");
        jsc.append(fieldName);
        jsc.append("= false;");
        //-- bound properties
        if (fieldInfo.isBound()) {
            //notify listeners
            jsc.add("notifyPropertyChangeListeners(\"");
            if (fieldName.startsWith("_")) {
                jsc.append(fieldName.substring(1));
            } else {
                jsc.append(fieldName);
            }
            jsc.append("\", ");
            //-- 'this.' ensures this refers to the class member not the parameter
            jsc.append(xsType.createToJavaObjectCode("this." + fieldName));
            jsc.append(", null");
            jsc.append(");");
        }
    } //-- createHasAndDeleteMethods
View Full Code Here

Examples of org.exolab.javasource.JSourceCode

     * @param useJava50 true if source code is supposed to be generated for Java 5
     */
     private void createSetterMethod(final FieldInfo fieldInfo,
             final JClass jClass, final boolean useJava50) {
        JMethod method    = null;
        JSourceCode jsc   = null;

        XMLInfoNature xmlNature = new XMLInfoNature(fieldInfo);
       
        String mname  = fieldInfo.getMethodSuffix();
        XSType xsType = xmlNature.getSchemaType();
        JType jType   = xsType.getJType();

        //-- create set method
        /*
         * fieldInfo.getWriteMethodName() will either prefix the method
         * with 'add' (for multivalued fields) or 'set'!
         * @see FieldInfo#getWriteMethodeName()
         */
        method = new JMethod(fieldInfo.getWriteMethodName());
        jClass.addMethod(method);

        String paramName = fieldInfo.getName();

        //-- make parameter name pretty,
        //-- simply for aesthetic beauty
        if (paramName.indexOf('_') == 0) {
            String tempName = paramName.substring(1);
            if (_javaNaming.isValidJavaIdentifier(tempName)) {
                paramName = tempName;
            }
        }

        method.addParameter(new JParameter(jType, paramName));
//        if (useJava50) {
//            Java5HacksHelper.addOverrideAnnotations(method.getSignature()); // DAB Java 5.0 hack
//        }
        createSetterComment(fieldInfo, method.getJDocComment());
        jsc = method.getSourceCode();

        String fieldName = fieldInfo.getName();
        //-- bound properties
        if (fieldInfo.isBound()) {
            // save old value
            jsc.add("java.lang.Object old");
            jsc.append(mname);
            jsc.append(" = ");
            //-- 'this.' ensures this refers to the class member not the parameter
            jsc.append(xsType.createToJavaObjectCode("this." + fieldName));
            jsc.append(";");
        }

        //-- set new value
        jsc.add("this.");
        jsc.append(fieldName);
        jsc.append(" = ");
        jsc.append(paramName);
        jsc.append(";");

        if (fieldInfo.getFieldInfoReference() != null) {
            jsc.add("this.");
            jsc.append(fieldInfo.getFieldInfoReference().getName());
            jsc.append(" = ");

            JType referencedJType = new XMLInfoNature(fieldInfo.getFieldInfoReference()).getSchemaType().getJType();
            if (referencedJType.isPrimitive()) {
                jsc.append(paramName);
            } else if (jType.isPrimitive()) {
                JPrimitiveType primitive = (JPrimitiveType) jType;
                jsc.append("new ");
                jsc.append(primitive.getWrapperName());
                jsc.append("(");
                jsc.append(paramName);
                jsc.append(")");
            } else {
                jsc.append(paramName);
            }

            jsc.append(";");
        }

        //-- hasProperty
        if (fieldInfo.requiresHasAndDeleteMethods()) {
            jsc.add("this._has");
            jsc.append(fieldName);
            jsc.append(" = true;");
        }

        //-- bound properties
        if (fieldInfo.isBound()) {
            //notify listeners
            jsc.add("notifyPropertyChangeListeners(\"");
            if (fieldName.startsWith("_")) {
                jsc.append(fieldName.substring(1));
            } else {
                jsc.append(fieldName);
            }
            jsc.append("\", old");
            jsc.append(mname);
            jsc.append(", ");
            //-- 'this.' ensures this refers to the class member not the parameter
            jsc.append(xsType.createToJavaObjectCode("this." + fieldName));
            jsc.append(");");
        }
    } //-- createSetterMethod
View Full Code Here

Examples of org.exolab.javasource.JSourceCode

                             "if we try to instantiate an abstract class or interface");
        jMethod.addException(new JClass("IllegalAccessException"),
                             "if we do not have access to the field, for example if it is private");
        jMethod.setComment("implementation of org.castor.xmlctf.CastorTestable");
        jclass.addMethod(jMethod);
        JSourceCode jsc = jMethod.getSourceCode();
        JField[] fields = jclass.getFields();

        for (int i = 0; i < fields.length; i++) {
            JField temp = fields[i];
            JType type = temp.getType();
            String name = temp.getName();

            if (state.getFieldInfoForChoice() != null
                    && name.equals(state.getFieldInfoForChoice().getName())) {
                continue;
            }

            if (name.startsWith("_")) {
                name = getJavaNaming().toJavaClassName(name.substring(1));
            } else {
                name = getJavaNaming().toJavaClassName(name);
            }

            String setName = "set" + name;
            if (name.indexOf("Has") == -1) {
                if (type instanceof JCollectionType) {
                    //Collection needs a specific handling
                    int listLocat = name.lastIndexOf("List");
                    String tempName = name;
                    if (listLocat != -1) {
                       tempName = tempName.substring(0, listLocat);
                    }
                    String methodName = getJavaNaming().toJavaClassName(tempName);
                    methodName = "get" + methodName;
                    JMethod method = jclass.getMethod(methodName, 0);
                    // TODO handle the Item introduced in with the group handling
                    if (method == null) {
                        continue;
                    }

                    String componentName = method.getReturnType().getName();

                    jsc.add(temp.getName());
                    jsc.append(" = RandomHelper.getRandom(");
                    jsc.append(temp.getName());
                    jsc.append(", ");
                    jsc.append(componentName);
                    jsc.append(".class);");
               } else if (type.isPrimitive()) {
                   // Primitive
                   jsc.add(setName);
                   jsc.append("(RandomHelper.getRandom(");
                   jsc.append(temp.getName());
                   jsc.append("));");
               } else if (type.isArray()) {
                   // Array
                   jsc.add(setName);
                   jsc.append("((");
                   jsc.append(type.toString());
                   jsc.append(")RandomHelper.getRandom(");
                   jsc.append(temp.getName());
                   // Any Class will do, but Array.class seems appropriate
                   jsc.append(", java.lang.reflect.Array.class));");
               } else {
                   // Object
                   jsc.add(setName);
                   jsc.append("((");
                   jsc.append(type.getName());
                   jsc.append(")RandomHelper.getRandom(");
                   jsc.append(temp.getName());
                   jsc.append(", ");
                   jsc.append(type.getName());
                   jsc.append(".class));");
               }
               jsc.add("");
            }
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.