Permalink
Browse files

Initial commit. Add set and get methods for applicable attributes.

  • Loading branch information...
katcavers committed Feb 27, 2017
1 parent 1cd3308 commit a08d7fc7981d55f4ad8d47d2753c5391c7b39923
@@ -6,7 +6,7 @@ class UmpleToJava {
{
List<TraceItem> traceItems= aMethod.getTraced("method",uClass);
- if(!aMethod.getExistsInLanguage("Java"))
+ if(!aMethod.getExistsInLanguage("Java") || aMethod.getSource() == Method.Source.fAutoAPI)
continue;
@@ -8,7 +8,7 @@ class UmpleToPhp {
"String", "Boolean", "Integer", "Float", "Double");
for (Method aMethod : uClass.getMethods())
{
- if(!aMethod.getExistsInLanguage("Php"))
+ if(!aMethod.getExistsInLanguage("Php") || aMethod.getSource() == Method.Source.fAutoAPI)
continue;
String methodModifier = aMethod.getModifier().equals("") ? "public" : aMethod.getModifier();
String methodImplementationModifier = aMethod.getIsAbstract() ? " abstract" : "";
@@ -4,6 +4,8 @@ class UmpleToRuby {
// the for loop will terminate immediately even if there are none with little to no
for (Method aMethod : uClass.getMethods())
{
+ if (aMethod.getSource() == Method.Source.fAutoAPI)
+ continue;
String methodName = aMethod.getName();
String methodType = aMethod.getType();
String customBeforeInjectionCode = GeneratorHelper.toCode(uClass.getApplicableCodeInjectionsCustomMethod("before", aMethod.getName(), aMethod.getMethodParameters()));
@@ -102,7 +102,17 @@ class <<=uClass.getName()>><<= gen.translate("isA",uClass) >>
<<@ UmpleToRuby.attribute_Set_All >><<@ UmpleToRuby.attribute_Get_All >><<@ UmpleToRuby.attribute_IsBoolean_All >><<@ UmpleToRuby.state_machine_Get_All >><<@ UmpleToRuby.state_machine_Events_All >><<@ UmpleToRuby.state_machine_Set_All >><<@ UmpleToRuby.association_Get_All >><<@ UmpleToRuby.association_Set_All >>
<<# if (uClass.getKey().isProvided()) { #>><<@ UmpleToRuby.equals >>
<<# } #>><<@ UmpleToRuby.state_machine_doActivity_All >><<@ UmpleToRuby.state_machine_timedEvent_All >><<@ UmpleToRuby.delete_All >>
- <<# if (uClass.hasMethods()) { #>><<@ UmpleToRuby.class_MethodDeclaration >>
+ <<# boolean hasNonAutoGeneratedMethods = false;
+ for (Method aMethod: uClass.getMethods())
+ {
+ if (aMethod.getSource() != Method.Source.fAutoAPI)
+ {
+ hasNonAutoGeneratedMethods = true;
+ break;
+ }
+ }
+ #>>
+ <<# if (uClass.hasMethods() && hasNonAutoGeneratedMethods) { #>><<@ UmpleToRuby.class_MethodDeclaration >>
<<# } #>>
<<# if (uClass.getExtraCode() != null && uClass.getExtraCode().length() > 0) { #>>
#------------------------
@@ -129,6 +129,9 @@ class GvClassDiagramGenerator
if (hasSuboption("showmethods")) {
boolean isFirstMethod = true;
for (Method uMethod : inUmpleClassifier.getMethods()) {
+ if (uMethod.getSource() == Method.Source.fAutoAPI)
+ continue;
+
if (manyClass) {
if(isFirstMethod) code.append("|"); // method block starter
else code.append("\\\n"); // separator between attributes
@@ -72,6 +72,9 @@ class JsonGenerator
for(Method method : aClass.getMethods())
{
+ if (method.getSource() == Method.Source.fAutoAPI)
+ continue;
+
if(jsonForMethods.length() > 0)
{
jsonForMethods += ", ";
@@ -88,7 +88,16 @@ class ScxmlGenerator
// Look for additional methods inside the class and add them in a <script> in
// the scxml document.
UmpleClass umpleClass = model.getUmpleClass(0);
- if (umpleClass.hasMethods()) {
+ Boolean hasNonAutoGeneratedMethods = false;
+ for (Method m : umpleClass.getMethods())
+ {
+ if (m.getSource() != Method.Source.fAutoAPI)
+ {
+ hasNonAutoGeneratedMethods = true;
+ break;
+ }
+ }
+ if (umpleClass.hasMethods() && hasNonAutoGeneratedMethods) {
generateScxmlScript(umpleClass.getMethods());
}
@@ -246,6 +255,8 @@ class ScxmlGenerator
_genGenericTag(0, code, "script", false);
for (Method method: methods) {
+ if (method.getSource() == Method.Source.fAutoAPI)
+ continue;
_genTextElement(0, code, method.toString());
}
@@ -933,7 +933,7 @@ class UmpleGenerator
switch(ruleName) {
case CONCRETE_METHOD_DECLARATION:
int curMethod = this.getScope().candidateIterators.get("Method");
- if(curMethod < ((UmpleClass)uContainer).getMethods().size()) {
+ if(curMethod < ((UmpleClass)uContainer).getMethods().size() && ((UmpleClass)uContainer).getMethods().get(curMethod).getSource() != Method.Source.fAutoAPI) {
this.getParentScope().candidateIterators.put("Method", curMethod+1);
generateRuleTokenOutput(getRuleTokenByName(ruleNameToken.getValue()), model, ((UmpleClass)uContainer).getMethods().get(curMethod));
}
@@ -103,6 +103,8 @@ class UmpleSelfGenerator
for (Method m : uClass.getMethods())
{
+ if (m.getSource() == Method.Source.fAutoAPI)
+ continue;
generateMethod(m, code);
}
@@ -44,10 +44,20 @@ class YumlGenerator
yuml.append(StringFormatter.format("{0}:{1};", name, type));
}
}
- if (aClass.numberOfMethods() > 0)
+
+ Boolean hasNonAutoGeneratedMethods = false;
+ for (Method aMethod: aClass.getMethods())
+ {
+ if (aMethod.getSource() != Method.Source.fAutoAPI)
+ hasNonAutoGeneratedMethods = true;
+ }
+
+ if (aClass.numberOfMethods() > 0 && hasNonAutoGeneratedMethods)
{
yuml.append("|");
for (Method method : aClass.getMethods()) {
+ if (method.getSource() == Method.Source.fAutoAPI)
+ continue;
String methodModifier = method.getModifier();
String methodName = method.getName();
methodName = methodModifier.contains("private")?"-"+methodName:"+"+methodName;
@@ -185,7 +185,8 @@ class Method
//This supposed to show from where this method came.
//fTrait means it comes from traits.
//fAuto means it comes from auto generation mechanism.
- source {none,fTrait,fAuto}
+ //fAutoAPI means it comes from auto generation mechanism to represent API methods at the model level.
+ source {none,fTrait,fAuto,fAutoAPI}
// The method parameters.
1 -> 0..* MethodParameter;
@@ -3950,6 +3950,52 @@ private Boolean checkIsDistributed(UmpleInterface uInterface)
lastattr = attribute;
lastassoc = null;
+ if (!attribute.getModifier().equals("internal") && !attribute.getModifier().equals("const"))
+ {
+ addAPIMethods(attribute, aClass);
+ }
+ }
+
+ private void addAPIMethods(Attribute attribute, UmpleClass aClass)
+ {
+ Glossary g = new Glossary();
+ Boolean isMany = attribute.getIsList();
+ String singularName = isMany ? g.getSingular(attribute.getName()) : attribute.getName();
+ String pluralName = isMany ? attribute.getName() : g.getPlural(attribute.getName());
+ String getSetName = isMany ? pluralName : singularName;
+
+ addGetMethod(getSetName, attribute, aClass);
+
+ if (!attribute.getIsAutounique() && (!attribute.getModifier().equals("immutable") || attribute.getIsLazy()))
+ {
+ addSetMethod(getSetName, attribute, aClass);
+ }
+
+ }
+
+ private void addSetMethod(String name, Attribute attribute, UmpleClass aClass)
+ {
+ String setName = StringFormatter.format("set{0}", StringFormatter.toPascalCase(name));
+
+ Method aMethod = new Method("public", setName, "Boolean", false);
+ aMethod.setMethodBody(new MethodBody(""));
+ aMethod.setPosition(new Position("", 0, 0, 0));
+ aMethod.setSource(Method.Source.fAutoAPI);
+ aMethod.addMethodParameter(new MethodParameter("a" + name, attribute.getType(), "", "", false));
+ aClass.addMethod(aMethod);
+
+ }
+
+ private void addGetMethod(String name, Attribute attribute, UmpleClass aClass)
+ {
+ String getName = StringFormatter.format("get{0}", StringFormatter.toPascalCase(name));
+
+ Method aMethod = new Method("public",getName, attribute.getType(), false);
+ aMethod.setMethodBody(new MethodBody(""));
+ aMethod.setPosition(new Position("", 0, 0, 0));
+ aMethod.setSource(Method.Source.fAutoAPI);
+ aClass.addMethod(aMethod);
+
}
@@ -2560,6 +2560,70 @@ public void methodNameConflictUsingDefaulted()
assertFailedParse("045_phpDefaultedNameConflict.ump", 48);
assertFailedParse("045_rubyDefaultedNameConflict.ump", 48);
}
+
+ @Test
+ public void autoGeneratedAPIMethods()
+ {
+ assertSimpleParse("008_autogeneratedMethodsAttribute.ump");
+ UmpleClass uClass = model.getUmpleClass("X");
+
+ Assert.assertEquals(uClass.getMethods().size(), 10);
+
+ // internal attributes should not have a set or get method.
+ Method internalGet = new Method("", "getInternalAttr", "Boolean", false);
+ Method internalSet = new Method("", "setInternalAttr", "Boolean", false);
+ internalSet.addMethodParameter(new MethodParameter("aInternalAttribute", "Boolean", "", "", false));
+ Assert.assertFalse(uClass.hasMethod(internalGet));
+ Assert.assertFalse(uClass.hasMethod(internalSet));
+
+ // const attributes should not have a set or get method.
+ Method constGet = new Method("", "getMAX", "Integer", false);
+ Method constSet = new Method("", "setMAX", "Boolean", false);
+ constSet.addMethodParameter(new MethodParameter("aMAX", "Integer", "", "", false));
+ Assert.assertFalse(uClass.hasMethod(constGet));
+ Assert.assertFalse(uClass.hasMethod(constSet));
+
+ // basic/settable attributes should have both a get and set method.
+ Method settableGet = new Method("", "getD", "Date", false);
+ Method settableSet = new Method("", "setD", "Boolean", false);
+ settableSet.addMethodParameter(new MethodParameter("aD", "Date", "", "", false));
+ Assert.assertTrue(uClass.hasMethod(settableGet));
+ Assert.assertTrue(uClass.hasMethod(settableSet));
+
+ // immutable attributes should only have a get method generated.
+ Method immutableGet = new Method("", "getStr", "String", false);
+ Method immutableSet = new Method("", "setStr", "Boolean", false);
+ Assert.assertTrue(uClass.hasMethod(immutableGet));
+ Assert.assertFalse(uClass.hasMethod(immutableSet));
+
+ // lazy attributes should have both a set and a get method.
+ Method lazyGet = new Method("", "getLDoub", "Double", false);
+ Method lazySet = new Method("", "setLDoub", "Boolean", false);
+ lazySet.addMethodParameter(new MethodParameter("aLDoub", "Double", "", "", false));
+ Assert.assertTrue(uClass.hasMethod(lazyGet));
+ Assert.assertTrue(uClass.hasMethod(lazySet));
+
+ // lazy immutable attributes should have both a set and a get method.
+ Method lazyImmutableGet = new Method("", "getStr2", "String", false);
+ Method lazyImmutableSet = new Method("", "setStr2", "Boolean", false);
+ lazyImmutableSet.addMethodParameter(new MethodParameter("aStr2", "String", "", "", false));
+ Assert.assertTrue(uClass.hasMethod(lazyImmutableGet));
+ Assert.assertTrue(uClass.hasMethod(lazyImmutableSet));
+
+ //autounique attributes should have a get method that returns an Integer, and no set method.
+ Method autoUniqueGet = new Method("", "getX", "Integer", false);
+ Method autoUniqueSet = new Method("", "setX", "Boolean", false);
+ autoUniqueSet.addMethodParameter(new MethodParameter("aX", "Integer", "", "", false));
+ Assert.assertTrue(uClass.hasMethod(autoUniqueGet));
+ Assert.assertFalse(uClass.hasMethod(autoUniqueSet));
+
+ // defaulted attributes should have a get and set method.
+ Method defaultedGet = new Method("", "getP", "String", false);
+ Method defaultedSet = new Method("", "setP", "Boolean", false);
+ defaultedSet.addMethodParameter(new MethodParameter("aP", "String", "", "", false));
+ Assert.assertTrue(uClass.hasMethod(defaultedGet));
+ Assert.assertTrue(uClass.hasMethod(defaultedSet));
+ }

0 comments on commit a08d7fc

Please sign in to comment.