implemented custom code injection functionality/parameter based injection for ruby/php #814

Merged
merged 1 commit into from Jun 3, 2016

Projects

None yet

4 participants

@Shikib
Member
Shikib commented Apr 10, 2016

Description

I added the code injection into custom methods functionality (previously available only to Java) to Php and Ruby. I think these are the two other languages that Umple supports that this feature applies to (based on what I see in the code, Umple supports Java/Php/Ruby/SQL).

Generator code was moved almost exactly from Java to Php. There were a few changes made for Ruby to account for the fact that you can write Ruby code without semicolons.

Tests

The tests that previously only tested Java were extended for Ruby/Php.

@Shikib Shikib implemented custom code injection functionality for ruby/php
3f37e96
@vahdat-ab
Member

I'll look at this after the issue related to previus PR is resolved.

@Nava2 Nava2 commented on the diff Apr 11, 2016
UmpleToPhp/UmpleTLTemplates/class_MethodDeclaration.ump
@@ -10,6 +10,8 @@ class UmpleToPhp {
String methodImplementationModifier = aMethod.getIsAbstract() ? " abstract" : "";
String methodName = aMethod.getName();
String methodType = "function";
+ String customBeforeInjectionCode = GeneratorHelper.toCode(uClass.getApplicableCodeInjectionsCustomMethod("before", aMethod.getName(), aMethod.getMethodParameters()));
@Nava2
Nava2 Apr 11, 2016 Member

Spacing

@Nava2 Nava2 commented on the diff Apr 11, 2016
UmpleToPhp/UmpleTLTemplates/class_MethodDeclaration.ump
@@ -72,7 +74,130 @@ class UmpleToPhp {
{
appendln(realSb, "\n {");
if (customPreconditionCode != null) { append(realSb, "\n{0}\n",GeneratorHelper.doIndent(customPreconditionCode, " "));}
+ if (customBeforeInjectionCode != null) { append(realSb, "\n{0}\n",GeneratorHelper.doIndent(customBeforeInjectionCode, " "));}
+
+ if(properMethodBody.contains("return"))
+ {
+ if(customAfterInjectionCode != null) {
+ // Do some pre-processing to handle returns not being on a new line. Doing this allows us to maintain suitable indentation.
+ String[] properMethodLines = properMethodBody.split("\\n");
+ String fixedProperMethodBody = "";
+ for(int i = 0; i < properMethodLines.length; i++) {
@Nava2
Nava2 Apr 11, 2016 Member

Use a for each here, especially since you index into the array quite a few times.

@Nava2 Nava2 commented on the diff Apr 11, 2016
UmpleToPhp/UmpleTLTemplates/class_MethodDeclaration.ump
+ if(properMethodLines[i].contains("return") && !properMethodLines[i].trim().substring(0, 6).equals("return")) {
+ String[] splitLines = properMethodLines[i].split("return", 2);
+ // Determine indentation of return by adding indentation amount to previous line
+ String returnIndent = "";
+ int j = 0;
+ while(splitLines[0].charAt(j) == ' ') {
+ returnIndent += " ";
+ j++;
+
+ }
+
+ fixedProperMethodBody += returnIndent + splitLines[0].trim() + "\n";
+
+ String[] returnLines = splitLines[1].split(";");
+ if(returnLines.length > 1 && returnLines[1].trim().length() > 0) {
+ fixedProperMethodBody += returnIndent + " return " + returnLines[0].trim() + ";\n" + returnIndent + returnLines[1].trim() + "\n";
@Nava2
Nava2 Apr 11, 2016 Member

Does this need to worry about new lines on windows vs. unix?

@Nava2 Nava2 commented on the diff Apr 11, 2016
UmpleToPhp/UmpleTLTemplates/class_MethodDeclaration.ump
+ while(splitLines[0].charAt(j) == ' ') {
+ returnIndent += " ";
+ j++;
+
+ }
+
+ fixedProperMethodBody += returnIndent + splitLines[0].trim() + "\n";
+
+ String[] returnLines = splitLines[1].split(";");
+ if(returnLines.length > 1 && returnLines[1].trim().length() > 0) {
+ fixedProperMethodBody += returnIndent + " return " + returnLines[0].trim() + ";\n" + returnIndent + returnLines[1].trim() + "\n";
+ } else {
+ fixedProperMethodBody += returnIndent + " return " + splitLines[1].trim() + "\n";
+ }
+ } else {
+ fixedProperMethodBody += properMethodLines[i] + "\n";
@Nava2
Nava2 Apr 11, 2016 Member

There's a lot of concatenations in here, it should be done with a StringBuilder.

@Nava2 Nava2 commented on the diff Apr 11, 2016
UmpleToRuby/UmpleTLTemplates/class_MethodDeclaration.ump
@@ -55,8 +56,99 @@ class UmpleToRuby {
}
if (customPreconditionCode != null) { append(realSb, "\n{0}\n",GeneratorHelper.doIndent(customPreconditionCode, " "));}
+ if (customBeforeInjectionCode != null) { append(realSb, "{0}\n",GeneratorHelper.doIndent(customBeforeInjectionCode, " "));}
+
+ if(properMethodBody.contains("return")) {
@Nava2
Nava2 Apr 11, 2016 Member

This looks the same as the previous change. Is it a copy-paste?

@TimLethbridge TimLethbridge merged commit 9bb16a4 into umple:master Jun 3, 2016

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment