javacodeaccessormethod.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "javacodeaccessormethod.h"
00019
00020
00021 #include <kdebug.h>
00022
00023
00024 #include "../attribute.h"
00025 #include "../codegenerator.h"
00026 #include "../codegenerationpolicy.h"
00027 #include "../classifiercodedocument.h"
00028 #include "../umlobject.h"
00029 #include "../umlrole.h"
00030 #include "../uml.h"
00031
00032 #include "javaclassifiercodedocument.h"
00033 #include "javacodegenerationpolicy.h"
00034 #include "javacodeclassfield.h"
00035 #include "javacodedocumentation.h"
00036
00037
00038
00039
00040 JavaCodeAccessorMethod::JavaCodeAccessorMethod ( CodeClassField * field, CodeAccessorMethod::AccessorType type)
00041 : CodeAccessorMethod ( field )
00042 {
00043 setType(type);
00044
00045
00046 JavaClassifierCodeDocument* jccd = dynamic_cast<JavaClassifierCodeDocument*>(field->getParentDocument());
00047 setComment(new JavaCodeDocumentation(jccd));
00048 }
00049
00050 JavaCodeAccessorMethod::~JavaCodeAccessorMethod ( ) { }
00051
00052
00053
00054
00055 void JavaCodeAccessorMethod::setAttributesOnNode ( QDomDocument & doc, QDomElement & blockElement)
00056 {
00057
00058
00059 CodeAccessorMethod::setAttributesOnNode(doc, blockElement);
00060
00061
00062 }
00063
00064 void JavaCodeAccessorMethod::setAttributesFromNode( QDomElement & root)
00065 {
00066
00067
00068 CodeAccessorMethod::setAttributesFromNode(root);
00069
00070
00071
00072 }
00073
00074 void JavaCodeAccessorMethod::updateContent( )
00075 {
00076
00077 CodeClassField * parentField = getParentClassField();
00078 JavaCodeClassField * javafield = dynamic_cast<JavaCodeClassField*>(parentField);
00079 QString fieldName = javafield->getFieldName();
00080
00081 QString text = "";
00082 switch(getType()) {
00083 case CodeAccessorMethod::ADD:
00084 {
00085 int maxOccurs = javafield->maximumListOccurances();
00086 QString fieldType = javafield->getTypeName();
00087 QString indent = getIndentation();
00088 QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00089 if(maxOccurs > 0)
00090 text += "if ("+fieldName+".size() < "+ QString::number(maxOccurs)+") {"+endLine+indent;
00091 text += fieldName+".add(value);";
00092 if(maxOccurs > 0)
00093 {
00094 text += endLine+"} else {"+endLine;
00095 text += indent + "System.err.println(\"ERROR: Cant add"+fieldType+" to "+fieldName+", minimum number of items reached.\");"+endLine+'}'+endLine;
00096 }
00097 break;
00098 }
00099 case CodeAccessorMethod::GET:
00100 text = "return "+fieldName+';';
00101 break;
00102 case CodeAccessorMethod::LIST:
00103 text = "return (List) "+fieldName+';';
00104 break;
00105 case CodeAccessorMethod::REMOVE:
00106 {
00107 int minOccurs = javafield->minimumListOccurances();
00108 QString fieldType = javafield->getTypeName();
00109 QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00110 QString indent = getIndentation();
00111
00112 if(minOccurs > 0)
00113 text += "if ("+fieldName+".size() >= "+ QString::number(minOccurs)+") {"+endLine+indent;
00114 text += fieldName+".remove(value);";
00115 if(minOccurs > 0)
00116 {
00117 text += endLine+"} else {"+endLine;
00118 text += indent + "System.err.println(\"ERROR: Cant remove"+fieldType+" from "+fieldName+", minimum number of items reached.\");"+endLine+'}'+endLine;
00119 }
00120 break;
00121 }
00122 case CodeAccessorMethod::SET:
00123 text = fieldName+" = value;";
00124 break;
00125 default:
00126
00127 break;
00128 }
00129
00130 setText(text);
00131
00132 }
00133
00134 void JavaCodeAccessorMethod::updateMethodDeclaration()
00135 {
00136
00137 JavaCodeClassField * javafield = dynamic_cast<JavaCodeClassField*>(getParentClassField());
00138 JavaClassifierCodeDocument * javadoc = dynamic_cast<JavaClassifierCodeDocument*>(javafield->getParentDocument());
00139 CodeGenerationPolicy *commonpolicy = UMLApp::app()->getCommonPolicy();
00140
00141
00142 CodeGenerationPolicy::ScopePolicy scopePolicy = commonpolicy->getAttributeAccessorScope();
00143 QString strVis = javadoc->scopeToJavaDecl(javafield->getVisibility());
00144 QString fieldName = javafield->getFieldName();
00145 QString fieldType = javafield->getTypeName();
00146 QString objectType = javafield->getListObjectType();
00147 if(objectType.isEmpty())
00148 objectType = fieldName;
00149 QString endLine = UMLApp::app()->getCommonPolicy()->getNewLineEndingChars();
00150
00151
00152
00153 if(javafield->parentIsAttribute())
00154 switch (scopePolicy) {
00155 case CodeGenerationPolicy::Public:
00156 case CodeGenerationPolicy::Private:
00157 case CodeGenerationPolicy::Protected:
00158 strVis = javadoc->scopeToJavaDecl((Uml::Visibility::Value) scopePolicy);
00159 break;
00160 default:
00161 case CodeGenerationPolicy::FromParent:
00162
00163 break;
00164 }
00165
00166
00167 QString headerText = "";
00168 QString methodReturnType = "";
00169 QString methodName = "";
00170 QString methodParams = "";
00171
00172 switch(getType()) {
00173 case CodeAccessorMethod::ADD:
00174 methodName = "add"+javadoc->capitalizeFirstLetter(fieldType);
00175 methodReturnType = "void";
00176 methodParams = objectType+" value ";
00177 headerText = "Add an object of type "+objectType+" to the List "+fieldName+endLine+getParentObject()->getDoc()+endLine+"@return void";
00178 break;
00179 case CodeAccessorMethod::GET:
00180 methodName = "get"+javadoc->capitalizeFirstLetter(fieldName);
00181 methodReturnType = fieldType;
00182 headerText = "Get the value of "+fieldName+endLine+getParentObject()->getDoc()+endLine+"@return the value of "+fieldName;
00183 break;
00184 case CodeAccessorMethod::LIST:
00185 methodName = "get"+javadoc->capitalizeFirstLetter(fieldType)+"List";
00186 methodReturnType = "List";
00187 headerText = "Get the list of "+fieldName+endLine+getParentObject()->getDoc()+endLine+"@return List of "+fieldName;
00188 break;
00189 case CodeAccessorMethod::REMOVE:
00190 methodName = "remove"+javadoc->capitalizeFirstLetter(fieldType);
00191 methodReturnType = "void";
00192 methodParams = objectType+" value ";
00193 headerText = "Remove an object of type "+objectType+" from the List "+fieldName+endLine+getParentObject()->getDoc();
00194 break;
00195 case CodeAccessorMethod::SET:
00196 methodName = "set"+javadoc->capitalizeFirstLetter(fieldName);
00197 methodReturnType = "void";
00198 methodParams = fieldType + " value ";
00199 headerText = "Set the value of "+fieldName+endLine+getParentObject()->getDoc()+endLine;
00200 break;
00201 default:
00202
00203 kWarning()<<"Warning: cant generate JavaCodeAccessorMethod for type: "<<getType()<<endl;
00204 break;
00205 }
00206
00207
00208 if(getComment()->getText().isEmpty())
00209 getComment()->setText(headerText);
00210
00211
00212 setStartMethodText(strVis+' '+methodReturnType+' '+methodName+" ( "+methodParams+" ) {");
00213 setEndMethodText("}");
00214
00215 }
00216
00217 void JavaCodeAccessorMethod::update()
00218 {
00219 updateMethodDeclaration();
00220 updateContent();
00221 }
00222
00223 #include "javacodeaccessormethod.moc"
This file is part of the documentation for umbrello Version 3.1.0.