Throw operator

This commit is contained in:
jeffcheasey88 2023-06-10 21:27:16 +02:00
parent b82e91fefa
commit a77404952f
2 changed files with 53 additions and 1 deletions

View file

@ -40,6 +40,8 @@ public class OperationFactory{
this.patterns.put(Pattern.compile("^\\s*continue\\s*;"), ContinueOperation.class);
this.patterns.put(Pattern.compile("^\\s*break\\s*;"), BreakOperation.class);
this.patterns.put(Pattern.compile("^\\s*throw\\s+[^;]+"), ThrowOperation.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);
@ -60,7 +62,7 @@ public class OperationFactory{
* continue OK
* break OK
* try catch finally
* throw
* throw OK
* else OK
* synchronized OK
* instance of

View file

@ -0,0 +1,50 @@
package be.jeffcheasey88.peeratcode.parser.java.operations;
import java.util.List;
import java.util.function.BiFunction;
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 ThrowOperation extends JavaElement{
private static Pattern PATTERN = Pattern.compile("^(\\s*throw\\s+([^;]+);)");
private String toChange;
public ThrowOperation(){}
@Override
public int parse(String content, CleanerPool global, CleanerPool local) throws Exception {
Matcher matcher = PATTERN.matcher(content);
matcher.lookingAt();
//To update for native obj
this.toChange = matcher.group(2);
return matcher.group(1).length();
}
@Override
public void build(ArrayBuffer<String> buffer, int tab) throws Exception{
super.build(buffer, tab);
buffer.append((s) -> s+="throw "+toChange+";");
buffer.add("");
}
@Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> search, java.util.function.Function<List<JavaElement>, Boolean> deep, List<JavaElement> trace){
return search.apply(this) ? (E)this : null;
}
@Override
public <E extends JavaElement> E find(BiFunction<JavaElement, List<JavaElement>, Boolean> search, List<JavaElement> trace){
trace.add(this);
return search.apply(this, trace) || trace.remove(trace.size()-1) == null ? (E)this : null;
}
}