Got rid of all Reflection. Sadly a bit of Performance loss

This commit is contained in:
Speiger 2021-06-23 00:13:53 +02:00
parent 8937d44428
commit 6f3f45d718
6 changed files with 98 additions and 61 deletions

View File

@ -5,7 +5,7 @@ repositories {
}
archivesBaseName = 'Simple Code Generator'
version = '1.0.3'
version = '1.0.4'
apply plugin: 'maven'
tasks.withType(JavaCompile) {

View File

@ -1,8 +1,8 @@
package speiger.src.builder.mappers;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import speiger.src.builder.misc.RegexMatcher;
import speiger.src.builder.misc.RegexUtil;
public class ArgumentMapper implements IMapper
@ -49,7 +49,7 @@ public class ArgumentMapper implements IMapper
@Override
public String apply(String t)
{
Matcher matcher = pattern.matcher(t);
RegexMatcher matcher = new RegexMatcher(pattern, t);
try
{
if(matcher.find())
@ -57,17 +57,16 @@ public class ArgumentMapper implements IMapper
StringBuffer buffer = new StringBuffer();
do
{
if(matcher.end() < RegexUtil.getLastAppendPosition(matcher)) return apply(matcher.appendTail(buffer).toString());
String text = RegexUtil.searchUntil(t, matcher.end()-1, braces.charAt(0), braces.charAt(1));
if(!text.isEmpty())
{
RegexUtil.skip(matcher.appendReplacement(buffer, ""), text.length());
matcher.appendReplacement(buffer, "").skip(text.length());
buffer.append(String.format(replacement, (Object[])getString(text).split(argumentBreaker)));
}
}
while(matcher.find());
matcher.appendTail(buffer);
return buffer.toString();
return apply(buffer.toString());
}
}
catch(Exception e)

View File

@ -1,8 +1,8 @@
package speiger.src.builder.mappers;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import speiger.src.builder.misc.RegexMatcher;
import speiger.src.builder.misc.RegexUtil;
public class InjectMapper implements IMapper
@ -47,7 +47,7 @@ public class InjectMapper implements IMapper
@Override
public String apply(String t)
{
Matcher matcher = pattern.matcher(t);
RegexMatcher matcher = new RegexMatcher(pattern, t);
try
{
if(matcher.find())
@ -55,16 +55,15 @@ public class InjectMapper implements IMapper
StringBuffer buffer = new StringBuffer();
do
{
if(matcher.end() < RegexUtil.getLastAppendPosition(matcher)) return apply(matcher.appendTail(buffer).toString());
String text = RegexUtil.searchUntil(t, matcher.end()-1, braces.charAt(0), braces.charAt(1));
if(!text.isEmpty())
{
RegexUtil.skip(matcher.appendReplacement(buffer, ""), text.length());
matcher.appendReplacement(buffer, "").skip(text.length());
buffer.append(String.format(replacement, getString(text)));
}
} while (matcher.find());
matcher.appendTail(buffer);
return buffer.toString();
return apply(buffer.toString());
}
}
catch(Exception e)

View File

@ -1,8 +1,8 @@
package speiger.src.builder.mappers;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import speiger.src.builder.misc.RegexMatcher;
import speiger.src.builder.misc.RegexUtil;
public class LineMapper implements IMapper
@ -30,7 +30,7 @@ public class LineMapper implements IMapper
@Override
public String apply(String t)
{
Matcher matcher = pattern.matcher(t);
RegexMatcher matcher = new RegexMatcher(pattern, t);
if(matcher.find())
{
StringBuffer buffer = new StringBuffer();
@ -42,11 +42,11 @@ public class LineMapper implements IMapper
{
matcher.appendReplacement(buffer, pattern.pattern());
buffer.setLength(buffer.length() - (start - result[0]));
RegexUtil.skip(matcher, result[1] - start);
matcher.skip(result[1] - start);
}
} while (matcher.find());
matcher.appendTail(buffer);
return buffer.toString();
return apply(buffer.toString());
}
return t;
}

View File

@ -0,0 +1,84 @@
package speiger.src.builder.misc;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatcher
{
Matcher match;
StringWrapper text;
public RegexMatcher(Pattern pattern, String text)
{
this.text = new StringWrapper(text);
match = pattern.matcher(this.text);
}
public boolean find()
{
return match.find();
}
public int end()
{
return match.end()+text.getOffset();
}
public RegexMatcher appendReplacement(StringBuffer sb, String replacement)
{
match.appendReplacement(sb, replacement);
return this;
}
public RegexMatcher appendTail(StringBuffer sb)
{
match.appendTail(sb);
return this;
}
public RegexMatcher skip(int amount)
{
text.offset(amount+match.end());
match.reset();
return this;
}
public static class StringWrapper implements CharSequence
{
String s;
int offset;
public StringWrapper(String s)
{
this.s = s;
}
@Override
public int length()
{
return s.length() - offset;
}
@Override
public char charAt(int index)
{
return s.charAt(index+offset);
}
@Override
public CharSequence subSequence(int start, int end)
{
return s.subSequence(start+offset, end+offset);
}
public void offset(int offset)
{
this.offset += offset;
}
public int getOffset()
{
return offset;
}
}
}

View File

@ -1,37 +1,7 @@
package speiger.src.builder.misc;
import java.lang.reflect.Field;
import java.util.regex.Matcher;
public class RegexUtil
{
static Field LAST_POS;
public static Matcher skip(Matcher matcher, int amount)
{
try
{
LAST_POS.setInt(matcher, LAST_POS.getInt(matcher) + amount);
}
catch(Exception e)
{
e.printStackTrace();
}
return matcher;
}
public static int getLastAppendPosition(Matcher matcher)
{
try
{
return LAST_POS.getInt(matcher);
}
catch(Exception e)
{
throw new RuntimeException(e);
}
}
public static String searchUntil(String text, int startIndex, char increase, char decrease)
{
if(text.charAt(startIndex + 1) != increase)
@ -72,19 +42,4 @@ public class RegexUtil
if(start == -1) return null;
return new int[]{start, offset};
}
static
{
try
{
Field field = Matcher.class.getDeclaredField("lastAppendPosition");
field.setAccessible(true);
LAST_POS = field;
}
catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
}
}