Compare commits
2 commits
b82e91fefa
...
31fbc33fa6
Author | SHA1 | Date | |
---|---|---|---|
31fbc33fa6 | |||
a77404952f |
4 changed files with 220 additions and 2 deletions
|
@ -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]);
|
||||
|
|
|
@ -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{
|
||||
|
||||
|
@ -40,6 +43,12 @@ 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*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);
|
||||
|
@ -59,8 +68,8 @@ public class OperationFactory{
|
|||
* return OK
|
||||
* continue OK
|
||||
* break OK
|
||||
* try catch finally
|
||||
* throw
|
||||
* try catch finally OK
|
||||
* throw OK
|
||||
* else OK
|
||||
* synchronized OK
|
||||
* instance of
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String> 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<String> 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<String> 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("");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue