From 31fbc33fa6ff31f7e540a53aabf3faf9b56a26b3 Mon Sep 17 00:00:00 2001 From: jeffcheasey88 Date: Sat, 10 Jun 2023 22:19:25 +0200 Subject: [PATCH] Try Catch Finally --- .../peeratcode/parser/java/Class.java | 8 + .../java/operations/OperationFactory.java | 9 +- .../java/operations/TryCatchOperation.java | 151 ++++++++++++++++++ 3 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 src/be/jeffcheasey88/peeratcode/parser/java/operations/TryCatchOperation.java diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java index b2ca569..1907f82 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/Class.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/Class.java @@ -24,6 +24,14 @@ public class Class extends JavaElement{ Matcher matcher = PATTERN.matcher(content); matcher.matches(); + try { + + }catch(Exception e) { + + }finally { + System.out.println("Hello"); + } + String[] split = matcher.group(2).split("\\s+"); for(int i = 0; i < split.length-1; i++){ this.modifier+=JavaParser.getModifier(split[i]); diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java index e73d056..6b20494 100644 --- a/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/OperationFactory.java @@ -14,6 +14,9 @@ import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.I import be.jeffcheasey88.peeratcode.parser.java.operations.ConditionalOperation.WhileOperation; 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.TryCatchOperation.CatchOperation; +import be.jeffcheasey88.peeratcode.parser.java.operations.TryCatchOperation.FinallyOperation; +import be.jeffcheasey88.peeratcode.parser.java.operations.TryCatchOperation.TryOperation; public class OperationFactory{ @@ -42,6 +45,10 @@ public class OperationFactory{ this.patterns.put(Pattern.compile("^\\s*throw\\s+[^;]+"), ThrowOperation.class); + this.patterns.put(Pattern.compile("^\\s*try\\s*\\^"), TryOperation.class); + this.patterns.put(Pattern.compile("^\\s*catch\\s*\\^GENERIC_PARENTHESIS\\d+"), CatchOperation.class); + this.patterns.put(Pattern.compile("^\\s*finally\\s*\\^GENERIC_FUNCTION\\d+"), FinallyOperation.class); + this.patterns.put(Pattern.compile("^\\s*[^\\^\\s]+\\^GENERIC_PARENTHESIS\\d+;"), MethodCallOperation.class); this.patterns.put(Pattern.compile("^\\s*[^\\s]+\\s+[^\\s]+\\s*=\\s*[^;]+;"), Variable.class); @@ -61,7 +68,7 @@ public class OperationFactory{ * return OK * continue OK * break OK - * try catch finally + * try catch finally OK * throw OK * else OK * synchronized OK diff --git a/src/be/jeffcheasey88/peeratcode/parser/java/operations/TryCatchOperation.java b/src/be/jeffcheasey88/peeratcode/parser/java/operations/TryCatchOperation.java new file mode 100644 index 0000000..3b21763 --- /dev/null +++ b/src/be/jeffcheasey88/peeratcode/parser/java/operations/TryCatchOperation.java @@ -0,0 +1,151 @@ +package be.jeffcheasey88.peeratcode.parser.java.operations; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import be.jeffcheasey88.peeratcode.parser.java.ArrayBuffer; +import be.jeffcheasey88.peeratcode.parser.java.CleanerPool; +import be.jeffcheasey88.peeratcode.parser.java.JavaElement; + +public class TryCatchOperation extends OperationContainer{ + + private static Pattern PATTERN = Pattern.compile("^(\\^GENERIC_FUNCTION\\d+)"); + private static Pattern PATTERN_SPACE = Pattern.compile("^(\\s+)"); + + public TryCatchOperation(){} + + @Override + public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ + Matcher matcher = PATTERN.matcher(content); + matcher.lookingAt(); + + content = matcher.group(1); + + int bodysize = 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 bodysize; + } + + public static class TryOperation extends TryCatchOperation{ + + private static Pattern PATTERN = Pattern.compile("^(\\s*try\\s*(\\^GENERIC_PARENTHESIS\\d+)?)"); + + private String resource; + + public TryOperation(){} + + @Override + public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ + Matcher matcher = TryOperation.PATTERN.matcher(content); + matcher.lookingAt(); + + if(matcher.group(2) != null) this.resource = local.unzipOne(matcher.group(2), (s,p) -> s); + + int index = matcher.group(1).length(); + content = content.substring(index); + + matcher = PATTERN_SPACE.matcher(content); + if(matcher.lookingAt()){ + index+=matcher.group(1).length(); + content = content.substring(matcher.group(1).length()); + } + + return index+super.parse(content, global, local); + } + + @Override + public void build(ArrayBuffer buffer, int tab) throws Exception{ + super.build(buffer, tab); + buffer.append((s) -> s+="try"+(resource == null ? "":resource)+"{"); + buffer.add(""); + + for(JavaElement child : getChilds()) child.build(buffer, tab+1); + + super.build(buffer, tab); + buffer.append((s) -> s+="}"); + buffer.add(""); + } + } + + public static class CatchOperation extends TryCatchOperation{ + + private static Pattern PATTERN = Pattern.compile("^(\\s*catch\\s*(\\^GENERIC_PARENTHESIS\\d+))"); + + private String exceptions; + + public CatchOperation(){} + + @Override + public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ + Matcher matcher = CatchOperation.PATTERN.matcher(content); + matcher.lookingAt(); + + this.exceptions = local.unzipOne(matcher.group(2), (s,p) -> s); + + int index = matcher.group(1).length(); + content = content.substring(index); + + matcher = PATTERN_SPACE.matcher(content); + if(matcher.lookingAt()){ + index+=matcher.group(1).length(); + content = content.substring(matcher.group(1).length()); + } + + return index+super.parse(content, global, local); + } + + @Override + public void build(ArrayBuffer buffer, int tab) throws Exception{ + super.build(buffer, tab); + buffer.append((s) -> s+="catch"+exceptions+"{"); + buffer.add(""); + + for(JavaElement child : getChilds()) child.build(buffer, tab+1); + + super.build(buffer, tab); + buffer.append((s) -> s+="}"); + buffer.add(""); + } + } + + public static class FinallyOperation extends TryCatchOperation{ + + private static Pattern PATTERN = Pattern.compile("^(\\s*finally\\s*)"); + + public FinallyOperation(){} + + @Override + public int parse(String content, CleanerPool global, CleanerPool local) throws Exception{ + Matcher matcher = FinallyOperation.PATTERN.matcher(content); + matcher.lookingAt(); + + int index = matcher.group(1).length(); + content = content.substring(index); + + matcher = PATTERN_SPACE.matcher(content); + if(matcher.lookingAt()){ + index+=matcher.group(1).length(); + content = content.substring(matcher.group(1).length()); + } + + return index+super.parse(content, global, local); + } + + @Override + public void build(ArrayBuffer buffer, int tab) throws Exception{ + super.build(buffer, tab); + buffer.append((s) -> s+="finally{"); + buffer.add(""); + + for(JavaElement child : getChilds()) child.build(buffer, tab+1); + + super.build(buffer, tab); + buffer.append((s) -> s+="}"); + buffer.add(""); + } + } +}