fixed regex bug and renamed regex patterns

This commit is contained in:
nora 2020-11-24 19:01:48 +01:00
parent f85d1f34bc
commit b90e34f501
2 changed files with 63 additions and 14 deletions

View file

@ -5,9 +5,30 @@ import java.util.regex.Pattern;
*/ */
public abstract class Regex { public abstract class Regex {
/**
* Matches any method in the UML format, including a constructor, encapsulation optional
*
* <p>Examples:</p>
* <ul>
* <li>+ doStuff(foo: int, foo: int): int</li>
* <li>+ getFoo(): int</li>
* <li>work()</li>
* </ul>
*
* <p> Groups:
* <ul>
* <li>1 The encapsulate modifier (+-~#)</li>
* <li>2 The name</li>
* <li>3 All Arguments</li>
* <li>4 The return type, "" if no return type is specified (void)</li>
* </ul>
*
*/
public static final Pattern METHOD_FIND_REGEX = Pattern.compile(" *(?<capsule>[+\\-~#])? *(?<name>\\w+) *\\( *(?<args>(?: *\\w+ *: *[\\w<>]+ *,? *)*) *\\) *(?:: *(?<return>[\\w<>]+))?");
/** /**
* Matches any method in the UML format, including a constructor * Matches any method in the UML format, including a constructor, forces the encapsulation modifier
* *
* <p>Examples:</p> * <p>Examples:</p>
* <ul> * <ul>
@ -25,10 +46,32 @@ public abstract class Regex {
* </ul> * </ul>
* *
*/ */
public static final Pattern METHOD_FIND_REGEX = Pattern.compile(" *(?<capsule>[+\\-~#]) *(?<name>\\w+) *\\( *(?<args>(?: *\\w+ *: *[\\w<>]+ *,? *)*) *\\) *(?:: *(?<return>[\\w<>]+))?"); public static final Pattern METHOD_FIND_REGEX_FORCE_ENCAPSULATION = Pattern.compile(" *(?<capsule>[+\\-~#]) *(?<name>\\w+) *\\( *(?<args>(?: *\\w+ *: *[\\w<>]+ *,? *)*) *\\) *(?:: *(?<return>[\\w<>]+))?");
/** /**
* Matches any Field in the UML format, including a constructor * Matches any Field in the UML format, including a constructor, encapsulation optional
*y
* <p>Examples:</p>
* <ul>
* <li>- age: int</li>
* <li>- name: String</li>
* <li>age: int</li>
* </ul>
*
* <p> Groups:
* <ul>
* <li>1 The encapsulate modifier (+-~#)</li>
* <li>2 The name</li>
* <li>3 The datatype</li>
* </ul>
*
*/
public static final Pattern FIELD_FIND_REGEX = Pattern.compile(" *(?<capsule>[+\\-~#])? *(?<name>\\w+) *: *(?<type>[\\w<>]+)");
/**
* Matches any Field in the UML format, including a constructor, forces the encapsulation modifier
*y *y
* <p>Examples:</p> * <p>Examples:</p>
* <ul> * <ul>
@ -45,7 +88,8 @@ public abstract class Regex {
* </ul> * </ul>
* *
*/ */
public static final Pattern FIELD_FIND_REGEX = Pattern.compile(" *(?<capsule>[+\\-~#]) *(?<name>\\w+) *: *(?<type>[\\w<>]+)"); public static final Pattern FIELD_FIND_REGEX_FORCE_ENCAPSULATION = Pattern.compile(" *(?<capsule>[+\\-~#]) *(?<name>\\w+) *: *(?<type>[\\w<>]+)");
/** /**
* Matches a single arg in a method * Matches a single arg in a method
@ -58,17 +102,22 @@ public abstract class Regex {
*/ */
public static final Pattern ARG_SPLIT_REGEX = Pattern.compile(" *(\\w+) *: *([\\w<>]+)"); public static final Pattern ARG_SPLIT_REGEX = Pattern.compile(" *(\\w+) *: *([\\w<>]+)");
public static final Pattern FIELD_FIND_REGEX_NO_ENCAPSULATION = Pattern.compile(" *(?<capsule>[+\\-~#])? *(?<name>\\w+) *: *(?<type>[\\w<>]+)"); /**
* Get a method pattern
public static final Pattern METHOD_FIND_REGEX_NO_ENCAPSULATION = Pattern.compile(" *(?<capsule>[+\\-~#])? *(?<name>\\w+) *\\( *(?<args>(?: *\\w+ *: *[\\w<>]+ *,? *)*) *\\) *(?:: *(?<return>[\\w<>]+))?"); * @param encapsulationOptional True: Do not force encapsulation, False: Force encapsulation
* @return The pattern as a String
*/
public static String getMethodPattern(boolean encapsulation){ public static String getMethodPattern(boolean encapsulationOptional){
return encapsulation ? METHOD_FIND_REGEX_NO_ENCAPSULATION.pattern() : METHOD_FIND_REGEX.pattern(); return encapsulationOptional ? METHOD_FIND_REGEX.pattern() : METHOD_FIND_REGEX_FORCE_ENCAPSULATION.pattern();
} }
public static String getFieldPattern(boolean encapsulation){ /**
return encapsulation ? FIELD_FIND_REGEX_NO_ENCAPSULATION.pattern() : FIELD_FIND_REGEX.pattern(); * Get a field pattern
* @param encapsulationOptional True: Do not force encapsulation, False: Force encapsulation
* @return The pattern as a String
*/
public static String getFieldPattern(boolean encapsulationOptional){
return encapsulationOptional ? FIELD_FIND_REGEX.pattern() : FIELD_FIND_REGEX_FORCE_ENCAPSULATION.pattern();
} }
} }

View file

@ -1,4 +1,5 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
/** /**
* Stores all information about a method in a class and converts it into Java code using the {@link #toString()} method * Stores all information about a method in a class and converts it into Java code using the {@link #toString()} method
@ -42,7 +43,6 @@ public class UMLMethod {
}; };
} else { } else {
this.encapsulation = manager.getDefaultEncapsulation(); this.encapsulation = manager.getDefaultEncapsulation();
System.out.println(manager.getDefaultEncapsulation());
} }
this.name = parts[2]; this.name = parts[2];