From 3a2e39f223368420ef717069b1dca4c4d9e2e37d Mon Sep 17 00:00:00 2001 From: jeffcheasey88 Date: Wed, 31 May 2023 15:55:00 +0200 Subject: [PATCH] Operation -> Synchronized --- .../java/operations/OperationFactory.java | 4 +- .../operations/SynchronizedOperation.java | 49 +++++++++++++++++++ .../peeratcode/parser/java/OperationTest.java | 23 +++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/operations/SynchronizedOperation.java diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java index ebd11d8..f64b660 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java @@ -35,6 +35,8 @@ public class OperationFactory{ this.patterns.put(Pattern.compile("^\\s*else\\s*.*$"), ElseOperation.class); this.patterns.put(Pattern.compile("^\\s*do\\s*\\^GENERIC_FUNCTION\\d+.*$"), DoOperation.class); + this.patterns.put(Pattern.compile("^\\s*synchronized\\s*\\^GENERIC_PARENTHESIS\\d+.*$"), SynchronizedOperation.class); + this.patterns.put(Pattern.compile("^\\s*continue\\s*;.*$"), ContinueOperation.class); this.patterns.put(Pattern.compile("^\\s*break\\s*;.*$"), BreakOperation.class); @@ -60,7 +62,7 @@ public class OperationFactory{ * try catch finally * throw * else OK - * synchronized + * synchronized OK * instance of * * native operation style i++ diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/SynchronizedOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/SynchronizedOperation.java new file mode 100644 index 0000000..d15ee88 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/SynchronizedOperation.java @@ -0,0 +1,49 @@ +package be.jeffcheasey88.peeratcode.parser.java.operations; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import be.jeffcheasey88.peeratcode.parser.java.CleanerPool; +import be.jeffcheasey88.peeratcode.parser.java.JavaElement; + +public class SynchronizedOperation extends OperationContainer{ + + private static Pattern PATTERN = Pattern.compile("^(\\s*synchronized\\s*)(\\^GENERIC_PARENTHESIS\\d+)(\\s*).*$"); + private static Pattern PATTERN_CLEANER = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+).*$"); + + private String include; + + @Override + public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ + Matcher matcher = PATTERN.matcher(content); + matcher.matches(); + + this.include = local.unzip(matcher.group(2), (s,p) -> s); + + int index = matcher.group(1).length()+matcher.group(2).length()+matcher.group(3).length(); + content = content.substring(index); + + matcher = PATTERN_CLEANER.matcher(content); + matcher.matches(); + + content = matcher.group(1); + + index += content.length(); + content = local.unzipOne(content, (s,p) -> s); + content = content.substring(1, content.length()-1); + content = local.clean(content); + super.parse(content, global, local); + + return index; + } + + @Override + public void show(int tab){ + String start = ""; + for(int i = 0; i < tab; i++) start+="\t"; + System.out.println(start+"synchronized"+this.include+"{"); + for(JavaElement child : getChilds()) child.show(tab+1); + System.out.println(start+"}"); + } + +} \ No newline at end of file diff --git a/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java index ddde132..b1dc778 100644 --- a/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java +++ b/test/be/jeffcheasey88/peeratcode/parser/java/OperationTest.java @@ -16,6 +16,7 @@ import be.jeffcheasey88.peeratcode.parser.java.operations.DoOperation; import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.BreakOperation; import be.jeffcheasey88.peeratcode.parser.java.operations.LoopAffectOperation.ContinueOperation; import be.jeffcheasey88.peeratcode.parser.java.operations.MethodCallOperation; +import be.jeffcheasey88.peeratcode.parser.java.operations.SynchronizedOperation; @TestInstance(Lifecycle.PER_CLASS) class OperationTest{ @@ -137,4 +138,26 @@ class OperationTest{ } } + + @Test + void synchronizedOperation(){ + try { + JavaParser parser = parse("package be.jeffcheasey88.peeratcode.parser.java; class Test{ void function(){ synchronized(this) { this.none(); } } }"); + Class clazz = parser.getClazz(); + + clazz.show(0); + + assertEquals(1, clazz.getChilds().size()); + Function function = (Function) clazz.getChilds().get(0); + assertEquals(1, function.getChilds().size()); + + assertEquals(SynchronizedOperation.class, function.getChilds().get(0).getClass()); + SynchronizedOperation sync = (SynchronizedOperation)function.getChilds().get(0); + assertEquals(1, sync.getChilds().size()); + assertEquals(MethodCallOperation.class, sync.getChilds().get(0).getClass()); + } catch (Exception e) { + e.printStackTrace(); + } + + } }